1 package com.irtimaled.bbor.client.commands;
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;
15 import java.util.function.Supplier;
17 public class Arguments {
18 public static BlockPosArgument coords() {
19 return BlockPosArgument.blockPos();
22 public static Vec3Argument point() {
23 return Vec3Argument.vec3();
26 public static IntegerArgumentType integer() {
27 return IntegerArgumentType.integer();
30 public static IntegerArgumentType integer(int min, int max) {
31 return IntegerArgumentType.integer(min, max);
34 public static DoubleArgumentType doubleArg() {
35 return DoubleArgumentType.doubleArg();
38 public static StringArgumentType string() {
39 return StringArgumentType.string();
42 public static BoolArgumentType bool() {
43 return BoolArgumentType.bool();
46 public static Coords getCoords(CommandContext<CommandSource> context, String name) throws CommandSyntaxException {
47 return new Coords(getArgumentValueOrDefault(context, name, Vec3Argument::getVec3, () -> context.getSource().getPos()));
50 public static Point getPoint(CommandContext<CommandSource> context, String name) throws CommandSyntaxException {
51 return new Point(getArgumentValueOrDefault(context, name, Vec3Argument::getVec3, () -> context.getSource().getPos()));
54 public static int getInteger(CommandContext<CommandSource> context, String name) throws CommandSyntaxException {
55 return getArgumentValueOrDefault(context, name, IntegerArgumentType::getInteger, () -> 0);
58 public static double getDouble(CommandContext<CommandSource> context, String name) throws CommandSyntaxException {
59 return getArgumentValueOrDefault(context, name, DoubleArgumentType::getDouble, () -> 0.0D);
62 public static String getString(CommandContext<CommandSource> context, String name) throws CommandSyntaxException {
63 return getArgumentValueOrDefault(context, name, StringArgumentType::getString, () -> "");
66 public static boolean getBool(CommandContext<CommandSource> context, String name) throws CommandSyntaxException {
67 return getArgumentValueOrDefault(context, name, BoolArgumentType::getBool, () -> false);
70 private static <T> T getArgumentValueOrDefault(CommandContext<CommandSource> context,
72 ArgumentFetcher<T> getValue,
73 Supplier<T> defaultValue) throws CommandSyntaxException {
75 return getValue.get(context, name);
76 } catch (IllegalArgumentException exception) {
77 return defaultValue.get();
82 private interface ArgumentFetcher<T> {
83 T get(CommandContext<CommandSource> context, String name) throws CommandSyntaxException;