Included Tick.java from PR#205 with slight modifications

This commit is contained in:
Németh Noel 2021-06-29 23:44:19 +02:00
parent 5e731e5ba9
commit 4b04539086
3 changed files with 78 additions and 11 deletions

View File

@ -36,7 +36,7 @@ public class ArgumentTime extends Argument<Duration> {
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<Duration> {
} 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);
}

View File

@ -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:

View File

@ -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 extends Temporal> 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);
}
}
}