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