mirror of
https://github.com/TeamMidnightDust/MidnightLib.git
synced 2025-12-16 17:25:09 +01:00
feat: optimize AutoCommand's file-size
This commit is contained in:
@@ -18,44 +18,41 @@ public class AutoCommand {
|
|||||||
final Field field;
|
final Field field;
|
||||||
final Class<?> type;
|
final Class<?> type;
|
||||||
final String modid;
|
final String modid;
|
||||||
final boolean isList, isNumber;
|
final boolean isList;
|
||||||
|
|
||||||
public AutoCommand(Field field, String modid) {
|
public AutoCommand(Field field, String modid) {
|
||||||
this.field = field; this.modid = modid;
|
this.field = field; this.modid = modid;
|
||||||
this.type = MidnightConfig.getUnderlyingType(field);
|
this.type = MidnightConfig.getUnderlyingType(field);
|
||||||
this.isList = field.getType() == List.class;
|
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);
|
var command = CommandManager.literal(field.getName()).executes(this::getValue);
|
||||||
|
|
||||||
if (type.isEnum()) {
|
if (type.isEnum()) {
|
||||||
for (Object enumValue : field.getType().getEnumConstants())
|
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) {
|
} else if (isList) {
|
||||||
for (String action : List.of("add", "remove"))
|
for (String action : new String[]{"add", "remove"})
|
||||||
command = command.then(CommandManager.literal(action).then(
|
command = command.then(CommandManager.literal(action)
|
||||||
CommandManager.argument(VALUE, getArgType()).executes(ctx -> setValueFromArg(ctx, action))));
|
.then(CommandManager.argument(VALUE, getArgType()).executes(ctx -> setValueFromArg(ctx, action))));
|
||||||
} else command = command.then(CommandManager.argument(VALUE, getArgType()).executes(ctx -> setValueFromArg(ctx, "")));
|
} 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)));
|
PlatformFunctions.registerCommand(CommandManager.literal("midnightconfig").requires(source -> source.hasPermissionLevel(2)).then(CommandManager.literal(modid).then(command)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArgumentType<?> getArgType() {
|
public ArgumentType<?> getArgType() {
|
||||||
if (isNumber) {
|
|
||||||
Entry entry = field.getAnnotation(Entry.class);
|
Entry entry = field.getAnnotation(Entry.class);
|
||||||
if (type == int.class) return IntegerArgumentType.integer((int) entry.min(), (int) entry.max());
|
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 == 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 == float.class) return FloatArgumentType.floatArg((float) entry.min(), (float) entry.max());
|
||||||
}
|
|
||||||
else if (type == boolean.class) return BoolArgumentType.bool();
|
else if (type == boolean.class) return BoolArgumentType.bool();
|
||||||
return StringArgumentType.string();
|
return StringArgumentType.string();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int setValueFromArg(CommandContext<ServerCommandSource> context, String action) {
|
public int setValueFromArg(CommandContext<ServerCommandSource> context, String action) {
|
||||||
if (isNumber) {
|
|
||||||
if (type == int.class) return setValue(context.getSource(), IntegerArgumentType.getInteger(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 == 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 == 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);
|
else if (type == boolean.class) return setValue(context.getSource(), BoolArgumentType.getBool(context, VALUE), action);
|
||||||
return setValue(context.getSource(), StringArgumentType.getString(context, VALUE), action);
|
return setValue(context.getSource(), StringArgumentType.getString(context, VALUE), action);
|
||||||
}
|
}
|
||||||
@@ -72,17 +69,16 @@ public class AutoCommand {
|
|||||||
MidnightConfig.write(modid);
|
MidnightConfig.write(modid);
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
if (!isList) source.sendError(Text.literal("Could not set "+field.getName()+" to value "+value+": " + 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)));
|
||||||
else source.sendError(Text.literal((add ? "Could not add "+value+" to " : "Could not remove "+value+" from ")+field.getName() +": " + e));
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if (!isList) source.sendFeedback(() -> Text.literal("Successfully set " + field.getName()+" to "+value), true);
|
source.sendFeedback(() -> Text.literal(isList ? "Successfully %s %s %s %s".formatted(add ? "added" : "removed", value, add ? "to" : "from", field.getName()) :
|
||||||
else source.sendFeedback(() -> Text.literal((add ? "Successfully added " +value+" to " : "Successfully removed " +value+" from ") +field.getName()), true);
|
"Successfully set %s to %s".formatted(field.getName(), value)), true);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
private int getValue(CommandContext<ServerCommandSource> context) {
|
private int getValue(CommandContext<ServerCommandSource> context) {
|
||||||
context.getSource().sendFeedback(() -> {
|
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);}
|
} catch (IllegalAccessException e) {throw new RuntimeException(e);}
|
||||||
}, true);
|
}, true);
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user