-
-
Notifications
You must be signed in to change notification settings - Fork 169
Config Commands #2051
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Config Commands #2051
Changes from 7 commits
d930d84
2aef8c9
c5d8a31
73f92fe
6ed84e5
1e36379
0ceab99
e5a9a93
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| package de.hysky.skyblocker.config; | ||
|
|
||
| import com.mojang.brigadier.Command; | ||
| import com.mojang.brigadier.builder.LiteralArgumentBuilder; | ||
| import de.hysky.skyblocker.utils.Constants; | ||
| import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; | ||
| import net.minecraft.ChatFormatting; | ||
| import net.minecraft.network.chat.Component; | ||
|
|
||
| import java.lang.reflect.Field; | ||
| import java.lang.reflect.Modifier; | ||
|
|
||
| import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal; | ||
|
|
||
| public class ConfigCommands { | ||
| static void registerConfigEntries(LiteralArgumentBuilder<FabricClientCommandSource> builder) { | ||
| try { | ||
| registerConfigEntries(builder, SkyblockerConfigManager.get()); | ||
| } catch (Exception e) { | ||
| SkyblockerConfigManager.LOGGER.error("[Skyblocker Config Manager] Failed to register config entries command!", e); | ||
| } | ||
| } | ||
|
|
||
| private static LiteralArgumentBuilder<FabricClientCommandSource> registerConfigEntries(LiteralArgumentBuilder<FabricClientCommandSource> builder, Object object) throws IllegalAccessException { | ||
| for (Field field : object.getClass().getDeclaredFields()) { | ||
| if (Modifier.isStatic(field.getModifiers())) continue; | ||
| field.setAccessible(true); | ||
|
|
||
| Class<?> type = field.getType(); | ||
| String name = field.getName(); | ||
| Object value = field.get(object); | ||
|
|
||
| if (type == boolean.class) { | ||
| builder.then(registerBooleanConfigEntry(field, object, name)); | ||
| } else if (value != null && SkyblockerConfigManager.isConfigClass(type)) { | ||
| builder.then(registerConfigEntries(literal(name), value)); | ||
| } | ||
| } | ||
|
|
||
| return builder; | ||
| } | ||
|
|
||
| private static LiteralArgumentBuilder<FabricClientCommandSource> registerBooleanConfigEntry(Field field, Object object, String name) { | ||
| return literal(name).then(literal("true").executes(context -> { | ||
| SkyblockerConfigManager.update(config -> { | ||
| try { | ||
| field.setBoolean(object, true); | ||
| context.getSource().sendFeedback(Constants.PREFIX.get().append(Component.translatable("skyblocker.config.commands.set", name, true).withStyle(ChatFormatting.GREEN))); | ||
| } catch (IllegalAccessException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| }); | ||
| return Command.SINGLE_SUCCESS; | ||
| })).then(literal("false").executes(context -> { | ||
| SkyblockerConfigManager.update(config -> { | ||
| try { | ||
| field.setBoolean(object, false); | ||
| context.getSource().sendFeedback(Constants.PREFIX.get().append(Component.translatable("skyblocker.config.commands.set", name, false).withStyle(ChatFormatting.GREEN))); | ||
| } catch (IllegalAccessException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| }); | ||
| return Command.SINGLE_SUCCESS; | ||
| })).then(literal("toggle").executes(context -> { | ||
| SkyblockerConfigManager.update(config -> { | ||
| try { | ||
| boolean toggled = !field.getBoolean(object); | ||
| field.setBoolean(object, toggled); | ||
| context.getSource().sendFeedback(Constants.PREFIX.get().append(Component.translatable("skyblocker.config.commands.set", name, toggled).withStyle(ChatFormatting.GREEN))); | ||
| } catch (IllegalAccessException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| }); | ||
| return Command.SINGLE_SUCCESS; | ||
| })).executes(context -> { | ||
| try { | ||
| context.getSource().sendFeedback(Constants.PREFIX.get().append(Component.translatable("skyblocker.config.commands.query", name, field.getBoolean(object)).withStyle(ChatFormatting.GREEN))); | ||
| } catch (IllegalAccessException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| return Command.SINGLE_SUCCESS; | ||
| }); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,8 @@ | ||
| package de.hysky.skyblocker.config; | ||
|
|
||
| import com.mojang.brigadier.arguments.StringArgumentType; | ||
| import com.google.gson.JsonObject; | ||
| import com.google.gson.JsonParser; | ||
| import com.mojang.brigadier.arguments.StringArgumentType; | ||
| import com.mojang.brigadier.builder.LiteralArgumentBuilder; | ||
| import com.mojang.logging.LogUtils; | ||
| import de.hysky.skyblocker.SkyblockerMod; | ||
|
|
@@ -42,22 +42,25 @@ | |
| import net.minecraft.network.chat.Component; | ||
| import org.apache.commons.lang3.function.Consumers; | ||
| import org.jspecify.annotations.Nullable; | ||
| import org.slf4j.Logger; | ||
|
|
||
| import java.io.BufferedReader; | ||
| import java.io.BufferedWriter; | ||
| import java.lang.StackWalker.Option; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.Collection; | ||
| import java.util.Map; | ||
| import java.util.function.Consumer; | ||
| import java.util.function.UnaryOperator; | ||
|
|
||
| import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.argument; | ||
| import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal; | ||
| import org.slf4j.Logger; | ||
|
|
||
| public class SkyblockerConfigManager { | ||
| public static final int CONFIG_VERSION = 6; | ||
| private static final Logger LOGGER = LogUtils.getLogger(); | ||
| static final Logger LOGGER = LogUtils.getLogger(); | ||
| private static final String CONFIGS_PACKAGE = "de.hysky.skyblocker.config.configs"; | ||
| private static final Path CONFIG_DIR = FabricLoader.getInstance().getConfigDir(); | ||
| private static final Path CONFIG_FILE = CONFIG_DIR.resolve("skyblocker.json"); | ||
| private static final ConfigManager<SkyblockerConfig> CONFIG_MANAGER = ConfigManager.create(SkyblockerConfig.class, CONFIG_FILE, UnaryOperator.identity()); | ||
|
|
@@ -103,7 +106,7 @@ public static void update(Consumer<SkyblockerConfig> action) { | |
| CONFIG_MANAGER.save(); | ||
| } | ||
|
|
||
| public static Screen createGUI(Screen parent) { | ||
| public static Screen createGUI(@Nullable Screen parent) { | ||
| return createGUI(parent, ""); | ||
| } | ||
|
|
||
|
|
@@ -150,8 +153,27 @@ public static void reload() { | |
| * @return the command builder | ||
| */ | ||
| private static LiteralArgumentBuilder<FabricClientCommandSource> configLiteral(String name) { | ||
| return literal(name).executes(Scheduler.queueOpenScreenCommand(() -> createGUI(null))) | ||
| .then(argument("option", StringArgumentType.greedyString()).executes((ctx) -> Scheduler.queueOpenScreen(createGUI(null, ctx.getArgument("option", String.class))))); | ||
| LiteralArgumentBuilder<FabricClientCommandSource> builder = literal(name).executes(Scheduler.queueOpenScreenCommand(() -> createGUI(null))) | ||
| .then(literal("search").then(argument("option", StringArgumentType.greedyString()).executes((ctx) -> Scheduler.queueOpenScreen(createGUI(null, ctx.getArgument("option", String.class)))))); | ||
|
||
| ConfigCommands.registerConfigEntries(builder); | ||
| return builder; | ||
| } | ||
|
|
||
| /** | ||
| * Returns {@code true} if the given class represents one of our config | ||
| * classes. This prevents {@link de.hysky.skyblocker.DisableAll#disableBooleans(Object)} from touching | ||
| * unrelated objects from other mods. | ||
| */ | ||
| @SuppressWarnings("JavadocReference") | ||
| public static boolean isConfigClass(Class<?> clazz) { | ||
| return !clazz.isPrimitive() | ||
| && !clazz.isEnum() | ||
| && !clazz.isRecord() | ||
| && !clazz.equals(String.class) | ||
| && !Number.class.isAssignableFrom(clazz) | ||
| && !Map.class.isAssignableFrom(clazz) | ||
| && !Collection.class.isAssignableFrom(clazz) | ||
| && clazz.getPackageName().startsWith(CONFIGS_PACKAGE); | ||
| } | ||
|
|
||
| public static void dataFix(Path configDir, Path backupDir) { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.