-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGuiCommand.java
More file actions
213 lines (191 loc) · 10.5 KB
/
Copy pathGuiCommand.java
File metadata and controls
213 lines (191 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package dev.toolkitmc.guiapi.command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;
import dev.toolkitmc.guiapi.gui.BarrelGuiHandler;
import dev.toolkitmc.guiapi.gui.GuiDefinition;
import dev.toolkitmc.guiapi.gui.GuiVarStore;
import dev.toolkitmc.guiapi.loader.GuiRegistry;
import net.minecraft.command.argument.EntityArgumentType;
import net.minecraft.command.argument.IdentifierArgumentType;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import java.util.Collection;
import java.util.List;
import java.util.Map;
/**
* /guiapi open <namespace:id> [<targets>]
* /guiapi list
* /guiapi reload
* /guiapi help
*
* Permission level 2 required.
*/
public class GuiCommand {
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
dispatcher.register(
CommandManager.literal("guiapi")
.requires(src -> src.hasPermissionLevel(
dev.toolkitmc.guiapi.config.GuiApiConfig.INSTANCE.getPermissionLevel()))
.executes(GuiCommand::showHelp)
.then(CommandManager.literal("open")
.then(CommandManager.argument("id", IdentifierArgumentType.identifier())
.suggests((ctx, builder) -> {
String input = builder.getRemainingLowerCase();
GuiRegistry.INSTANCE.getAll().keySet().stream()
.map(Identifier::toString)
.filter(s -> s.contains(input))
.forEach(builder::suggest);
return builder.buildFuture();
})
.executes(ctx -> {
ServerPlayerEntity player = ctx.getSource().getPlayer();
if (player == null) {
ctx.getSource().sendError(
Text.literal("[GuiAPI] Must be a player, or specify <targets>."));
return 0;
}
return openGui(ctx, List.of(player));
})
.then(CommandManager.argument("targets", EntityArgumentType.players())
.executes(ctx -> openGui(ctx,
EntityArgumentType.getPlayers(ctx, "targets"))))
)
)
.then(CommandManager.literal("list")
.executes(GuiCommand::listGuis))
.then(CommandManager.literal("reload")
.executes(GuiCommand::reloadGuis))
.then(CommandManager.literal("help")
.executes(GuiCommand::showHelp))
.then(CommandManager.literal("var")
.then(CommandManager.literal("get")
.then(CommandManager.argument("target", EntityArgumentType.player())
.then(CommandManager.argument("key", StringArgumentType.word())
.executes(GuiCommand::varGet))))
.then(CommandManager.literal("set")
.then(CommandManager.argument("target", EntityArgumentType.player())
.then(CommandManager.argument("key", StringArgumentType.word())
.then(CommandManager.argument("value", StringArgumentType.greedyString())
.executes(GuiCommand::varSet)))))
.then(CommandManager.literal("clear")
.then(CommandManager.argument("target", EntityArgumentType.player())
.executes(GuiCommand::varClear)))
)
);
}
// ── Subcommand handlers ──────────────────────────────────────────────────
private static int openGui(CommandContext<ServerCommandSource> ctx,
Collection<ServerPlayerEntity> targets) {
Identifier id = IdentifierArgumentType.getIdentifier(ctx, "id");
GuiDefinition def = GuiRegistry.INSTANCE.get(id).orElse(null);
if (def == null) {
ctx.getSource().sendError(Text.literal("[GuiAPI] GUI not found: " + id));
return 0;
}
for (ServerPlayerEntity player : targets) {
BarrelGuiHandler.open(player, def);
}
ctx.getSource().sendFeedback(
() -> Text.literal("[GuiAPI] Opened '" + id + "' for " + targets.size() + " player(s)."),
false);
return targets.size();
}
private static int listGuis(CommandContext<ServerCommandSource> ctx) {
var all = GuiRegistry.INSTANCE.getAll();
if (all.isEmpty()) {
ctx.getSource().sendFeedback(() -> Text.literal("[GuiAPI] No GUIs loaded."), false);
return 0;
}
StringBuilder sb = new StringBuilder("[GuiAPI] Loaded GUIs (" + all.size() + "):\n");
all.forEach((id, def) ->
sb.append(" ").append(id)
.append(" [rows=").append(def.getRows())
.append(", pages=").append(def.getPageCount()).append("]\n"));
ctx.getSource().sendFeedback(() -> Text.literal(sb.toString().trim()), false);
return all.size();
}
private static int reloadGuis(CommandContext<ServerCommandSource> ctx) {
ctx.getSource().getServer()
.reloadResources(ctx.getSource().getServer().getDataPackManager().getEnabledIds())
.thenRun(() -> ctx.getSource().sendFeedback(
() -> Text.literal("[GuiAPI] Reload complete. " +
GuiRegistry.INSTANCE.getAll().size() + " GUI(s) loaded."),
true))
.exceptionally(ex -> {
ctx.getSource().sendError(
Text.literal("[GuiAPI] Reload failed: " + ex.getMessage()));
return null;
});
return 1;
}
private static int varGet(CommandContext<ServerCommandSource> ctx) throws com.mojang.brigadier.exceptions.CommandSyntaxException {
ServerPlayerEntity target = EntityArgumentType.getPlayer(ctx, "target");
String key = StringArgumentType.getString(ctx, "key");
String val = GuiVarStore.INSTANCE.get(target.getUuid(), key);
if (val == null) {
ctx.getSource().sendFeedback(
() -> Text.literal("[GuiAPI] " + target.getName().getString() + "." + key + " is not set."), false);
} else {
ctx.getSource().sendFeedback(
() -> Text.literal("[GuiAPI] " + target.getName().getString() + "." + key + " = " + val), false);
}
return val != null ? 1 : 0;
}
private static int varSet(CommandContext<ServerCommandSource> ctx) throws com.mojang.brigadier.exceptions.CommandSyntaxException {
ServerPlayerEntity target = EntityArgumentType.getPlayer(ctx, "target");
String key = StringArgumentType.getString(ctx, "key");
String value = StringArgumentType.getString(ctx, "value");
GuiVarStore.INSTANCE.set(target.getUuid(), key, value);
ctx.getSource().sendFeedback(
() -> Text.literal("[GuiAPI] Set " + target.getName().getString() + "." + key + " = " + value), false);
return 1;
}
private static int varClear(CommandContext<ServerCommandSource> ctx) throws com.mojang.brigadier.exceptions.CommandSyntaxException {
ServerPlayerEntity target = EntityArgumentType.getPlayer(ctx, "target");
Map<String, String> vars = GuiVarStore.INSTANCE.getAll(target.getUuid());
int count = vars.size();
GuiVarStore.INSTANCE.clear(target.getUuid());
ctx.getSource().sendFeedback(
() -> Text.literal("[GuiAPI] Cleared " + count + " var(s) for " + target.getName().getString() + "."), false);
return count;
}
private static int showHelp(CommandContext<ServerCommandSource> ctx) {
String help =
"[GuiAPI] Commands (permission level 2):\n" +
" /guiapi open <id> [targets] - Open a GUI for yourself or target players\n" +
" /guiapi list - List all loaded GUI definitions\n" +
" /guiapi reload - Reload all datapack resources (including GUIs)\n" +
" /guiapi var get <player> <key> - Get a runtime variable\n" +
" /guiapi var set <player> <key> <val> - Set a runtime variable\n" +
" /guiapi var clear <player> - Clear all runtime variables\n" +
" /guiapi help - Show this help message\n" +
"\n" +
"Variable actions: set_var | add_var | sub_var | reset_var | clear_vars\n" +
"Variable conditions: var_eq | var_gt | var_lt | var_set\n" +
"Variable placeholder: {var:key}\n" +
"Input placeholder: {input} (last anvil input)\n" +
"XP placeholder: {xp} (player experience level)\n" +
"\n" +
"Macro functions: define reusable action blocks in JSON with \"macros\": {}\n" +
" actions: run_function:<macro_name>\n" +
"\n" +
"Anvil input: anvil_input action saves text to a variable and {input}\n" +
" Example: {\"type\": \"anvil_input\", \"var\": \"myVar\", \"value\": \"Enter name|Default\"}\n" +
"\n" +
"Button JSON fields:\n" +
" slot, page, item, name, lore, glint\n" +
" click_type: any | left | right | shift\n" +
" condition: has_tag | not_tag | score_gt | score_lt | score_eq\n" +
" var_eq | var_gt | var_lt | var_set\n" +
" actions: run_command | close | open_gui | message | sound\n" +
" next_page | prev_page | goto_page | run_function\n" +
" set_var | add_var | sub_var | reset_var | clear_vars\n" +
" anvil_input" ;
ctx.getSource().sendFeedback(() -> Text.literal(help), false);
return 1;
}
}