Search in sources :

Example 1 with SuggestionContext

use of com.mojang.brigadier.context.SuggestionContext in project SpongeCommon by SpongePowered.

the class SpongeCommandContextBuilder method findSuggestionContext.

@Override
public SuggestionContext<CommandSourceStack> findSuggestionContext(final int cursor) {
    if (this.transaction != null && !this.transaction.isEmpty()) {
        return this.transaction.peek().getCopyBuilder().findSuggestionContext(cursor);
    }
    // where appropriate. There is one change marked below.
    if (this.getRange().getStart() <= cursor) {
        if (this.getRange().getEnd() < cursor) {
            if (this.getChild() != null) {
                return this.getChild().findSuggestionContext(cursor);
            } else if (!this.getNodes().isEmpty()) {
                final ParsedCommandNode<CommandSourceStack> last = this.getNodes().get(this.getNodes().size() - 1);
                return new SuggestionContext<>(last.getNode(), last.getRange().getEnd() + 1);
            } else {
                return new SuggestionContext<>(this.getRootNode(), this.getRange().getStart());
            }
        } else {
            CommandNode<CommandSourceStack> prev = this.getRootNode();
            for (final ParsedCommandNode<CommandSourceStack> node : this.getNodes()) {
                final StringRange nodeRange = node.getRange();
                // Sponge Start
                if (SpongeCommandContextBuilder.checkNodeCannotBeEmpty(node.getNode(), nodeRange)) {
                    // Sponge End
                    if (nodeRange.getStart() <= cursor && cursor <= nodeRange.getEnd()) {
                        return new SuggestionContext<>(prev, nodeRange.getStart());
                    }
                // Sponge Start: End if
                }
                // Sponge End
                prev = node.getNode();
            }
            if (prev == null) {
                throw new IllegalStateException("Can't find node before cursor");
            }
            return new SuggestionContext<>(prev, this.getRange().getStart());
        }
    }
    throw new IllegalStateException("Can't find node before cursor");
}
Also used : SuggestionContext(com.mojang.brigadier.context.SuggestionContext) ParsedCommandNode(com.mojang.brigadier.context.ParsedCommandNode) CommandSourceStack(net.minecraft.commands.CommandSourceStack) StringRange(com.mojang.brigadier.context.StringRange)

Aggregations

ParsedCommandNode (com.mojang.brigadier.context.ParsedCommandNode)1 StringRange (com.mojang.brigadier.context.StringRange)1 SuggestionContext (com.mojang.brigadier.context.SuggestionContext)1 CommandSourceStack (net.minecraft.commands.CommandSourceStack)1