Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package io.papermc.paper.event.world.border;

import io.papermc.paper.util.Tick;
import org.bukkit.World;
import org.bukkit.WorldBorder;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Range;
import org.jspecify.annotations.NullMarked;
import java.time.Duration;

/**
* Called when a world border changes its bounds, either over time, or instantly.
Expand Down Expand Up @@ -66,27 +69,49 @@ public void setNewSize(final double newSize) {
this.newSize = Math.min(this.worldBorder.getMaxSize(), Math.max(1.0D, newSize));
}

/**
* Gets the time in ticks for the change. Will be 0 if instant.
*
* @return the time in ticks for the change
*/
public long getDurationTicks() {
return this.duration;
}

/**
* Gets the time in milliseconds for the change. Will be 0 if instant.
*
* @return the time in milliseconds for the change
* @deprecated in favor of {@link #getDurationTicks()}
*/
@Deprecated(forRemoval = true, since = "1.21.11")
public long getDuration() {
return this.duration;
return Tick.of(this.duration).toMillis();
}

/**
* Sets the time in ticks for the change. Will change {@link #getType()} to return
* {@link Type#STARTED_MOVE}.
*
* @param duration the time in ticks for the change
*/
public void setDurationTicks(@Range(from = 0, to = Integer.MAX_VALUE) final long duration) {
this.duration = Math.clamp(duration, 0L, Integer.MAX_VALUE);
if (this.type == Type.INSTANT_MOVE) {
this.type = Type.STARTED_MOVE;
}
}

/**
* Sets the time in milliseconds for the change. Will change {@link #getType()} to return
* {@link Type#STARTED_MOVE}.
*
* @param duration the time in milliseconds for the change
* @deprecated in favor of {@link #setDurationTicks(long)}
*/
@Deprecated(forRemoval = true, since = "1.21.11")
public void setDuration(final long duration) {
// PAIL: TODO: Magic Values
this.duration = Math.min(9223372036854775L, Math.max(0L, duration));
if (duration >= 0 && this.type == Type.INSTANT_MOVE) {
this.type = Type.STARTED_MOVE;
}
this.setDurationTicks(Tick.tick().fromDuration(Duration.ofMillis(Math.clamp(duration, 0L, Integer.MAX_VALUE))));
}

@Override
Expand Down
45 changes: 23 additions & 22 deletions paper-api/src/main/java/org/bukkit/WorldBorder.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.bukkit;

