|
1 | 1 | package net.gunivers.sniffer.command; |
2 | 2 |
|
3 | 3 | import com.google.common.collect.Queues; |
4 | | -import com.mojang.brigadier.arguments.*; |
| 4 | +import com.mojang.brigadier.arguments.IntegerArgumentType; |
| 5 | +import com.mojang.brigadier.arguments.StringArgumentType; |
5 | 6 | import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; |
| 7 | +import net.gunivers.sniffer.DatapackDebugger; |
| 8 | +import net.gunivers.sniffer.dap.DebuggerState; |
| 9 | +import net.gunivers.sniffer.dap.ScopeManager; |
| 10 | +import net.gunivers.sniffer.util.ReflectUtil; |
6 | 11 | import net.minecraft.command.CommandExecutionContext; |
7 | | - |
8 | | -import static net.gunivers.sniffer.Utils.addSnifferPrefix; |
9 | | -import static net.minecraft.server.command.CommandManager.literal; |
10 | | -import static net.minecraft.server.command.CommandManager.argument; |
11 | | - |
12 | 12 | import net.minecraft.nbt.NbtElement; |
13 | 13 | import net.minecraft.nbt.NbtHelper; |
14 | 14 | import net.minecraft.server.command.ServerCommandSource; |
15 | 15 | import net.minecraft.text.MutableText; |
16 | 16 | import net.minecraft.text.Text; |
| 17 | +import net.minecraft.text.TextColor; |
17 | 18 | import net.minecraft.util.Formatting; |
18 | 19 | import net.minecraft.util.Pair; |
19 | 20 | import org.jetbrains.annotations.NotNull; |
20 | 21 | import org.jetbrains.annotations.Nullable; |
21 | | -import net.gunivers.sniffer.DatapackDebugger; |
22 | | -import net.gunivers.sniffer.dap.DebuggerState; |
23 | | -import net.gunivers.sniffer.dap.ScopeManager; |
24 | 22 |
|
25 | 23 | import java.util.Deque; |
26 | 24 |
|
| 25 | +import static net.gunivers.sniffer.util.Utils.addSnifferPrefix; |
| 26 | +import static net.minecraft.server.command.CommandManager.argument; |
| 27 | +import static net.minecraft.server.command.CommandManager.literal; |
| 28 | + |
27 | 29 | /** |
28 | 30 | * Main command handler for the datapack debugging system. |
29 | 31 | * Provides commands for setting breakpoints, stepping through code, and inspecting variables. |
@@ -173,7 +175,7 @@ public static void onInitialize() { |
173 | 175 | var t = Text.literal(stack.getFunction()); |
174 | 176 | var style = t.getStyle(); |
175 | 177 | if(stacks.indexOf(stack) == 0){ |
176 | | - style = style.withBold(true); |
| 178 | + style = style.withBold(true).withColor(TextColor.parse("aqua").getOrThrow()); |
177 | 179 | }else { |
178 | 180 | style = style.withBold(false); |
179 | 181 | } |
@@ -240,40 +242,30 @@ public static void step(int steps, ServerCommandSource source) { |
240 | 242 | isDebugCommand = true; |
241 | 243 | moveSteps = steps; |
242 | 244 | CommandExecutionContext<?> context = null; |
243 | | - try { |
244 | | - while (moveSteps > 0) { |
245 | | - context = storedCommandExecutionContext.peekFirst(); |
246 | | - if (context != null) { |
247 | | - var cls = context.getClass(); |
248 | | - var method = cls.getDeclaredMethod("onStep"); |
249 | | - method.setAccessible(true); |
250 | | - method.invoke(context); |
251 | | - if (moveSteps != 0) { |
| 245 | + while (moveSteps > 0) { |
| 246 | + context = storedCommandExecutionContext.peekFirst(); |
| 247 | + if (context != null) { |
| 248 | + ReflectUtil.invoke(context, "onStep").onFailure(LOGGER::error); |
| 249 | + if (moveSteps != 0) { |
| 250 | + storedCommandExecutionContext.pollFirst().close(); |
| 251 | + }else { |
| 252 | + var result = (boolean) ReflectUtil.invoke(context, "ifContainsCommandAction").onFailure(LOGGER::error).getData(); |
| 253 | + if(!result){ |
252 | 254 | storedCommandExecutionContext.pollFirst().close(); |
253 | | - }else { |
254 | | - var method1 = cls.getDeclaredMethod("ifContainsCommandAction"); |
255 | | - method1.setAccessible(true); |
256 | | - boolean result = (boolean) method1.invoke(context); |
257 | | - if(!result){ |
258 | | - storedCommandExecutionContext.pollFirst().close(); |
259 | | - } |
260 | | - break; |
261 | 255 | } |
262 | | - } else { |
263 | | - source.sendFeedback(() -> addSnifferPrefix(Text.translatable("sniffer.commands.breakpoint.step.over").formatted(Formatting.WHITE)), false); |
264 | | - continueExec(source); |
| 256 | + break; |
265 | 257 | } |
| 258 | + } else { |
| 259 | + source.sendFeedback(() -> addSnifferPrefix(Text.translatable("sniffer.commands.breakpoint.step.over").formatted(Formatting.WHITE)), false); |
| 260 | + continueExec(source); |
266 | 261 | } |
267 | | - } catch (Exception e) { |
268 | | - LOGGER.error(e.getMessage(), e); |
269 | | - } finally { |
270 | | - isDebugCommand = false; |
271 | | - if (context != null) { |
272 | | - try { |
273 | | - context.close(); |
274 | | - } catch (Exception e) { |
275 | | - LOGGER.error(e.getMessage()); |
276 | | - } |
| 262 | + } |
| 263 | + isDebugCommand = false; |
| 264 | + if (context != null) { |
| 265 | + try { |
| 266 | + context.close(); |
| 267 | + } catch (Exception e) { |
| 268 | + LOGGER.error(e.getMessage()); |
277 | 269 | } |
278 | 270 | } |
279 | 271 | } |
@@ -313,10 +305,10 @@ public static void continueExec(@NotNull ServerCommandSource source) { |
313 | 305 | return null; |
314 | 306 | } |
315 | 307 | try { |
316 | | - var cls = context.getClass(); |
317 | | - var method = cls.getDeclaredMethod("getKey", String.class); |
318 | | - method.setAccessible(true); |
319 | | - return (Pair<NbtElement, Boolean>) method.invoke(context, key); |
| 308 | + //noinspection unchecked |
| 309 | + return (Pair<NbtElement, Boolean>) ReflectUtil.invoke(context, "getKey", key) |
| 310 | + .onFailure(LOGGER::error) |
| 311 | + .getDataOrElse(null); |
320 | 312 | }catch (Exception e){ |
321 | 313 | LOGGER.error(e.toString()); |
322 | 314 | source.sendError(Text.translatable("sniffer.commands.breakpoint.get.fail.error", e.toString())); |
|
0 commit comments