| 1 | package net.minecraft.commands.arguments;
|
| 2 |
|
| 3 | import com.mojang.brigadier.StringReader;
|
| 4 | import com.mojang.brigadier.arguments.ArgumentType;
|
| 5 | import com.mojang.brigadier.context.CommandContext;
|
| 6 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 7 | import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
| 8 | import java.util.Arrays;
|
| 9 | import java.util.Collection;
|
| 10 | import net.minecraft.commands.CommandSourceStack;
|
| 11 | import net.minecraft.commands.arguments.coordinates.WorldCoordinate;
|
| 12 | import net.minecraft.network.chat.Component;
|
| 13 | import net.minecraft.util.Mth;
|
| 14 |
|
| 15 | public class AngleArgument implements ArgumentType<AngleArgument.SingleAngle> {
|
| 16 | private static final Collection<String> EXAMPLES = Arrays.asList("0", "~", "~-5");
|
| 17 | public static final SimpleCommandExceptionType ERROR_NOT_COMPLETE = new SimpleCommandExceptionType(Component.translatable("argument.angle.incomplete"));
|
| 18 | public static final SimpleCommandExceptionType ERROR_INVALID_ANGLE = new SimpleCommandExceptionType(Component.translatable("argument.angle.invalid"));
|
| 19 |
|
| 20 | public static AngleArgument angle() {
|
| 21 | return new AngleArgument();
|
| 22 | }
|
| 23 |
|
| 24 | public static float getAngle(final CommandContext<CommandSourceStack> context, final String name) {
|
| 25 | return context.getArgument(name, AngleArgument.SingleAngle.class).getAngle(context.getSource());
|
| 26 | }
|
| 27 |
|
| 28 | public AngleArgument.SingleAngle parse(final StringReader reader) throws CommandSyntaxException {
|
| 29 | if (!reader.canRead()) {
|
| 30 | throw ERROR_NOT_COMPLETE.createWithContext(reader);
|
| 31 | } else {
|
| 32 | boolean isRelative = WorldCoordinate.isRelative(reader);
|
| 33 | float value = reader.canRead() && reader.peek() != ' ' ? reader.readFloat() : 0.0F;
|
| 34 | if (!Float.isNaN(value) && !Float.isInfinite(value)) {
|
| 35 | return new AngleArgument.SingleAngle(value, isRelative);
|
| 36 | } else {
|
| 37 | throw ERROR_INVALID_ANGLE.createWithContext(reader);
|
| 38 | }
|
| 39 | }
|
| 40 | }
|
| 41 |
|
| 42 | @Override
|
| 43 | public Collection<String> getExamples() {
|
| 44 | return EXAMPLES;
|
| 45 | }
|
| 46 |
|
| 47 | public static final class SingleAngle {
|
| 48 | private final float angle;
|
| 49 | private final boolean isRelative;
|
| 50 |
|
| 51 | private SingleAngle(final float angle, final boolean isRelative) {
|
| 52 | this.angle = angle;
|
| 53 | this.isRelative = isRelative;
|
| 54 | }
|
| 55 |
|
| 56 | public float getAngle(final CommandSourceStack sender) {
|
| 57 | return Mth.wrapDegrees(this.isRelative ? this.angle + sender.getRotation().y : this.angle);
|
| 58 | }
|
| 59 | }
|
| 60 | }
|
| 61 |
|