import java.util.concurrent.TimeUnit;
import io.papermc.paper.util.Tick;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -12,20 +13,19 @@ public interface WorldBorder {
* @return the associated world, or null if this world border is not associated
* with any specific world, such as those created via {@link Server#createWorldBorder()}
*/
@Nullable
public World getWorld();
@Nullable World getWorld();

/**
* Resets the border to default values.
*/
public void reset();
void reset();

/**
* Gets the current side length of the border.
*
* @return The current side length of the border.
*/
public double getSize();
double getSize();

/**
* Sets the border to a square region with the specified side length in blocks.
Expand All @@ -34,7 +34,7 @@ public interface WorldBorder {
*
* @throws IllegalArgumentException if newSize is less than 1.0D or greater than {@link #getMaxSize()}
*/
public void setSize(double newSize);
void setSize(double newSize);

/**
* Sets the border to a square region with the specified side length in blocks.
Expand All @@ -44,7 +44,7 @@ public interface WorldBorder {
*
* @throws IllegalArgumentException if newSize is less than 1.0D or greater than {@link #getMaxSize()}
*/
public void setSize(double newSize, long seconds);
void setSize(double newSize, long seconds);

/**
* Sets the border to a square region with the specified side length in blocks.
Expand All @@ -54,16 +54,17 @@ public interface WorldBorder {
* @param time The time in which the border grows or shrinks from the previous size to that being set.
*
* @throws IllegalArgumentException if unit is <code>null</code> or newSize is less than 1.0D or greater than {@link #getMaxSize()}
*
* @see Tick
*/
public void setSize(double newSize, @NotNull TimeUnit unit, long time);
void setSize(double newSize, @NotNull TimeUnit unit, long time);

/**
* Gets the current border center.
*
* @return The current border center.
*/
@NotNull
public Location getCenter();
@NotNull Location getCenter();

/**
* Sets the new border center.
Expand All @@ -73,7 +74,7 @@ public interface WorldBorder {
*
* @throws IllegalArgumentException if the absolute value of x or z is higher than {@link #getMaxCenterCoordinate()}
*/
public void setCenter(double x, double z);
void setCenter(double x, double z);

/**
* Sets the new border center.
Expand All @@ -82,84 +83,84 @@ public interface WorldBorder {
*
* @throws IllegalArgumentException if location is <code>null</code> or the absolute value of {@link Location#getX()} or {@link Location#getZ()} is higher than {@link #getMaxCenterCoordinate()}
*/
public void setCenter(@NotNull Location location);
void setCenter(@NotNull Location location);

/**
* Gets the current border damage buffer.
*
* @return The current border damage buffer.
*/
public double getDamageBuffer();
double getDamageBuffer();

/**
* Sets the amount of blocks a player may safely be outside the border before taking damage.
*
* @param blocks The amount of blocks. (The default is 5 blocks.)
*/
public void setDamageBuffer(double blocks);
void setDamageBuffer(double blocks);

/**
* Gets the current border damage amount.
*
* @return The current border damage amount.
*/
public double getDamageAmount();
double getDamageAmount();

/**
* Sets the amount of damage a player takes when outside the border plus the border buffer.
*
* @param damage The amount of damage. (The default is 0.2 damage per second per block.)
*/
public void setDamageAmount(double damage);
void setDamageAmount(double damage);

/**
* Gets the current border warning time in seconds.
*
* @return The current border warning time in seconds.
*/
public int getWarningTime();
int getWarningTime();

/**
* Sets the warning time that causes the screen to be tinted red when a contracting border will reach the player within the specified time.
*
* @param seconds The amount of time in seconds. (The default is 15 seconds.)
*/
public void setWarningTime(int seconds);
void setWarningTime(int seconds);

/**
* Gets the current border warning distance.
*
* @return The current border warning distance.
*/
public int getWarningDistance();
int getWarningDistance();

/**
* Sets the warning distance that causes the screen to be tinted red when the player is within the specified number of blocks from the border.
*
* @param distance The distance in blocks. (The default is 5 blocks.)
*/
public void setWarningDistance(int distance);
void setWarningDistance(int distance);

/**
* Check if the specified location is inside this border.
*
* @param location the location to check
* @return if this location is inside the border or not
*/
public boolean isInside(@NotNull Location location);
boolean isInside(@NotNull Location location);

/**
* Gets the maximum possible size of a WorldBorder.
*
* @return The maximum size the WorldBorder
*/
public double getMaxSize();
double getMaxSize();

/**
* Gets the absolute value of the maximum x/z center coordinate of a
* WorldBorder.
*
* @return The absolute maximum center coordinate of the WorldBorder
*/
public double getMaxCenterCoordinate();
double getMaxCenterCoordinate();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
double centerZ;
int absoluteMaxSize = 29999984;
WorldBorder.BorderExtent extent = new WorldBorder.StaticBorderExtent(5.999997E7F);
+ public net.minecraft.server.level.ServerLevel world; // CraftBukkit
+ public @javax.annotation.Nullable net.minecraft.server.level.ServerLevel world; // CraftBukkit

public WorldBorder() {
this(WorldBorder.Settings.DEFAULT);
Expand Down Expand Up @@ -52,8 +52,8 @@
+ if (this.world != null) {
+ io.papermc.paper.event.world.border.WorldBorderBoundsChangeEvent event = new io.papermc.paper.event.world.border.WorldBorderBoundsChangeEvent(world.getWorld(), world.getWorld().getWorldBorder(), io.papermc.paper.event.world.border.WorldBorderBoundsChangeEvent.Type.INSTANT_MOVE, getSize(), size, 0);
+ if (!event.callEvent()) return;
+ if (event.getType() == io.papermc.paper.event.world.border.WorldBorderBoundsChangeEvent.Type.STARTED_MOVE && event.getDuration() > 0) { // If changed to a timed transition
+ lerpSizeBetween(event.getOldSize(), event.getNewSize(), event.getDuration(), this.world.getGameTime());
+ if (event.getType() == io.papermc.paper.event.world.border.WorldBorderBoundsChangeEvent.Type.STARTED_MOVE && event.getDurationTicks() > 0) { // If changed to a timed transition
+ lerpSizeBetween(event.getOldSize(), event.getNewSize(), event.getDurationTicks(), this.world.getGameTime());
+ return;
+ }
+ size = event.getNewSize();
Expand All @@ -77,7 +77,7 @@
+ io.papermc.paper.event.world.border.WorldBorderBoundsChangeEvent event = new io.papermc.paper.event.world.border.WorldBorderBoundsChangeEvent(world.getWorld(), world.getWorld().getWorldBorder(), type, oldSize, newSize, time);
+ if (!event.callEvent()) return;
+ newSize = event.getNewSize();
+ time = event.getDuration();
+ time = event.getDurationTicks();
+ }
+ // Paper end - Add worldborder events
this.extent = (WorldBorder.BorderExtent)(oldSize == newSize
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.bukkit.craftbukkit;

import com.google.common.base.Preconditions;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
import io.papermc.paper.util.Tick;
import net.minecraft.core.BlockPos;
import org.bukkit.Location;
import org.bukkit.World;
Expand Down Expand Up @@ -44,7 +46,7 @@ public void setSize(double newSize) {

@Override
public void setSize(double newSize, long time) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method and the other one should probably be deprecated (with a new method) now since the duration is in ticks and 20 ticks is not always equivalent to 1s especially with the tick command. That's the whole reason the bukkit scheduler doesn't have the TimeUnit equivalent.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can replace this?
i cannot make another method like this not? unless mark the deprecation to use the method with timeunit and include a see to the Tick TimeUnit

this.setSize(Math.min(this.getMaxSize(), Math.max(1.0D, newSize)), TimeUnit.SECONDS, Math.min(9223372036854775L, Math.max(0L, time)));
this.setSize(Math.min(this.getMaxSize(), Math.max(1.0D, newSize)), TimeUnit.SECONDS, Math.clamp(time, 0L, Integer.MAX_VALUE));
}

@Override
Expand All @@ -54,7 +56,8 @@ public void setSize(double newSize, TimeUnit unit, long time) {
Preconditions.checkArgument(newSize >= 1.0D && newSize <= this.getMaxSize(), "newSize must be between 1.0D and %s", this.getMaxSize());

if (time > 0L) {
this.handle.lerpSizeBetween(this.handle.getSize(), newSize, unit.toMillis(time), this.getWorld().getGameTime());
final long startTime = (this.getWorld() != null) ? this.getWorld().getGameTime() : 0; // Virtual Borders don't have a World
this.handle.lerpSizeBetween(this.handle.getSize(), newSize, Tick.tick().fromDuration(Duration.of(time, unit.toChronoUnit())), startTime);
} else {
this.handle.setSize(newSize);
}
Expand Down
Loading