]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/commands/Arguments.java
Simplify commands to use Coords & Pos objects
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / client / commands / Arguments.java
1 package com.irtimaled.bbor.client.commands;
2
3 import com.irtimaled.bbor.common.models.Coords;
4 import com.irtimaled.bbor.common.models.Point;
5 import com.mojang.brigadier.arguments.BoolArgumentType;
6 import com.mojang.brigadier.arguments.DoubleArgumentType;
7 import com.mojang.brigadier.arguments.IntegerArgumentType;
8 import com.mojang.brigadier.arguments.StringArgumentType;
9 import com.mojang.brigadier.context.CommandContext;
10 import com.mojang.brigadier.exceptions.CommandSyntaxException;
11 import net.minecraft.command.CommandSource;
12 import net.minecraft.command.arguments.BlockPosArgument;
13 import net.minecraft.command.arguments.Vec3Argument;
14
15 import java.util.function.Supplier;
16
17 public class Arguments {
18     public static BlockPosArgument coords() {
19         return BlockPosArgument.blockPos();
20     }
21
22     public static Vec3Argument point() {
23         return Vec3Argument.vec3();
24     }
25
26     public static IntegerArgumentType integer() {
27         return IntegerArgumentType.integer();
28     }
29
30     public static IntegerArgumentType integer(int min, int max) {
31         return IntegerArgumentType.integer(min, max);
32     }
33
34     public static DoubleArgumentType doubleArg() {
35         return DoubleArgumentType.doubleArg();
36     }
37
38     public static StringArgumentType string() {
39         return StringArgumentType.greedyString();
40     }
41
42     public static BoolArgumentType bool() {
43         return BoolArgumentType.bool();
44     }
45
46     public static Coords getCoords(CommandContext<CommandSource> context, String name) throws CommandSyntaxException {
47         return new Coords(getArgumentValueOrDefault(context, name, Vec3Argument::getVec3, () -> context.getSource().getPos()));
48     }
49
50     public static Point getPoint(CommandContext<CommandSource> context, String name) throws CommandSyntaxException {
51         return new Point(getArgumentValueOrDefault(context, name, Vec3Argument::getVec3, () -> context.getSource().getPos()));
52     }
53
54     public static int getInteger(CommandContext<CommandSource> context, String name) throws CommandSyntaxException {
55         return getArgumentValueOrDefault(context, name, IntegerArgumentType::getInteger, () -> 0);
56     }
57
58     public static double getDouble(CommandContext<CommandSource> context, String name) throws CommandSyntaxException {
59         return getArgumentValueOrDefault(context, name, DoubleArgumentType::getDouble, () -> 0.0D);
60     }
61
62     public static String getString(CommandContext<CommandSource> context, String name) throws CommandSyntaxException {
63         return getArgumentValueOrDefault(context, name, StringArgumentType::getString, () -> "");
64     }
65
66     public static boolean getBool(CommandContext<CommandSource> context, String name) throws CommandSyntaxException {
67         return getArgumentValueOrDefault(context, name, BoolArgumentType::getBool, () -> false);
68     }
69
70
71     public static <T> T getArgumentValueOrDefault(CommandContext<CommandSource> context,
72                                                   String name,
73                                                   ArgumentFetcher<T> getValue,
74                                                   Supplier<T> defaultValue) throws CommandSyntaxException {
75         try {
76             return getValue.get(context, name);
77         } catch (IllegalArgumentException exception) {
78             return defaultValue.get();
79         }
80     }
81
82     @FunctionalInterface
83     private interface ArgumentFetcher<T> {
84         T get(CommandContext<CommandSource> context, String name) throws CommandSyntaxException;
85     }
86 }