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