1 package com.irtimaled.bbor.client.commands;
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.CommandSource;
10 import net.minecraft.command.arguments.BlockPosArgument;
11 import net.minecraft.command.arguments.Vec3Argument;
13 import java.util.function.Supplier;
15 public class Arguments {
16 public static BlockPosArgument coords() {
17 return BlockPosArgument.blockPos();
20 public static Vec3Argument point() {
21 return Vec3Argument.vec3();
24 public static IntegerArgumentType integer() {
25 return IntegerArgumentType.integer();
28 public static IntegerArgumentType integer(int min, int max) {
29 return IntegerArgumentType.integer(min, max);
32 public static DoubleArgumentType doubleArg() {
33 return DoubleArgumentType.doubleArg();
36 public static StringArgumentType string() {
37 return StringArgumentType.string();
40 public static BoolArgumentType bool() {
41 return BoolArgumentType.bool();
44 public static ArgumentType<HexColor> hexColor() {
45 return new HexColorArgument();
48 public static Coords getCoords(CommandContext<CommandSource> context, String name) throws CommandSyntaxException {
49 return new Coords(getArgumentValueOrDefault(context, name, Vec3Argument::getVec3, () -> context.getSource().getPos()));
52 public static Point getPoint(CommandContext<CommandSource> context, String name) throws CommandSyntaxException {
53 return new Point(getArgumentValueOrDefault(context, name, Vec3Argument::getVec3, () -> context.getSource().getPos()));
56 public static int getInteger(CommandContext<CommandSource> context, String name) throws CommandSyntaxException {
57 return getArgumentValueOrDefault(context, name, IntegerArgumentType::getInteger, () -> 0);
60 public static double getDouble(CommandContext<CommandSource> context, String name) throws CommandSyntaxException {
61 return getArgumentValueOrDefault(context, name, DoubleArgumentType::getDouble, () -> 0.0D);
64 public static String getString(CommandContext<CommandSource> context, String name) throws CommandSyntaxException {
65 return getArgumentValueOrDefault(context, name, StringArgumentType::getString, () -> "");
68 public static boolean getBool(CommandContext<CommandSource> context, String name) throws CommandSyntaxException {
69 return getArgumentValueOrDefault(context, name, BoolArgumentType::getBool, () -> false);
72 private static <T> T getArgumentValueOrDefault(CommandContext<CommandSource> context,
74 ArgumentFetcher<T> getValue,
75 Supplier<T> defaultValue) throws CommandSyntaxException {
77 return getValue.get(context, name);
78 } catch (IllegalArgumentException exception) {
79 return defaultValue.get();
84 private interface ArgumentFetcher<T> {
85 T get(CommandContext<CommandSource> context, String name) throws CommandSyntaxException;