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.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;
17 import java.util.function.Supplier;
19 public class Arguments {
20 public static BlockPosArgumentType coords() {
21 return BlockPosArgumentType.blockPos();
24 public static Vec3ArgumentType point() {
25 return Vec3ArgumentType.vec3();
28 public static IntegerArgumentType integer() {
29 return IntegerArgumentType.integer();
32 public static IntegerArgumentType integer(int min, int max) {
33 return IntegerArgumentType.integer(min, max);
36 public static DoubleArgumentType doubleArg() {
37 return DoubleArgumentType.doubleArg();
40 public static StringArgumentType string() {
41 return StringArgumentType.string();
44 public static BoolArgumentType bool() {
45 return BoolArgumentType.bool();
48 public static ArgumentType<HexColor> hexColor() {
49 return new HexColorArgument();
52 public static Coords getCoords(CommandContext<ServerCommandSource> context, String name) throws CommandSyntaxException {
53 return new Coords(getArgumentValueOrDefault(context, name, Vec3ArgumentType::getVec3, () -> context.getSource().getPosition()));
56 public static Point getPoint(CommandContext<ServerCommandSource> context, String name) throws CommandSyntaxException {
57 return new Point(getArgumentValueOrDefault(context, name, Vec3ArgumentType::getVec3, () -> context.getSource().getPosition()));
60 public static int getInteger(CommandContext<ServerCommandSource> context, String name) throws CommandSyntaxException {
61 return getArgumentValueOrDefault(context, name, IntegerArgumentType::getInteger, () -> 0);
64 public static double getDouble(CommandContext<ServerCommandSource> context, String name) throws CommandSyntaxException {
65 return getArgumentValueOrDefault(context, name, DoubleArgumentType::getDouble, () -> 0.0D);
68 public static String getString(CommandContext<ServerCommandSource> context, String name) throws CommandSyntaxException {
69 return getArgumentValueOrDefault(context, name, StringArgumentType::getString, () -> "");
72 public static boolean getBool(CommandContext<ServerCommandSource> context, String name) throws CommandSyntaxException {
73 return getArgumentValueOrDefault(context, name, BoolArgumentType::getBool, () -> false);
76 private static <T> T getArgumentValueOrDefault(CommandContext<ServerCommandSource> context,
78 ArgumentFetcher<T> getValue,
79 Supplier<T> defaultValue) throws CommandSyntaxException {
81 return getValue.get(context, name);
82 } catch (IllegalArgumentException exception) {
83 return defaultValue.get();
88 private interface ArgumentFetcher<T> {
89 T get(CommandContext<ServerCommandSource> context, String name) throws CommandSyntaxException;