Skip to content

Commit cc06f3c

Browse files
committed
会在执行单步的时候发送执行的命令是什么
1 parent cae2b7c commit cc06f3c

9 files changed

Lines changed: 92 additions & 47 deletions

build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,8 @@ publishing {
8585
// The repositories here will be used for publishing your artifact, not for
8686
// retrieving dependencies.
8787
}
88+
}
89+
90+
loom {
91+
accessWidenerPath = file("src/main/resources/datapack-breakpoint.accesswidener")
8892
}

src/main/java/top/mcfpp/mod/breakpoint/DatapackBreakpoint.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ public class DatapackBreakpoint implements ModInitializer {
1010

1111
@Override
1212
public void onInitialize() {
13+
14+
15+
1316
BreakPointCommand.onInitialize();
1417
}
1518

src/main/java/top/mcfpp/mod/breakpoint/command/BreakPointCommand.java

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
package top.mcfpp.mod.breakpoint.command;
22

33
import com.google.common.collect.Queues;
4-
import com.mojang.brigadier.CommandDispatcher;
4+
import com.mojang.brigadier.arguments.IntegerArgumentType;
55
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
66
import net.minecraft.command.CommandExecutionContext;
7-
import net.minecraft.command.CommandRegistryAccess;
8-
import net.minecraft.server.command.CommandManager.RegistrationEnvironment;
97
import net.minecraft.server.command.CommandManager;
108
import net.minecraft.server.command.ServerCommandSource;
119
import net.minecraft.text.Text;
12-
import org.spongepowered.asm.launch.MixinBootstrap;
10+
import org.jetbrains.annotations.NotNull;
1311
import top.mcfpp.mod.breakpoint.DatapackBreakpoint;
12+
1413
import java.util.Deque;
15-
import java.util.Locale;
1614

1715
public class BreakPointCommand {
1816

@@ -25,31 +23,30 @@ public class BreakPointCommand {
2523
public static void onInitialize() {
2624
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
2725
dispatcher.register(CommandManager.literal("breakpoint")
28-
.requires(source -> source.hasPermissionLevel(2))
29-
.executes(context -> {
30-
context.getSource().sendFeedback(() -> Text.literal("已触发断点"), false);
31-
breakPoint(context.getSource());
32-
return 1;
33-
})
34-
.then(CommandManager.literal("step")
35-
.executes(context -> {
36-
context.getSource().sendFeedback(() -> Text.literal("已触发单步"), false);
37-
step(1, context.getSource());
38-
return 1;
39-
})
40-
)
41-
.then(CommandManager.literal("move")
42-
.executes(context -> {
43-
context.getSource().sendFeedback(() -> Text.literal("已恢复断点"), false);
44-
moveOn(context.getSource());
45-
return 1;
46-
})
47-
)
26+
.requires(source -> source.hasPermissionLevel(2))
27+
.executes(context -> {
28+
context.getSource().sendFeedback(() -> Text.literal("已触发断点"), false);
29+
breakPoint(context.getSource());
30+
return 1;
31+
})
32+
.then(CommandManager.literal("step")
33+
.executes(context -> {
34+
step(1, context.getSource());
35+
return 1;
36+
})
37+
)
38+
.then(CommandManager.literal("move")
39+
.executes(context -> {
40+
context.getSource().sendFeedback(() -> Text.literal("已恢复断点"), false);
41+
moveOn(context.getSource());
42+
return 1;
43+
})
44+
)
4845
);
4946
});
5047
}
5148

52-
private static void breakPoint(ServerCommandSource source) {
49+
private static void breakPoint(@NotNull ServerCommandSource source) {
5350
source.getServer().getTickManager().setFrozen(true);
5451
isDebugging = true;
5552
}
@@ -66,7 +63,6 @@ private static void step(int steps, ServerCommandSource source) {
6663
while (moveSteps > 0) {
6764
context = storedCommandExecutionContext.peekFirst();
6865
if (context != null) {
69-
LOGGER.info("before mod invokes run()");
7066
var cls = context.getClass();
7167
var method = cls.getDeclaredMethod("onStep");
7268
method.setAccessible(true);
@@ -93,7 +89,7 @@ private static void step(int steps, ServerCommandSource source) {
9389
}
9490
}
9591

96-
private static void moveOn(ServerCommandSource source) {
92+
private static void moveOn(@NotNull ServerCommandSource source) {
9793
source.getServer().getTickManager().setFrozen(false);
9894
isDebugging = false;
9995
moveSteps = 0;

src/main/java/top/mcfpp/mod/breakpoint/mixin/CommandExecutionContextMixin.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,10 @@
1313
import org.spongepowered.asm.mixin.injection.At;
1414
import org.spongepowered.asm.mixin.injection.Inject;
1515
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
16-
import top.mcfpp.mod.breakpoint.DatapackBreakpoint;
17-
import top.mcfpp.mod.breakpoint.command.BreakPointCommand;
1816

1917
import java.lang.reflect.Field;
2018
import java.util.Deque;
2119
import java.util.List;
22-
import java.util.Objects;
2320

2421
import static top.mcfpp.mod.breakpoint.command.BreakPointCommand.*;
2522

@@ -64,8 +61,6 @@ private void onRun(CallbackInfo ci){
6461
return;
6562
}
6663

67-
LOGGER.info("Method run()V is injected!");
68-
6964
if(isDebugging && commandQueueEntry.frame().depth() != 0 && moveSteps == 0) {
7065
//在函数中执行的,把命令暂存
7166
commandQueue.addFirst(commandQueueEntry);
@@ -111,8 +106,6 @@ private void onStep(){
111106
return;
112107
}
113108

114-
LOGGER.info("Method run()V is injected!");
115-
116109
if(isDebugging && commandQueueEntry.frame().depth() != 0 && moveSteps == 0) {
117110
//在函数中执行的,把所有命令暂存
118111
commandQueue.addFirst(commandQueueEntry);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package top.mcfpp.mod.breakpoint.mixin;
2+
3+
import com.llamalad7.mixinextras.sugar.Local;
4+
import com.mojang.brigadier.StringReader;
5+
import net.minecraft.server.command.AbstractServerCommandSource;
6+
import net.minecraft.server.function.CommandFunction;
7+
import net.minecraft.util.Identifier;
8+
import org.spongepowered.asm.mixin.Mixin;
9+
import org.spongepowered.asm.mixin.injection.At;
10+
import org.spongepowered.asm.mixin.injection.ModifyVariable;
11+
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
15+
@Mixin(CommandFunction.class)
16+
public interface CommandFunctionMixin {
17+
18+
@ModifyVariable(method = "create", at = @At("HEAD"), ordinal = 0, argsOnly = true)
19+
private static List<String> create(List<String> value) {
20+
ArrayList<String> list = new ArrayList<>();
21+
for (String str : value){
22+
if(str.equals("#breakpoint")){
23+
list.add("breakpoint");
24+
}else {
25+
list.add(str);
26+
}
27+
}
28+
return list;
29+
}
30+
31+
32+
}
Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,40 @@
11
package top.mcfpp.mod.breakpoint.mixin;
22

3-
import com.mojang.brigadier.context.ContextChain;
4-
import com.mojang.brigadier.exceptions.CommandSyntaxException;
3+
import com.mojang.brigadier.context.CommandContext;
54
import net.minecraft.command.CommandExecutionContext;
5+
import net.minecraft.command.ExecutionFlags;
66
import net.minecraft.command.FixedCommandAction;
77
import net.minecraft.command.Frame;
8+
import net.minecraft.server.MinecraftServer;
89
import net.minecraft.server.command.AbstractServerCommandSource;
9-
import net.minecraft.server.function.Tracer;
10+
import net.minecraft.server.command.ServerCommandSource;
11+
import net.minecraft.text.Text;
12+
import org.spongepowered.asm.mixin.Final;
1013
import org.spongepowered.asm.mixin.Mixin;
14+
import org.spongepowered.asm.mixin.Shadow;
1115
import org.spongepowered.asm.mixin.injection.At;
1216
import org.spongepowered.asm.mixin.injection.Inject;
1317
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
1418
import top.mcfpp.mod.breakpoint.command.BreakPointCommand;
1519

16-
import java.util.function.Supplier;
17-
1820
@Mixin(FixedCommandAction.class)
1921
public class FixCommandActionMixin<T extends AbstractServerCommandSource<T>> {
22+
@Shadow @Final private String command;
23+
@Shadow @Final private ExecutionFlags flags;
24+
@Shadow @Final private CommandContext<T> context;
2025

2126
@Inject(method = "execute(Lnet/minecraft/server/command/AbstractServerCommandSource;Lnet/minecraft/command/CommandExecutionContext;Lnet/minecraft/command/Frame;)V", at = @At("HEAD"))
2227
private void execute(T abstractServerCommandSource, CommandExecutionContext<T> commandExecutionContext, Frame frame, CallbackInfo ci) {
23-
if(BreakPointCommand.moveSteps > 0) BreakPointCommand.moveSteps --;
28+
if(BreakPointCommand.isDebugging){
29+
if(BreakPointCommand.moveSteps > 0) BreakPointCommand.moveSteps --;
30+
if(this.command.startsWith("breakpoint")) return;
31+
if(abstractServerCommandSource instanceof ServerCommandSource serverCommandSource){
32+
var players = serverCommandSource.getServer().getPlayerManager().getPlayerList();
33+
for(var player : players){
34+
player.sendMessage(Text.literal("已执行: " + this.command));
35+
}
36+
}
37+
}
2438
}
2539

2640
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
accessWidener v1 named
2+
accessible class net/minecraft/server/function/FunctionBuilder

src/main/resources/datapack-breakpoint.mixins.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"compatibilityLevel": "JAVA_21",
55
"mixins": [
66
"CommandExecutionContextMixin",
7+
"CommandFunctionMixin",
78
"FixCommandActionMixin"
89
],
910
"injectors": {

src/main/resources/fabric.mod.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
{
22
"schemaVersion": 1,
33
"id": "datapack-breakpoint",
4-
"version": "${version}",
4+
"version": "0.1.0",
55
"name": "Datapack Breakpoint",
6-
"description": "This is an example description! Tell everyone what your mod is about!",
6+
"description": "A mod implements breakpoint functionality to datapack development.",
77
"authors": [
8-
"Me!"
8+
"Alumopper"
99
],
1010
"contact": {
11-
"homepage": "https://fabricmc.net/",
12-
"sources": "https://github.com/FabricMC/fabric-example-mod"
11+
"sources": "https://github.com/Alumopper/datapack-breakpoint-template-1.21"
1312
},
1413
"license": "CC0-1.0",
1514
"icon": "assets/datapack-breakpoint/icon.png",
@@ -38,5 +37,6 @@
3837
},
3938
"suggests": {
4039
"another-mod": "*"
41-
}
40+
},
41+
"accessWidener": "datapack-breakpoint.accesswidener"
4242
}

0 commit comments

Comments
 (0)