Search in sources :

Example 1 with EventContext

use of org.spongepowered.api.event.EventContext 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;
    }
}
Also used : TextComponent(net.minecraft.network.chat.TextComponent) NonNull(org.checkerframework.checker.nullness.qual.NonNull) EventContextKeys(org.spongepowered.api.event.EventContextKeys) CommandSourceStack(net.minecraft.commands.CommandSourceStack) SpongeAdventure(org.spongepowered.common.adventure.SpongeAdventure) CommandSourceProviderBridge(org.spongepowered.common.bridge.commands.CommandSourceProviderBridge) ServerLevel(net.minecraft.server.level.ServerLevel) EventContext(org.spongepowered.api.event.EventContext) Locatable(org.spongepowered.api.world.Locatable) CommandSourceStackBridge(org.spongepowered.common.bridge.commands.CommandSourceStackBridge) CauseStackManager(org.spongepowered.api.event.CauseStackManager) Nameable(org.spongepowered.api.util.Nameable) Nullable(org.checkerframework.checker.nullness.qual.Nullable) Component(net.minecraft.network.chat.Component) SpongeCommon(org.spongepowered.common.SpongeCommon) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) Entity(org.spongepowered.api.entity.Entity) Cause(org.spongepowered.api.event.Cause) CommandCause(org.spongepowered.api.command.CommandCause) TextComponent(net.minecraft.network.chat.TextComponent) Keys(org.spongepowered.api.data.Keys) Vec2(net.minecraft.world.phys.Vec2) Vec3(net.minecraft.world.phys.Vec3) VecHelper(org.spongepowered.common.util.VecHelper) CommandSource(net.minecraft.commands.CommandSource) Level(net.minecraft.world.level.Level) Entity(org.spongepowered.api.entity.Entity) ServerLevel(net.minecraft.server.level.ServerLevel) CommandSourceStackBridge(org.spongepowered.common.bridge.commands.CommandSourceStackBridge) CommandSource(net.minecraft.commands.CommandSource) CommandCause(org.spongepowered.api.command.CommandCause) CommandSourceStack(net.minecraft.commands.CommandSourceStack) EventContext(org.spongepowered.api.event.EventContext) Nameable(org.spongepowered.api.util.Nameable) CauseStackManager(org.spongepowered.api.event.CauseStackManager) Vec2(net.minecraft.world.phys.Vec2) Cause(org.spongepowered.api.event.Cause) CommandCause(org.spongepowered.api.command.CommandCause) SpongeAdventure(org.spongepowered.common.adventure.SpongeAdventure) CommandSourceProviderBridge(org.spongepowered.common.bridge.commands.CommandSourceProviderBridge) Component(net.minecraft.network.chat.Component) TextComponent(net.minecraft.network.chat.TextComponent) Nullable(org.checkerframework.checker.nullness.qual.Nullable) Locatable(org.spongepowered.api.world.Locatable) NonNull(org.checkerframework.checker.nullness.qual.NonNull)

Example 2 with EventContext

use of org.spongepowered.api.event.EventContext in project SpongeCommon by SpongePowered.

the class CommandSourceStackMixin_API method rotation.

@Override
public Optional<Vector3d> rotation() {
    final Cause cause = this.cause();
    final EventContext eventContext = cause.context();
    if (eventContext.containsKey(EventContextKeys.ROTATION)) {
        return eventContext.get(EventContextKeys.ROTATION);
    }
    return cause.first(Entity.class).map(Entity::rotation);
}
Also used : EventContext(org.spongepowered.api.event.EventContext) Entity(org.spongepowered.api.entity.Entity) Cause(org.spongepowered.api.event.Cause) CommandCause(org.spongepowered.api.command.CommandCause)

Example 3 with EventContext

use of org.spongepowered.api.event.EventContext in project SpongeCommon by SpongePowered.

the class CommandSourceStackMixin_API method location.

