From 7e82edcbd9b7d87e49ffe8fd549bc9d6200aece8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Tue, 29 Jun 2021 22:21:30 +0200 Subject: [PATCH 01/64] Marked classes deprecated for removal --- src/main/java/net/minestom/server/utils/time/TimeUnit.java | 4 ++++ .../java/net/minestom/server/utils/time/UpdateOption.java | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/src/main/java/net/minestom/server/utils/time/TimeUnit.java b/src/main/java/net/minestom/server/utils/time/TimeUnit.java index 0b9805391..73d00450f 100644 --- a/src/main/java/net/minestom/server/utils/time/TimeUnit.java +++ b/src/main/java/net/minestom/server/utils/time/TimeUnit.java @@ -2,6 +2,10 @@ package net.minestom.server.utils.time; import net.minestom.server.MinecraftServer; +/** + * @deprecated Replaced by {@link java.time.temporal.TemporalUnit} + */ +@Deprecated(forRemoval = true) public enum TimeUnit { TICK, DAY, HOUR, MINUTE, SECOND, MILLISECOND; diff --git a/src/main/java/net/minestom/server/utils/time/UpdateOption.java b/src/main/java/net/minestom/server/utils/time/UpdateOption.java index 7f186bc73..603d3de13 100644 --- a/src/main/java/net/minestom/server/utils/time/UpdateOption.java +++ b/src/main/java/net/minestom/server/utils/time/UpdateOption.java @@ -4,6 +4,10 @@ import org.jetbrains.annotations.NotNull; import java.util.Objects; +/** + * @deprecated Replaced by {@link java.time.Duration} + */ +@Deprecated(forRemoval = true) public class UpdateOption { private final long value; From 8f906d80d02226ce799d06625dafad9a0c7b59fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Tue, 29 Jun 2021 22:22:30 +0200 Subject: [PATCH 02/64] Added tick as a TemporalUnit --- .../net/minestom/server/utils/time/Tick.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/main/java/net/minestom/server/utils/time/Tick.java diff --git a/src/main/java/net/minestom/server/utils/time/Tick.java b/src/main/java/net/minestom/server/utils/time/Tick.java new file mode 100644 index 000000000..51dbb8391 --- /dev/null +++ b/src/main/java/net/minestom/server/utils/time/Tick.java @@ -0,0 +1,45 @@ +package net.minestom.server.utils.time; + +import net.minestom.server.MinecraftServer; + +import java.time.Duration; +import java.time.temporal.Temporal; +import java.time.temporal.TemporalUnit; + +public class Tick implements TemporalUnit { + public static final Tick TICKS = new Tick(); + + private Tick() { + } + + @Override + public Duration getDuration() { + return Duration.ofMillis(MinecraftServer.TICK_MS); + } + + @Override + public boolean isDurationEstimated() { + return false; + } + + @Override + public boolean isDateBased() { + return false; + } + + @Override + public boolean isTimeBased() { + return true; + } + + @Override + public R addTo(R temporal, long amount) { + //noinspection unchecked + return (R) temporal.plus(amount, this); + } + + @Override + public long between(Temporal temporal1Inclusive, Temporal temporal2Exclusive) { + return temporal1Inclusive.until(temporal2Exclusive, this); + } +} From b98abe8dd5ba63bc613977f0e95e35a92bb0fe77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Tue, 29 Jun 2021 22:24:34 +0200 Subject: [PATCH 03/64] Updated Cooldown.java --- .../minestom/server/utils/time/Cooldown.java | 69 +++++++++++++++++-- 1 file changed, 64 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/minestom/server/utils/time/Cooldown.java b/src/main/java/net/minestom/server/utils/time/Cooldown.java index 1110da38b..e96449c7b 100644 --- a/src/main/java/net/minestom/server/utils/time/Cooldown.java +++ b/src/main/java/net/minestom/server/utils/time/Cooldown.java @@ -2,18 +2,30 @@ package net.minestom.server.utils.time; import org.jetbrains.annotations.NotNull; +import java.time.Duration; +import java.time.temporal.TemporalUnit; + +@SuppressWarnings("removal") public final class Cooldown { - private UpdateOption updateOption; + private Duration duration; private long lastUpdate; + /** + * @deprecated Replaced by {@link #Cooldown(Duration)} + */ + @Deprecated(forRemoval = true) public Cooldown(@NotNull UpdateOption updateOption) { - this.updateOption = updateOption; + this.duration = Duration.ofMillis(updateOption.toMilliseconds()); this.lastUpdate = System.currentTimeMillis(); } - public UpdateOption getUpdateOption() { - return this.updateOption; + public Cooldown(Duration duration) { + this.duration = duration; + } + + public Duration getDuration() { + return this.duration; } public void refreshLastUpdate(long lastUpdate) { @@ -21,7 +33,7 @@ public final class Cooldown { } public boolean isReady(long time) { - return !hasCooldown(time, lastUpdate, updateOption.getTimeUnit(), updateOption.getValue()); + return !hasCooldown(time, lastUpdate, duration); } /** @@ -32,12 +44,28 @@ public final class Cooldown { * @param timeUnit the time unit of the cooldown * @param cooldown the value of the cooldown * @return true if the cooldown is in progress, false otherwise + * + * @deprecated Replaced by {@link #hasCooldown(long, long, TemporalUnit, long)} */ + @Deprecated(forRemoval = true) public static boolean hasCooldown(long currentTime, long lastUpdate, @NotNull TimeUnit timeUnit, long cooldown) { final long cooldownMs = timeUnit.toMilliseconds(cooldown); return currentTime - lastUpdate < cooldownMs; } + /** + * Gets if something is in cooldown based on the current time. + * + * @param currentTime the current time in milliseconds + * @param lastUpdate the last update in milliseconds + * @param temporalUnit the time unit of the cooldown + * @param cooldown the value of the cooldown + * @return true if the cooldown is in progress, false otherwise + */ + public static boolean hasCooldown(long currentTime, long lastUpdate, @NotNull TemporalUnit temporalUnit, long cooldown) { + return hasCooldown(currentTime, lastUpdate, Duration.of(cooldown, temporalUnit)); + } + /** * Gets if something is in cooldown based on the current time. * @@ -45,11 +73,27 @@ public final class Cooldown { * @param lastUpdate the last update in milliseconds * @param updateOption the cooldown * @return true if the cooldown is in progress, false otherwise + * + * @deprecated Replaced by {@link #hasCooldown(long, long, Duration)} */ + @Deprecated(forRemoval = true) public static boolean hasCooldown(long currentTime, long lastUpdate, @NotNull UpdateOption updateOption) { return hasCooldown(currentTime, lastUpdate, updateOption.getTimeUnit(), updateOption.getValue()); } + /** + * Gets if something is in cooldown based on the current time. + * + * @param currentTime the current time in milliseconds + * @param lastUpdate the last update in milliseconds + * @param duration the cooldown + * @return true if the cooldown is in progress, false otherwise + */ + public static boolean hasCooldown(long currentTime, long lastUpdate, @NotNull Duration duration) { + final long cooldownMs = duration.toMillis(); + return currentTime - lastUpdate < cooldownMs; + } + /** * Gets if something is in cooldown based on the current time ({@link System#currentTimeMillis()}). * @@ -57,8 +101,23 @@ public final class Cooldown { * @param timeUnit the time unit of the cooldown * @param cooldown the value of the cooldown * @return true if the cooldown is in progress, false otherwise + * + * @deprecated Replaced by {@link #hasCooldown(long, TemporalUnit, int)} */ + @Deprecated(forRemoval = true) public static boolean hasCooldown(long lastUpdate, @NotNull TimeUnit timeUnit, int cooldown) { return hasCooldown(System.currentTimeMillis(), lastUpdate, timeUnit, cooldown); } + + /** + * Gets if something is in cooldown based on the current time ({@link System#currentTimeMillis()}). + * + * @param lastUpdate the last update in milliseconds + * @param temporalUnit the time unit of the cooldown + * @param cooldown the value of the cooldown + * @return true if the cooldown is in progress, false otherwise + */ + public static boolean hasCooldown(long lastUpdate, @NotNull TemporalUnit temporalUnit, int cooldown) { + return hasCooldown(System.currentTimeMillis(), lastUpdate, temporalUnit, cooldown); + } } From 0a4166faf25d8fc9394db916f1c2b560f2d1e173 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Tue, 29 Jun 2021 22:25:32 +0200 Subject: [PATCH 04/64] Made duration final --- src/main/java/net/minestom/server/utils/time/Cooldown.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/net/minestom/server/utils/time/Cooldown.java b/src/main/java/net/minestom/server/utils/time/Cooldown.java index e96449c7b..6f3703b48 100644 --- a/src/main/java/net/minestom/server/utils/time/Cooldown.java +++ b/src/main/java/net/minestom/server/utils/time/Cooldown.java @@ -8,7 +8,7 @@ import java.time.temporal.TemporalUnit; @SuppressWarnings("removal") public final class Cooldown { - private Duration duration; + private final Duration duration; private long lastUpdate; /** From ab80ffcd0e85d7a265a7b76a2b5b23bdaa05957e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Tue, 29 Jun 2021 22:38:37 +0200 Subject: [PATCH 05/64] Updated ArgumentTime.java --- .../arguments/minecraft/ArgumentTime.java | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/main/java/net/minestom/server/command/builder/arguments/minecraft/ArgumentTime.java b/src/main/java/net/minestom/server/command/builder/arguments/minecraft/ArgumentTime.java index 37c80edc6..548448790 100644 --- a/src/main/java/net/minestom/server/command/builder/arguments/minecraft/ArgumentTime.java +++ b/src/main/java/net/minestom/server/command/builder/arguments/minecraft/ArgumentTime.java @@ -6,16 +6,19 @@ import net.minestom.server.command.builder.NodeMaker; import net.minestom.server.command.builder.arguments.Argument; import net.minestom.server.command.builder.exception.ArgumentSyntaxException; import net.minestom.server.network.packet.server.play.DeclareCommandsPacket; -import net.minestom.server.utils.time.TimeUnit; -import net.minestom.server.utils.time.UpdateOption; +import net.minestom.server.utils.time.Tick; import org.jetbrains.annotations.NotNull; +import java.time.Duration; +import java.time.temporal.ChronoUnit; +import java.time.temporal.TemporalUnit; + /** * Represents an argument giving a time (day/second/tick). *

* Example: 50d, 25s, 75t */ -public class ArgumentTime extends Argument { +public class ArgumentTime extends Argument { public static final int INVALID_TIME_FORMAT = -2; public static final int NO_NUMBER = -3; @@ -28,21 +31,21 @@ public class ArgumentTime extends Argument { @NotNull @Override - public UpdateOption parse(@NotNull String input) throws ArgumentSyntaxException { + public Duration parse(@NotNull String input) throws ArgumentSyntaxException { final char lastChar = input.charAt(input.length() - 1); - TimeUnit timeUnit; + TemporalUnit timeUnit; if (Character.isDigit(lastChar)) - timeUnit = TimeUnit.TICK; + timeUnit = Tick.TICKS; else if (SUFFIXES.contains(lastChar)) { input = input.substring(0, input.length() - 1); if (lastChar == 'd') { - timeUnit = TimeUnit.DAY; + timeUnit = ChronoUnit.DAYS; } else if (lastChar == 's') { - timeUnit = TimeUnit.SECOND; + timeUnit = ChronoUnit.SECONDS; } else if (lastChar == 't') { - timeUnit = TimeUnit.TICK; + timeUnit = Tick.TICKS; } else { throw new ArgumentSyntaxException("Time needs to have the unit d, s, t, or none", input, NO_NUMBER); } @@ -52,7 +55,7 @@ public class ArgumentTime extends Argument { try { // Check if value is a number final int time = Integer.parseInt(input); - return new UpdateOption(time, timeUnit); + return Duration.of(time, timeUnit); } catch (NumberFormatException e) { throw new ArgumentSyntaxException("Time needs to be a number", input, NO_NUMBER); } From 5e731e5ba915df2e6bc2e2072fe2d4fbd146c044 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Tue, 29 Jun 2021 23:04:25 +0200 Subject: [PATCH 06/64] Updated EntityFireEvent.java --- .../server/event/entity/EntityFireEvent.java | 47 ++++++++++++++----- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/src/main/java/net/minestom/server/event/entity/EntityFireEvent.java b/src/main/java/net/minestom/server/event/entity/EntityFireEvent.java index 5ffdad699..9bcce987a 100644 --- a/src/main/java/net/minestom/server/event/entity/EntityFireEvent.java +++ b/src/main/java/net/minestom/server/event/entity/EntityFireEvent.java @@ -3,37 +3,62 @@ package net.minestom.server.event.entity; import net.minestom.server.entity.Entity; import net.minestom.server.event.trait.CancellableEvent; import net.minestom.server.event.trait.EntityEvent; -import net.minestom.server.utils.time.TimeUnit; +import net.minestom.server.utils.time.Tick; import org.jetbrains.annotations.NotNull; +import java.time.Duration; +import java.time.temporal.TemporalUnit; + public class EntityFireEvent implements EntityEvent, CancellableEvent { private final Entity entity; - private int duration; - private TimeUnit timeUnit; + private Duration duration; private boolean cancelled; - public EntityFireEvent(Entity entity, int duration, TimeUnit timeUnit) { - this.entity = entity; - setFireTime(duration, timeUnit); + @SuppressWarnings("removal") + @Deprecated(forRemoval = true) + public EntityFireEvent(Entity entity, int duration, net.minestom.server.utils.time.TimeUnit timeUnit) { + this(entity, Duration.ofMillis(timeUnit.toMilliseconds(duration))); } - public long getFireTime(TimeUnit timeUnit) { + public EntityFireEvent(Entity entity, Duration duration) { + this.entity = entity; + setFireTime(duration); + } + + /** + * @deprecated Replaced by {@link #getFireTime(TemporalUnit)} + */ + @SuppressWarnings("removal") + @Deprecated(forRemoval = true) + public long getFireTime(net.minestom.server.utils.time.TimeUnit timeUnit) { switch (timeUnit) { case TICK: - return duration; + return duration.toMillis() / Tick.TICKS.getDuration().toMillis(); case MILLISECOND: - return timeUnit.toMilliseconds(duration); + return duration.toMillis(); default: // Unexpected return -1; } } - public void setFireTime(int duration, TimeUnit timeUnit) { + public long getFireTime(TemporalUnit temporalUnit) { + return duration.toNanos() / temporalUnit.getDuration().toNanos(); + } + + /** + * @deprecated Replaced by {@link #setFireTime(Duration)} + */ + @SuppressWarnings("removal") + @Deprecated(forRemoval = true) + public void setFireTime(int duration, net.minestom.server.utils.time.TimeUnit timeUnit) { + setFireTime(Duration.ofMillis(timeUnit.toMilliseconds(duration))); + } + + public void setFireTime(Duration duration) { this.duration = duration; - this.timeUnit = timeUnit; } @Override From 4b04539086fac16fb2e4052e8c8935daf38636ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Tue, 29 Jun 2021 23:44:19 +0200 Subject: [PATCH 07/64] Included Tick.java from PR#205 with slight modifications --- .../arguments/minecraft/ArgumentTime.java | 4 +- .../server/event/entity/EntityFireEvent.java | 2 +- .../net/minestom/server/utils/time/Tick.java | 83 +++++++++++++++++-- 3 files changed, 78 insertions(+), 11 deletions(-) diff --git a/src/main/java/net/minestom/server/command/builder/arguments/minecraft/ArgumentTime.java b/src/main/java/net/minestom/server/command/builder/arguments/minecraft/ArgumentTime.java index 548448790..423916b4e 100644 --- a/src/main/java/net/minestom/server/command/builder/arguments/minecraft/ArgumentTime.java +++ b/src/main/java/net/minestom/server/command/builder/arguments/minecraft/ArgumentTime.java @@ -36,7 +36,7 @@ public class ArgumentTime extends Argument { TemporalUnit timeUnit; if (Character.isDigit(lastChar)) - timeUnit = Tick.TICKS; + timeUnit = Tick.SERVER_TICKS; else if (SUFFIXES.contains(lastChar)) { input = input.substring(0, input.length() - 1); @@ -45,7 +45,7 @@ public class ArgumentTime extends Argument { } else if (lastChar == 's') { timeUnit = ChronoUnit.SECONDS; } else if (lastChar == 't') { - timeUnit = Tick.TICKS; + timeUnit = Tick.SERVER_TICKS; } else { throw new ArgumentSyntaxException("Time needs to have the unit d, s, t, or none", input, NO_NUMBER); } diff --git a/src/main/java/net/minestom/server/event/entity/EntityFireEvent.java b/src/main/java/net/minestom/server/event/entity/EntityFireEvent.java index 9bcce987a..87a2fc59c 100644 --- a/src/main/java/net/minestom/server/event/entity/EntityFireEvent.java +++ b/src/main/java/net/minestom/server/event/entity/EntityFireEvent.java @@ -35,7 +35,7 @@ public class EntityFireEvent implements EntityEvent, CancellableEvent { public long getFireTime(net.minestom.server.utils.time.TimeUnit timeUnit) { switch (timeUnit) { case TICK: - return duration.toMillis() / Tick.TICKS.getDuration().toMillis(); + return duration.toMillis() / Tick.SERVER_TICKS.getDuration().toMillis(); case MILLISECOND: return duration.toMillis(); default: diff --git a/src/main/java/net/minestom/server/utils/time/Tick.java b/src/main/java/net/minestom/server/utils/time/Tick.java index 51dbb8391..e749e2018 100644 --- a/src/main/java/net/minestom/server/utils/time/Tick.java +++ b/src/main/java/net/minestom/server/utils/time/Tick.java @@ -6,15 +6,82 @@ import java.time.Duration; import java.time.temporal.Temporal; import java.time.temporal.TemporalUnit; -public class Tick implements TemporalUnit { - public static final Tick TICKS = new Tick(); +/** + * A TemporalUnit that represents one tick. + */ +public final class Tick implements TemporalUnit { + /** + * A TemporalUnit representing the server tick. This is defined using + * {@link MinecraftServer#TICK_MS}. + */ + public static Tick SERVER_TICKS = new Tick(MinecraftServer.TICK_MS); - private Tick() { + /** + * A TemporalUnit representing the client tick. This is always equal to 50ms. + */ + public static Tick CLIENT_TICKS = new Tick(50); + + private final long milliseconds; + private final int tps; + + /** + * Creates a new tick. + * + * @param length the length of the tick in milliseconds + */ + private Tick(long length) { + if (length <= 0) { + throw new IllegalArgumentException("length cannot be negative"); + } + + this.milliseconds = length; + this.tps = Math.toIntExact(Duration.ofSeconds(1).dividedBy(Duration.ofMillis(this.milliseconds))); + } + + /** + * Creates a duration from an amount of ticks. + * + * @param ticks the amount of ticks + * @return the duration + */ + public static Duration server(long ticks) { + return Duration.of(ticks, SERVER_TICKS); + } + + /** + * Creates a duration from an amount of client-side ticks. + * + * @param ticks the amount of ticks + * @return the duration + */ + public static Duration client(long ticks) { + return Duration.of(ticks, CLIENT_TICKS); + } + + /** + * Gets the number of whole ticks that occur in the provided duration. Note that this + * method returns an {@code int} as this is the unit that Minecraft stores ticks in. + * + * @param duration the duration + * @return the number of whole ticks in this duration + * @throws ArithmeticException if the duration is zero or an overflow occurs + */ + public int fromDuration(Duration duration) { + return Math.toIntExact(duration.dividedBy(this.getDuration())); + } + + /** + * Gets the whole number of these ticks that occur in one second. + * + * @return the number + */ + public int getTicksPerSecond() { + return this.tps; } @Override public Duration getDuration() { - return Duration.ofMillis(MinecraftServer.TICK_MS); + return Duration.ofMillis(this.milliseconds); } @Override @@ -32,14 +99,14 @@ public class Tick implements TemporalUnit { return true; } + @SuppressWarnings("unchecked") // following ChronoUnit#addTo @Override public R addTo(R temporal, long amount) { - //noinspection unchecked return (R) temporal.plus(amount, this); } @Override - public long between(Temporal temporal1Inclusive, Temporal temporal2Exclusive) { - return temporal1Inclusive.until(temporal2Exclusive, this); + public long between(Temporal start, Temporal end) { + return start.until(end, this); } -} +} \ No newline at end of file From ff4bb20a04c1da2fa490295209d1432163fe9df6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Wed, 30 Jun 2021 00:14:26 +0200 Subject: [PATCH 08/64] Repurpose TimeUnit.java --- .../minestom/server/utils/time/TimeUnit.java | 44 +++++-------------- 1 file changed, 10 insertions(+), 34 deletions(-) diff --git a/src/main/java/net/minestom/server/utils/time/TimeUnit.java b/src/main/java/net/minestom/server/utils/time/TimeUnit.java index 73d00450f..ea5487edc 100644 --- a/src/main/java/net/minestom/server/utils/time/TimeUnit.java +++ b/src/main/java/net/minestom/server/utils/time/TimeUnit.java @@ -1,38 +1,14 @@ package net.minestom.server.utils.time; -import net.minestom.server.MinecraftServer; - -/** - * @deprecated Replaced by {@link java.time.temporal.TemporalUnit} - */ -@Deprecated(forRemoval = true) -public enum TimeUnit { - - TICK, DAY, HOUR, MINUTE, SECOND, MILLISECOND; - - /** - * Converts a value and its unit to milliseconds. - * - * @param value the time value - * @return the converted milliseconds based on the time value and the unit - */ - public long toMilliseconds(long value) { - switch (this) { - case TICK: - return MinecraftServer.TICK_MS * value; - case DAY: - return value * 86_400_000; - case HOUR: - return value * 3_600_000; - case MINUTE: - return value * 60_000; - case SECOND: - return value * 1000; - case MILLISECOND: - return value; - default: - return -1; // Unexpected - } - } +import java.time.temporal.ChronoUnit; +import java.time.temporal.TemporalUnit; +public class TimeUnit { + public static final TemporalUnit DAY = ChronoUnit.DAYS; + public static final TemporalUnit HOUR = ChronoUnit.HOURS; + public static final TemporalUnit MINUTE = ChronoUnit.MINUTES; + public static final TemporalUnit SECOND = ChronoUnit.SECONDS; + public static final TemporalUnit MILLISECOND = ChronoUnit.MILLIS; + public static final TemporalUnit SERVER_TICK = Tick.SERVER_TICKS; + public static final TemporalUnit CLIENT_TICK = Tick.CLIENT_TICKS; } From e23b65c2df4c7d55c8024ed9d9ed0bc0bc15778e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Wed, 30 Jun 2021 00:29:44 +0200 Subject: [PATCH 09/64] Updated OpenToLANConfig.java --- .../minestom/server/extras/lan/OpenToLANConfig.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/net/minestom/server/extras/lan/OpenToLANConfig.java b/src/main/java/net/minestom/server/extras/lan/OpenToLANConfig.java index 5290567e0..61a5e5d6a 100644 --- a/src/main/java/net/minestom/server/extras/lan/OpenToLANConfig.java +++ b/src/main/java/net/minestom/server/extras/lan/OpenToLANConfig.java @@ -2,10 +2,10 @@ package net.minestom.server.extras.lan; import net.minestom.server.event.server.ServerListPingEvent; import net.minestom.server.utils.time.TimeUnit; -import net.minestom.server.utils.time.UpdateOption; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; +import java.time.Duration; import java.util.Objects; /** @@ -15,7 +15,7 @@ import java.util.Objects; */ public class OpenToLANConfig { int port; - UpdateOption delayBetweenPings, delayBetweenEvent; + Duration delayBetweenPings, delayBetweenEvent; /** * Creates a new config with the port set to random and the delay between pings set @@ -23,8 +23,8 @@ public class OpenToLANConfig { */ public OpenToLANConfig() { this.port = 0; - this.delayBetweenPings = new UpdateOption(1500, TimeUnit.MILLISECOND); - this.delayBetweenEvent = new UpdateOption(30, TimeUnit.SECOND); + this.delayBetweenPings = Duration.of(1500, TimeUnit.MILLISECOND); + this.delayBetweenEvent = Duration.of(30, TimeUnit.SECOND); } /** @@ -46,7 +46,7 @@ public class OpenToLANConfig { * @return {@code this}, for chaining */ @Contract("_ -> this") - public @NotNull OpenToLANConfig pingDelay(@NotNull UpdateOption delay) { + public @NotNull OpenToLANConfig pingDelay(@NotNull Duration delay) { this.delayBetweenPings = Objects.requireNonNull(delay, "delay"); return this; } @@ -58,7 +58,7 @@ public class OpenToLANConfig { * @return {@code this}, for chaining */ @Contract("_ -> this") - public @NotNull OpenToLANConfig eventCallDelay(@NotNull UpdateOption delay) { + public @NotNull OpenToLANConfig eventCallDelay(@NotNull Duration delay) { this.delayBetweenEvent = Objects.requireNonNull(delay, "delay"); return this; } From 7b126f5d13a9905644ee260978fce9216f2684a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Wed, 30 Jun 2021 00:32:06 +0200 Subject: [PATCH 10/64] Updated BenchmarkManager.java --- .../net/minestom/server/monitoring/BenchmarkManager.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/minestom/server/monitoring/BenchmarkManager.java b/src/main/java/net/minestom/server/monitoring/BenchmarkManager.java index 7521b6a8e..389147358 100644 --- a/src/main/java/net/minestom/server/monitoring/BenchmarkManager.java +++ b/src/main/java/net/minestom/server/monitoring/BenchmarkManager.java @@ -7,13 +7,13 @@ import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.format.NamedTextColor; import net.minestom.server.MinecraftServer; import net.minestom.server.utils.MathUtils; -import net.minestom.server.utils.time.UpdateOption; import net.minestom.server.utils.validate.Check; import org.jetbrains.annotations.NotNull; import java.lang.management.ManagementFactory; import java.lang.management.ThreadInfo; import java.lang.management.ThreadMXBean; +import java.time.Duration; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -25,7 +25,7 @@ import static net.minestom.server.MinecraftServer.*; /** * Small monitoring tools that can be used to check the current memory usage and Minestom threads CPU usage. *

- * Needs to be enabled with {@link #enable(UpdateOption)}. Memory can then be accessed with {@link #getUsedMemory()} + * Needs to be enabled with {@link #enable(Duration)}. Memory can then be accessed with {@link #getUsedMemory()} * and the CPUs usage with {@link #getResultMap()} or {@link #getCpuMonitoringMessage()}. *

* Be aware that this is not the most accurate method, you should use a proper java profiler depending on your needs. @@ -57,10 +57,10 @@ public final class BenchmarkManager { private long time; - public void enable(@NotNull UpdateOption updateOption) { + public void enable(@NotNull Duration duration) { Check.stateCondition(enabled, "A benchmark is already running, please disable it first."); - time = updateOption.getTimeUnit().toMilliseconds(updateOption.getValue()); + time = duration.toMillis(); final Thread thread = new Thread(null, () -> { From 7f9737d866f54ba64566e32ec28c2673d24178fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Wed, 30 Jun 2021 00:46:08 +0200 Subject: [PATCH 11/64] Updated UpdateOption.java so it doesn't break with the TimeUnit.java change --- .../server/utils/time/UpdateOption.java | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/main/java/net/minestom/server/utils/time/UpdateOption.java b/src/main/java/net/minestom/server/utils/time/UpdateOption.java index 603d3de13..91bf9895e 100644 --- a/src/main/java/net/minestom/server/utils/time/UpdateOption.java +++ b/src/main/java/net/minestom/server/utils/time/UpdateOption.java @@ -2,6 +2,7 @@ package net.minestom.server.utils.time; import org.jetbrains.annotations.NotNull; +import java.time.temporal.TemporalUnit; import java.util.Objects; /** @@ -11,11 +12,11 @@ import java.util.Objects; public class UpdateOption { private final long value; - private final TimeUnit timeUnit; + private final TemporalUnit temporalUnit; - public UpdateOption(long value, @NotNull TimeUnit timeUnit) { + public UpdateOption(long value, @NotNull TemporalUnit temporalUnit) { this.value = value; - this.timeUnit = timeUnit; + this.temporalUnit = temporalUnit; } public long getValue() { @@ -23,13 +24,13 @@ public class UpdateOption { } @NotNull - public TimeUnit getTimeUnit() { - return timeUnit; + public TemporalUnit getTemporalUnit() { + return temporalUnit; } @Override public int hashCode() { - return Objects.hash(value, timeUnit); + return Objects.hash(value, temporalUnit); } @Override @@ -37,7 +38,7 @@ public class UpdateOption { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; UpdateOption updateOption = (UpdateOption) o; - return Objects.equals(value, updateOption.value) && Objects.equals(timeUnit, updateOption.timeUnit); + return Objects.equals(value, updateOption.value) && Objects.equals(temporalUnit, updateOption.temporalUnit); } /** @@ -46,6 +47,6 @@ public class UpdateOption { * @return the converted milliseconds based on the time value and the unit */ public long toMilliseconds() { - return timeUnit.toMilliseconds(value); + return temporalUnit.getDuration().multipliedBy(value).toMillis(); } } From 58bae20f10b5192173236a9ffa6db85d95a22e71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Wed, 30 Jun 2021 00:47:57 +0200 Subject: [PATCH 12/64] Updated Main.java --- src/test/java/demo/Main.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/test/java/demo/Main.java b/src/test/java/demo/Main.java index c3cec28cc..8347e5018 100644 --- a/src/test/java/demo/Main.java +++ b/src/test/java/demo/Main.java @@ -22,7 +22,8 @@ import net.minestom.server.storage.StorageManager; import net.minestom.server.storage.systems.FileStorageSystem; import net.minestom.server.utils.identity.NamedAndIdentified; import net.minestom.server.utils.time.TimeUnit; -import net.minestom.server.utils.time.UpdateOption; + +import java.time.Duration; public class Main { @@ -62,7 +63,7 @@ public class Main { StorageManager storageManager = MinecraftServer.getStorageManager(); storageManager.defineDefaultStorageSystem(FileStorageSystem::new); - MinecraftServer.getBenchmarkManager().enable(new UpdateOption(10 * 1000, TimeUnit.MILLISECOND)); + MinecraftServer.getBenchmarkManager().enable(Duration.of(10 * 1000, TimeUnit.MILLISECOND)); MinecraftServer.getSchedulerManager().buildShutdownTask(() -> System.out.println("Good night")).schedule(); @@ -110,7 +111,7 @@ public class Main { //MojangAuth.init(); // useful for testing - we don't need to worry about event calls so just set this to a long time - OpenToLAN.open(new OpenToLANConfig().eventCallDelay(new UpdateOption(1, TimeUnit.DAY))); + OpenToLAN.open(new OpenToLANConfig().eventCallDelay(Duration.of(1, TimeUnit.DAY))); minecraftServer.start("0.0.0.0", 25565); //Runtime.getRuntime().addShutdownHook(new Thread(MinecraftServer::stopCleanly)); From 384c556caf0b218451b851149637b861145321d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Wed, 30 Jun 2021 00:51:22 +0200 Subject: [PATCH 13/64] Updated LivingEntity.java --- .../minestom/server/entity/LivingEntity.java | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/LivingEntity.java b/src/main/java/net/minestom/server/entity/LivingEntity.java index f1fd8642e..6fc129abc 100644 --- a/src/main/java/net/minestom/server/entity/LivingEntity.java +++ b/src/main/java/net/minestom/server/entity/LivingEntity.java @@ -30,11 +30,14 @@ import net.minestom.server.utils.Position; import net.minestom.server.utils.Vector; import net.minestom.server.utils.block.BlockIterator; import net.minestom.server.utils.time.Cooldown; +import net.minestom.server.utils.time.Tick; import net.minestom.server.utils.time.TimeUnit; -import net.minestom.server.utils.time.UpdateOption; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.time.Duration; +import java.time.temporal.ChronoUnit; +import java.time.temporal.TemporalUnit; import java.util.*; import java.util.concurrent.ConcurrentHashMap; @@ -42,7 +45,7 @@ public class LivingEntity extends Entity implements EquipmentHandler { // ItemStack pickup protected boolean canPickupItem; - protected Cooldown itemPickupCooldown = new Cooldown(new UpdateOption(5, TimeUnit.TICK)); + protected Cooldown itemPickupCooldown = new Cooldown(Duration.of(5, Tick.SERVER_TICKS)); protected boolean isDead; @@ -316,23 +319,23 @@ public class LivingEntity extends Entity implements EquipmentHandler { * @param duration duration in ticks of the effect */ public void setFireForDuration(int duration) { - setFireForDuration(duration, TimeUnit.TICK); + setFireForDuration(duration, Tick.SERVER_TICKS); } /** * Sets fire to this entity for a given duration. * * @param duration duration of the effect - * @param unit unit used to express the duration + * @param temporalUnit unit used to express the duration * @see #setOnFire(boolean) if you want it to be permanent without any event callback */ - public void setFireForDuration(int duration, TimeUnit unit) { - EntityFireEvent entityFireEvent = new EntityFireEvent(this, duration, unit); + public void setFireForDuration(int duration, TemporalUnit temporalUnit) { + EntityFireEvent entityFireEvent = new EntityFireEvent(this, temporalUnit.getDuration().multipliedBy(duration)); // Do not start fire event if the fire needs to be removed (< 0 duration) if (duration > 0) { EventDispatcher.callCancellable(entityFireEvent, () -> { - final long fireTime = entityFireEvent.getFireTime(TimeUnit.MILLISECOND); + final long fireTime = entityFireEvent.getFireTime(ChronoUnit.MILLIS); setOnFire(true); fireExtinguishTime = System.currentTimeMillis() + fireTime; }); @@ -659,7 +662,7 @@ public class LivingEntity extends Entity implements EquipmentHandler { * Gets the time in ms between two fire damage applications. * * @return the time in ms - * @see #setFireDamagePeriod(long, TimeUnit) + * @see #setFireDamagePeriod(long, TemporalUnit) */ public long getFireDamagePeriod() { return fireDamagePeriod; @@ -669,10 +672,10 @@ public class LivingEntity extends Entity implements EquipmentHandler { * Changes the delay between two fire damage applications. * * @param fireDamagePeriod the delay - * @param timeUnit the time unit + * @param temporalUnit the time unit */ - public void setFireDamagePeriod(long fireDamagePeriod, @NotNull TimeUnit timeUnit) { - fireDamagePeriod = timeUnit.toMilliseconds(fireDamagePeriod); + public void setFireDamagePeriod(long fireDamagePeriod, @NotNull TemporalUnit temporalUnit) { + fireDamagePeriod = temporalUnit.getDuration().multipliedBy(fireDamagePeriod).toMillis(); this.fireDamagePeriod = fireDamagePeriod; } From 0c849c005b67c4b62c1aca226ecbf0b6c0bdd868 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Wed, 30 Jun 2021 00:52:16 +0200 Subject: [PATCH 14/64] Optimized imports of LivingEntity.java --- src/main/java/net/minestom/server/entity/LivingEntity.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/net/minestom/server/entity/LivingEntity.java b/src/main/java/net/minestom/server/entity/LivingEntity.java index 6fc129abc..18a8fdb12 100644 --- a/src/main/java/net/minestom/server/entity/LivingEntity.java +++ b/src/main/java/net/minestom/server/entity/LivingEntity.java @@ -31,7 +31,6 @@ import net.minestom.server.utils.Vector; import net.minestom.server.utils.block.BlockIterator; import net.minestom.server.utils.time.Cooldown; import net.minestom.server.utils.time.Tick; -import net.minestom.server.utils.time.TimeUnit; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; From 67dc8bf9965bc9fde68f490a2a52d6b45381dfb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Wed, 30 Jun 2021 00:59:26 +0200 Subject: [PATCH 15/64] Updated Entity.java --- .../net/minestom/server/entity/Entity.java | 39 +++++++++++++------ 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/Entity.java b/src/main/java/net/minestom/server/entity/Entity.java index 3878aa7f9..5b19f3132 100644 --- a/src/main/java/net/minestom/server/entity/Entity.java +++ b/src/main/java/net/minestom/server/entity/Entity.java @@ -45,14 +45,15 @@ import net.minestom.server.utils.chunk.ChunkUtils; import net.minestom.server.utils.entity.EntityUtils; import net.minestom.server.utils.player.PlayerUtils; import net.minestom.server.utils.time.Cooldown; -import net.minestom.server.utils.time.TimeUnit; -import net.minestom.server.utils.time.UpdateOption; import net.minestom.server.utils.validate.Check; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jglrxavpok.hephaistos.nbt.NBTCompound; +import java.time.Duration; +import java.time.temporal.ChronoUnit; +import java.time.temporal.TemporalUnit; import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentLinkedQueue; @@ -127,8 +128,8 @@ public class Entity implements Viewable, Tickable, EventHandler, Da protected EntityType entityType; // UNSAFE to change, modify at your own risk // Network synchronization, send the absolute position of the entity each X milliseconds - private static final UpdateOption SYNCHRONIZATION_COOLDOWN = new UpdateOption(1, TimeUnit.MINUTE); - private UpdateOption customSynchronizationCooldown; + private static final Duration SYNCHRONIZATION_COOLDOWN = Duration.of(1, ChronoUnit.MINUTES); + private Duration customSynchronizationCooldown; private long lastAbsoluteSynchronizationTime; // Events @@ -1142,7 +1143,7 @@ public class Entity implements Viewable, Tickable, EventHandler, Da * Sets the entity in fire visually. *

* WARNING: if you want to apply damage or specify a duration, - * see {@link LivingEntity#setFireForDuration(int, TimeUnit)}. + * see {@link LivingEntity#setFireForDuration(int, TemporalUnit)}. * * @param fire should the entity be set in fire */ @@ -1528,18 +1529,18 @@ public class Entity implements Viewable, Tickable, EventHandler, Da * * @param delay the time before removing the entity, * 0 to cancel the removing - * @param timeUnit the unit of the delay + * @param temporalUnit the unit of the delay */ - public void scheduleRemove(long delay, @NotNull TimeUnit timeUnit) { + public void scheduleRemove(long delay, @NotNull TemporalUnit temporalUnit) { if (delay == 0) { // Cancel the scheduled remove this.scheduledRemoveTime = 0; return; } - this.scheduledRemoveTime = System.currentTimeMillis() + timeUnit.toMilliseconds(delay); + this.scheduledRemoveTime = System.currentTimeMillis() + temporalUnit.getDuration().multipliedBy(delay).toMillis(); } /** - * Gets if the entity removal has been scheduled with {@link #scheduleRemove(long, TimeUnit)}. + * Gets if the entity removal has been scheduled with {@link #scheduleRemove(long, TemporalUnit)}. * * @return true if the entity removal has been scheduled */ @@ -1609,8 +1610,24 @@ public class Entity implements Viewable, Tickable, EventHandler, Da * Set custom cooldown for position synchronization. * * @param cooldown custom cooldown for position synchronization. + * @deprecated Replaced by {@link #setCustomSynchronizationCooldown(Duration)} */ - public void setCustomSynchronizationCooldown(@Nullable UpdateOption cooldown) { + @SuppressWarnings("removal") + @Deprecated(forRemoval = true) + public void setCustomSynchronizationCooldown(@Nullable net.minestom.server.utils.time.UpdateOption cooldown) { + setCustomSynchronizationCooldown(cooldown == null ? null : Duration.ofMillis(cooldown.toMilliseconds())); + } + + /** + * Set custom cooldown for position synchronization. + * + * @param cooldown custom cooldown for position synchronization. + */ + public void setCustomSynchronizationCooldown(@Nullable Duration cooldown) { + if (cooldown == null) { + this.customSynchronizationCooldown = null; + return; + } this.customSynchronizationCooldown = cooldown; } @@ -1619,7 +1636,7 @@ public class Entity implements Viewable, Tickable, EventHandler, Da return HoverEvent.showEntity(ShowEntity.of(this.entityType, this.uuid)); } - private UpdateOption getSynchronizationCooldown() { + private Duration getSynchronizationCooldown() { return Objects.requireNonNullElse(this.customSynchronizationCooldown, SYNCHRONIZATION_COOLDOWN); } From 580509c42013f303f1a31d42ad205bb2b1b916ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Wed, 30 Jun 2021 01:05:20 +0200 Subject: [PATCH 16/64] Added UpdateOption#toDuration method --- .../java/net/minestom/server/utils/time/UpdateOption.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/net/minestom/server/utils/time/UpdateOption.java b/src/main/java/net/minestom/server/utils/time/UpdateOption.java index 91bf9895e..8fb693e42 100644 --- a/src/main/java/net/minestom/server/utils/time/UpdateOption.java +++ b/src/main/java/net/minestom/server/utils/time/UpdateOption.java @@ -2,6 +2,7 @@ package net.minestom.server.utils.time; import org.jetbrains.annotations.NotNull; +import java.time.Duration; import java.time.temporal.TemporalUnit; import java.util.Objects; @@ -49,4 +50,8 @@ public class UpdateOption { public long toMilliseconds() { return temporalUnit.getDuration().multipliedBy(value).toMillis(); } + + public Duration toDuration() { + return Duration.ofMillis(toMilliseconds()); + } } From 30abfce554a9230717fa1e684c2843ca450654d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Wed, 30 Jun 2021 01:07:38 +0200 Subject: [PATCH 17/64] Updated Cooldown.java --- .../minestom/server/utils/time/Cooldown.java | 41 +++---------------- 1 file changed, 5 insertions(+), 36 deletions(-) diff --git a/src/main/java/net/minestom/server/utils/time/Cooldown.java b/src/main/java/net/minestom/server/utils/time/Cooldown.java index 6f3703b48..0ee9efd9b 100644 --- a/src/main/java/net/minestom/server/utils/time/Cooldown.java +++ b/src/main/java/net/minestom/server/utils/time/Cooldown.java @@ -5,7 +5,6 @@ import org.jetbrains.annotations.NotNull; import java.time.Duration; import java.time.temporal.TemporalUnit; -@SuppressWarnings("removal") public final class Cooldown { private final Duration duration; @@ -14,14 +13,15 @@ public final class Cooldown { /** * @deprecated Replaced by {@link #Cooldown(Duration)} */ + @SuppressWarnings("removal") @Deprecated(forRemoval = true) public Cooldown(@NotNull UpdateOption updateOption) { - this.duration = Duration.ofMillis(updateOption.toMilliseconds()); - this.lastUpdate = System.currentTimeMillis(); + this(updateOption.toDuration()); } public Cooldown(Duration duration) { this.duration = duration; + this.lastUpdate = System.currentTimeMillis(); } public Duration getDuration() { @@ -36,23 +36,6 @@ public final class Cooldown { return !hasCooldown(time, lastUpdate, duration); } - /** - * Gets if something is in cooldown based on the current time. - * - * @param currentTime the current time in milliseconds - * @param lastUpdate the last update in milliseconds - * @param timeUnit the time unit of the cooldown - * @param cooldown the value of the cooldown - * @return true if the cooldown is in progress, false otherwise - * - * @deprecated Replaced by {@link #hasCooldown(long, long, TemporalUnit, long)} - */ - @Deprecated(forRemoval = true) - public static boolean hasCooldown(long currentTime, long lastUpdate, @NotNull TimeUnit timeUnit, long cooldown) { - final long cooldownMs = timeUnit.toMilliseconds(cooldown); - return currentTime - lastUpdate < cooldownMs; - } - /** * Gets if something is in cooldown based on the current time. * @@ -76,9 +59,10 @@ public final class Cooldown { * * @deprecated Replaced by {@link #hasCooldown(long, long, Duration)} */ + @SuppressWarnings("removal") @Deprecated(forRemoval = true) public static boolean hasCooldown(long currentTime, long lastUpdate, @NotNull UpdateOption updateOption) { - return hasCooldown(currentTime, lastUpdate, updateOption.getTimeUnit(), updateOption.getValue()); + return hasCooldown(currentTime, lastUpdate, updateOption.toDuration()); } /** @@ -94,21 +78,6 @@ public final class Cooldown { return currentTime - lastUpdate < cooldownMs; } - /** - * Gets if something is in cooldown based on the current time ({@link System#currentTimeMillis()}). - * - * @param lastUpdate the last update in milliseconds - * @param timeUnit the time unit of the cooldown - * @param cooldown the value of the cooldown - * @return true if the cooldown is in progress, false otherwise - * - * @deprecated Replaced by {@link #hasCooldown(long, TemporalUnit, int)} - */ - @Deprecated(forRemoval = true) - public static boolean hasCooldown(long lastUpdate, @NotNull TimeUnit timeUnit, int cooldown) { - return hasCooldown(System.currentTimeMillis(), lastUpdate, timeUnit, cooldown); - } - /** * Gets if something is in cooldown based on the current time ({@link System#currentTimeMillis()}). * From d522730cd78c6a98d9ec35bc1add6b9bcceded49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Wed, 30 Jun 2021 01:15:54 +0200 Subject: [PATCH 18/64] Updated FollowTargetGoal.java --- .../entity/ai/goal/FollowTargetGoal.java | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/ai/goal/FollowTargetGoal.java b/src/main/java/net/minestom/server/entity/ai/goal/FollowTargetGoal.java index 4dbe90ecd..47a895279 100644 --- a/src/main/java/net/minestom/server/entity/ai/goal/FollowTargetGoal.java +++ b/src/main/java/net/minestom/server/entity/ai/goal/FollowTargetGoal.java @@ -6,12 +6,13 @@ import net.minestom.server.entity.ai.GoalSelector; import net.minestom.server.entity.pathfinding.Navigator; import net.minestom.server.utils.MathUtils; import net.minestom.server.utils.Position; -import net.minestom.server.utils.time.UpdateOption; import org.jetbrains.annotations.NotNull; +import java.time.Duration; + public class FollowTargetGoal extends GoalSelector { - private final UpdateOption pathUpdateOption; + private final Duration pathDuration; private long lastUpdateTime = 0; private boolean forceEnd = false; private Position lastTargetPos; @@ -21,10 +22,24 @@ public class FollowTargetGoal extends GoalSelector { * * @param entityCreature the entity * @param pathUpdateOption the time between each path update (to check if the target moved) + * + * @deprecated Replaced by {@link #FollowTargetGoal(EntityCreature, Duration)} */ - public FollowTargetGoal(@NotNull EntityCreature entityCreature, @NotNull UpdateOption pathUpdateOption) { + @SuppressWarnings("removal") + @Deprecated(forRemoval = true) + public FollowTargetGoal(@NotNull EntityCreature entityCreature, @NotNull net.minestom.server.utils.time.UpdateOption pathUpdateOption) { + this(entityCreature, pathUpdateOption.toDuration()); + } + + /** + * Creates a follow target goal object. + * + * @param entityCreature the entity + * @param pathDuration the time between each path update (to check if the target moved) + */ + public FollowTargetGoal(@NotNull EntityCreature entityCreature, @NotNull Duration pathDuration) { super(entityCreature); - this.pathUpdateOption = pathUpdateOption; + this.pathDuration = pathDuration; } @Override @@ -64,8 +79,8 @@ public class FollowTargetGoal extends GoalSelector { @Override public void tick(long time) { if (forceEnd || - pathUpdateOption.getValue() == 0 || - pathUpdateOption.getTimeUnit().toMilliseconds(pathUpdateOption.getValue()) + lastUpdateTime > time) { + pathDuration.isZero() || + pathDuration.toMillis() + lastUpdateTime > time) { return; } Position targetPos = entityCreature.getTarget() != null ? entityCreature.getTarget().getPosition() : null; From 6db9ba63f4ac274e36f386b168da88b95db5af6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Wed, 30 Jun 2021 01:20:52 +0200 Subject: [PATCH 19/64] Updated EntityFireEvent.java --- .../server/event/entity/EntityFireEvent.java | 33 +++---------------- 1 file changed, 4 insertions(+), 29 deletions(-) diff --git a/src/main/java/net/minestom/server/event/entity/EntityFireEvent.java b/src/main/java/net/minestom/server/event/entity/EntityFireEvent.java index 87a2fc59c..3eac073d4 100644 --- a/src/main/java/net/minestom/server/event/entity/EntityFireEvent.java +++ b/src/main/java/net/minestom/server/event/entity/EntityFireEvent.java @@ -3,7 +3,6 @@ package net.minestom.server.event.entity; import net.minestom.server.entity.Entity; import net.minestom.server.event.trait.CancellableEvent; import net.minestom.server.event.trait.EntityEvent; -import net.minestom.server.utils.time.Tick; import org.jetbrains.annotations.NotNull; import java.time.Duration; @@ -16,10 +15,8 @@ public class EntityFireEvent implements EntityEvent, CancellableEvent { private boolean cancelled; - @SuppressWarnings("removal") - @Deprecated(forRemoval = true) - public EntityFireEvent(Entity entity, int duration, net.minestom.server.utils.time.TimeUnit timeUnit) { - this(entity, Duration.ofMillis(timeUnit.toMilliseconds(duration))); + public EntityFireEvent(Entity entity, int duration, TemporalUnit temporalUnit) { + this(entity, Duration.of(duration, temporalUnit)); } public EntityFireEvent(Entity entity, Duration duration) { @@ -27,34 +24,12 @@ public class EntityFireEvent implements EntityEvent, CancellableEvent { setFireTime(duration); } - /** - * @deprecated Replaced by {@link #getFireTime(TemporalUnit)} - */ - @SuppressWarnings("removal") - @Deprecated(forRemoval = true) - public long getFireTime(net.minestom.server.utils.time.TimeUnit timeUnit) { - switch (timeUnit) { - case TICK: - return duration.toMillis() / Tick.SERVER_TICKS.getDuration().toMillis(); - case MILLISECOND: - return duration.toMillis(); - default: - // Unexpected - return -1; - } - } - public long getFireTime(TemporalUnit temporalUnit) { return duration.toNanos() / temporalUnit.getDuration().toNanos(); } - /** - * @deprecated Replaced by {@link #setFireTime(Duration)} - */ - @SuppressWarnings("removal") - @Deprecated(forRemoval = true) - public void setFireTime(int duration, net.minestom.server.utils.time.TimeUnit timeUnit) { - setFireTime(Duration.ofMillis(timeUnit.toMilliseconds(duration))); + public void setFireTime(int duration, TemporalUnit temporalUnit) { + setFireTime(Duration.of(duration, temporalUnit)); } public void setFireTime(Duration duration) { From 56017b90e28a787fc00ec218bc5a5c6706c24683 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Wed, 30 Jun 2021 01:29:02 +0200 Subject: [PATCH 20/64] Updated ItemEntity.java --- .../minestom/server/entity/ItemEntity.java | 36 +++++++++++++------ 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/ItemEntity.java b/src/main/java/net/minestom/server/entity/ItemEntity.java index 275348262..a2ddbec69 100644 --- a/src/main/java/net/minestom/server/entity/ItemEntity.java +++ b/src/main/java/net/minestom/server/entity/ItemEntity.java @@ -10,10 +10,11 @@ import net.minestom.server.item.StackingRule; import net.minestom.server.utils.Position; import net.minestom.server.utils.time.Cooldown; import net.minestom.server.utils.time.TimeUnit; -import net.minestom.server.utils.time.UpdateOption; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.time.Duration; +import java.time.temporal.TemporalUnit; import java.util.Set; /** @@ -24,7 +25,7 @@ public class ItemEntity extends Entity { /** * Used to slow down the merge check delay */ - private static UpdateOption mergeUpdateOption = new UpdateOption(10, TimeUnit.TICK); + private static Duration mergeDelay = Duration.of(10, TimeUnit.SERVER_TICK); /** * The last time that this item has checked his neighbors for merge @@ -60,8 +61,8 @@ public class ItemEntity extends Entity { * @return the merge update option */ @Nullable - public static UpdateOption getMergeUpdateOption() { - return mergeUpdateOption; + public static Duration getMergeDelay() { + return mergeDelay; } /** @@ -69,15 +70,28 @@ public class ItemEntity extends Entity { * Can be set to null to entirely remove the delay. * * @param mergeUpdateOption the new merge update option + * + * @deprecated Replaced by {@link #setMergeDelay(Duration)} */ - public static void setMergeUpdateOption(@Nullable UpdateOption mergeUpdateOption) { - ItemEntity.mergeUpdateOption = mergeUpdateOption; + @SuppressWarnings("removal") + public static void setMergeUpdateOption(@Nullable net.minestom.server.utils.time.UpdateOption mergeUpdateOption) { + setMergeDelay(mergeUpdateOption == null ? null : mergeUpdateOption.toDuration()); + } + + /** + * Changes the merge delay. + * Can be set to null to entirely remove the delay. + * + * @param delay the new merge delay + */ + public static void setMergeDelay(@Nullable Duration delay) { + ItemEntity.mergeDelay = delay; } @Override public void update(long time) { if (isMergeable() && isPickable() && - (mergeUpdateOption == null || !Cooldown.hasCooldown(time, lastMergeCheck, mergeUpdateOption))) { + (mergeDelay == null || !Cooldown.hasCooldown(time, lastMergeCheck, mergeDelay))) { this.lastMergeCheck = time; final Chunk chunk = instance.getChunkAt(getPosition()); @@ -213,7 +227,7 @@ public class ItemEntity extends Entity { } /** - * Gets the pickup delay in milliseconds, defined by {@link #setPickupDelay(long, TimeUnit)}. + * Gets the pickup delay in milliseconds, defined by {@link #setPickupDelay(long, TemporalUnit)}. * * @return the pickup delay */ @@ -225,10 +239,10 @@ public class ItemEntity extends Entity { * Sets the pickup delay of the ItemEntity. * * @param delay the pickup delay - * @param timeUnit the unit of the delay + * @param temporalUnit the unit of the delay */ - public void setPickupDelay(long delay, @NotNull TimeUnit timeUnit) { - this.pickupDelay = timeUnit.toMilliseconds(delay); + public void setPickupDelay(long delay, @NotNull TemporalUnit temporalUnit) { + this.pickupDelay = temporalUnit.getDuration().multipliedBy(delay).toMillis(); } /** From 7e82c02536b413e73ee12da4060d273b4ad1c50e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Wed, 30 Jun 2021 01:31:09 +0200 Subject: [PATCH 21/64] Updated Player.java --- src/main/java/net/minestom/server/entity/Player.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 0b875b1fd..ccd89ab08 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -76,7 +76,6 @@ import net.minestom.server.utils.instance.InstanceUtils; import net.minestom.server.utils.inventory.PlayerInventoryUtils; import net.minestom.server.utils.time.Cooldown; import net.minestom.server.utils.time.TimeUnit; -import net.minestom.server.utils.time.UpdateOption; import net.minestom.server.utils.validate.Check; import net.minestom.server.world.DimensionType; import org.jetbrains.annotations.ApiStatus; @@ -84,6 +83,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.nio.charset.StandardCharsets; +import java.time.Duration; import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentLinkedQueue; @@ -157,7 +157,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable, private final Set targetBreakers = Collections.singleton(this); // Experience orb pickup - protected Cooldown experiencePickupCooldown = new Cooldown(new UpdateOption(10, TimeUnit.TICK)); + protected Cooldown experiencePickupCooldown = new Cooldown(Duration.of(10, TimeUnit.SERVER_TICK)); private BelowNameTag belowNameTag; From b8b8a40a99456aefc3888509a35e5bc7600e9ab4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Wed, 30 Jun 2021 01:33:13 +0200 Subject: [PATCH 22/64] Updated CombinedAttackGoal.java --- .../server/entity/ai/goal/CombinedAttackGoal.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/ai/goal/CombinedAttackGoal.java b/src/main/java/net/minestom/server/entity/ai/goal/CombinedAttackGoal.java index 88dcfd66e..99629f64e 100644 --- a/src/main/java/net/minestom/server/entity/ai/goal/CombinedAttackGoal.java +++ b/src/main/java/net/minestom/server/entity/ai/goal/CombinedAttackGoal.java @@ -9,10 +9,11 @@ import net.minestom.server.entity.EntityProjectile; import net.minestom.server.utils.Position; import net.minestom.server.utils.time.Cooldown; import net.minestom.server.utils.time.TimeUnit; -import net.minestom.server.utils.time.UpdateOption; import net.minestom.server.utils.validate.Check; import org.jetbrains.annotations.NotNull; +import java.time.Duration; +import java.time.temporal.TemporalUnit; import java.util.function.Function; /** @@ -20,16 +21,16 @@ import java.util.function.Function; */ public class CombinedAttackGoal extends GoalSelector { - private final Cooldown cooldown = new Cooldown(new UpdateOption(5, TimeUnit.TICK)); + private final Cooldown cooldown = new Cooldown(Duration.of(5, TimeUnit.SERVER_TICK)); private final int meleeRangeSquared; private final int meleeDelay; - private final TimeUnit meleeTimeUnit; + private final TemporalUnit meleeTimeUnit; private final int rangedRangeSquared; private final double rangedPower; private final double rangedSpread; private final int rangedDelay; - private final TimeUnit rangedTimeUnit; + private final TemporalUnit rangedTimeUnit; private final int desirableRangeSquared; private final boolean comeClose; @@ -52,7 +53,7 @@ public class CombinedAttackGoal extends GoalSelector { */ public CombinedAttackGoal(@NotNull EntityCreature entityCreature, int meleeRange, int rangedRange, double rangedPower, double rangedSpread, - int delay, TimeUnit timeUnit, + int delay, TemporalUnit timeUnit, int desirableRange, boolean comeClose) { this( entityCreature, @@ -76,8 +77,8 @@ public class CombinedAttackGoal extends GoalSelector { * @param comeClose if entity should go as close as possible to the target whether target is not in line of sight for a ranged attack. */ public CombinedAttackGoal(@NotNull EntityCreature entityCreature, - int meleeRange, int meleeDelay, TimeUnit meleeTimeUnit, - int rangedRange, double rangedPower, double rangedSpread, int rangedDelay, TimeUnit rangedTimeUnit, + int meleeRange, int meleeDelay, TemporalUnit meleeTimeUnit, + int rangedRange, double rangedPower, double rangedSpread, int rangedDelay, TemporalUnit rangedTimeUnit, int desirableRange, boolean comeClose) { super(entityCreature); this.meleeRangeSquared = meleeRange * meleeRange; From 68b94674d022e42d2f7ebf61e215f80e96350b18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Wed, 30 Jun 2021 01:34:27 +0200 Subject: [PATCH 23/64] Updated MeleeAttackGoal.java --- .../server/entity/ai/goal/MeleeAttackGoal.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/ai/goal/MeleeAttackGoal.java b/src/main/java/net/minestom/server/entity/ai/goal/MeleeAttackGoal.java index dec9fc85b..39cd597ee 100644 --- a/src/main/java/net/minestom/server/entity/ai/goal/MeleeAttackGoal.java +++ b/src/main/java/net/minestom/server/entity/ai/goal/MeleeAttackGoal.java @@ -8,21 +8,23 @@ import net.minestom.server.entity.pathfinding.Navigator; import net.minestom.server.utils.Position; import net.minestom.server.utils.time.Cooldown; import net.minestom.server.utils.time.TimeUnit; -import net.minestom.server.utils.time.UpdateOption; import org.jetbrains.annotations.NotNull; +import java.time.Duration; +import java.time.temporal.TemporalUnit; + /** * Attacks the entity's target ({@link EntityCreature#getTarget()}) OR the closest entity * which can be targeted with the entity {@link TargetSelector}. */ public class MeleeAttackGoal extends GoalSelector { - private final Cooldown cooldown = new Cooldown(new UpdateOption(5, TimeUnit.TICK)); + private final Cooldown cooldown = new Cooldown(Duration.of(5, TimeUnit.SERVER_TICK)); private long lastHit; private final double range; private final int delay; - private final TimeUnit timeUnit; + private final TemporalUnit timeUnit; private boolean stop; private Entity cachedTarget; @@ -33,7 +35,7 @@ public class MeleeAttackGoal extends GoalSelector { * @param delay the delay between each attacks * @param timeUnit the unit of the delay */ - public MeleeAttackGoal(@NotNull EntityCreature entityCreature, double range, int delay, @NotNull TimeUnit timeUnit) { + public MeleeAttackGoal(@NotNull EntityCreature entityCreature, double range, int delay, @NotNull TemporalUnit timeUnit) { super(entityCreature); this.range = range; this.delay = delay; From 6a4de332a6f64c86f3b9a27e2db3d4b412e2b077 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Wed, 30 Jun 2021 01:35:33 +0200 Subject: [PATCH 24/64] Updated RangedAttackGoal.java --- .../minestom/server/entity/ai/goal/RangedAttackGoal.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/ai/goal/RangedAttackGoal.java b/src/main/java/net/minestom/server/entity/ai/goal/RangedAttackGoal.java index 4332ac8bd..d1d40d410 100644 --- a/src/main/java/net/minestom/server/entity/ai/goal/RangedAttackGoal.java +++ b/src/main/java/net/minestom/server/entity/ai/goal/RangedAttackGoal.java @@ -9,19 +9,20 @@ import net.minestom.server.entity.EntityProjectile; import net.minestom.server.utils.Position; import net.minestom.server.utils.time.Cooldown; import net.minestom.server.utils.time.TimeUnit; -import net.minestom.server.utils.time.UpdateOption; import net.minestom.server.utils.validate.Check; import org.jetbrains.annotations.NotNull; +import java.time.Duration; +import java.time.temporal.TemporalUnit; import java.util.function.Function; public class RangedAttackGoal extends GoalSelector { - private final Cooldown cooldown = new Cooldown(new UpdateOption(5, TimeUnit.TICK)); + private final Cooldown cooldown = new Cooldown(Duration.of(5, TimeUnit.SERVER_TICK)); private long lastShot; private final int delay; - private final TimeUnit timeUnit; + private final TemporalUnit timeUnit; private final int attackRangeSquared; private final int desirableRangeSquared; private final boolean comeClose; @@ -43,7 +44,7 @@ public class RangedAttackGoal extends GoalSelector { * @param power shot power (1 for normal). * @param timeUnit the unit of the delay. */ - public RangedAttackGoal(@NotNull EntityCreature entityCreature, int delay, int attackRange, int desirableRange, boolean comeClose, double power, double spread, @NotNull TimeUnit timeUnit) { + public RangedAttackGoal(@NotNull EntityCreature entityCreature, int delay, int attackRange, int desirableRange, boolean comeClose, double power, double spread, @NotNull TemporalUnit timeUnit) { super(entityCreature); this.delay = delay; this.timeUnit = timeUnit; From 2c9f0c5e6759f9c2487e76e7784874d9095951e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Wed, 30 Jun 2021 01:42:54 +0200 Subject: [PATCH 25/64] Updated ClientPingServerEvent.java --- .../event/server/ClientPingServerEvent.java | 56 ++++++++++++++++--- 1 file changed, 47 insertions(+), 9 deletions(-) diff --git a/src/main/java/net/minestom/server/event/server/ClientPingServerEvent.java b/src/main/java/net/minestom/server/event/server/ClientPingServerEvent.java index f1cddd191..b6df76970 100644 --- a/src/main/java/net/minestom/server/event/server/ClientPingServerEvent.java +++ b/src/main/java/net/minestom/server/event/server/ClientPingServerEvent.java @@ -3,9 +3,10 @@ package net.minestom.server.event.server; import net.minestom.server.event.trait.CancellableEvent; import net.minestom.server.network.player.PlayerConnection; import net.minestom.server.utils.time.TimeUnit; -import net.minestom.server.utils.time.UpdateOption; import org.jetbrains.annotations.NotNull; +import java.time.Duration; + /** * Called when a {@link PlayerConnection} sends a ping packet, @@ -14,13 +15,13 @@ import org.jetbrains.annotations.NotNull; * @see ServerListPingEvent */ public class ClientPingServerEvent implements CancellableEvent { - private static final UpdateOption DEFAULT_DELAY = new UpdateOption(0, TimeUnit.MILLISECOND); + private static final Duration DEFAULT_DELAY = Duration.of(0, TimeUnit.MILLISECOND); private final PlayerConnection connection; private long payload; private boolean cancelled = false; - private UpdateOption delay; + private Duration delay; /** * Creates a new client ping server event with 0 delay @@ -40,13 +41,24 @@ public class ClientPingServerEvent implements CancellableEvent { * @param connection the player connection * @param payload the payload the client sent */ - public ClientPingServerEvent(@NotNull PlayerConnection connection, long payload, UpdateOption delay) { + @SuppressWarnings("removal") + @Deprecated(forRemoval = true) + public ClientPingServerEvent(@NotNull PlayerConnection connection, long payload, net.minestom.server.utils.time.UpdateOption delay) { + this(connection, payload, delay.toDuration()); + } + + /** + * Creates a new client ping server event with 0 delay + * + * @param connection the player connection + * @param payload the payload the client sent + */ + public ClientPingServerEvent(@NotNull PlayerConnection connection, long payload, Duration delay) { this.connection = connection; this.payload = payload; this.delay = delay; } - /** * PlayerConnection of received packet. Note that the player has not joined the server * at this time. @@ -82,7 +94,7 @@ public class ClientPingServerEvent implements CancellableEvent { * * @return the delay */ - public @NotNull UpdateOption getDelay() { + public @NotNull Duration getDelay() { return delay; } @@ -90,9 +102,35 @@ public class ClientPingServerEvent implements CancellableEvent { * Adds to the delay until minestom will send the ping response packet. * * @param delay the delay + * + * @deprecated Replaced by {@link #addDelay(Duration)} */ - public void addDelay(@NotNull UpdateOption delay) { - this.delay = new UpdateOption(this.delay.toMilliseconds() + delay.toMilliseconds(), TimeUnit.MILLISECOND); + @SuppressWarnings("removal") + @Deprecated(forRemoval = true) + public void addDelay(@NotNull net.minestom.server.utils.time.UpdateOption delay) { + addDelay(delay.toDuration()); + } + + /** + * Adds to the delay until minestom will send the ping response packet. + * + * @param delay the delay + */ + public void addDelay(@NotNull Duration delay) { + this.delay = this.delay.plus(delay); + } + + /** + * Sets the delay until minestom will send the ping response packet. + * + * @param delay the delay + * + * @deprecated Replaced by {@link #setDelay(Duration)} + */ + @SuppressWarnings("removal") + @Deprecated(forRemoval = true) + public void setDelay(@NotNull net.minestom.server.utils.time.UpdateOption delay) { + setDelay(delay.toDuration()); } /** @@ -100,7 +138,7 @@ public class ClientPingServerEvent implements CancellableEvent { * * @param delay the delay */ - public void setDelay(@NotNull UpdateOption delay) { + public void setDelay(@NotNull Duration delay) { this.delay = delay; } From 25183973dc75e92c585cc1db800cab67b11646b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Wed, 30 Jun 2021 01:46:03 +0200 Subject: [PATCH 26/64] Updated Instance.java --- .../minestom/server/instance/Instance.java | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/minestom/server/instance/Instance.java b/src/main/java/net/minestom/server/instance/Instance.java index cae571ff2..c9921f11e 100644 --- a/src/main/java/net/minestom/server/instance/Instance.java +++ b/src/main/java/net/minestom/server/instance/Instance.java @@ -39,7 +39,6 @@ import net.minestom.server.utils.chunk.ChunkUtils; import net.minestom.server.utils.entity.EntityUtils; import net.minestom.server.utils.time.Cooldown; import net.minestom.server.utils.time.TimeUnit; -import net.minestom.server.utils.time.UpdateOption; import net.minestom.server.utils.validate.Check; import net.minestom.server.world.DimensionType; import org.jetbrains.annotations.ApiStatus; @@ -47,6 +46,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jglrxavpok.hephaistos.nbt.NBTCompound; +import java.time.Duration; import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentLinkedQueue; @@ -80,7 +80,7 @@ public abstract class Instance implements BlockModifier, Tickable, TagHandler, P // The time of the instance private long time; private int timeRate = 1; - private UpdateOption timeUpdate = new UpdateOption(1, TimeUnit.SECOND); + private Duration timeUpdate = Duration.of(1, TimeUnit.SECOND); private long lastTimeUpdate; // Field for tick events @@ -416,7 +416,7 @@ public abstract class Instance implements BlockModifier, Tickable, TagHandler, P * @return the client update rate for time related packet */ @Nullable - public UpdateOption getTimeUpdate() { + public Duration getTimeUpdate() { return timeUpdate; } @@ -428,7 +428,21 @@ public abstract class Instance implements BlockModifier, Tickable, TagHandler, P * * @param timeUpdate the new update rate concerning time */ - public void setTimeUpdate(@Nullable UpdateOption timeUpdate) { + @SuppressWarnings("removal") + @Deprecated(forRemoval = true) + public void setTimeUpdate(@Nullable net.minestom.server.utils.time.UpdateOption timeUpdate) { + setTimeUpdate(timeUpdate.toDuration()); + } + + /** + * Changes the rate at which the client is updated about the time + *

+ * Setting it to null means that the client will never know about time change + * (but will still change server-side) + * + * @param timeUpdate the new update rate concerning time + */ + public void setTimeUpdate(@Nullable Duration timeUpdate) { this.timeUpdate = timeUpdate; } From aaa3b91424a16dadd3107b8c9f7e515458ca6b05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Wed, 30 Jun 2021 01:54:11 +0200 Subject: [PATCH 27/64] Updated multiple files --- .../minestom/server/instance/DynamicChunk.java | 8 ++++---- .../server/instance/block/CustomBlock.java | 16 ++++++++-------- src/test/java/demo/blocks/CustomBlockSample.java | 9 ++++----- .../java/demo/blocks/UpdatableBlockDemo.java | 8 ++++---- 4 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/main/java/net/minestom/server/instance/DynamicChunk.java b/src/main/java/net/minestom/server/instance/DynamicChunk.java index 00f4a14c2..831f46b7d 100644 --- a/src/main/java/net/minestom/server/instance/DynamicChunk.java +++ b/src/main/java/net/minestom/server/instance/DynamicChunk.java @@ -20,13 +20,13 @@ import net.minestom.server.utils.callback.OptionalCallback; import net.minestom.server.utils.chunk.ChunkCallback; import net.minestom.server.utils.chunk.ChunkUtils; import net.minestom.server.utils.time.Cooldown; -import net.minestom.server.utils.time.UpdateOption; import net.minestom.server.utils.validate.Check; import net.minestom.server.world.biomes.Biome; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.lang.ref.SoftReference; +import java.time.Duration; import java.util.Set; /** @@ -143,10 +143,10 @@ public class DynamicChunk extends Chunk { final CustomBlock customBlock = getCustomBlock(index); // Update cooldown - final UpdateOption updateOption = customBlock.getUpdateOption(); - if (updateOption != null) { + final Duration updateFrequency = customBlock.getUpdateFrequency(); + if (updateFrequency != null) { final long lastUpdate = updatableBlocksLastUpdate.get(index); - final boolean hasCooldown = Cooldown.hasCooldown(time, lastUpdate, updateOption); + final boolean hasCooldown = Cooldown.hasCooldown(time, lastUpdate, updateFrequency); if (hasCooldown) continue; diff --git a/src/main/java/net/minestom/server/instance/block/CustomBlock.java b/src/main/java/net/minestom/server/instance/block/CustomBlock.java index da9df7f7d..eb386384c 100644 --- a/src/main/java/net/minestom/server/instance/block/CustomBlock.java +++ b/src/main/java/net/minestom/server/instance/block/CustomBlock.java @@ -14,12 +14,12 @@ import net.minestom.server.instance.Chunk; import net.minestom.server.instance.Instance; import net.minestom.server.network.packet.server.play.BlockBreakAnimationPacket; import net.minestom.server.utils.BlockPosition; -import net.minestom.server.utils.time.UpdateOption; import net.minestom.server.utils.validate.Check; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jglrxavpok.hephaistos.nbt.NBTCompound; +import java.time.Duration; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -67,12 +67,12 @@ public abstract class CustomBlock { } /** - * Calling delay depends on {@link #getUpdateOption()} which should be overridden. + * Calling delay depends on {@link #getUpdateFrequency()} which should be overridden. * * @param instance the instance of the block * @param blockPosition the position of the block * @param data the data associated with the block - * @throws UnsupportedOperationException if {@link #getUpdateOption()} + * @throws UnsupportedOperationException if {@link #getUpdateFrequency()} * is not null but the update method is not overridden */ public void update(@NotNull Instance instance, @NotNull BlockPosition blockPosition, @Nullable Data data) { @@ -89,7 +89,7 @@ public abstract class CustomBlock { * @return the update option of the block, null if not any */ @Nullable - public UpdateOption getUpdateOption() { + public Duration getUpdateFrequency() { return null; } @@ -172,14 +172,14 @@ public abstract class CustomBlock { /** * Gets if this {@link CustomBlock} requires any tick update. * - * @return true if {@link #getUpdateOption()} is not null and the value is positive + * @return true if {@link #getUpdateFrequency()} is not null and the value is positive */ public boolean hasUpdate() { - final UpdateOption updateOption = getUpdateOption(); - if (updateOption == null) + final Duration updateFrequency = getUpdateFrequency(); + if (updateFrequency == null) return false; - return updateOption.getValue() > 0; + return !updateFrequency.isZero(); } diff --git a/src/test/java/demo/blocks/CustomBlockSample.java b/src/test/java/demo/blocks/CustomBlockSample.java index 03c9388fd..336efcc8d 100644 --- a/src/test/java/demo/blocks/CustomBlockSample.java +++ b/src/test/java/demo/blocks/CustomBlockSample.java @@ -7,15 +7,15 @@ import net.minestom.server.instance.block.Block; import net.minestom.server.instance.block.CustomBlock; import net.minestom.server.utils.BlockPosition; import net.minestom.server.utils.time.TimeUnit; -import net.minestom.server.utils.time.UpdateOption; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.time.Duration; import java.util.Set; public class CustomBlockSample extends CustomBlock { - private static final UpdateOption UPDATE_OPTION = new UpdateOption(3, TimeUnit.TICK); + private static final Duration DURATION = Duration.of(3, TimeUnit.SERVER_TICK); public CustomBlockSample() { super(Block.GOLD_BLOCK, "custom_block"); @@ -45,10 +45,9 @@ public class CustomBlockSample extends CustomBlock { //instance.refreshBlockStateId(blockPosition, (short) (blockId+1)); } - @Nullable @Override - public UpdateOption getUpdateOption() { - return UPDATE_OPTION; + public Duration getUpdateFrequency() { + return DURATION; } @Override diff --git a/src/test/java/demo/blocks/UpdatableBlockDemo.java b/src/test/java/demo/blocks/UpdatableBlockDemo.java index 4bf1a2c43..454f7f38f 100644 --- a/src/test/java/demo/blocks/UpdatableBlockDemo.java +++ b/src/test/java/demo/blocks/UpdatableBlockDemo.java @@ -7,14 +7,14 @@ import net.minestom.server.instance.block.Block; import net.minestom.server.instance.block.CustomBlock; import net.minestom.server.utils.BlockPosition; import net.minestom.server.utils.time.TimeUnit; -import net.minestom.server.utils.time.UpdateOption; import org.jetbrains.annotations.NotNull; +import java.time.Duration; import java.util.Set; public class UpdatableBlockDemo extends CustomBlock { - private static final UpdateOption UPDATE_OPTION = new UpdateOption(20, TimeUnit.TICK); + private static final Duration DURATION = Duration.of(20, TimeUnit.SERVER_TICK); public UpdatableBlockDemo() { super(Block.DIRT, "updatable"); @@ -41,8 +41,8 @@ public class UpdatableBlockDemo extends CustomBlock { } @Override - public UpdateOption getUpdateOption() { - return UPDATE_OPTION; + public Duration getUpdateFrequency() { + return DURATION; } @Override From 779e6bd22efa1df1e0c303c7204e8c444dcbb5ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Wed, 30 Jun 2021 12:45:37 +0200 Subject: [PATCH 28/64] Added TimeUnit#getMillis method --- src/main/java/net/minestom/server/utils/time/TimeUnit.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/net/minestom/server/utils/time/TimeUnit.java b/src/main/java/net/minestom/server/utils/time/TimeUnit.java index ea5487edc..9a3b3644a 100644 --- a/src/main/java/net/minestom/server/utils/time/TimeUnit.java +++ b/src/main/java/net/minestom/server/utils/time/TimeUnit.java @@ -11,4 +11,8 @@ public class TimeUnit { public static final TemporalUnit MILLISECOND = ChronoUnit.MILLIS; public static final TemporalUnit SERVER_TICK = Tick.SERVER_TICKS; public static final TemporalUnit CLIENT_TICK = Tick.CLIENT_TICKS; + + public static long getMillis(long amount, TemporalUnit unit) { + return unit.getDuration().multipliedBy(amount).toMillis(); + } } From f4b761c50c8d95b5dc1f95e7b773d11dc291e7a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Wed, 30 Jun 2021 12:48:42 +0200 Subject: [PATCH 29/64] Made TimeUnit constructor private --- src/main/java/net/minestom/server/utils/time/TimeUnit.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/net/minestom/server/utils/time/TimeUnit.java b/src/main/java/net/minestom/server/utils/time/TimeUnit.java index 9a3b3644a..2e6dd98c3 100644 --- a/src/main/java/net/minestom/server/utils/time/TimeUnit.java +++ b/src/main/java/net/minestom/server/utils/time/TimeUnit.java @@ -12,6 +12,9 @@ public class TimeUnit { public static final TemporalUnit SERVER_TICK = Tick.SERVER_TICKS; public static final TemporalUnit CLIENT_TICK = Tick.CLIENT_TICKS; + private TimeUnit() { + } + public static long getMillis(long amount, TemporalUnit unit) { return unit.getDuration().multipliedBy(amount).toMillis(); } From b8441a0fd7ce909e5dbb08aef13eb46cf15af163 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Wed, 30 Jun 2021 12:51:20 +0200 Subject: [PATCH 30/64] Updated TaskBuilder.java --- .../minestom/server/timer/TaskBuilder.java | 54 +++++++++++++++---- 1 file changed, 43 insertions(+), 11 deletions(-) diff --git a/src/main/java/net/minestom/server/timer/TaskBuilder.java b/src/main/java/net/minestom/server/timer/TaskBuilder.java index bb7a03121..838b2dc67 100644 --- a/src/main/java/net/minestom/server/timer/TaskBuilder.java +++ b/src/main/java/net/minestom/server/timer/TaskBuilder.java @@ -3,13 +3,15 @@ package net.minestom.server.timer; import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import net.minestom.server.extras.selfmodification.MinestomRootClassLoader; import net.minestom.server.utils.time.TimeUnit; -import net.minestom.server.utils.time.UpdateOption; import org.jetbrains.annotations.NotNull; +import java.time.Duration; +import java.time.temporal.TemporalUnit; + /** * A builder which represents a fluent Object to schedule tasks. *

- * You can specify a delay with {@link #delay(long, TimeUnit)} or {@link #repeat(long, TimeUnit)} + * You can specify a delay with {@link #delay(long, TemporalUnit)} or {@link #repeat(long, TemporalUnit)} * and then schedule the {@link Task} with {@link #schedule()}. */ public class TaskBuilder { @@ -68,8 +70,8 @@ public class TaskBuilder { * @return this builder, for chaining */ @NotNull - public TaskBuilder delay(long time, @NotNull TimeUnit unit) { - this.delay = unit.toMilliseconds(time); + public TaskBuilder delay(long time, @NotNull TemporalUnit unit) { + this.delay = TimeUnit.getMillis(time, unit); return this; } @@ -78,10 +80,25 @@ public class TaskBuilder { * * @param updateOption the UpdateOption for this builder. * @return this builder, for chaining + * + * @deprecated Replaced by {@link #delay(Duration)} + */ + @SuppressWarnings("removal") + @NotNull + @Deprecated(forRemoval = true) + public TaskBuilder delay(net.minestom.server.utils.time.UpdateOption updateOption) { + return delay(updateOption.toDuration()); + } + + /** + * Specifies that the {@link Task} should delay its execution by the specified amount of time. + * + * @param duration the Duration for this builder. + * @return this builder, for chaining */ @NotNull - public TaskBuilder delay(UpdateOption updateOption) { - this.delay = updateOption.toMilliseconds(); + public TaskBuilder delay(Duration duration) { + this.delay = duration.toMillis(); return this; } @@ -89,12 +106,12 @@ public class TaskBuilder { * Specifies that the {@link Task} should continue to run after waiting for the specified value until it is terminated. * * @param time The time until the repetition - * @param unit The {@link TimeUnit} for {@code time} + * @param unit The {@link TemporalUnit} for {@code time} * @return this builder, for chaining */ @NotNull - public TaskBuilder repeat(long time, @NotNull TimeUnit unit) { - this.repeat = unit.toMilliseconds(time); + public TaskBuilder repeat(long time, @NotNull TemporalUnit unit) { + this.repeat = TimeUnit.getMillis(time, unit); return this; } @@ -103,10 +120,25 @@ public class TaskBuilder { * * @param updateOption the UpdateOption for this builder. * @return this builder, for chaining + * + * @deprecated Replaced by {@link #repeat(Duration)} + */ + @SuppressWarnings("removal") + @NotNull + @Deprecated(forRemoval = true) + public TaskBuilder repeat(net.minestom.server.utils.time.UpdateOption updateOption) { + return repeat(updateOption.toDuration()); + } + + /** + * Specifies that the {@link Task} should continue to run after waiting for the specified value until it is terminated. + * + * @param duration the Duration for this builder. + * @return this builder, for chaining */ @NotNull - public TaskBuilder repeat(UpdateOption updateOption) { - this.repeat = updateOption.toMilliseconds(); + public TaskBuilder repeat(Duration duration) { + this.repeat = duration.toMillis(); return this; } From 8bd54dd300c778442e6bc2dac8acf6ee31e75eb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Wed, 30 Jun 2021 12:52:41 +0200 Subject: [PATCH 31/64] Updated GLFWCapableBuffer.java --- .../minestom/server/map/framebuffers/GLFWCapableBuffer.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lwjgl/java/net/minestom/server/map/framebuffers/GLFWCapableBuffer.java b/src/lwjgl/java/net/minestom/server/map/framebuffers/GLFWCapableBuffer.java index 1ab0676f7..613f38fbb 100644 --- a/src/lwjgl/java/net/minestom/server/map/framebuffers/GLFWCapableBuffer.java +++ b/src/lwjgl/java/net/minestom/server/map/framebuffers/GLFWCapableBuffer.java @@ -4,7 +4,6 @@ import net.minestom.server.MinecraftServer; import net.minestom.server.map.Framebuffer; import net.minestom.server.map.MapColors; import net.minestom.server.timer.Task; -import net.minestom.server.utils.time.TimeUnit; import org.lwjgl.BufferUtils; import org.lwjgl.PointerBuffer; import org.lwjgl.glfw.GLFWErrorCallback; @@ -12,6 +11,7 @@ import org.lwjgl.opengl.GL; import org.lwjgl.system.MemoryStack; import java.nio.ByteBuffer; +import java.time.temporal.TemporalUnit; import static org.lwjgl.glfw.GLFW.*; import static org.lwjgl.opengl.GL11.*; @@ -71,7 +71,7 @@ public abstract class GLFWCapableBuffer { GL.createCapabilities(); } - public Task setupRenderLoop(long period, TimeUnit unit, Runnable rendering) { + public Task setupRenderLoop(long period, TemporalUnit unit, Runnable rendering) { return MinecraftServer.getSchedulerManager() .buildTask(new Runnable() { private boolean first = true; @@ -97,7 +97,7 @@ public abstract class GLFWCapableBuffer { /** * Called in render after glFlush to read the pixel buffer contents and convert it to map colors. - * Only call if you do not use {@link #render(Runnable)} nor {@link #setupRenderLoop(long, TimeUnit, Runnable)} + * Only call if you do not use {@link #render(Runnable)} nor {@link #setupRenderLoop(long, TemporalUnit, Runnable)} */ public void prepareMapColors() { if(onlyMapColors) { From b0af01ee895e7b4b73f0a0e3603fff7570ac2d94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Wed, 30 Jun 2021 12:59:38 +0200 Subject: [PATCH 32/64] Updated multiple files --- src/main/java/net/minestom/server/instance/Instance.java | 3 ++- .../java/net/minestom/server/instance/InstanceContainer.java | 4 ++-- .../java/net/minestom/server/instance/SharedInstance.java | 4 ++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/minestom/server/instance/Instance.java b/src/main/java/net/minestom/server/instance/Instance.java index c9921f11e..07f553bda 100644 --- a/src/main/java/net/minestom/server/instance/Instance.java +++ b/src/main/java/net/minestom/server/instance/Instance.java @@ -47,6 +47,7 @@ import org.jetbrains.annotations.Nullable; import org.jglrxavpok.hephaistos.nbt.NBTCompound; import java.time.Duration; +import java.time.temporal.TemporalUnit; import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentLinkedQueue; @@ -1034,7 +1035,7 @@ public abstract class Instance implements BlockModifier, Tickable, TagHandler, P * @param unit in what unit is the time expressed * @param position the location of the block to update */ - public abstract void scheduleUpdate(int time, @NotNull TimeUnit unit, @NotNull BlockPosition position); + public abstract void scheduleUpdate(int time, @NotNull TemporalUnit unit, @NotNull BlockPosition position); /** * Performs a single tick in the instance, including scheduled tasks from {@link #scheduleNextTick(Consumer)}. diff --git a/src/main/java/net/minestom/server/instance/InstanceContainer.java b/src/main/java/net/minestom/server/instance/InstanceContainer.java index d8a7d5fe2..ffc209330 100644 --- a/src/main/java/net/minestom/server/instance/InstanceContainer.java +++ b/src/main/java/net/minestom/server/instance/InstanceContainer.java @@ -24,13 +24,13 @@ import net.minestom.server.utils.callback.OptionalCallback; import net.minestom.server.utils.chunk.ChunkCallback; import net.minestom.server.utils.chunk.ChunkSupplier; import net.minestom.server.utils.chunk.ChunkUtils; -import net.minestom.server.utils.time.TimeUnit; import net.minestom.server.utils.validate.Check; import net.minestom.server.world.DimensionType; import net.minestom.server.world.biomes.Biome; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.time.temporal.TemporalUnit; import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArrayList; @@ -754,7 +754,7 @@ public class InstanceContainer extends Instance { } @Override - public void scheduleUpdate(int time, @NotNull TimeUnit unit, @NotNull BlockPosition position) { + public void scheduleUpdate(int time, @NotNull TemporalUnit unit, @NotNull BlockPosition position) { final CustomBlock toUpdate = getCustomBlock(position); if (toUpdate == null) { return; diff --git a/src/main/java/net/minestom/server/instance/SharedInstance.java b/src/main/java/net/minestom/server/instance/SharedInstance.java index 19644b480..14a5c9710 100644 --- a/src/main/java/net/minestom/server/instance/SharedInstance.java +++ b/src/main/java/net/minestom/server/instance/SharedInstance.java @@ -6,10 +6,10 @@ import net.minestom.server.storage.StorageLocation; import net.minestom.server.utils.BlockPosition; import net.minestom.server.utils.Position; import net.minestom.server.utils.chunk.ChunkCallback; -import net.minestom.server.utils.time.TimeUnit; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.time.temporal.TemporalUnit; import java.util.Collection; import java.util.UUID; @@ -133,7 +133,7 @@ public class SharedInstance extends Instance { } @Override - public void scheduleUpdate(int time, @NotNull TimeUnit unit, @NotNull BlockPosition position) { + public void scheduleUpdate(int time, @NotNull TemporalUnit unit, @NotNull BlockPosition position) { this.instanceContainer.scheduleUpdate(time, unit, position); } From 117bcb467a645a5d4e85f61e2341e7e3a985b736 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Wed, 30 Jun 2021 13:02:42 +0200 Subject: [PATCH 33/64] Replaced Tick and ChronoUnit with TimeUnit for consistency --- .../builder/arguments/minecraft/ArgumentTime.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/main/java/net/minestom/server/command/builder/arguments/minecraft/ArgumentTime.java b/src/main/java/net/minestom/server/command/builder/arguments/minecraft/ArgumentTime.java index 423916b4e..57c2330ac 100644 --- a/src/main/java/net/minestom/server/command/builder/arguments/minecraft/ArgumentTime.java +++ b/src/main/java/net/minestom/server/command/builder/arguments/minecraft/ArgumentTime.java @@ -6,11 +6,10 @@ import net.minestom.server.command.builder.NodeMaker; import net.minestom.server.command.builder.arguments.Argument; import net.minestom.server.command.builder.exception.ArgumentSyntaxException; import net.minestom.server.network.packet.server.play.DeclareCommandsPacket; -import net.minestom.server.utils.time.Tick; +import net.minestom.server.utils.time.TimeUnit; import org.jetbrains.annotations.NotNull; import java.time.Duration; -import java.time.temporal.ChronoUnit; import java.time.temporal.TemporalUnit; /** @@ -36,16 +35,16 @@ public class ArgumentTime extends Argument { TemporalUnit timeUnit; if (Character.isDigit(lastChar)) - timeUnit = Tick.SERVER_TICKS; + timeUnit = TimeUnit.SERVER_TICK; else if (SUFFIXES.contains(lastChar)) { input = input.substring(0, input.length() - 1); if (lastChar == 'd') { - timeUnit = ChronoUnit.DAYS; + timeUnit = TimeUnit.DAY; } else if (lastChar == 's') { - timeUnit = ChronoUnit.SECONDS; + timeUnit = TimeUnit.SECOND; } else if (lastChar == 't') { - timeUnit = Tick.SERVER_TICKS; + timeUnit = TimeUnit.SERVER_TICK; } else { throw new ArgumentSyntaxException("Time needs to have the unit d, s, t, or none", input, NO_NUMBER); } From c037ea9d843b94f1038687c7706f2205d9c2c03d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Wed, 30 Jun 2021 13:04:59 +0200 Subject: [PATCH 34/64] Replaced Tick and ChronoUnit with TimeUnit for consistency --- .../java/net/minestom/server/entity/LivingEntity.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/LivingEntity.java b/src/main/java/net/minestom/server/entity/LivingEntity.java index 18a8fdb12..899a7cb3a 100644 --- a/src/main/java/net/minestom/server/entity/LivingEntity.java +++ b/src/main/java/net/minestom/server/entity/LivingEntity.java @@ -30,12 +30,11 @@ import net.minestom.server.utils.Position; import net.minestom.server.utils.Vector; import net.minestom.server.utils.block.BlockIterator; import net.minestom.server.utils.time.Cooldown; -import net.minestom.server.utils.time.Tick; +import net.minestom.server.utils.time.TimeUnit; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.time.Duration; -import java.time.temporal.ChronoUnit; import java.time.temporal.TemporalUnit; import java.util.*; import java.util.concurrent.ConcurrentHashMap; @@ -44,7 +43,7 @@ public class LivingEntity extends Entity implements EquipmentHandler { // ItemStack pickup protected boolean canPickupItem; - protected Cooldown itemPickupCooldown = new Cooldown(Duration.of(5, Tick.SERVER_TICKS)); + protected Cooldown itemPickupCooldown = new Cooldown(Duration.of(5, TimeUnit.SERVER_TICK)); protected boolean isDead; @@ -318,7 +317,7 @@ public class LivingEntity extends Entity implements EquipmentHandler { * @param duration duration in ticks of the effect */ public void setFireForDuration(int duration) { - setFireForDuration(duration, Tick.SERVER_TICKS); + setFireForDuration(duration, TimeUnit.SERVER_TICK); } /** @@ -334,7 +333,7 @@ public class LivingEntity extends Entity implements EquipmentHandler { // Do not start fire event if the fire needs to be removed (< 0 duration) if (duration > 0) { EventDispatcher.callCancellable(entityFireEvent, () -> { - final long fireTime = entityFireEvent.getFireTime(ChronoUnit.MILLIS); + final long fireTime = entityFireEvent.getFireTime(TimeUnit.MILLISECOND); setOnFire(true); fireExtinguishTime = System.currentTimeMillis() + fireTime; }); From 01dfe2f48d975586571fc1c1e7c4aa5962b17a37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Wed, 30 Jun 2021 13:05:52 +0200 Subject: [PATCH 35/64] Replaced ChronoUnit with TimeUnit for consistency --- src/main/java/net/minestom/server/entity/Entity.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/Entity.java b/src/main/java/net/minestom/server/entity/Entity.java index 5b19f3132..1f8e26baf 100644 --- a/src/main/java/net/minestom/server/entity/Entity.java +++ b/src/main/java/net/minestom/server/entity/Entity.java @@ -45,6 +45,7 @@ import net.minestom.server.utils.chunk.ChunkUtils; import net.minestom.server.utils.entity.EntityUtils; import net.minestom.server.utils.player.PlayerUtils; import net.minestom.server.utils.time.Cooldown; +import net.minestom.server.utils.time.TimeUnit; import net.minestom.server.utils.validate.Check; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; @@ -52,7 +53,6 @@ import org.jetbrains.annotations.Nullable; import org.jglrxavpok.hephaistos.nbt.NBTCompound; import java.time.Duration; -import java.time.temporal.ChronoUnit; import java.time.temporal.TemporalUnit; import java.util.*; import java.util.concurrent.ConcurrentHashMap; @@ -128,7 +128,7 @@ public class Entity implements Viewable, Tickable, EventHandler, Da protected EntityType entityType; // UNSAFE to change, modify at your own risk // Network synchronization, send the absolute position of the entity each X milliseconds - private static final Duration SYNCHRONIZATION_COOLDOWN = Duration.of(1, ChronoUnit.MINUTES); + private static final Duration SYNCHRONIZATION_COOLDOWN = Duration.of(1, TimeUnit.MINUTE); private Duration customSynchronizationCooldown; private long lastAbsoluteSynchronizationTime; From c9dd06ba20ae838a96f1dbc9ddddc0e6b51bb524 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Wed, 30 Jun 2021 13:10:22 +0200 Subject: [PATCH 36/64] Simplified millis calculation --- src/main/java/net/minestom/server/entity/Entity.java | 2 +- src/main/java/net/minestom/server/entity/ItemEntity.java | 2 +- src/main/java/net/minestom/server/entity/LivingEntity.java | 4 ++-- .../java/net/minestom/server/utils/time/UpdateOption.java | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/Entity.java b/src/main/java/net/minestom/server/entity/Entity.java index 1f8e26baf..8e2a4c758 100644 --- a/src/main/java/net/minestom/server/entity/Entity.java +++ b/src/main/java/net/minestom/server/entity/Entity.java @@ -1536,7 +1536,7 @@ public class Entity implements Viewable, Tickable, EventHandler, Da this.scheduledRemoveTime = 0; return; } - this.scheduledRemoveTime = System.currentTimeMillis() + temporalUnit.getDuration().multipliedBy(delay).toMillis(); + this.scheduledRemoveTime = System.currentTimeMillis() + TimeUnit.getMillis(delay, temporalUnit); } /** diff --git a/src/main/java/net/minestom/server/entity/ItemEntity.java b/src/main/java/net/minestom/server/entity/ItemEntity.java index a2ddbec69..0fd2ff9ff 100644 --- a/src/main/java/net/minestom/server/entity/ItemEntity.java +++ b/src/main/java/net/minestom/server/entity/ItemEntity.java @@ -242,7 +242,7 @@ public class ItemEntity extends Entity { * @param temporalUnit the unit of the delay */ public void setPickupDelay(long delay, @NotNull TemporalUnit temporalUnit) { - this.pickupDelay = temporalUnit.getDuration().multipliedBy(delay).toMillis(); + this.pickupDelay = TimeUnit.getMillis(delay, temporalUnit); } /** diff --git a/src/main/java/net/minestom/server/entity/LivingEntity.java b/src/main/java/net/minestom/server/entity/LivingEntity.java index 899a7cb3a..97d14d42c 100644 --- a/src/main/java/net/minestom/server/entity/LivingEntity.java +++ b/src/main/java/net/minestom/server/entity/LivingEntity.java @@ -670,10 +670,10 @@ public class LivingEntity extends Entity implements EquipmentHandler { * Changes the delay between two fire damage applications. * * @param fireDamagePeriod the delay - * @param temporalUnit the time unit + * @param temporalUnit the time unit */ public void setFireDamagePeriod(long fireDamagePeriod, @NotNull TemporalUnit temporalUnit) { - fireDamagePeriod = temporalUnit.getDuration().multipliedBy(fireDamagePeriod).toMillis(); + fireDamagePeriod = TimeUnit.getMillis(fireDamagePeriod, temporalUnit); this.fireDamagePeriod = fireDamagePeriod; } diff --git a/src/main/java/net/minestom/server/utils/time/UpdateOption.java b/src/main/java/net/minestom/server/utils/time/UpdateOption.java index 8fb693e42..9e4ac7824 100644 --- a/src/main/java/net/minestom/server/utils/time/UpdateOption.java +++ b/src/main/java/net/minestom/server/utils/time/UpdateOption.java @@ -48,7 +48,7 @@ public class UpdateOption { * @return the converted milliseconds based on the time value and the unit */ public long toMilliseconds() { - return temporalUnit.getDuration().multipliedBy(value).toMillis(); + return TimeUnit.getMillis(value, temporalUnit); } public Duration toDuration() { From ca78305d4042ad5378033bca678a95e85569a457 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Wed, 30 Jun 2021 13:19:03 +0200 Subject: [PATCH 37/64] Fix OpenToLAN.java --- src/main/java/net/minestom/server/extras/lan/OpenToLAN.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/net/minestom/server/extras/lan/OpenToLAN.java b/src/main/java/net/minestom/server/extras/lan/OpenToLAN.java index 85a713ea8..350b05b83 100644 --- a/src/main/java/net/minestom/server/extras/lan/OpenToLAN.java +++ b/src/main/java/net/minestom/server/extras/lan/OpenToLAN.java @@ -83,7 +83,7 @@ public class OpenToLAN { eventCooldown = new Cooldown(config.delayBetweenEvent); task = MinecraftServer.getSchedulerManager().buildTask(OpenToLAN::ping) - .repeat(config.delayBetweenPings.getValue(), config.delayBetweenPings.getTimeUnit()) + .repeat(config.delayBetweenPings) .schedule(); return true; } From f9c9b4c1ebbc5d5a2eb510d293d8c2c86e519dea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Wed, 30 Jun 2021 13:20:02 +0200 Subject: [PATCH 38/64] Fix old usage of TimeUnit --- .../java/net/minestom/server/entity/fakeplayer/FakePlayer.java | 2 +- src/test/java/demo/PlayerInit.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/fakeplayer/FakePlayer.java b/src/main/java/net/minestom/server/entity/fakeplayer/FakePlayer.java index 52acf40c6..ad4584280 100644 --- a/src/main/java/net/minestom/server/entity/fakeplayer/FakePlayer.java +++ b/src/main/java/net/minestom/server/entity/fakeplayer/FakePlayer.java @@ -151,7 +151,7 @@ public class FakePlayer extends Player implements NavigableEntity { private void handleTabList(PlayerConnection connection) { if (!option.isInTabList()) { // Remove from tab-list - MinecraftServer.getSchedulerManager().buildTask(() -> connection.sendPacket(getRemovePlayerToList())).delay(20, TimeUnit.TICK).schedule(); + MinecraftServer.getSchedulerManager().buildTask(() -> connection.sendPacket(getRemovePlayerToList())).delay(20, TimeUnit.SERVER_TICK).schedule(); } } } diff --git a/src/test/java/demo/PlayerInit.java b/src/test/java/demo/PlayerInit.java index 9048b56a9..d9f2213bd 100644 --- a/src/test/java/demo/PlayerInit.java +++ b/src/test/java/demo/PlayerInit.java @@ -175,7 +175,7 @@ public class PlayerInit { .append(Component.text("ACQ TIME: " + MathUtils.round(tickMonitor.getAcquisitionTime(), 2) + "ms")); final Component footer = benchmarkManager.getCpuMonitoringMessage(); Audiences.players().sendPlayerListHeaderAndFooter(header, footer); - }).repeat(10, TimeUnit.TICK).schedule(); + }).repeat(10, TimeUnit.SERVER_TICK).schedule(); } From ba2dcaac43b8c344073e8bd4e91d92f328ad0326 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Wed, 30 Jun 2021 13:22:46 +0200 Subject: [PATCH 39/64] Deprecate TimeUnit#TICK instead of removing --- src/main/java/net/minestom/server/utils/time/TimeUnit.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/net/minestom/server/utils/time/TimeUnit.java b/src/main/java/net/minestom/server/utils/time/TimeUnit.java index 2e6dd98c3..f6c188f6a 100644 --- a/src/main/java/net/minestom/server/utils/time/TimeUnit.java +++ b/src/main/java/net/minestom/server/utils/time/TimeUnit.java @@ -11,6 +11,11 @@ public class TimeUnit { public static final TemporalUnit MILLISECOND = ChronoUnit.MILLIS; public static final TemporalUnit SERVER_TICK = Tick.SERVER_TICKS; public static final TemporalUnit CLIENT_TICK = Tick.CLIENT_TICKS; + /** + * @deprecated Please use either {@link #SERVER_TICK} or {@link #CLIENT_TICK} + */ + @Deprecated(forRemoval = true) + public static final TemporalUnit TICK = CLIENT_TICK; private TimeUnit() { } From 7ff5173e03d58dc3050a31a001c50e13d53ceb11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Wed, 30 Jun 2021 13:23:32 +0200 Subject: [PATCH 40/64] Fix PingPacket.java --- .../server/network/packet/client/status/PingPacket.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/net/minestom/server/network/packet/client/status/PingPacket.java b/src/main/java/net/minestom/server/network/packet/client/status/PingPacket.java index b5b1dd779..5e336a12f 100644 --- a/src/main/java/net/minestom/server/network/packet/client/status/PingPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/status/PingPacket.java @@ -25,7 +25,7 @@ public class PingPacket implements ClientPreplayPacket { if (clientPingEvent.isCancelled()) { connection.disconnect(); } else { - if (clientPingEvent.getDelay().toMilliseconds() == 0) { + if (clientPingEvent.getDelay().isZero()) { connection.sendPacket(new PongPacket(clientPingEvent.getPayload())); connection.disconnect(); } else { From d735ba5fff53331763c8aa43c40ccaf0fca9ca64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Wed, 30 Jun 2021 13:25:24 +0200 Subject: [PATCH 41/64] Fix missing Deprecated annotation --- src/main/java/net/minestom/server/entity/ItemEntity.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/net/minestom/server/entity/ItemEntity.java b/src/main/java/net/minestom/server/entity/ItemEntity.java index 0fd2ff9ff..54dc39663 100644 --- a/src/main/java/net/minestom/server/entity/ItemEntity.java +++ b/src/main/java/net/minestom/server/entity/ItemEntity.java @@ -74,6 +74,7 @@ public class ItemEntity extends Entity { * @deprecated Replaced by {@link #setMergeDelay(Duration)} */ @SuppressWarnings("removal") + @Deprecated(forRemoval = true) public static void setMergeUpdateOption(@Nullable net.minestom.server.utils.time.UpdateOption mergeUpdateOption) { setMergeDelay(mergeUpdateOption == null ? null : mergeUpdateOption.toDuration()); } From c683acaf6e08d02b0a8dc5b46e507b68717c8db9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Wed, 30 Jun 2021 13:26:45 +0200 Subject: [PATCH 42/64] Fix PotionMeta.java --- src/main/java/net/minestom/server/item/metadata/PotionMeta.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/net/minestom/server/item/metadata/PotionMeta.java b/src/main/java/net/minestom/server/item/metadata/PotionMeta.java index e7ee9ad7d..dd231e7fb 100644 --- a/src/main/java/net/minestom/server/item/metadata/PotionMeta.java +++ b/src/main/java/net/minestom/server/item/metadata/PotionMeta.java @@ -98,7 +98,7 @@ public class PotionMeta extends ItemMeta implements ItemMetaBuilder.Provider Date: Wed, 30 Jun 2021 13:36:26 +0200 Subject: [PATCH 43/64] Fix javadoc --- src/main/java/net/minestom/server/timer/SchedulerManager.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/minestom/server/timer/SchedulerManager.java b/src/main/java/net/minestom/server/timer/SchedulerManager.java index 4c126be8d..bddfd29b8 100644 --- a/src/main/java/net/minestom/server/timer/SchedulerManager.java +++ b/src/main/java/net/minestom/server/timer/SchedulerManager.java @@ -20,8 +20,8 @@ import java.util.stream.Collectors; * An object which manages all the {@link Task}'s. *

* {@link Task} first need to be built with {@link #buildTask(Runnable)}, you can then specify a delay with as example - * {@link TaskBuilder#delay(long, net.minestom.server.utils.time.TimeUnit)} - * or {@link TaskBuilder#repeat(long, net.minestom.server.utils.time.TimeUnit)}, + * {@link TaskBuilder#delay(long, java.time.temporal.TemporalUnit)} + * or {@link TaskBuilder#repeat(long, java.time.temporal.TemporalUnit)}, * and to finally schedule: {@link TaskBuilder#schedule()}. *

* Shutdown tasks are built with {@link #buildShutdownTask(Runnable)} and are executed, as the name implies, when the server stops. From 92cbc4727c5cd56ce49a17e213247349e571fec1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Wed, 30 Jun 2021 13:37:37 +0200 Subject: [PATCH 44/64] Fix javadoc and optimize imports --- .../net/minestom/server/map/framebuffers/GLFWFramebuffer.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/lwjgl/java/net/minestom/server/map/framebuffers/GLFWFramebuffer.java b/src/lwjgl/java/net/minestom/server/map/framebuffers/GLFWFramebuffer.java index c02a5aff4..c34476c64 100644 --- a/src/lwjgl/java/net/minestom/server/map/framebuffers/GLFWFramebuffer.java +++ b/src/lwjgl/java/net/minestom/server/map/framebuffers/GLFWFramebuffer.java @@ -1,7 +1,6 @@ package net.minestom.server.map.framebuffers; import net.minestom.server.map.Framebuffer; -import net.minestom.server.utils.time.TimeUnit; import org.lwjgl.BufferUtils; import java.nio.ByteBuffer; @@ -22,7 +21,7 @@ import static org.lwjgl.glfw.GLFW.GLFW_OPENGL_API; * to automatically render to the offscreen buffer on a specialized thread. * * GLFWFramebuffer does not provide guarantee that the result of {@link #toMapColors()} is synchronized with rendering, but - * it will be updated after each frame rendered through {@link #render(Runnable)} or {@link #setupRenderLoop(long, TimeUnit, Runnable)}. + * it will be updated after each frame rendered through {@link #render(Runnable)} or {@link #setupRenderLoop(long, java.time.temporal.TemporalUnit, Runnable)}. * * This framebuffer is meant to render to a single map (ie it is only compatible with 128x128 rendering) */ From a5a370783d8cfc8317d5a19e9bc3ef88fb53c5c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Wed, 30 Jun 2021 22:00:05 +0200 Subject: [PATCH 45/64] Removed redundant check --- src/main/java/net/minestom/server/entity/Entity.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/Entity.java b/src/main/java/net/minestom/server/entity/Entity.java index 8e2a4c758..71670481a 100644 --- a/src/main/java/net/minestom/server/entity/Entity.java +++ b/src/main/java/net/minestom/server/entity/Entity.java @@ -1624,10 +1624,6 @@ public class Entity implements Viewable, Tickable, EventHandler, Da * @param cooldown custom cooldown for position synchronization. */ public void setCustomSynchronizationCooldown(@Nullable Duration cooldown) { - if (cooldown == null) { - this.customSynchronizationCooldown = null; - return; - } this.customSynchronizationCooldown = cooldown; } From ec4d24b9a874a773eb11347d6c2704bd49e7e5cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Wed, 30 Jun 2021 22:14:58 +0200 Subject: [PATCH 46/64] Add null check --- src/main/java/net/minestom/server/instance/Instance.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/net/minestom/server/instance/Instance.java b/src/main/java/net/minestom/server/instance/Instance.java index 07f553bda..ec00aa34a 100644 --- a/src/main/java/net/minestom/server/instance/Instance.java +++ b/src/main/java/net/minestom/server/instance/Instance.java @@ -432,7 +432,7 @@ public abstract class Instance implements BlockModifier, Tickable, TagHandler, P @SuppressWarnings("removal") @Deprecated(forRemoval = true) public void setTimeUpdate(@Nullable net.minestom.server.utils.time.UpdateOption timeUpdate) { - setTimeUpdate(timeUpdate.toDuration()); + setTimeUpdate(timeUpdate == null ? null : timeUpdate.toDuration()); } /** From 059565b397abf6a45515fc2eb699f57d2661a945 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Wed, 30 Jun 2021 22:25:24 +0200 Subject: [PATCH 47/64] Flip ?: null checks --- src/main/java/net/minestom/server/entity/Entity.java | 2 +- src/main/java/net/minestom/server/instance/Instance.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/Entity.java b/src/main/java/net/minestom/server/entity/Entity.java index 71670481a..05a3b7239 100644 --- a/src/main/java/net/minestom/server/entity/Entity.java +++ b/src/main/java/net/minestom/server/entity/Entity.java @@ -1615,7 +1615,7 @@ public class Entity implements Viewable, Tickable, EventHandler, Da @SuppressWarnings("removal") @Deprecated(forRemoval = true) public void setCustomSynchronizationCooldown(@Nullable net.minestom.server.utils.time.UpdateOption cooldown) { - setCustomSynchronizationCooldown(cooldown == null ? null : Duration.ofMillis(cooldown.toMilliseconds())); + setCustomSynchronizationCooldown(cooldown != null ? Duration.ofMillis(cooldown.toMilliseconds()) : null); } /** diff --git a/src/main/java/net/minestom/server/instance/Instance.java b/src/main/java/net/minestom/server/instance/Instance.java index ec00aa34a..32534ccf4 100644 --- a/src/main/java/net/minestom/server/instance/Instance.java +++ b/src/main/java/net/minestom/server/instance/Instance.java @@ -432,7 +432,7 @@ public abstract class Instance implements BlockModifier, Tickable, TagHandler, P @SuppressWarnings("removal") @Deprecated(forRemoval = true) public void setTimeUpdate(@Nullable net.minestom.server.utils.time.UpdateOption timeUpdate) { - setTimeUpdate(timeUpdate == null ? null : timeUpdate.toDuration()); + setTimeUpdate(timeUpdate != null ? timeUpdate.toDuration() : null); } /** From fdf715398678e0fe0c5bd9e636ab5a2303523f8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Wed, 30 Jun 2021 22:28:54 +0200 Subject: [PATCH 48/64] Flip ?: null check --- src/main/java/net/minestom/server/entity/ItemEntity.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/net/minestom/server/entity/ItemEntity.java b/src/main/java/net/minestom/server/entity/ItemEntity.java index 54dc39663..cd1ca7de2 100644 --- a/src/main/java/net/minestom/server/entity/ItemEntity.java +++ b/src/main/java/net/minestom/server/entity/ItemEntity.java @@ -76,7 +76,7 @@ public class ItemEntity extends Entity { @SuppressWarnings("removal") @Deprecated(forRemoval = true) public static void setMergeUpdateOption(@Nullable net.minestom.server.utils.time.UpdateOption mergeUpdateOption) { - setMergeDelay(mergeUpdateOption == null ? null : mergeUpdateOption.toDuration()); + setMergeDelay(mergeUpdateOption != null ? mergeUpdateOption.toDuration() : null); } /** From bcc1132ed7720d5990b7f334e3ceb2cbd6b58a68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Thu, 1 Jul 2021 15:03:16 +0200 Subject: [PATCH 49/64] Deprecated net.minestom.server.entity.Entity.scheduleRemove(long, java.time.temporal.TemporalUnit) --- .../net/minestom/server/entity/Entity.java | 24 ++++++++++++++----- .../server/entity/EntityCreature.java | 3 ++- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/Entity.java b/src/main/java/net/minestom/server/entity/Entity.java index 05a3b7239..0abadce61 100644 --- a/src/main/java/net/minestom/server/entity/Entity.java +++ b/src/main/java/net/minestom/server/entity/Entity.java @@ -1530,17 +1530,29 @@ public class Entity implements Viewable, Tickable, EventHandler, Da * @param delay the time before removing the entity, * 0 to cancel the removing * @param temporalUnit the unit of the delay + * @deprecated Replaced by {@link #scheduleRemove(Duration)} */ + @Deprecated public void scheduleRemove(long delay, @NotNull TemporalUnit temporalUnit) { - if (delay == 0) { // Cancel the scheduled remove - this.scheduledRemoveTime = 0; - return; - } - this.scheduledRemoveTime = System.currentTimeMillis() + TimeUnit.getMillis(delay, temporalUnit); + scheduleRemove(Duration.of(delay, temporalUnit)); } /** - * Gets if the entity removal has been scheduled with {@link #scheduleRemove(long, TemporalUnit)}. + * Triggers {@link #remove()} after the specified time. + * + * @param delay the time before removing the entity, + * 0 to cancel the removing + */ + public void scheduleRemove(Duration delay) { + if (delay.isZero()) { // Cancel the scheduled remove + this.scheduledRemoveTime = 0; + return; + } + this.scheduledRemoveTime = System.currentTimeMillis() + delay.toMillis(); + } + + /** + * Gets if the entity removal has been scheduled with {@link #scheduleRemove(Duration)}. * * @return true if the entity removal has been scheduled */ diff --git a/src/main/java/net/minestom/server/entity/EntityCreature.java b/src/main/java/net/minestom/server/entity/EntityCreature.java index e7f7e18be..c8166c737 100644 --- a/src/main/java/net/minestom/server/entity/EntityCreature.java +++ b/src/main/java/net/minestom/server/entity/EntityCreature.java @@ -14,6 +14,7 @@ import net.minestom.server.utils.time.TimeUnit; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.time.Duration; import java.util.Collection; import java.util.Set; import java.util.UUID; @@ -80,7 +81,7 @@ public class EntityCreature extends LivingEntity implements NavigableEntity, Ent if (removalAnimationDelay > 0) { // Needed for proper death animation (wait for it to finish before destroying the entity) - scheduleRemove(removalAnimationDelay, TimeUnit.MILLISECOND); + scheduleRemove(Duration.of(removalAnimationDelay, TimeUnit.MILLISECOND)); } else { // Instant removal without animation playback remove(); From 157eb6357ae1f1d4295e3097457c8e494cb2132e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Thu, 1 Jul 2021 15:08:31 +0200 Subject: [PATCH 50/64] Deprecated net.minestom.server.entity.ItemEntity.setPickupDelay(long, java.time.temporal.TemporalUnit) --- .../net/minestom/server/entity/ItemEntity.java | 16 ++++++++++++++-- src/test/java/demo/PlayerInit.java | 3 ++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/ItemEntity.java b/src/main/java/net/minestom/server/entity/ItemEntity.java index cd1ca7de2..eba2ab87d 100644 --- a/src/main/java/net/minestom/server/entity/ItemEntity.java +++ b/src/main/java/net/minestom/server/entity/ItemEntity.java @@ -228,7 +228,7 @@ public class ItemEntity extends Entity { } /** - * Gets the pickup delay in milliseconds, defined by {@link #setPickupDelay(long, TemporalUnit)}. + * Gets the pickup delay in milliseconds, defined by {@link #setPickupDelay(Duration)}. * * @return the pickup delay */ @@ -241,9 +241,21 @@ public class ItemEntity extends Entity { * * @param delay the pickup delay * @param temporalUnit the unit of the delay + * + * @deprecated Replaced by {@link #setPickupDelay(Duration)} */ + @Deprecated public void setPickupDelay(long delay, @NotNull TemporalUnit temporalUnit) { - this.pickupDelay = TimeUnit.getMillis(delay, temporalUnit); + setPickupDelay(Duration.of(delay, temporalUnit)); + } + + /** + * Sets the pickup delay of the ItemEntity. + * + * @param delay the pickup delay + */ + public void setPickupDelay(Duration delay) { + this.pickupDelay = delay.toMillis(); } /** diff --git a/src/test/java/demo/PlayerInit.java b/src/test/java/demo/PlayerInit.java index d9f2213bd..14d70f303 100644 --- a/src/test/java/demo/PlayerInit.java +++ b/src/test/java/demo/PlayerInit.java @@ -36,6 +36,7 @@ import net.minestom.server.utils.Vector; import net.minestom.server.utils.time.TimeUnit; import net.minestom.server.world.DimensionType; +import java.time.Duration; import java.util.Collection; import java.util.Random; import java.util.Set; @@ -77,7 +78,7 @@ public class PlayerInit { Position position = player.getPosition().clone().add(0, 1.5f, 0); ItemEntity itemEntity = new ItemEntity(droppedItem, position); - itemEntity.setPickupDelay(500, TimeUnit.MILLISECOND); + itemEntity.setPickupDelay(Duration.of(500, TimeUnit.MILLISECOND)); itemEntity.setInstance(player.getInstance()); Vector velocity = player.getPosition().clone().getDirection().multiply(6); itemEntity.setVelocity(velocity); From a5f270511804df4bbf766ba25c3254ebc01b07b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Thu, 1 Jul 2021 15:16:10 +0200 Subject: [PATCH 51/64] Fix hasUpdate check --- .../java/net/minestom/server/instance/block/CustomBlock.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/net/minestom/server/instance/block/CustomBlock.java b/src/main/java/net/minestom/server/instance/block/CustomBlock.java index eb386384c..ff6a6aaa7 100644 --- a/src/main/java/net/minestom/server/instance/block/CustomBlock.java +++ b/src/main/java/net/minestom/server/instance/block/CustomBlock.java @@ -179,7 +179,7 @@ public abstract class CustomBlock { if (updateFrequency == null) return false; - return !updateFrequency.isZero(); + return !updateFrequency.isNegative() && !updateFrequency.isZero(); } From 075dab79c93b1498194f189f97cad99b48b16961 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Fri, 2 Jul 2021 19:35:19 +0200 Subject: [PATCH 52/64] Use ChronoUnit instead of TimeUnit --- .../java/net/minestom/server/item/metadata/PotionMeta.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/minestom/server/item/metadata/PotionMeta.java b/src/main/java/net/minestom/server/item/metadata/PotionMeta.java index dd231e7fb..8deff8901 100644 --- a/src/main/java/net/minestom/server/item/metadata/PotionMeta.java +++ b/src/main/java/net/minestom/server/item/metadata/PotionMeta.java @@ -6,13 +6,13 @@ import net.minestom.server.item.ItemMetaBuilder; import net.minestom.server.potion.CustomPotionEffect; import net.minestom.server.potion.PotionType; import net.minestom.server.registry.Registries; -import net.minestom.server.utils.time.TimeUnit; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jglrxavpok.hephaistos.nbt.NBTCompound; import org.jglrxavpok.hephaistos.nbt.NBTList; import org.jglrxavpok.hephaistos.nbt.NBTTypes; +import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.List; import java.util.function.Supplier; @@ -98,7 +98,7 @@ public class PotionMeta extends ItemMeta implements ItemMetaBuilder.Provider Date: Fri, 2 Jul 2021 19:47:15 +0200 Subject: [PATCH 53/64] Use a more logical way to convert to milliseconds --- .../java/net/minestom/server/item/metadata/PotionMeta.java | 4 ++-- src/main/java/net/minestom/server/utils/time/TimeUnit.java | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/minestom/server/item/metadata/PotionMeta.java b/src/main/java/net/minestom/server/item/metadata/PotionMeta.java index 8deff8901..db2255560 100644 --- a/src/main/java/net/minestom/server/item/metadata/PotionMeta.java +++ b/src/main/java/net/minestom/server/item/metadata/PotionMeta.java @@ -12,7 +12,7 @@ import org.jglrxavpok.hephaistos.nbt.NBTCompound; import org.jglrxavpok.hephaistos.nbt.NBTList; import org.jglrxavpok.hephaistos.nbt.NBTTypes; -import java.time.temporal.ChronoUnit; +import java.time.Duration; import java.util.ArrayList; import java.util.List; import java.util.function.Supplier; @@ -98,7 +98,7 @@ public class PotionMeta extends ItemMeta implements ItemMetaBuilder.Provider Date: Fri, 2 Jul 2021 19:58:59 +0200 Subject: [PATCH 54/64] Added #setFireForDuration(Duration) --- .../net/minestom/server/entity/LivingEntity.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/LivingEntity.java b/src/main/java/net/minestom/server/entity/LivingEntity.java index 97d14d42c..13ea79adf 100644 --- a/src/main/java/net/minestom/server/entity/LivingEntity.java +++ b/src/main/java/net/minestom/server/entity/LivingEntity.java @@ -328,10 +328,20 @@ public class LivingEntity extends Entity implements EquipmentHandler { * @see #setOnFire(boolean) if you want it to be permanent without any event callback */ public void setFireForDuration(int duration, TemporalUnit temporalUnit) { - EntityFireEvent entityFireEvent = new EntityFireEvent(this, temporalUnit.getDuration().multipliedBy(duration)); + setFireForDuration(Duration.of(duration, temporalUnit)); + } + + /** + * Sets fire to this entity for a given duration. + * + * @param duration duration of the effect + * @see #setOnFire(boolean) if you want it to be permanent without any event callback + */ + public void setFireForDuration(Duration duration) { + EntityFireEvent entityFireEvent = new EntityFireEvent(this, duration); // Do not start fire event if the fire needs to be removed (< 0 duration) - if (duration > 0) { + if (duration.toMillis() > 0) { EventDispatcher.callCancellable(entityFireEvent, () -> { final long fireTime = entityFireEvent.getFireTime(TimeUnit.MILLISECOND); setOnFire(true); From 3f1c433c944bb79c604b3eca545548be4e54fa40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Sat, 3 Jul 2021 14:41:57 +0200 Subject: [PATCH 55/64] Removed unnecessary deprecations --- src/main/java/net/minestom/server/entity/Entity.java | 2 -- src/main/java/net/minestom/server/entity/ItemEntity.java | 3 --- 2 files changed, 5 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/Entity.java b/src/main/java/net/minestom/server/entity/Entity.java index 0abadce61..8512a9221 100644 --- a/src/main/java/net/minestom/server/entity/Entity.java +++ b/src/main/java/net/minestom/server/entity/Entity.java @@ -1530,9 +1530,7 @@ public class Entity implements Viewable, Tickable, EventHandler, Da * @param delay the time before removing the entity, * 0 to cancel the removing * @param temporalUnit the unit of the delay - * @deprecated Replaced by {@link #scheduleRemove(Duration)} */ - @Deprecated public void scheduleRemove(long delay, @NotNull TemporalUnit temporalUnit) { scheduleRemove(Duration.of(delay, temporalUnit)); } diff --git a/src/main/java/net/minestom/server/entity/ItemEntity.java b/src/main/java/net/minestom/server/entity/ItemEntity.java index eba2ab87d..aae2eaebb 100644 --- a/src/main/java/net/minestom/server/entity/ItemEntity.java +++ b/src/main/java/net/minestom/server/entity/ItemEntity.java @@ -241,10 +241,7 @@ public class ItemEntity extends Entity { * * @param delay the pickup delay * @param temporalUnit the unit of the delay - * - * @deprecated Replaced by {@link #setPickupDelay(Duration)} */ - @Deprecated public void setPickupDelay(long delay, @NotNull TemporalUnit temporalUnit) { setPickupDelay(Duration.of(delay, temporalUnit)); } From efb450ce501f160920047b2eba79d3a0cc02edf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Sat, 3 Jul 2021 14:45:31 +0200 Subject: [PATCH 56/64] Simplify conversion logic and only use java.time --- .../java/net/minestom/server/utils/time/UpdateOption.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/minestom/server/utils/time/UpdateOption.java b/src/main/java/net/minestom/server/utils/time/UpdateOption.java index 9e4ac7824..cc9faa457 100644 --- a/src/main/java/net/minestom/server/utils/time/UpdateOption.java +++ b/src/main/java/net/minestom/server/utils/time/UpdateOption.java @@ -48,10 +48,10 @@ public class UpdateOption { * @return the converted milliseconds based on the time value and the unit */ public long toMilliseconds() { - return TimeUnit.getMillis(value, temporalUnit); + return toDuration().toMillis(); } public Duration toDuration() { - return Duration.ofMillis(toMilliseconds()); + return Duration.of(value, temporalUnit); } } From 663e53c875ae4a2fe16aa3497f0a80ae3c391314 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Sat, 3 Jul 2021 14:52:51 +0200 Subject: [PATCH 57/64] Add alternative to LivingEntity#setFireDamagePeriod that takes Duration --- .../net/minestom/server/entity/LivingEntity.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/LivingEntity.java b/src/main/java/net/minestom/server/entity/LivingEntity.java index 13ea79adf..28657961c 100644 --- a/src/main/java/net/minestom/server/entity/LivingEntity.java +++ b/src/main/java/net/minestom/server/entity/LivingEntity.java @@ -670,7 +670,7 @@ public class LivingEntity extends Entity implements EquipmentHandler { * Gets the time in ms between two fire damage applications. * * @return the time in ms - * @see #setFireDamagePeriod(long, TemporalUnit) + * @see #setFireDamagePeriod(Duration) */ public long getFireDamagePeriod() { return fireDamagePeriod; @@ -683,8 +683,16 @@ public class LivingEntity extends Entity implements EquipmentHandler { * @param temporalUnit the time unit */ public void setFireDamagePeriod(long fireDamagePeriod, @NotNull TemporalUnit temporalUnit) { - fireDamagePeriod = TimeUnit.getMillis(fireDamagePeriod, temporalUnit); - this.fireDamagePeriod = fireDamagePeriod; + setFireDamagePeriod(Duration.of(fireDamagePeriod, temporalUnit)); + } + + /** + * Changes the delay between two fire damage applications. + * + * @param fireDamagePeriod the delay + */ + public void setFireDamagePeriod(Duration fireDamagePeriod) { + this.fireDamagePeriod = fireDamagePeriod.toMillis(); } /** From 39dd5bfbf94beded5c71a35c99ef34414e52289a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Sat, 3 Jul 2021 14:56:22 +0200 Subject: [PATCH 58/64] Removed duplicated code --- src/main/java/net/minestom/server/timer/TaskBuilder.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/minestom/server/timer/TaskBuilder.java b/src/main/java/net/minestom/server/timer/TaskBuilder.java index 838b2dc67..cf3af3b57 100644 --- a/src/main/java/net/minestom/server/timer/TaskBuilder.java +++ b/src/main/java/net/minestom/server/timer/TaskBuilder.java @@ -71,8 +71,7 @@ public class TaskBuilder { */ @NotNull public TaskBuilder delay(long time, @NotNull TemporalUnit unit) { - this.delay = TimeUnit.getMillis(time, unit); - return this; + return delay(Duration.of(time, unit)); } /** @@ -111,8 +110,7 @@ public class TaskBuilder { */ @NotNull public TaskBuilder repeat(long time, @NotNull TemporalUnit unit) { - this.repeat = TimeUnit.getMillis(time, unit); - return this; + return repeat(Duration.of(time, unit)); } /** From 34d2d5892eb3fbeb3fe11a4ec3b83419e24868cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Sat, 3 Jul 2021 14:57:45 +0200 Subject: [PATCH 59/64] Remove unnecessary util method --- src/main/java/net/minestom/server/utils/time/TimeUnit.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/main/java/net/minestom/server/utils/time/TimeUnit.java b/src/main/java/net/minestom/server/utils/time/TimeUnit.java index c03901dfa..cda2f73a2 100644 --- a/src/main/java/net/minestom/server/utils/time/TimeUnit.java +++ b/src/main/java/net/minestom/server/utils/time/TimeUnit.java @@ -1,6 +1,5 @@ package net.minestom.server.utils.time; -import java.time.Duration; import java.time.temporal.ChronoUnit; import java.time.temporal.TemporalUnit; @@ -20,8 +19,4 @@ public class TimeUnit { private TimeUnit() { } - - public static long getMillis(long amount, TemporalUnit unit) { - return Duration.of(amount, unit).toMillis(); - } } From 1e390d5efb1ee8ca10d6fd945eb87143456e4940 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Sat, 3 Jul 2021 15:02:43 +0200 Subject: [PATCH 60/64] Add alternative to GLFWCapableBuffer#setupRenderLoop that takes Duration instead of amount and unit --- .../server/map/framebuffers/GLFWCapableBuffer.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/lwjgl/java/net/minestom/server/map/framebuffers/GLFWCapableBuffer.java b/src/lwjgl/java/net/minestom/server/map/framebuffers/GLFWCapableBuffer.java index 613f38fbb..c3e9ab9ae 100644 --- a/src/lwjgl/java/net/minestom/server/map/framebuffers/GLFWCapableBuffer.java +++ b/src/lwjgl/java/net/minestom/server/map/framebuffers/GLFWCapableBuffer.java @@ -11,6 +11,7 @@ import org.lwjgl.opengl.GL; import org.lwjgl.system.MemoryStack; import java.nio.ByteBuffer; +import java.time.Duration; import java.time.temporal.TemporalUnit; import static org.lwjgl.glfw.GLFW.*; @@ -72,6 +73,10 @@ public abstract class GLFWCapableBuffer { } public Task setupRenderLoop(long period, TemporalUnit unit, Runnable rendering) { + return setupRenderLoop(Duration.of(period, unit), rendering); + } + + public Task setupRenderLoop(Duration period, Runnable rendering) { return MinecraftServer.getSchedulerManager() .buildTask(new Runnable() { private boolean first = true; @@ -85,7 +90,7 @@ public abstract class GLFWCapableBuffer { render(rendering); } }) - .repeat(period, unit) + .repeat(period) .schedule(); } From 6a27a9d932319b755a8ff23d197c347e21f67c16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Sat, 3 Jul 2021 15:05:14 +0200 Subject: [PATCH 61/64] Update javadoc --- .../net/minestom/server/map/framebuffers/GLFWCapableBuffer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lwjgl/java/net/minestom/server/map/framebuffers/GLFWCapableBuffer.java b/src/lwjgl/java/net/minestom/server/map/framebuffers/GLFWCapableBuffer.java index c3e9ab9ae..92dc949f0 100644 --- a/src/lwjgl/java/net/minestom/server/map/framebuffers/GLFWCapableBuffer.java +++ b/src/lwjgl/java/net/minestom/server/map/framebuffers/GLFWCapableBuffer.java @@ -102,7 +102,7 @@ public abstract class GLFWCapableBuffer { /** * Called in render after glFlush to read the pixel buffer contents and convert it to map colors. - * Only call if you do not use {@link #render(Runnable)} nor {@link #setupRenderLoop(long, TemporalUnit, Runnable)} + * Only call if you do not use {@link #render(Runnable)} nor {@link #setupRenderLoop} */ public void prepareMapColors() { if(onlyMapColors) { From 67860d5afb63f103fa1602ebec64ff874fa57862 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Sat, 3 Jul 2021 15:25:41 +0200 Subject: [PATCH 62/64] Use Duration --- .../entity/ai/goal/CombinedAttackGoal.java | 53 ++++++++++++++++--- 1 file changed, 45 insertions(+), 8 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/ai/goal/CombinedAttackGoal.java b/src/main/java/net/minestom/server/entity/ai/goal/CombinedAttackGoal.java index 99629f64e..df309307d 100644 --- a/src/main/java/net/minestom/server/entity/ai/goal/CombinedAttackGoal.java +++ b/src/main/java/net/minestom/server/entity/ai/goal/CombinedAttackGoal.java @@ -24,13 +24,11 @@ public class CombinedAttackGoal extends GoalSelector { private final Cooldown cooldown = new Cooldown(Duration.of(5, TimeUnit.SERVER_TICK)); private final int meleeRangeSquared; - private final int meleeDelay; - private final TemporalUnit meleeTimeUnit; + private final Duration meleeDelay; private final int rangedRangeSquared; private final double rangedPower; private final double rangedSpread; - private final int rangedDelay; - private final TemporalUnit rangedTimeUnit; + private final Duration rangedDelay; private final int desirableRangeSquared; private final boolean comeClose; @@ -63,6 +61,28 @@ public class CombinedAttackGoal extends GoalSelector { ); } + /** + * @param entityCreature the entity to add the goal to. + * @param meleeRange the allowed range the entity can hit others in melee. + * @param rangedRange the allowed range the entity can shoot others. + * @param rangedPower shot power (1 for normal). + * @param rangedSpread shot spread (0 for best accuracy). + * @param delay the delay between any attacks. + * @param desirableRange the desirable range: the entity will try to stay no further than this distance. + * @param comeClose if entity should go as close as possible to the target whether target is not in line of sight for a ranged attack. + */ + public CombinedAttackGoal(@NotNull EntityCreature entityCreature, + int meleeRange, int rangedRange, double rangedPower, double rangedSpread, + Duration delay, + int desirableRange, boolean comeClose) { + this( + entityCreature, + meleeRange, delay, + rangedRange, rangedPower, rangedSpread, delay, + desirableRange, comeClose + ); + } + /** * @param entityCreature the entity to add the goal to. * @param meleeRange the allowed range the entity can hit others in melee. @@ -80,15 +100,32 @@ public class CombinedAttackGoal extends GoalSelector { int meleeRange, int meleeDelay, TemporalUnit meleeTimeUnit, int rangedRange, double rangedPower, double rangedSpread, int rangedDelay, TemporalUnit rangedTimeUnit, int desirableRange, boolean comeClose) { + this(entityCreature, meleeRange, Duration.of(meleeDelay, meleeTimeUnit), rangedRange, rangedPower, rangedSpread, + Duration.of(rangedDelay, rangedTimeUnit), desirableRange, comeClose); + } + + /** + * @param entityCreature the entity to add the goal to. + * @param meleeRange the allowed range the entity can hit others in melee. + * @param meleeDelay the delay between melee attacks. + * @param rangedRange the allowed range the entity can shoot others. + * @param rangedPower shot power (1 for normal). + * @param rangedSpread shot spread (0 for best accuracy). + * @param rangedDelay the delay between ranged attacks. + * @param desirableRange the desirable range: the entity will try to stay no further than this distance. + * @param comeClose if entity should go as close as possible to the target whether target is not in line of sight for a ranged attack. + */ + public CombinedAttackGoal(@NotNull EntityCreature entityCreature, + int meleeRange, Duration meleeDelay, + int rangedRange, double rangedPower, double rangedSpread, Duration rangedDelay, + int desirableRange, boolean comeClose) { super(entityCreature); this.meleeRangeSquared = meleeRange * meleeRange; this.meleeDelay = meleeDelay; - this.meleeTimeUnit = meleeTimeUnit; this.rangedRangeSquared = rangedRange * rangedRange; this.rangedPower = rangedPower; this.rangedSpread = rangedSpread; this.rangedDelay = rangedDelay; - this.rangedTimeUnit = rangedTimeUnit; this.desirableRangeSquared = desirableRange * desirableRange; this.comeClose = comeClose; Check.argCondition(desirableRange > rangedRange, "Desirable range can not exceed ranged range!"); @@ -130,12 +167,12 @@ public class CombinedAttackGoal extends GoalSelector { boolean comeClose = false; // First of all, checking if to perform melee or ranged attack depending on the distance to target. if (distanceSquared <= this.meleeRangeSquared) { - if (!Cooldown.hasCooldown(time, this.lastAttack, this.meleeTimeUnit, this.meleeDelay)) { + if (!Cooldown.hasCooldown(time, this.lastAttack, this.meleeDelay)) { this.entityCreature.attack(target, true); this.lastAttack = time; } } else if (distanceSquared <= this.rangedRangeSquared) { - if (!Cooldown.hasCooldown(time, this.lastAttack, this.rangedTimeUnit, this.rangedDelay)) { + if (!Cooldown.hasCooldown(time, this.lastAttack, this.rangedDelay)) { if (this.entityCreature.hasLineOfSight(target)) { // If target is on line of entity sight, ranged attack can be performed Position to = target.getPosition().clone().add(0D, target.getEyeHeight(), 0D); From b012ffbff3c7dca9b9e1b9ede3cfe63aee8efe4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Sat, 3 Jul 2021 15:27:23 +0200 Subject: [PATCH 63/64] Use Duration --- .../server/entity/ai/goal/MeleeAttackGoal.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/ai/goal/MeleeAttackGoal.java b/src/main/java/net/minestom/server/entity/ai/goal/MeleeAttackGoal.java index 39cd597ee..761d8ecb1 100644 --- a/src/main/java/net/minestom/server/entity/ai/goal/MeleeAttackGoal.java +++ b/src/main/java/net/minestom/server/entity/ai/goal/MeleeAttackGoal.java @@ -23,8 +23,7 @@ public class MeleeAttackGoal extends GoalSelector { private long lastHit; private final double range; - private final int delay; - private final TemporalUnit timeUnit; + private final Duration delay; private boolean stop; private Entity cachedTarget; @@ -36,10 +35,18 @@ public class MeleeAttackGoal extends GoalSelector { * @param timeUnit the unit of the delay */ public MeleeAttackGoal(@NotNull EntityCreature entityCreature, double range, int delay, @NotNull TemporalUnit timeUnit) { + this(entityCreature, range, Duration.of(delay, timeUnit)); + } + + /** + * @param entityCreature the entity to add the goal to + * @param range the allowed range the entity can attack others. + * @param delay the delay between each attacks + */ + public MeleeAttackGoal(@NotNull EntityCreature entityCreature, double range, Duration delay) { super(entityCreature); this.range = range; this.delay = delay; - this.timeUnit = timeUnit; } public @NotNull Cooldown getCooldown() { @@ -74,7 +81,7 @@ public class MeleeAttackGoal extends GoalSelector { // Attack the target entity if (entityCreature.getDistance(target) <= range) { - if (!Cooldown.hasCooldown(time, lastHit, timeUnit, delay)) { + if (!Cooldown.hasCooldown(time, lastHit, delay)) { entityCreature.attack(target, true); this.lastHit = time; } From cd65085df204a8bedb888462cc391cb8a56572d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Sat, 3 Jul 2021 15:29:28 +0200 Subject: [PATCH 64/64] Use Duration --- .../entity/ai/goal/RangedAttackGoal.java | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/ai/goal/RangedAttackGoal.java b/src/main/java/net/minestom/server/entity/ai/goal/RangedAttackGoal.java index d1d40d410..e0af845e8 100644 --- a/src/main/java/net/minestom/server/entity/ai/goal/RangedAttackGoal.java +++ b/src/main/java/net/minestom/server/entity/ai/goal/RangedAttackGoal.java @@ -21,8 +21,7 @@ public class RangedAttackGoal extends GoalSelector { private final Cooldown cooldown = new Cooldown(Duration.of(5, TimeUnit.SERVER_TICK)); private long lastShot; - private final int delay; - private final TemporalUnit timeUnit; + private final Duration delay; private final int attackRangeSquared; private final int desirableRangeSquared; private final boolean comeClose; @@ -45,9 +44,21 @@ public class RangedAttackGoal extends GoalSelector { * @param timeUnit the unit of the delay. */ public RangedAttackGoal(@NotNull EntityCreature entityCreature, int delay, int attackRange, int desirableRange, boolean comeClose, double power, double spread, @NotNull TemporalUnit timeUnit) { + this(entityCreature, Duration.of(delay, timeUnit), attackRange, desirableRange, comeClose, power, spread); + } + + /** + * @param entityCreature the entity to add the goal to. + * @param delay the delay between each shots. + * @param attackRange the allowed range the entity can shoot others. + * @param desirableRange the desirable range: the entity will try to stay no further than this distance. + * @param comeClose whether entity should go as close as possible to the target whether target is not in line of sight. + * @param spread shot spread (0 for best accuracy). + * @param power shot power (1 for normal). + */ + public RangedAttackGoal(@NotNull EntityCreature entityCreature, Duration delay, int attackRange, int desirableRange, boolean comeClose, double power, double spread) { super(entityCreature); this.delay = delay; - this.timeUnit = timeUnit; this.attackRangeSquared = attackRange * attackRange; this.desirableRangeSquared = desirableRange * desirableRange; this.comeClose = comeClose; @@ -91,7 +102,7 @@ public class RangedAttackGoal extends GoalSelector { double distanceSquared = this.entityCreature.getDistanceSquared(target); boolean comeClose = false; if (distanceSquared <= this.attackRangeSquared) { - if (!Cooldown.hasCooldown(time, this.lastShot, this.timeUnit, this.delay)) { + if (!Cooldown.hasCooldown(time, this.lastShot, this.delay)) { if (this.entityCreature.hasLineOfSight(target)) { Position to = target.getPosition().clone().add(0D, target.getEyeHeight(), 0D);