-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGuiCommand.java
More file actions
244 lines (222 loc) · 12.8 KB
/
Copy pathGuiCommand.java
File metadata and controls
244 lines (222 loc) · 12.8 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
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.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.EntityArgument;
import net.minecraft.commands.arguments.IdentifierArgument;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.Identifier;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.permissions.Permission;
import net.minecraft.server.permissions.PermissionLevel;
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 (GAMEMASTERS) required.
*/
public class GuiCommand {
public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
dispatcher.register(
Commands.literal("guiapi")
.requires(src -> src.permissions().hasPermission(new Permission.HasCommandLevel(PermissionLevel.byId(
dev.toolkitmc.guiapi.config.GuiApiConfig.INSTANCE.getPermissionLevel()))))
.executes(GuiCommand::showHelp)
.then(Commands.literal("open")
.then(Commands.argument("id", IdentifierArgument.id())
.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 -> {
ServerPlayer player = ctx.getSource().getPlayer();
if (player == null) {
ctx.getSource().sendFailure(
Component.literal("[GuiAPI] Must be a player, or specify <targets>."));
return 0;
}
return openGui(ctx, List.of(player));
})
.then(Commands.argument("targets", EntityArgument.players())
.executes(ctx -> openGui(ctx,
EntityArgument.getPlayers(ctx, "targets"))))
)
)
.then(Commands.literal("list")
.executes(GuiCommand::listGuis))
.then(Commands.literal("reload")
.executes(GuiCommand::reloadGuis))
.then(Commands.literal("help")
.executes(GuiCommand::showHelp))
.then(Commands.literal("var")
.then(Commands.literal("get")
.then(Commands.argument("target", EntityArgument.player())
.then(Commands.argument("key", StringArgumentType.word())
.executes(GuiCommand::varGet))))
.then(Commands.literal("set")
.then(Commands.argument("target", EntityArgument.player())
.then(Commands.argument("key", StringArgumentType.word())
.then(Commands.argument("value", StringArgumentType.greedyString())
.executes(GuiCommand::varSet)))))
.then(Commands.literal("clear")
.then(Commands.argument("target", EntityArgument.player())
.executes(GuiCommand::varClear)))
)
);
}
// ── Subcommand handlers ──────────────────────────────────────────────────
private static int openGui(CommandContext<CommandSourceStack> ctx,
Collection<ServerPlayer> targets) {
Identifier id = IdentifierArgument.getId(ctx, "id");
GuiDefinition def = GuiRegistry.INSTANCE.get(id).orElse(null);
if (def == null) {
ctx.getSource().sendFailure(Component.literal("[GuiAPI] GUI not found: " + id));
return 0;
}
for (ServerPlayer player : targets) {
BarrelGuiHandler.open(player, def);
}
ctx.getSource().sendSuccess(
() -> Component.literal("[GuiAPI] Opened '" + id + "' for " + targets.size() + " player(s)."),
false);
return targets.size();
}
private static int listGuis(CommandContext<CommandSourceStack> ctx) {
var all = GuiRegistry.INSTANCE.getAll();
if (all.isEmpty()) {
ctx.getSource().sendSuccess(() -> Component.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().sendSuccess(() -> Component.literal(sb.toString().trim()), false);
return all.size();
}
private static int reloadGuis(CommandContext<CommandSourceStack> ctx) {
ctx.getSource().getServer()
.reloadResources(ctx.getSource().getServer().getPackRepository().getSelectedIds())
.thenRun(() -> ctx.getSource().sendSuccess(
() -> Component.literal("[GuiAPI] Reload complete. " +
GuiRegistry.INSTANCE.getAll().size() + " GUI(s) loaded."),
true))
.exceptionally(ex -> {
ctx.getSource().sendFailure(
Component.literal("[GuiAPI] Reload failed: " + ex.getMessage()));
return null;
});
return 1;
}
private static int varGet(CommandContext<CommandSourceStack> ctx) throws com.mojang.brigadier.exceptions.CommandSyntaxException {
ServerPlayer target = EntityArgument.getPlayer(ctx, "target");
String key = StringArgumentType.getString(ctx, "key");
String val = GuiVarStore.INSTANCE.get(target.getUUID(), key);
if (val == null) {
ctx.getSource().sendSuccess(
() -> Component.literal("[GuiAPI] " + target.getName().getString() + "." + key + " is not set."), false);
} else {
ctx.getSource().sendSuccess(
() -> Component.literal("[GuiAPI] " + target.getName().getString() + "." + key + " = " + val), false);
}
return val != null ? 1 : 0;
}
private static int varSet(CommandContext<CommandSourceStack> ctx) throws com.mojang.brigadier.exceptions.CommandSyntaxException {
ServerPlayer target = EntityArgument.getPlayer(ctx, "target");
String key = StringArgumentType.getString(ctx, "key");
String value = StringArgumentType.getString(ctx, "value");
GuiVarStore.INSTANCE.set(target.getUUID(), key, value);
ctx.getSource().sendSuccess(
() -> Component.literal("[GuiAPI] Set " + target.getName().getString() + "." + key + " = " + value), false);
return 1;
}
private static int varClear(CommandContext<CommandSourceStack> ctx) throws com.mojang.brigadier.exceptions.CommandSyntaxException {
ServerPlayer target = EntityArgument.getPlayer(ctx, "target");
Map<String, String> vars = GuiVarStore.INSTANCE.getAll(target.getUUID());
int count = vars.size();
GuiVarStore.INSTANCE.clear(target.getUUID());
ctx.getSource().sendSuccess(
() -> Component.literal("[GuiAPI] Cleared " + count + " var(s) for " + target.getName().getString() + "."), false);
return count;
}
private static int showHelp(CommandContext<CommandSourceStack> 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" +
" random: run_random_function:<name>[*weight](,<name>[*weight]...)\n" +
" e.g. run_random_function:common*70,rare*25,legendary*5\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" +
"Item/XP actions:\n" +
" give_item:<itemId>:<amount> - give item(s), overflow drops at feet\n" +
" take_item:<itemId>:<amount> - remove item(s) from inventory\n" +
" add_xp:<n> - add n XP points\n" +
" add_xp:L<n> - add n XP levels (prefix with L)\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" +
" has_item | not_item | level_gt | level_lt\n" +
" health_gt | health_lt | food_gt | food_lt\n" +
" permission:<0-4> (checks player's command permission level)\n" +
" actions: run_command | close | open_gui | message | sound | action_bar\n" +
" next_page | prev_page | goto_page | run_function\n" +
" run_random_function | give_item | take_item | add_xp\n" +
" set_var | add_var | sub_var | reset_var | clear_vars\n" +
" set_score | add_score | sub_score\n" +
" add_effect | remove_effect | clear_effects\n" +
" anvil_input\n" +
"\n" +
"Conditional item display: add \"else_item\" (same fields as a button)\n" +
" alongside \"condition\" to show an alternate item instead of hiding\n" +
" the button when the condition is false. Button stays inert while\n" +
" showing else_item (clicks are ignored, not the normal actions).\n" +
"\n" +
"Non-button widgets (top-level GUI JSON fields):\n" +
" progress_bars: [{ start_slot, length, page, value_source,\n" +
" max_value, filled_item, empty_item, name, lore }]\n" +
" value_source: \"score:<objective>\" or \"var:<key>\"\n" +
" Fills [start_slot, start_slot+length) proportionally, recalculated\n" +
" on every open/refresh (e.g. via tick_rate). Read-only, not clickable.\n" +
" displays: [{ slot, page, item, name, lore, glint, amount, condition }]\n" +
" A single read-only info item. Supports the same condition types as\n" +
" buttons. Clicks on a display slot are always ignored.";
ctx.getSource().sendSuccess(() -> Component.literal(help), false);
return 1;
}
}