Search in sources :

Example 1 with ArgumentCommandNode

use of com.mojang.brigadier.tree.ArgumentCommandNode in project SpongeCommon by SpongePowered.

the class CommandsMixin method impl$preventPutIntoMapIfNodeIsComplex.

@SuppressWarnings("unchecked")
@Redirect(method = "fillUsableCommands", at = @At(value = "INVOKE", target = "Ljava/util/Map;put(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", remap = false))
private <K, V> V impl$preventPutIntoMapIfNodeIsComplex(final Map<K, V> map, final K key, final V value, final CommandNode<CommandSourceStack> rootCommandSource, final CommandNode<SharedSuggestionProvider> rootSuggestion, final CommandSourceStack source, final Map<CommandNode<CommandSourceStack>, CommandNode<SharedSuggestionProvider>> commandNodeToSuggestionNode) {
    if (!map.containsKey(key)) {
        // done here because this check is applicable
        final ServerPlayer e = (ServerPlayer) source.getEntity();
        final Map<CommandNode<CommandSourceStack>, List<CommandNode<SharedSuggestionProvider>>> playerNodes = this.impl$playerNodeCache.get(e);
        if (!playerNodes.containsKey(key)) {
            final List<CommandNode<SharedSuggestionProvider>> children = new ArrayList<>();
            children.add((CommandNode<SharedSuggestionProvider>) value);
            playerNodes.put((CommandNode<CommandSourceStack>) key, children);
        }
        // we need to swap it out.
        if (value instanceof ArgumentCommandNode && CommandUtil.checkForCustomSuggestions(rootSuggestion)) {
            rootSuggestion.addChild(this.impl$cloneArgumentCommandNodeWithoutSuggestions((ArgumentCommandNode<SharedSuggestionProvider, ?>) value));
        } else {
            rootSuggestion.addChild((CommandNode<SharedSuggestionProvider>) value);
        }
        return map.put(key, value);
    }
    // it's ignored anyway.
    return null;
}
Also used : ArgumentCommandNode(com.mojang.brigadier.tree.ArgumentCommandNode) SpongeArgumentCommandNode(org.spongepowered.common.command.brigadier.tree.SpongeArgumentCommandNode) ArgumentCommandNode(com.mojang.brigadier.tree.ArgumentCommandNode) RootCommandNode(com.mojang.brigadier.tree.RootCommandNode) SpongeArgumentCommandNode(org.spongepowered.common.command.brigadier.tree.SpongeArgumentCommandNode) CommandNode(com.mojang.brigadier.tree.CommandNode) ServerPlayer(net.minecraft.server.level.ServerPlayer) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) SharedSuggestionProvider(net.minecraft.commands.SharedSuggestionProvider) CommandSourceStack(net.minecraft.commands.CommandSourceStack) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 2 with ArgumentCommandNode

use of com.mojang.brigadier.tree.ArgumentCommandNode in project SpongeCommon by SpongePowered.

the class CommandsMixin method impl$testPermissionAndPreventRecalculationWhenSendingNodes.

