|
| 1 | +package core.paper.command.argument; |
| 2 | + |
| 3 | +import com.mojang.brigadier.arguments.ArgumentType; |
| 4 | +import io.papermc.paper.command.brigadier.argument.ArgumentTypes; |
| 5 | +import io.papermc.paper.command.brigadier.argument.CustomArgumentType; |
| 6 | +import io.papermc.paper.util.Tick; |
| 7 | +import org.jetbrains.annotations.Contract; |
| 8 | +import org.jspecify.annotations.NullMarked; |
| 9 | + |
| 10 | +import java.time.Duration; |
| 11 | + |
| 12 | +/** |
| 13 | + * A custom argument type implementation to handle durations in a converted format. |
| 14 | + */ |
| 15 | +@NullMarked |
| 16 | +public class DurationArgument implements CustomArgumentType.Converted<Duration, Integer> { |
| 17 | + private final Duration minimum; |
| 18 | + |
| 19 | + private DurationArgument(Duration minimum) { |
| 20 | + this.minimum = minimum; |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * Creates a {@link DurationArgument} instance with a default minimum duration of zero. |
| 25 | + * |
| 26 | + * @return a new {@link DurationArgument} instance with a minimum duration of zero |
| 27 | + */ |
| 28 | + @Contract(value = " -> new", pure = true) |
| 29 | + public static DurationArgument duration() { |
| 30 | + return duration(Duration.ZERO); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Creates a new {@link DurationArgument} instance with the specified minimum duration. |
| 35 | + * |
| 36 | + * @param minimum the minimum duration for the argument |
| 37 | + * @return a new {@link DurationArgument} instance with the specified minimum duration |
| 38 | + */ |
| 39 | + @Contract(value = "_ -> new", pure = true) |
| 40 | + public static DurationArgument duration(Duration minimum) { |
| 41 | + return new DurationArgument(minimum); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public Duration convert(Integer nativeType) { |
| 46 | + return Tick.of(nativeType); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public ArgumentType<Integer> getNativeType() { |
| 51 | + return ArgumentTypes.time(Tick.tick().fromDuration(minimum)); |
| 52 | + } |
| 53 | +} |
0 commit comments