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);
}
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!"));
}
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);
}
}
}
Aggregations