Skip to content

Commit f0b6890

Browse files
committed
config command
1 parent d58e049 commit f0b6890

4 files changed

Lines changed: 91 additions & 1 deletion

File tree

src/main/java/dev/dfonline/codeclient/CodeClient.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ public static void onTick() {
124124
RecentChestInsert.tick();
125125
KeyBinds.tick();
126126
SlotGhostManager.tick();
127+
Commands.tick();
127128

128129
if(!(location instanceof Dev) || !(MC.currentScreen instanceof HandledScreen<?>)) {
129130
InsertOverlay.reset();
@@ -180,6 +181,7 @@ public static void clean() {
180181
CodeClient.location = null;
181182
BuildPhaser.disableClipping();
182183
Commands.confirm = null;
184+
Commands.screen = null;
183185
Debug.clean();
184186
SlotGhostManager.reset();
185187
InsertOverlay.reset();

src/main/java/dev/dfonline/codeclient/Commands.java

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import dev.dfonline.codeclient.action.Action;
99
import dev.dfonline.codeclient.action.None;
1010
import dev.dfonline.codeclient.action.impl.*;
11+
import dev.dfonline.codeclient.config.Config;
1112
import dev.dfonline.codeclient.dev.Debug.Debug;
1213
import dev.dfonline.codeclient.hypercube.template.Template;
1314
import dev.dfonline.codeclient.location.Dev;
@@ -16,6 +17,7 @@
1617
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
1718
import net.minecraft.block.entity.SignText;
1819
import net.minecraft.client.font.TextRenderer;
20+
import net.minecraft.client.gui.screen.Screen;
1921
import net.minecraft.item.ItemStack;
2022
import net.minecraft.text.ClickEvent;
2123
import net.minecraft.text.HoverEvent;
@@ -34,19 +36,97 @@
3436
import java.util.regex.Pattern;
3537

3638
import static com.mojang.brigadier.arguments.StringArgumentType.greedyString;
39+
import static com.mojang.brigadier.arguments.StringArgumentType.string;
3740
import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.argument;
3841
import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal;
3942

4043
public class Commands {
4144
private static final Text DONE = Text.translatable("gui.done");
4245
public static Action confirm = null;
46+
public static Screen screen = null;
4347

4448
private static void actionCallback() {
4549
CodeClient.currentAction = new None();
4650
Utility.sendMessage(DONE, ChatType.SUCCESS);
4751
}
4852

53+
public static void tick() {
54+
if(screen != null) {
55+
CodeClient.MC.setScreen(screen);
56+
screen = null;
57+
}
58+
}
59+
4960
public static void register(CommandDispatcher<FabricClientCommandSource> dispatcher) {
61+
dispatcher.register(literal("ccconfig").executes(context -> {
62+
screen = Config.getConfig().getLibConfig().generateScreen(null);
63+
return 0;
64+
}).then(argument("option", string()).suggests((context, builder) -> {
65+
var option = builder.getRemaining();
66+
for (var field : Config.class.getFields()) {
67+
if(field.getName().toLowerCase().startsWith(option.toLowerCase())) builder.suggest(field.getName());
68+
}
69+
return builder.buildFuture();
70+
}).executes(context -> {
71+
var option = context.getArgument("option",String.class);
72+
try {
73+
Utility.sendMessage(
74+
Text.translatable("codeclient.config.command.query",
75+
Text.literal(option).formatted(Formatting.AQUA),
76+
Text.literal(String.valueOf(Config.class.getField(option).get(Config.getConfig()))).formatted(Formatting.AQUA)));
77+
} catch (Exception e) {
78+
Utility.sendMessage(Text.translatable("codeclient.config.command.query.fail",Text.literal(option).formatted(Formatting.YELLOW)),ChatType.FAIL);
79+
}
80+
return 0;
81+
}).then(argument("value",greedyString()).suggests((context, builder) -> {
82+
var option = context.getArgument("option",String.class);
83+
try {
84+
var value = builder.getRemaining();
85+
var field = Config.class.getField(option);
86+
List<String> options;
87+
if(field.getType().equals(boolean.class)) {
88+
options = List.of("true","false");
89+
}
90+
else if(field.getType().isEnum()) {
91+
options = new ArrayList<>();
92+
for(Object member : field.getType().getEnumConstants()) {
93+
options.add(((Enum<?>) member).name());
94+
}
95+
}
96+
else options = List.of();
97+
for (var possibility : options) {
98+
if(possibility.toLowerCase().startsWith(value.toLowerCase())) builder.suggest(possibility);
99+
}
100+
} catch (Exception ignored) {}
101+
return builder.buildFuture();
102+
}).executes(context -> {
103+
var option = context.getArgument("option",String.class);
104+
try {
105+
var value = context.getArgument("value",String.class);
106+
var field = Config.class.getField(option);
107+
if(field.getType().equals(boolean.class)) {
108+
var bool = Boolean.valueOf(value);
109+
field.set(Config.getConfig(),bool);
110+
Utility.sendMessage(Text.translatable(bool ? "codeclient.config.command.enable" : "codeclient.config.command.disable",Text.literal(option).formatted(Formatting.AQUA)),ChatType.SUCCESS);
111+
Config.getConfig().save();
112+
}
113+
else if(field.getType().isEnum()) {
114+
for(Object member : field.getType().getEnumConstants()) {
115+
if(((Enum<?>) member).name().equalsIgnoreCase(value)) {
116+
field.set(Config.getConfig(),member);
117+
Utility.sendMessage(Text.translatable("codeclient.config.command.enum",Text.literal(option).formatted(Formatting.AQUA),Text.literal(value).formatted(Formatting.AQUA)),ChatType.SUCCESS);
118+
Config.getConfig().save();
119+
return 0;
120+
}
121+
}
122+
Utility.sendMessage(Text.translatable("codeclient.config.command.enum.fail",Text.literal(option).formatted(Formatting.YELLOW),Text.literal(value).formatted(Formatting.YELLOW)),ChatType.FAIL);
123+
return -1;
124+
}
125+
Utility.sendMessage(Text.translatable("codeclient.config.command.fail"),ChatType.FAIL);
126+
} catch (Exception ignored) {}
127+
return 0;
128+
}))));
129+
50130
dispatcher.register(literal("auth").executes(context -> {
51131
SocketHandler.setAuthorised(true);
52132
Utility.sendMessage(Text.translatable("codeclient.api.authorised")

src/main/java/dev/dfonline/codeclient/config/Config.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public static void clear() {
9090
instance = null;
9191
}
9292

93-
private void save() {
93+
public void save() {
9494
try {
9595
JsonObject object = new JsonObject();
9696
object.addProperty("NoClipEnabled", NoClipEnabled);

src/main/resources/assets/codeclient/lang/en_us.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@
1717
"codeclient.config.insert_overlay": "Click to insert value",
1818
"codeclient.config.insert_overlay.description": "Click on an empty slot in a code chest to insert a value.",
1919

20+
"codeclient.config.command.query": "Option %s is set to %s.",
21+
"codeclient.config.command.query.fail": "There is no option with name %s.",
22+
"codeclient.config.command.enable": "Enabled option %s.",
23+
"codeclient.config.command.disable": "Disabled option %s.",
24+
"codeclient.config.command.enum": "Set option %s to %s.",
25+
"codeclient.config.command.enum.fail": "Couldn't set option %s to %s.",
26+
"codeclient.config.command.fail": "You cannot set this config option.",
27+
2028
"codeclient.parse_db": "Could not parse ActionDump. Go to %s to find out how to fix this.",
2129

2230
"codeclient.phaser.plot_origin": "CodeClient doesn't know the plot origin.",

0 commit comments

Comments
 (0)