Search in sources :

Example 6 with CommandCause

use of org.spongepowered.api.command.CommandCause in project SpongeCommon by SpongePowered.

the class SpongeParameterValueBuilder method build.

@Override
@NonNull
public SpongeParameterValue<T> build() throws IllegalStateException {
    Preconditions.checkState(this.key != null, "The command key may not be null");
    Preconditions.checkState(!this.parsers.isEmpty(), "There must be parsers");
    final ImmutableList.Builder<ValueParser<? extends T>> parsersBuilder = ImmutableList.builder();
    parsersBuilder.addAll(this.parsers);
    final ValueCompleter completer;
    if (this.completer != null) {
        completer = this.completer;
    } else {
        final ImmutableList.Builder<ValueCompleter> completersBuilder = ImmutableList.builder();
        for (final ValueParser<? extends T> parser : this.parsers) {
            if (parser instanceof ValueCompleter) {
                completersBuilder.add((ValueCompleter) parser);
            }
        }
        final ImmutableList<ValueCompleter> completers = completersBuilder.build();
        if (completers.isEmpty()) {
            completer = SpongeParameterValueBuilder.EMPTY_COMPLETER;
        } else if (completers.size() == 1) {
            completer = completers.get(0);
        } else {
            completer = (context, currentInput) -> {
                final ImmutableList.Builder<CommandCompletion> builder = ImmutableList.builder();
                for (final ValueCompleter valueCompleter : completers) {
                    builder.addAll(valueCompleter.complete(context, currentInput));
                }
                return builder.build();
            };
        }
    }
    return new SpongeParameterValue<>(parsersBuilder.build(), completer, this.usage, this.executionRequirements == null ? commandCause -> true : this.executionRequirements, this.key, this.isOptional, this.consumesAll, this.terminal, this.modifier);
}
Also used : NonNull(org.checkerframework.checker.nullness.qual.NonNull) CommandCompletion(org.spongepowered.api.command.CommandCompletion) Predicate(java.util.function.Predicate) ValueParameterModifier(org.spongepowered.api.command.parameter.managed.ValueParameterModifier) ArrayList(java.util.ArrayList) ValueParser(org.spongepowered.api.command.parameter.managed.ValueParser) Objects(java.util.Objects) CommandCause(org.spongepowered.api.command.CommandCause) ValueCompleter(org.spongepowered.api.command.parameter.managed.ValueCompleter) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Parameter(org.spongepowered.api.command.parameter.Parameter) Type(java.lang.reflect.Type) ValueUsage(org.spongepowered.api.command.parameter.managed.ValueUsage) Preconditions(com.google.common.base.Preconditions) Collections(java.util.Collections) Nullable(org.checkerframework.checker.nullness.qual.Nullable) ValueCompleter(org.spongepowered.api.command.parameter.managed.ValueCompleter) ValueParser(org.spongepowered.api.command.parameter.managed.ValueParser) ImmutableList(com.google.common.collect.ImmutableList) NonNull(org.checkerframework.checker.nullness.qual.NonNull)

Example 7 with CommandCause

use of org.spongepowered.api.command.CommandCause in project SpongeCommon by SpongePowered.

the class SpongeTargetBlockValueParameter method parseValue.

@Override
@NonNull
public Optional<? extends ServerLocation> parseValue(@NonNull final CommandCause cause, final ArgumentReader.@NonNull Mutable reader) throws ArgumentParseException {
    final Object root = cause.cause().root();
    if (root instanceof Living) {
        final Living living = (Living) root;
        final Optional<RayTraceResult<@NonNull LocatableBlock>> rayTraceResult = RayTrace.block().sourceEyePosition(living).direction(living.headDirection()).limit(30).continueWhileBlock(RayTrace.onlyAir()).select(RayTrace.nonAir()).continueWhileEntity(r -> false).execute();
        if (rayTraceResult.isPresent()) {
            return rayTraceResult.map(x -> x.selectedObject().serverLocation());
        }
        throw reader.createException(Component.text("The cause root is not looking at a block!"));
    }
    throw reader.createException(Component.text("The cause root must be a Living!"));
}
Also used : NonNull(org.checkerframework.checker.nullness.qual.NonNull) LocatableBlock(org.spongepowered.api.world.LocatableBlock) ArgumentReader(org.spongepowered.api.command.parameter.ArgumentReader) CommandCause(org.spongepowered.api.command.CommandCause) Component(net.kyori.adventure.text.Component) ArgumentParseException(org.spongepowered.api.command.exception.ArgumentParseException) ResourceKey(org.spongepowered.api.ResourceKey) CommandContext(org.spongepowered.api.command.parameter.CommandContext) ResourceKeyedZeroAdvanceValueParameter(org.spongepowered.common.command.brigadier.argument.ResourceKeyedZeroAdvanceValueParameter) Optional(java.util.Optional) RayTrace(org.spongepowered.api.util.blockray.RayTrace) Living(org.spongepowered.api.entity.living.Living) RayTraceResult(org.spongepowered.api.util.blockray.RayTraceResult) ServerLocation(org.spongepowered.api.world.server.ServerLocation) Living(org.spongepowered.api.entity.living.Living) NonNull(org.checkerframework.checker.nullness.qual.NonNull) RayTraceResult(org.spongepowered.api.util.blockray.RayTraceResult) NonNull(org.checkerframework.checker.nullness.qual.NonNull)

