mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-06 16:37:38 +01:00
Updated ArgumentTime.java
This commit is contained in:
parent
0a4166faf2
commit
ab80ffcd0e
@ -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.arguments.Argument;
|
||||||
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
|
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
|
||||||
import net.minestom.server.network.packet.server.play.DeclareCommandsPacket;
|
import net.minestom.server.network.packet.server.play.DeclareCommandsPacket;
|
||||||
import net.minestom.server.utils.time.TimeUnit;
|
import net.minestom.server.utils.time.Tick;
|
||||||
import net.minestom.server.utils.time.UpdateOption;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
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).
|
* Represents an argument giving a time (day/second/tick).
|
||||||
* <p>
|
* <p>
|
||||||
* Example: 50d, 25s, 75t
|
* Example: 50d, 25s, 75t
|
||||||
*/
|
*/
|
||||||
public class ArgumentTime extends Argument<UpdateOption> {
|
public class ArgumentTime extends Argument<Duration> {
|
||||||
|
|
||||||
public static final int INVALID_TIME_FORMAT = -2;
|
public static final int INVALID_TIME_FORMAT = -2;
|
||||||
public static final int NO_NUMBER = -3;
|
public static final int NO_NUMBER = -3;
|
||||||
@ -28,21 +31,21 @@ public class ArgumentTime extends Argument<UpdateOption> {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@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);
|
final char lastChar = input.charAt(input.length() - 1);
|
||||||
|
|
||||||
TimeUnit timeUnit;
|
TemporalUnit timeUnit;
|
||||||
if (Character.isDigit(lastChar))
|
if (Character.isDigit(lastChar))
|
||||||
timeUnit = TimeUnit.TICK;
|
timeUnit = Tick.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);
|
||||||
|
|
||||||
if (lastChar == 'd') {
|
if (lastChar == 'd') {
|
||||||
timeUnit = TimeUnit.DAY;
|
timeUnit = ChronoUnit.DAYS;
|
||||||
} else if (lastChar == 's') {
|
} else if (lastChar == 's') {
|
||||||
timeUnit = TimeUnit.SECOND;
|
timeUnit = ChronoUnit.SECONDS;
|
||||||
} else if (lastChar == 't') {
|
} else if (lastChar == 't') {
|
||||||
timeUnit = TimeUnit.TICK;
|
timeUnit = Tick.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);
|
||||||
}
|
}
|
||||||
@ -52,7 +55,7 @@ public class ArgumentTime extends Argument<UpdateOption> {
|
|||||||
try {
|
try {
|
||||||
// Check if value is a number
|
// Check if value is a number
|
||||||
final int time = Integer.parseInt(input);
|
final int time = Integer.parseInt(input);
|
||||||
return new UpdateOption(time, timeUnit);
|
return Duration.of(time, timeUnit);
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
throw new ArgumentSyntaxException("Time needs to be a number", input, NO_NUMBER);
|
throw new ArgumentSyntaxException("Time needs to be a number", input, NO_NUMBER);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user