@Override
public Optional<ServerLocation> location() {
    final Cause cause = this.cause();
    final EventContext eventContext = cause.context();
    if (eventContext.containsKey(EventContextKeys.LOCATION)) {
        return eventContext.get(EventContextKeys.LOCATION);
    }
    final Optional<ServerLocation> optionalLocation = this.targetBlock().flatMap(BlockSnapshot::location);
    if (optionalLocation.isPresent()) {
        return optionalLocation;
    }
    return cause.first(Locatable.class).map(Locatable::serverLocation);
}
Also used : EventContext(org.spongepowered.api.event.EventContext) ServerLocation(org.spongepowered.api.world.server.ServerLocation) Cause(org.spongepowered.api.event.Cause) CommandCause(org.spongepowered.api.command.CommandCause) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) Locatable(org.spongepowered.api.world.Locatable)

Example 4 with EventContext

use of org.spongepowered.api.event.EventContext in project SpongeCommon by SpongePowered.

the class CommandSourceStackMixin method impl$setCauseOnConstruction.

@Inject(method = CommandSourceStackMixin.PROTECTED_CTOR_METHOD, at = @At("RETURN"))
private void impl$setCauseOnConstruction(final CommandSource p_i49553_1_, final Vec3 p_i49553_2_, final Vec2 p_i49553_3_, final ServerLevel p_i49553_4_, final int p_i49553_5_, final String p_i49553_6_, final Component p_i49553_7_, final MinecraftServer p_i49553_8_, @Nullable final Entity p_i49553_9_, final boolean p_i49553_10_, final ResultConsumer<CommandSourceStack> p_i49553_11_, final EntityAnchorArgument.Anchor p_i49553_12_, final CallbackInfo ci) {
    this.impl$cause = PhaseTracker.getCauseStackManager().currentCause();
    final EventContext context = this.impl$cause.context();
    context.get(EventContextKeys.LOCATION).ifPresent(x -> {
        this.worldPosition = VecHelper.toVanillaVector3d(x.position());
        this.level = (ServerLevel) x.world();
    });
    context.get(EventContextKeys.ROTATION).ifPresent(x -> this.rotation = new Vec2((float) x.x(), (float) x.y()));
    context.get(EventContextKeys.SUBJECT).ifPresent(x -> {
        if (x instanceof EntityAccessor) {
            this.permissionLevel = ((EntityAccessor) x).invoker$getPermissionLevel();
        } else if (x instanceof MinecraftServer && !((MinecraftServer) x).isSingleplayer()) {
            this.permissionLevel = 4;
        }
    });
}
Also used : EventContext(org.spongepowered.api.event.EventContext) EntityAccessor(org.spongepowered.common.accessor.world.entity.EntityAccessor) Vec2(net.minecraft.world.phys.Vec2) MinecraftServer(net.minecraft.server.MinecraftServer) Inject(org.spongepowered.asm.mixin.injection.Inject)

Aggregations

EventContext (org.spongepowered.api.event.EventContext)4 CommandCause (org.spongepowered.api.command.CommandCause)3 Cause (org.spongepowered.api.event.Cause)3 Vec2 (net.minecraft.world.phys.Vec2)2 Entity (org.spongepowered.api.entity.Entity)2 Locatable (org.spongepowered.api.world.Locatable)2 CommandSource (net.minecraft.commands.CommandSource)1 CommandSourceStack (net.minecraft.commands.CommandSourceStack)1 Component (net.minecraft.network.chat.Component)1 TextComponent (net.minecraft.network.chat.TextComponent)1 MinecraftServer (net.minecraft.server.MinecraftServer)1 ServerLevel (net.minecraft.server.level.ServerLevel)1 Level (net.minecraft.world.level.Level)1 Vec3 (net.minecraft.world.phys.Vec3)1 NonNull (org.checkerframework.checker.nullness.qual.NonNull)1 Nullable (org.checkerframework.checker.nullness.qual.Nullable)1 BlockSnapshot (org.spongepowered.api.block.BlockSnapshot)1 Keys (org.spongepowered.api.data.Keys)1 CauseStackManager (org.spongepowered.api.event.CauseStackManager)1 EventContextKeys (org.spongepowered.api.event.EventContextKeys)1