Skip to content

Commit

Permalink
conjoined configure() function
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysdh540 committed May 18, 2024
1 parent 621a663 commit 071d4b2
Showing 1 changed file with 20 additions and 82 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -45,15 +46,15 @@ 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) {
if (category != null) base.then(category);
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;
}));
Expand All @@ -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<? extends Enum<?>> 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)
Expand All @@ -100,7 +85,10 @@ private boolean perms(CommandSourceStack source) {
return integrated || source.hasPermission(2);
}

private <T> void gdr(LiteralArgumentBuilder<CommandSourceStack> category, String name, ConfigValue<T> value) {
private <T> void configure(LiteralArgumentBuilder<CommandSourceStack> category, String name, ConfigValue<T> value) {
@SuppressWarnings("unchecked")
Class<T> clazz = (Class<T>) value.getDefault().getClass();

category.then(literal(name)
.executes(context -> {
message(context, name + ": " + CUServer.getComment(name));
Expand All @@ -119,78 +107,28 @@ private <T> void gdr(LiteralArgumentBuilder<CommandSourceStack> category, String
return Command.SINGLE_SUCCESS;
})
)
);
}

private void setBoolean(LiteralArgumentBuilder<CommandSourceStack> 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<CommandSourceStack> 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<CommandSourceStack> 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 <T extends Enum<T>> void setEnum(LiteralArgumentBuilder<CommandSourceStack> category, String name, EnumValue<T> value) {
@SuppressWarnings({"unchecked", "rawtypes"})
private <T> ArgumentType<T> getArgument(ConfigValue<T> value) {
if(value instanceof BooleanValue) return (ArgumentType<T>) BoolArgumentType.bool();
if(value instanceof DoubleValue) return (ArgumentType<T>) DoubleArgumentType.doubleArg();
if(value instanceof IntValue) return (ArgumentType<T>) IntegerArgumentType.integer();
Class<T> clazz = (Class<T>) 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<T>) EnumArgument.enumArg((Class<Enum>) clazz, true);
throw new IllegalArgumentException("Unsupported class for argument: " + clazz);
}
}

0 comments on commit 071d4b2

Please sign in to comment.