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