Skip to content

Commit

Permalink
a swing and a miss for abstracting config set commands, so basically …
Browse files Browse the repository at this point in the history
…just a newline
  • Loading branch information
rhysdh540 committed Jul 24, 2023
1 parent 0f44f7f commit 6540379
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 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 @@ -29,7 +30,9 @@
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 @@ -111,9 +114,9 @@ public static void registerConfigCommand() {
if (value instanceof ForgeConfigSpec.BooleanValue bValue)
setBoolean(category, field, bValue);

// set for PlacmentCheck enum
else if (value.get() instanceof CUConfig.PlacementCheck)
setEnum(category, field, (ForgeConfigSpec.EnumValue<CUConfig.PlacementCheck>) value);
// set for enums
else if (value.get() instanceof Enum<?>)
setEnum(category, field, (ForgeConfigSpec.EnumValue<? extends Enum<?>>) value);

// set for int
else if (value instanceof ForgeConfigSpec.IntValue iValue)
Expand Down Expand Up @@ -143,8 +146,8 @@ private static <T> void gdr(LiteralArgumentBuilder<CommandSourceStack> category,
.then(literal("reset").requires(CreateUnlimitedCommands::perms)
.executes(context -> {
if(value.get().equals(value.getDefault())) {
message("Value is already default!", context, ChatFormatting.RED);
return Command.SINGLE_SUCCESS;
error("Value is already default!", context);
return 0;
}
value.set(value.getDefault());
message(field.getName() + " reset to: " + value.get(), context);
Expand Down Expand Up @@ -208,8 +211,6 @@ private static <T extends Enum<T>> void setEnum(LiteralArgumentBuilder<CommandSo
}))));
}



private static MutableComponent link(String link, String display, ChatFormatting color) {
return ComponentUtils.wrapInSquareBrackets(Component.nullToEmpty(display))
.withStyle(color)
Expand All @@ -222,7 +223,7 @@ private static MutableComponent link(String link, String display, ChatFormatting
private static void message(String message, CommandContext<CommandSourceStack> context) {
context.getSource().sendSuccess(nullToEmpty(message), false);
}
private static void message(@NotNull String message, CommandContext<CommandSourceStack> context, ChatFormatting color) {
context.getSource().sendSuccess(Component.literal(message).withStyle(color), false);
private static void error(String message, CommandContext<CommandSourceStack> context) {
context.getSource().sendFailure(nullToEmpty(message));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class EnumArgument<T extends Enum<T>> implements ArgumentType<T> {
public static <R extends Enum<R>> EnumArgument<R> enumArg(Class<R> enumClass, boolean lowercase) {
return new EnumArgument<>(enumClass, lowercase);
}

private EnumArgument(final Class<T> enumClass, final boolean lowercase) {
this.enumClass = enumClass;
this.lowercase = lowercase;
Expand Down

0 comments on commit 6540379

Please sign in to comment.