Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import net.minecraft.commands.CommandBuildContext;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.protocol.game.ClientboundCommandsPacket;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.permissions.PermissionProviderCheck;

Expand All @@ -30,6 +31,7 @@
import org.cardboardpowered.bridge.commands.PermissionProviderCheckBridge;
import org.cardboardpowered.bridge.server.level.ServerPlayerBridge;

import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -46,6 +48,10 @@ public class CommandsMixin {
@Shadow
public com.mojang.brigadier.CommandDispatcher<CommandSourceStack> dispatcher;

@Shadow
@Final
private static ClientboundCommandsPacket.NodeInspector<CommandSourceStack> COMMAND_NODE_INSPECTOR;

@Shadow
private static <S> void fillUsableCommands(
CommandNode<S> root,
Expand Down Expand Up @@ -81,58 +87,58 @@ private static <S> void fillUsableCommands(
}
}

/**
* Build the command tree for this specific player, expose its top-level
* labels through Bukkit's PlayerCommandSendEvent, then send a root that
* contains only labels retained by plugins.
*
* Cardboard previously fired PlayerCommandSendEvent but ignored changes to
* event.getCommands(), which made command-hiding plugins unable to remove
* commands from client-side slash/TAB suggestions.
*/
@SuppressWarnings({"rawtypes", "unchecked"})
@Inject(at = @At("HEAD"), method = "sendCommands")
public void bukkitize(
@Inject(at = @At("HEAD"), method = "sendCommands", cancellable = true)
private void cardboard$filterAndSendCommands(
ServerPlayer entityplayer,
CallbackInfo ci
) {
Map<CommandNode<CommandSourceStack>, CommandNode<CommandSourceStack>> map =
Maps.newIdentityHashMap();

RootCommandNode vanillaRoot = new RootCommandNode();

RootCommandNode<CommandSourceStack> vanilla =
entityplayer.level()
.getServer()
.getCommands()
.getDispatcher()
.getRoot();

map.put(vanilla, vanillaRoot);

fillUsableCommands(
vanilla,
vanillaRoot,
entityplayer.createCommandSourceStack(),
(Map) map
);

RootCommandNode<CommandSourceStack> rootcommandnode =
RootCommandNode<CommandSourceStack> rootCommandNode =
new RootCommandNode<>();

map.put(this.dispatcher.getRoot(), rootcommandnode);
map.put(this.dispatcher.getRoot(), rootCommandNode);

fillUsableCommands(
this.dispatcher.getRoot(),
rootcommandnode,
rootCommandNode,
entityplayer.createCommandSourceStack(),
(Map) map
);

Collection<String> bukkit = new LinkedHashSet<>();
Collection<String> originalCommands = new LinkedHashSet<>();
for (CommandNode<CommandSourceStack> node : rootCommandNode.getChildren()) {
originalCommands.add(node.getName());
}

PlayerCommandSendEvent event = new PlayerCommandSendEvent(
(Player) ((ServerPlayerBridge) entityplayer).getBukkitEntity(),
new LinkedHashSet<>(originalCommands)
);
CraftEventFactory.callEvent(event);

for (CommandNode node : rootcommandnode.getChildren()) {
bukkit.add(node.getName());
RootCommandNode<CommandSourceStack> filteredRoot = new RootCommandNode<>();
for (CommandNode<CommandSourceStack> node : rootCommandNode.getChildren()) {
if (event.getCommands().contains(node.getName())) {
filteredRoot.addChild(node);
}
}

PlayerCommandSendEvent event =
new PlayerCommandSendEvent(
(Player) ((ServerPlayerBridge) entityplayer)
.getBukkitEntity(),
new LinkedHashSet<>(bukkit)
);
entityplayer.connection.send(
new ClientboundCommandsPacket(filteredRoot, COMMAND_NODE_INSPECTOR)
);

CraftEventFactory.callEvent(event);
ci.cancel();
}
}
}
Loading