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;
}
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;
}
Aggregations