From 2067384aefbac900669afda018a41f272f5416be Mon Sep 17 00:00:00 2001 From: rhysdh540 Date: Mon, 24 Jul 2023 21:53:21 +0700 Subject: [PATCH] config command changes --- .../command/CreateUnlimitedCommands.java | 75 +++++++++---------- .../createunlimited/command/EnumArgument.java | 10 ++- 2 files changed, 43 insertions(+), 42 deletions(-) diff --git a/common/src/main/java/dev/rdh/createunlimited/command/CreateUnlimitedCommands.java b/common/src/main/java/dev/rdh/createunlimited/command/CreateUnlimitedCommands.java index 293b098..cdbe812 100644 --- a/common/src/main/java/dev/rdh/createunlimited/command/CreateUnlimitedCommands.java +++ b/common/src/main/java/dev/rdh/createunlimited/command/CreateUnlimitedCommands.java @@ -1,7 +1,6 @@ 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; @@ -27,12 +26,8 @@ import net.minecraftforge.common.ForgeConfigSpec; -import org.jetbrains.annotations.NotNull; - import java.lang.reflect.Field; -import java.util.HashMap; import java.util.List; -import java.util.function.Supplier; import static net.minecraft.commands.Commands.argument; import static net.minecraft.commands.Commands.literal; @@ -140,7 +135,9 @@ private static boolean perms(CommandSourceStack source) { private static void gdr(LiteralArgumentBuilder category, Field field, ForgeConfigSpec.ConfigValue value) { category.then(literal(field.getName()) .executes(context -> { - message(CUConfig.comments.get(field.getName()), context); + message(field.getName() + ": " + CUConfig.comments.get(field.getName()), context); + message("Current value: " + value.get(), context); + message("Default value: " + value.getDefault(), context); return Command.SINGLE_SUCCESS; }) .then(literal("reset").requires(CreateUnlimitedCommands::perms) @@ -159,56 +156,54 @@ private static void gdr(LiteralArgumentBuilder category, private static void setBoolean(LiteralArgumentBuilder category, Field field, ForgeConfigSpec.BooleanValue value) { category.then(literal(field.getName()) - .then(literal("set").requires(CreateUnlimitedCommands::perms) - .then(argument("value", BoolArgumentType.bool()) - .executes(context -> { - boolean set = BoolArgumentType.getBool(context, "value"); - value.set(set); - message(field.getName() + " set to: " + set, context); - return Command.SINGLE_SUCCESS; - }) - ) + .then(argument("value", BoolArgumentType.bool()).requires(CreateUnlimitedCommands::perms) + .executes(context -> { + boolean set = BoolArgumentType.getBool(context, "value"); + value.set(set); + message(field.getName() + " set to: " + set, context); + return Command.SINGLE_SUCCESS; + }) ) ); } private static void setInt(LiteralArgumentBuilder category, Field field, ForgeConfigSpec.IntValue value) { category.then(literal(field.getName()) - .then(literal("set").requires(CreateUnlimitedCommands::perms) - .then(argument("value", IntegerArgumentType.integer()) - .executes(context -> { - int set = IntegerArgumentType.getInteger(context, "value"); - value.set(set); - message(field.getName() + " set to: " + set, context); - return Command.SINGLE_SUCCESS; - }) - ) + .then(argument("value", IntegerArgumentType.integer()).requires(CreateUnlimitedCommands::perms) + .executes(context -> { + int set = IntegerArgumentType.getInteger(context, "value"); + value.set(set); + message(field.getName() + " set to: " + set, context); + return Command.SINGLE_SUCCESS; + }) ) ); } private static void setDouble(LiteralArgumentBuilder category, Field field, ForgeConfigSpec.DoubleValue value) { category.then(literal(field.getName()) - .then(literal("set").requires(CreateUnlimitedCommands::perms) - .then(argument("value", DoubleArgumentType.doubleArg()) - .executes(context -> { - double set = DoubleArgumentType.getDouble(context, "value"); - value.set(set); - message(field.getName() + " set to: " + set, context); - return Command.SINGLE_SUCCESS; - })))); + .then(argument("value", DoubleArgumentType.doubleArg()).requires(CreateUnlimitedCommands::perms) + .executes(context -> { + double set = DoubleArgumentType.getDouble(context, "value"); + value.set(set); + message(field.getName() + " set to: " + set, context); + return Command.SINGLE_SUCCESS; + }) + ) + ); } private static > void setEnum(LiteralArgumentBuilder category, Field field, ForgeConfigSpec.EnumValue value) { category.then(literal(field.getName()) - .then(literal("set").requires(CreateUnlimitedCommands::perms) - .then(argument("value", EnumArgument.enumArg(value.get().getClass(), true)) - .executes(context -> { - T set = (T) context.getArgument("value", value.get().getClass()); - value.set(set); - message(field.getName() + " set to: " + set.name().toLowerCase(), context); - return Command.SINGLE_SUCCESS; - })))); + .then(argument("value", EnumArgument.enumArg(value.get().getClass(), true)) + .executes(context -> { + T set = (T) context.getArgument("value", value.get().getClass()); + value.set(set); + message(field.getName() + " set to: " + set.name().toLowerCase(), context); + return Command.SINGLE_SUCCESS; + }) + ) + ); } private static MutableComponent link(String link, String display, ChatFormatting color) { diff --git a/common/src/main/java/dev/rdh/createunlimited/command/EnumArgument.java b/common/src/main/java/dev/rdh/createunlimited/command/EnumArgument.java index c759812..4587404 100644 --- a/common/src/main/java/dev/rdh/createunlimited/command/EnumArgument.java +++ b/common/src/main/java/dev/rdh/createunlimited/command/EnumArgument.java @@ -32,7 +32,7 @@ */ public class EnumArgument> implements ArgumentType { private static final Dynamic2CommandExceptionType INVALID_ENUM = new Dynamic2CommandExceptionType( - (found, constants) -> Component.translatable("commands.forge.arguments.enum.invalid", constants, found)); + (found, constants) -> Component.literal(String.format("Invalid enum value '%s', expected one of: %s", found, constants))); private final Class enumClass; private final boolean lowercase; @@ -119,6 +119,12 @@ private String lowercase(String s) { return lowercase ? s.toLowerCase() : s; } private String unlowercase(String s) { - return lowercase ? s.toUpperCase() : s; + if(!lowercase) + return s; + return Arrays.stream(enumClass.getEnumConstants()) + .map(Enum::name) + .filter(n -> n.equalsIgnoreCase(s)) + .findFirst() + .orElse(s); } } \ No newline at end of file