From 386a95aca82d2a81c78887a398e3973d70d00410 Mon Sep 17 00:00:00 2001 From: Martin Prokoph Date: Sat, 5 Apr 2025 12:10:11 +0200 Subject: [PATCH] feat: optimize AutoCommand's file-size --- .../midnightdust/lib/config/AutoCommand.java | 40 +++++++++---------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/common/src/main/java/eu/midnightdust/lib/config/AutoCommand.java b/common/src/main/java/eu/midnightdust/lib/config/AutoCommand.java index 95571c8..e41f920 100644 --- a/common/src/main/java/eu/midnightdust/lib/config/AutoCommand.java +++ b/common/src/main/java/eu/midnightdust/lib/config/AutoCommand.java @@ -18,44 +18,41 @@ public class AutoCommand { final Field field; final Class type; final String modid; - final boolean isList, isNumber; + final boolean isList; public AutoCommand(Field field, String modid) { this.field = field; this.modid = modid; this.type = MidnightConfig.getUnderlyingType(field); this.isList = field.getType() == List.class; - this.isNumber = type == int.class || type == double.class || type == float.class; var command = CommandManager.literal(field.getName()).executes(this::getValue); if (type.isEnum()) { for (Object enumValue : field.getType().getEnumConstants()) - command = command.then(CommandManager.literal(enumValue.toString()).executes(ctx -> this.setValue(ctx.getSource(), enumValue, ""))); + command = command.then(CommandManager.literal(enumValue.toString()) + .executes(ctx -> this.setValue(ctx.getSource(), enumValue, ""))); } else if (isList) { - for (String action : List.of("add", "remove")) - command = command.then(CommandManager.literal(action).then( - CommandManager.argument(VALUE, getArgType()).executes(ctx -> setValueFromArg(ctx, action)))); + for (String action : new String[]{"add", "remove"}) + command = command.then(CommandManager.literal(action) + .then(CommandManager.argument(VALUE, getArgType()).executes(ctx -> setValueFromArg(ctx, action)))); } else command = command.then(CommandManager.argument(VALUE, getArgType()).executes(ctx -> setValueFromArg(ctx, ""))); PlatformFunctions.registerCommand(CommandManager.literal("midnightconfig").requires(source -> source.hasPermissionLevel(2)).then(CommandManager.literal(modid).then(command))); } public ArgumentType getArgType() { - if (isNumber) { - Entry entry = field.getAnnotation(Entry.class); - if (type == int.class) return IntegerArgumentType.integer((int) entry.min(), (int) entry.max()); - else if (type == double.class) return DoubleArgumentType.doubleArg(entry.min(), entry.max()); - else if (type == float.class) return FloatArgumentType.floatArg((float) entry.min(), (float) entry.max()); - } + Entry entry = field.getAnnotation(Entry.class); + if (type == int.class) return IntegerArgumentType.integer((int) entry.min(), (int) entry.max()); + else if (type == double.class) return DoubleArgumentType.doubleArg(entry.min(), entry.max()); + else if (type == float.class) return FloatArgumentType.floatArg((float) entry.min(), (float) entry.max()); else if (type == boolean.class) return BoolArgumentType.bool(); return StringArgumentType.string(); } + public int setValueFromArg(CommandContext context, String action) { - if (isNumber) { - if (type == int.class) return setValue(context.getSource(), IntegerArgumentType.getInteger(context, VALUE), action); - else if (type == double.class) return setValue(context.getSource(), DoubleArgumentType.getDouble(context, VALUE), action); - else if (type == float.class) return setValue(context.getSource(), FloatArgumentType.getFloat(context, VALUE), action); - } + if (type == int.class) return setValue(context.getSource(), IntegerArgumentType.getInteger(context, VALUE), action); + else if (type == double.class) return setValue(context.getSource(), DoubleArgumentType.getDouble(context, VALUE), action); + else if (type == float.class) return setValue(context.getSource(), FloatArgumentType.getFloat(context, VALUE), action); else if (type == boolean.class) return setValue(context.getSource(), BoolArgumentType.getBool(context, VALUE), action); return setValue(context.getSource(), StringArgumentType.getString(context, VALUE), action); } @@ -72,17 +69,16 @@ public class AutoCommand { MidnightConfig.write(modid); } catch (Exception e) { - if (!isList) source.sendError(Text.literal("Could not set "+field.getName()+" to value "+value+": " + e)); - else source.sendError(Text.literal((add ? "Could not add "+value+" to " : "Could not remove "+value+" from ")+field.getName() +": " + e)); + source.sendError(Text.literal(isList ? "Could not %s %s %s %s: %s".formatted(add ? "add" : "remove", value, add ? "to" : "from", field.getName(), e) : "Could not set %s to value %s: %s".formatted(field.getName(), value, e))); return 0; } - if (!isList) source.sendFeedback(() -> Text.literal("Successfully set " + field.getName()+" to "+value), true); - else source.sendFeedback(() -> Text.literal((add ? "Successfully added " +value+" to " : "Successfully removed " +value+" from ") +field.getName()), true); + source.sendFeedback(() -> Text.literal(isList ? "Successfully %s %s %s %s".formatted(add ? "added" : "removed", value, add ? "to" : "from", field.getName()) : + "Successfully set %s to %s".formatted(field.getName(), value)), true); return 1; } private int getValue(CommandContext context) { context.getSource().sendFeedback(() -> { - try { return Text.literal("The value of "+field.getName()+" is "+field.get(null)); + try { return Text.literal("The value of %s is %s".formatted(field.getName(), field.get(null))); } catch (IllegalAccessException e) {throw new RuntimeException(e);} }, true); return 0;