diff --git a/common/src/main/java/dev/rdh/createunlimited/command/CUConfigCommand.java b/common/src/main/java/dev/rdh/createunlimited/command/CUConfigCommand.java index 8a62c99..ff87f05 100644 --- a/common/src/main/java/dev/rdh/createunlimited/command/CUConfigCommand.java +++ b/common/src/main/java/dev/rdh/createunlimited/command/CUConfigCommand.java @@ -1,6 +1,7 @@ package dev.rdh.createunlimited.command; import com.mojang.brigadier.Command; +import com.mojang.brigadier.arguments.ArgumentType; import com.mojang.brigadier.arguments.BoolArgumentType; import com.mojang.brigadier.arguments.DoubleArgumentType; import com.mojang.brigadier.arguments.IntegerArgumentType; @@ -45,7 +46,7 @@ public CUConfigCommand(boolean integrated) { // skip if not config value if (!CValue.class.isAssignableFrom(field.getType())) continue; - String name = field.getName(); + final String name = field.getName(); // change category if needed if (field.getType() == ConfigGroup.class) { @@ -53,7 +54,7 @@ public CUConfigCommand(boolean integrated) { category = literal(name); // add description for category - base.then(literal(field.getName()).executes(context -> { + base.then(literal(name).executes(context -> { message(context, CUServer.getComment(name)); return Command.SINGLE_SUCCESS; })); @@ -67,28 +68,12 @@ public CUConfigCommand(boolean integrated) { try { cValue = (CValue) field.get(CUConfigs.server); } catch (IllegalAccessException | ClassCastException e) { - CreateUnlimited.LOGGER.error("Failed to get config value for {}", field.getName(), e); + //noinspection StringConcatenationArgumentToLogCall + CreateUnlimited.LOGGER.error("Failed to get config value for " + name, e); continue; } - // get config as forge config value - ConfigValue value = ((CValueAccessor) cValue).getValue(); - - // handle getting, description, and resetting - gdr(category, name, value); - - if (value instanceof BooleanValue bValue) - setBoolean(category, name, bValue); - - else if (value instanceof EnumValue> eValue) - setEnum(category, name, eValue); - - else if (value instanceof IntValue iValue) - setInt(category, name, iValue); - - else if (value instanceof DoubleValue dValue) - setDouble(category, name, dValue); - + configure(category, name, ((CValueAccessor) cValue).getValue()); } if (category != null) @@ -100,7 +85,10 @@ private boolean perms(CommandSourceStack source) { return integrated || source.hasPermission(2); } - private void gdr(LiteralArgumentBuilder category, String name, ConfigValue value) { + private void configure(LiteralArgumentBuilder category, String name, ConfigValue value) { + @SuppressWarnings("unchecked") + Class clazz = (Class) value.getDefault().getClass(); + category.then(literal(name) .executes(context -> { message(context, name + ": " + CUServer.getComment(name)); @@ -119,78 +107,28 @@ private void gdr(LiteralArgumentBuilder category, String return Command.SINGLE_SUCCESS; }) ) - ); - } - - private void setBoolean(LiteralArgumentBuilder category, String name, BooleanValue value) { - category.then(literal(name) - .then(argument("value", BoolArgumentType.bool()).requires(this::perms) - .executes(context -> { - boolean set = BoolArgumentType.getBool(context, "value"); - if(set == value.get()) { - error(context, "Value is already set to " + set); - return 0; - } - value.set(set); - message(context, name + " set to: " + set); - return Command.SINGLE_SUCCESS; - }) - ) - ); - } - - private void setInt(LiteralArgumentBuilder category, String name, IntValue value) { - category.then(literal(name) - .then(argument("value", IntegerArgumentType.integer()).requires(this::perms) - .executes(context -> { - int set = IntegerArgumentType.getInteger(context, "value"); - if(set == value.get()) { - error(context, "Value is already set to " + set); - return 0; - } - value.set(set); - message(context, name + " set to: " + set); - return Command.SINGLE_SUCCESS; - }) - ) - ); - } - - private void setDouble(LiteralArgumentBuilder category, String name, DoubleValue value) { - category.then(literal(name) - .then(argument("value", DoubleArgumentType.doubleArg()) - .requires(this::perms) + .then(argument("value", getArgument(value)).requires(this::perms) .executes(context -> { - double set = DoubleArgumentType.getDouble(context, "value"); + T set = context.getArgument("value", clazz); if(set == value.get()) { error(context, "Value is already set to " + set); return 0; } value.set(set); - message(context, name + " set to: " + set); + message(context, "Value set to: " + set); return Command.SINGLE_SUCCESS; }) ) ); } - @SuppressWarnings("unchecked") - private > void setEnum(LiteralArgumentBuilder category, String name, EnumValue value) { + @SuppressWarnings({"unchecked", "rawtypes"}) + private ArgumentType getArgument(ConfigValue value) { + if(value instanceof BooleanValue) return (ArgumentType) BoolArgumentType.bool(); + if(value instanceof DoubleValue) return (ArgumentType) DoubleArgumentType.doubleArg(); + if(value instanceof IntValue) return (ArgumentType) IntegerArgumentType.integer(); Class clazz = (Class) value.getDefault().getClass(); - category.then(literal(name) - .then(argument("value", EnumArgument.enumArg(clazz, true)) - .requires(this::perms) - .executes(context -> { - T set = EnumArgument.getEnum(context, "value", clazz); - if(set == value.get()) { - error(context, "Value is already set to " + set.name().toLowerCase()); - return 0; - } - value.set(set); - message(context, name + " set to: " + set.name().toLowerCase()); - return Command.SINGLE_SUCCESS; - }) - ) - ); + if(value instanceof EnumValue && clazz.isEnum()) return (ArgumentType) EnumArgument.enumArg((Class) clazz, true); + throw new IllegalArgumentException("Unsupported class for argument: " + clazz); } }