Skip to content

Commit

Permalink
fix: 1.21で補完を出すと鯖落ちする
Browse files Browse the repository at this point in the history
  • Loading branch information
Maru32768 committed Oct 11, 2024
1 parent 76ef65f commit 39e31f6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
48 changes: 33 additions & 15 deletions bukkit/src/main/java/net/kunmc/lab/commandlib/CommandLib.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package net.kunmc.lab.commandlib;

import com.google.common.collect.Lists;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.tree.CommandNode;
import com.mojang.brigadier.tree.RootCommandNode;
import net.kunmc.lab.commandlib.util.bukkit.BukkitUtil;
import net.kunmc.lab.commandlib.util.bukkit.MinecraftVersion;
import net.kunmc.lab.commandlib.util.nms.command.NMSCommandDispatcher;
import net.kunmc.lab.commandlib.util.nms.command.NMSVanillaCommandWrapper;
import net.kunmc.lab.commandlib.util.nms.server.NMSCraftServer;
Expand Down Expand Up @@ -54,32 +57,47 @@ private void enable() {
@Override
public void run() {
registeredCommands.addAll(new CommandNodeCreator<>(commands).build());
NMSCommandDispatcher dispatcher = NMSCraftServer.create(plugin.getServer())
.getServer()
.getCommandDispatcher();
RootCommandNode root = dispatcher.getBrigadier()
.getRoot();
registeredCommands.forEach(x -> {
root.addChild(x);

try {
CommandMap commandMap = ((CommandMap) NMSCraftServer.create()
.getValue("commandMap"));
Field knownCommandsField = SimpleCommandMap.class.getDeclaredField("knownCommands");
knownCommandsField.setAccessible(true);
Map<String, org.bukkit.command.Command> knownCommands = ((Map<String, org.bukkit.command.Command>) knownCommandsField.get(
commandMap));
knownCommands.put(x.getName(),
NMSVanillaCommandWrapper.create()
.createInstance(dispatcher, x));

if (new MinecraftVersion(BukkitUtil.getMinecraftVersion()).isLessThan(new MinecraftVersion(
"1.21.0"))) {
NMSCommandDispatcher dispatcher = NMSCraftServer.create(plugin.getServer())
.getServer()
.getCommandDispatcher();
RootCommandNode root = dispatcher.getBrigadier()
.getRoot();

root.addChild(x);
knownCommands.put(x.getName(),
NMSVanillaCommandWrapper.create()
.createInstance(dispatcher, x));

root.getChild("execute")
.getChild("run")
.getRedirect()
.addChild(x);
} else {
CommandNode shadowBrigNode = (CommandNode) Class.forName(
"io.papermc.paper.command.brigadier.ShadowBrigNode")
.getConstructor(CommandNode.class)
.newInstance(x);
CommandDispatcher dispatcher = ((CommandDispatcher) knownCommands.getClass()
.getDeclaredMethod(
"getDispatcher")
.invoke(knownCommands));
dispatcher.getRoot()
.addChild(shadowBrigNode);
}
} catch (Exception e) {
throw new RuntimeException(e);
}

root.getChild("execute")
.getChild("run")
.getRedirect()
.addChild(x);
});

commands.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,8 @@ protected final String getKey() {
public final List<TestResult> results() {
return new ArrayList<>(Collections.unmodifiableCollection(resultMap.values()));
}

public final void clearResults() {
resultMap.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public void runAllTests(Consumer<List<TestResult>> consumer) {
.flatMap(x -> x.results()
.stream())
.collect(Collectors.toList()));

tests.forEach(TestBase::clearResults);
};

mainCommand.addChildren(new Command("rerun") {{
Expand Down

0 comments on commit 39e31f6

Please sign in to comment.