]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/commands/Arguments.java
5a71d2fee3739519161c65aa8cdffb390365c29e
[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.string();
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     private static <T> T getArgumentValueOrDefault(CommandContext<CommandSource> context,
71                                                    String name,
72                                                    ArgumentFetcher<T> getValue,
73                                                    Supplier<T> defaultValue) throws CommandSyntaxException {
74         try {
75             return getValue.get(context, name);
76         } catch (IllegalArgumentException exception) {
77             return defaultValue.get();
78         }
79     }
80
81     @FunctionalInterface
82     private interface ArgumentFetcher<T> {
83         T get(CommandContext<CommandSource> context, String name) throws CommandSyntaxException;
84     }
85 }