use of org.spongepowered.common.bridge.commands.CommandSourceStackBridge in project SpongeCommon by SpongePowered.
the class SpongeCommandDispatcher method execute.
@Override
public int execute(final StringReader input, final CommandSourceStack source) throws CommandSyntaxException {
try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
final CommandSourceStackBridge sourceBridge = (CommandSourceStackBridge) source;
frame.addContext(EventContextKeys.COMMAND, input.getString());
sourceBridge.bridge$updateFrameFromCommandSource(frame);
return this.commandManager.process(sourceBridge.bridge$withCurrentCause(), input.getRemaining()).result();
} catch (final CommandException e) {
throw new net.minecraft.commands.CommandRuntimeException(SpongeAdventure.asVanilla(e.componentMessage()));
}
}
use of org.spongepowered.common.bridge.commands.CommandSourceStackBridge in project SpongeCommon by SpongePowered.
the class CommandSourceStackMixin method impl$updateCauseOnWithPosition.
@Inject(method = "withPosition", at = @At("RETURN"))
private void impl$updateCauseOnWithPosition(final Vec3 pos, final CallbackInfoReturnable<CommandSourceStack> cir) {
if (cir.getReturnValue() != (Object) this) {
final org.spongepowered.math.vector.Vector3d position = VecHelper.toVector3d(pos);
final ServerLocation location = this.impl$cause.context().get(EventContextKeys.LOCATION).map(x -> ServerLocation.of(x.world(), position)).orElseGet(() -> ServerLocation.of((org.spongepowered.api.world.server.ServerWorld) cir.getReturnValue().getLevel(), position));
((CommandSourceStackBridge) cir.getReturnValue()).bridge$setCause(this.impl$applyToCause(EventContextKeys.LOCATION, location));
}
}
use of org.spongepowered.common.bridge.commands.CommandSourceStackBridge in project SpongeCommon by SpongePowered.
the class SpongeCommandCauseFactory method create.
@Override
@NonNull
public CommandCause create() {
try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
final Cause cause = frame.currentCause();
final CommandSource iCommandSource = cause.first(CommandSource.class).orElseGet(() -> SpongeCommon.game().systemSubject());
final CommandSourceStack commandSource;
if (iCommandSource instanceof CommandSourceProviderBridge) {
// We know about this one so we can create it using the factory method on the source.
commandSource = ((CommandSourceProviderBridge) iCommandSource).bridge$getCommandSource(cause);
} else {
// try to create a command cause from the given ICommandSource, but as Mojang did not see fit to
// put any identifying characteristics on the object, we have to go it alone...
final EventContext context = cause.context();
@Nullable final Locatable locatable = iCommandSource instanceof Locatable ? (Locatable) iCommandSource : null;
final Component displayName;
if (iCommandSource instanceof Entity) {
displayName = ((Entity) iCommandSource).get(Keys.DISPLAY_NAME).map(SpongeAdventure::asVanilla).orElseGet(() -> new TextComponent(iCommandSource instanceof Nameable ? ((Nameable) iCommandSource).name() : iCommandSource.getClass().getSimpleName()));
} else {
displayName = new TextComponent(iCommandSource instanceof Nameable ? ((Nameable) iCommandSource).name() : iCommandSource.getClass().getSimpleName());
}
final String name = displayName.getString();
commandSource = new CommandSourceStack(iCommandSource, context.get(EventContextKeys.LOCATION).map(x -> VecHelper.toVanillaVector3d(x.position())).orElseGet(() -> locatable == null ? Vec3.ZERO : VecHelper.toVanillaVector3d(locatable.location().position())), context.get(EventContextKeys.ROTATION).map(rot -> new Vec2((float) rot.x(), (float) rot.y())).orElse(Vec2.ZERO), context.get(EventContextKeys.LOCATION).map(x -> (ServerLevel) x.world()).orElseGet(() -> locatable == null ? SpongeCommon.server().getLevel(Level.OVERWORLD) : (ServerLevel) locatable.serverLocation().world()), 4, name, displayName, SpongeCommon.server(), iCommandSource instanceof Entity ? (net.minecraft.world.entity.Entity) iCommandSource : null);
}
// We don't want the command source to have altered the cause here (unless there is the special case of the
// server), so we reset it back to what it was (in the ctor of CommandSource, it will add the current source
// to the cause - that's for if the source is created elsewhere, not here)
((CommandSourceStackBridge) commandSource).bridge$setCause(frame.currentCause());
return (CommandCause) commandSource;
}
}
use of org.spongepowered.common.bridge.commands.CommandSourceStackBridge 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);
}
}
}
use of org.spongepowered.common.bridge.commands.CommandSourceStackBridge in project SpongeCommon by SpongePowered.
the class CommandSourceStackMixin method impl$updateCauseOnWithWorld.
/*
* A note on why we're doing this with the cause manually.
*
* When the object is first constructed, we get the cause from the stack manager. However, as the command processor
* works through the nodes, this entire source may get replaced. We want to keep some of the changes in sync,
* but the original cause may have gone by the time the source changes. Really, this command source is the analogue
* of our Cause, NOT our CauseStackManager, so we just need to do `Cause.with(...)` along with their select `with*(...)`
* methods.
*/
@Inject(method = "withLevel", at = @At("RETURN"))
private void impl$updateCauseOnWithWorld(final ServerLevel serverWorld, final CallbackInfoReturnable<CommandSourceStack> cir) {
if (cir.getReturnValue() != (Object) this) {
final ServerLocation location = this.impl$cause.context().get(EventContextKeys.LOCATION).map(x -> ServerLocation.of((org.spongepowered.api.world.server.ServerWorld) serverWorld, x.position())).orElseGet(() -> ServerLocation.of((org.spongepowered.api.world.server.ServerWorld) serverWorld, VecHelper.toVector3d(cir.getReturnValue().getPosition())));
((CommandSourceStackBridge) cir.getReturnValue()).bridge$setCause(this.impl$applyToCause(EventContextKeys.LOCATION, location));
}
}
Aggregations