Example 8 with CommandCause

use of org.spongepowered.api.command.CommandCause in project SpongeCommon by SpongePowered.

the class CommandsMixin method impl$addNonBrigSuggestions.

@Redirect(method = "sendCommands", at = @At(value = "INVOKE", target = "Lnet/minecraft/commands/Commands;fillUsableCommands(Lcom/mojang/brigadier/tree/CommandNode;Lcom/mojang/brigadier/tree/CommandNode;Lnet/minecraft/commands/CommandSourceStack;Ljava/util/Map;)V"))
private void impl$addNonBrigSuggestions(final Commands commands, final CommandNode<CommandSourceStack> p_197052_1_, final CommandNode<SharedSuggestionProvider> p_197052_2_, final CommandSourceStack p_197052_3_, final Map<CommandNode<CommandSourceStack>, CommandNode<SharedSuggestionProvider>> p_197052_4_, final ServerPlayer playerEntity) {
    try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
        frame.addContext(EventContextKeys.SUBJECT, (Subject) playerEntity);
        final CommandCause sourceToUse = ((CommandSourceStackBridge) p_197052_3_).bridge$withCurrentCause();
        try {
            this.impl$playerNodeCache.put(playerEntity, new IdentityHashMap<>());
            // We use this because the redirects should be a 1:1 mapping (which is what this map is for).
            final IdentityHashMap<CommandNode<CommandSourceStack>, CommandNode<SharedSuggestionProvider>> idMap = new IdentityHashMap<>(p_197052_4_);
            this.shadow$fillUsableCommands(p_197052_1_, p_197052_2_, (CommandSourceStack) sourceToUse, idMap);
        } finally {
            this.impl$playerNodeCache.remove(playerEntity);
        }
        for (final CommandNode<SharedSuggestionProvider> node : this.impl$commandManager.getNonBrigadierSuggestions(sourceToUse)) {
            p_197052_2_.addChild(node);
        }
    }
}
Also used : 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) CauseStackManager(org.spongepowered.api.event.CauseStackManager) IdentityHashMap(java.util.IdentityHashMap) CommandSourceStackBridge(org.spongepowered.common.bridge.commands.CommandSourceStackBridge) CommandCause(org.spongepowered.api.command.CommandCause) SharedSuggestionProvider(net.minecraft.commands.SharedSuggestionProvider) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Aggregations

CommandCause (org.spongepowered.api.command.CommandCause)8 NonNull (org.checkerframework.checker.nullness.qual.NonNull)6 Optional (java.util.Optional)4 Component (net.kyori.adventure.text.Component)4 Nullable (org.checkerframework.checker.nullness.qual.Nullable)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 CauseStackManager (org.spongepowered.api.event.CauseStackManager)3 Preconditions (com.google.common.base.Preconditions)2 ImmutableList (com.google.common.collect.ImmutableList)2 CommandNode (com.mojang.brigadier.tree.CommandNode)2 Collections (java.util.Collections)2 Objects (java.util.Objects)2 CommandSourceStack (net.minecraft.commands.CommandSourceStack)2 SharedSuggestionProvider (net.minecraft.commands.SharedSuggestionProvider)2 CommandCompletion (org.spongepowered.api.command.CommandCompletion)2 Cause (org.spongepowered.api.event.Cause)2 EventContextKeys (org.spongepowered.api.event.EventContextKeys)2 SpongeCommon (org.spongepowered.common.SpongeCommon)2 SpongeAdventure (org.spongepowered.common.adventure.SpongeAdventure)2