Forwarding commands registered outside RegisterCommandsEvent (#291)

This commit is contained in:
IzzelAliz 2021-06-14 16:40:50 +08:00
parent a4f015fce6
commit 0fcaba618f
2 changed files with 33 additions and 1 deletions

View File

@ -7,6 +7,7 @@ import com.mojang.brigadier.tree.RootCommandNode;
import io.izzel.arclight.common.bridge.command.CommandNodeBridge;
import io.izzel.arclight.common.bridge.entity.player.ServerPlayerEntityBridge;
import io.izzel.arclight.common.bridge.server.MinecraftServerBridge;
import io.izzel.arclight.common.mod.util.BukkitDispatcher;
import net.minecraft.command.CommandSource;
import net.minecraft.command.Commands;
import net.minecraft.command.ISuggestionProvider;
@ -34,7 +35,7 @@ public abstract class CommandsMixin {
// @formatter:on
public void arclight$constructor() {
this.dispatcher = new CommandDispatcher<>();
this.dispatcher = new BukkitDispatcher((Commands) (Object) this);
this.dispatcher.setConsumer((context, b, i) -> context.getSource().onCommandComplete(context, b, i));
}

View File

@ -0,0 +1,31 @@
package io.izzel.arclight.common.mod.util;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.tree.LiteralCommandNode;
import net.minecraft.command.CommandSource;
import net.minecraft.command.Commands;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.v.CraftServer;
import org.bukkit.craftbukkit.v.command.BukkitCommandWrapper;
import org.bukkit.craftbukkit.v.command.VanillaCommandWrapper;
public class BukkitDispatcher extends CommandDispatcher<CommandSource> {
private final Commands commands;
public BukkitDispatcher(Commands commands) {
this.commands = commands;
}
@Override
public LiteralCommandNode<CommandSource> register(LiteralArgumentBuilder<CommandSource> command) {
LiteralCommandNode<CommandSource> node = command.build();
if (!(node.getCommand() instanceof BukkitCommandWrapper)) {
VanillaCommandWrapper wrapper = new VanillaCommandWrapper(this.commands, node);
((CraftServer) Bukkit.getServer()).getCommandMap().register("forge", wrapper);
}
getRoot().addChild(node);
return node;
}
}