@Redirect(method = "fillUsableCommands", at = @At(value = "INVOKE", target = "Lcom/mojang/brigadier/tree/CommandNode;canUse(Ljava/lang/Object;)Z", remap = false))
private boolean impl$testPermissionAndPreventRecalculationWhenSendingNodes(final CommandNode<CommandSourceStack> commandNode, final Object source, final CommandNode<CommandSourceStack> rootCommandNode, final CommandNode<SharedSuggestionProvider> rootSuggestion, final CommandSourceStack sourceButTyped, final Map<CommandNode<CommandSourceStack>, CommandNode<SharedSuggestionProvider>> commandNodeToSuggestionNode) {
    final ServerPlayer e = (ServerPlayer) sourceButTyped.getEntity();
    final Map<CommandNode<CommandSourceStack>, List<CommandNode<SharedSuggestionProvider>>> playerNodes = this.impl$playerNodeCache.get(e);
    final List<CommandNode<SharedSuggestionProvider>> existingNodes = playerNodes.get(commandNode);
    if (existingNodes != null) {
        if (!existingNodes.isEmpty()) {
            boolean hasCustomSuggestionsAlready = CommandUtil.checkForCustomSuggestions(rootSuggestion);
            for (final CommandNode<SharedSuggestionProvider> node : existingNodes) {
                // Because we don't control the client, we have to work around it here.
                if (hasCustomSuggestionsAlready && node instanceof ArgumentCommandNode) {
                    final ArgumentCommandNode<SharedSuggestionProvider, ?> argNode = (ArgumentCommandNode<SharedSuggestionProvider, ?>) node;
                    if (argNode.getCustomSuggestions() != null) {
                        // Rebuild the node without the custom suggestions.
                        rootSuggestion.addChild(this.impl$cloneArgumentCommandNodeWithoutSuggestions(argNode));
                        continue;
                    }
                } else if (node instanceof ArgumentCommandNode && ((ArgumentCommandNode<?, ?>) node).getCustomSuggestions() != null) {
                    // no more custom suggestions
                    hasCustomSuggestionsAlready = true;
                }
                rootSuggestion.addChild(node);
            }
        }
        // If empty, we have a node won't resolve (even if not complex), so we ignore it.
        return false;
    // If we have already processed this node and it appears in the suggestion node list, prevent a potentially costly
    // canUse check as we know we can already use it.
    } else if (!commandNodeToSuggestionNode.containsKey(commandNode) && !SpongeNodePermissionCache.canUse(rootCommandNode instanceof RootCommandNode, this.impl$commandManager.getDispatcher(), commandNode, sourceButTyped)) {
        playerNodes.put(commandNode, Collections.emptyList());
        return false;
    }
    if (commandNode instanceof SpongeArgumentCommandNode && ((SpongeArgumentCommandNode<?>) commandNode).isComplex()) {
        final boolean hasCustomSuggestionsAlready = CommandUtil.checkForCustomSuggestions(rootSuggestion);
        final CommandNode<SharedSuggestionProvider> finalCommandNode = ((SpongeArgumentCommandNode<?>) commandNode).getComplexSuggestions(rootSuggestion, commandNodeToSuggestionNode, playerNodes, !hasCustomSuggestionsAlready);
        if (!this.impl$getChildrenFromNode(commandNode).isEmpty()) {
            this.shadow$fillUsableCommands(commandNode, finalCommandNode, sourceButTyped, commandNodeToSuggestionNode);
        }
        return false;
    }
    // Normal node, handle it normally. We don't add to the playerNodeCache - the commandNodeToSuggestionNode map handles this.
    return true;
}
Also used : SpongeArgumentCommandNode(org.spongepowered.common.command.brigadier.tree.SpongeArgumentCommandNode) ArgumentCommandNode(com.mojang.brigadier.tree.ArgumentCommandNode) SpongeArgumentCommandNode(org.spongepowered.common.command.brigadier.tree.SpongeArgumentCommandNode) ArgumentCommandNode(com.mojang.brigadier.tree.ArgumentCommandNode) RootCommandNode(com.mojang.brigadier.tree.RootCommandNode) SpongeArgumentCommandNode(org.spongepowered.common.command.brigadier.tree.SpongeArgumentCommandNode) CommandNode(com.mojang.brigadier.tree.CommandNode) RootCommandNode(com.mojang.brigadier.tree.RootCommandNode) ServerPlayer(net.minecraft.server.level.ServerPlayer) ArrayList(java.util.ArrayList) List(java.util.List) SharedSuggestionProvider(net.minecraft.commands.SharedSuggestionProvider) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Aggregations

ArgumentCommandNode (com.mojang.brigadier.tree.ArgumentCommandNode)2 CommandNode (com.mojang.brigadier.tree.CommandNode)2 RootCommandNode (com.mojang.brigadier.tree.RootCommandNode)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 SharedSuggestionProvider (net.minecraft.commands.SharedSuggestionProvider)2 ServerPlayer (net.minecraft.server.level.ServerPlayer)2 Redirect (org.spongepowered.asm.mixin.injection.Redirect)2 SpongeArgumentCommandNode (org.spongepowered.common.command.brigadier.tree.SpongeArgumentCommandNode)2 CommandSourceStack (net.minecraft.commands.CommandSourceStack)1