mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-08 01:17:47 +01:00
Included Tick.java from PR#205 with slight modifications
This commit is contained in:
parent
5e731e5ba9
commit
4b04539086
@ -36,7 +36,7 @@ public class ArgumentTime extends Argument<Duration> {
|
|||||||
|
|
||||||
TemporalUnit timeUnit;
|
TemporalUnit timeUnit;
|
||||||
if (Character.isDigit(lastChar))
|
if (Character.isDigit(lastChar))
|
||||||
timeUnit = Tick.TICKS;
|
timeUnit = Tick.SERVER_TICKS;
|
||||||
else if (SUFFIXES.contains(lastChar)) {
|
else if (SUFFIXES.contains(lastChar)) {
|
||||||
input = input.substring(0, input.length() - 1);
|
input = input.substring(0, input.length() - 1);
|
||||||
|
|
||||||
@ -45,7 +45,7 @@ public class ArgumentTime extends Argument<Duration> {
|
|||||||
} else if (lastChar == 's') {
|
} else if (lastChar == 's') {
|
||||||
timeUnit = ChronoUnit.SECONDS;
|
timeUnit = ChronoUnit.SECONDS;
|
||||||
} else if (lastChar == 't') {
|
} else if (lastChar == 't') {
|
||||||
timeUnit = Tick.TICKS;
|
timeUnit = Tick.SERVER_TICKS;
|
||||||
} else {
|
} else {
|
||||||
throw new ArgumentSyntaxException("Time needs to have the unit d, s, t, or none", input, NO_NUMBER);
|
throw new ArgumentSyntaxException("Time needs to have the unit d, s, t, or none", input, NO_NUMBER);
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,7 @@ public class EntityFireEvent implements EntityEvent, CancellableEvent {
|
|||||||
public long getFireTime(net.minestom.server.utils.time.TimeUnit timeUnit) {
|
public long getFireTime(net.minestom.server.utils.time.TimeUnit timeUnit) {
|
||||||
switch (timeUnit) {
|
switch (timeUnit) {
|
||||||
case TICK:
|
case TICK:
|
||||||
return duration.toMillis() / Tick.TICKS.getDuration().toMillis();
|
return duration.toMillis() / Tick.SERVER_TICKS.getDuration().toMillis();
|
||||||
case MILLISECOND:
|
case MILLISECOND:
|
||||||
return duration.toMillis();
|
return duration.toMillis();
|
||||||
default:
|
default:
|
||||||
|
@ -6,15 +6,82 @@ import java.time.Duration;
|
|||||||
import java.time.temporal.Temporal;
|
import java.time.temporal.Temporal;
|
||||||
import java.time.temporal.TemporalUnit;
|
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
|
@Override
|
||||||
public Duration getDuration() {
|
public Duration getDuration() {
|
||||||
return Duration.ofMillis(MinecraftServer.TICK_MS);
|
return Duration.ofMillis(this.milliseconds);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -32,14 +99,14 @@ public class Tick implements TemporalUnit {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked") // following ChronoUnit#addTo
|
||||||
@Override
|
@Override
|
||||||
public <R extends Temporal> R addTo(R temporal, long amount) {
|
public <R extends Temporal> R addTo(R temporal, long amount) {
|
||||||
//noinspection unchecked
|
|
||||||
return (R) temporal.plus(amount, this);
|
return (R) temporal.plus(amount, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long between(Temporal temporal1Inclusive, Temporal temporal2Exclusive) {
|
public long between(Temporal start, Temporal end) {
|
||||||
return temporal1Inclusive.until(temporal2Exclusive, this);
|
return start.until(end, this);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user