Skip to content

Commit

Permalink
config command changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysdh540 committed Jul 24, 2023
1 parent 6540379 commit 2067384
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -140,7 +135,9 @@ private static boolean perms(CommandSourceStack source) {
private static <T> void gdr(LiteralArgumentBuilder<CommandSourceStack> category, Field field, ForgeConfigSpec.ConfigValue<T> 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)
Expand All @@ -159,56 +156,54 @@ private static <T> void gdr(LiteralArgumentBuilder<CommandSourceStack> category,

private static void setBoolean(LiteralArgumentBuilder<CommandSourceStack> 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<CommandSourceStack> 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<CommandSourceStack> 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 <T extends Enum<T>> void setEnum(LiteralArgumentBuilder<CommandSourceStack> category, Field field, ForgeConfigSpec.EnumValue<T> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
*/
public class EnumArgument<T extends Enum<T>> implements ArgumentType<T> {
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<T> enumClass;
private final boolean lowercase;

Expand Down Expand Up @@ -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);
}
}

0 comments on commit 2067384

Please sign in to comment.