use of net.minecraft.server.level.ServerPlayer in project SpongeCommon by SpongePowered.
the class EntityMixin method bridge$changeDimension.
/**
* This is effectively an overwrite of changeDimension: required due to
* Forge changing the signature.
*
* @author dualspiral - 18th December 2020 - 1.16.4
* @author dualspiral - 8th August 2021 - 1.16.5 (adjusted for SpongeForge)
*
* @param originalDestinationWorld The original target world
* @param originalPortalLogic performs additional teleportation logic, as required.
* @return The {@link Entity} that is either this one, or replaces this one
*/
@SuppressWarnings("ConstantConditions")
@org.checkerframework.checker.nullness.qual.Nullable
public Entity bridge$changeDimension(final net.minecraft.server.level.ServerLevel originalDestinationWorld, final PortalLogic originalPortalLogic) {
// Sponge Start
if (this.shadow$getCommandSenderWorld().isClientSide || this.removed) {
return null;
}
final boolean isPlayer = ((Object) this) instanceof ServerPlayer;
final TeleportContext contextToSwitchTo = EntityPhase.State.PORTAL_DIMENSION_CHANGE.createPhaseContext(PhaseTracker.getInstance()).worldChange();
if (isPlayer) {
contextToSwitchTo.player();
}
try (final TeleportContext context = contextToSwitchTo.buildAndSwitch();
final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
frame.pushCause(this);
frame.pushCause(originalPortalLogic.getPortalType());
frame.addContext(EventContextKeys.MOVEMENT_TYPE, originalPortalLogic.getMovementType());
this.impl$originalDestinationWorld = new WeakReference<>((ServerWorld) originalDestinationWorld);
final ChangeEntityWorldEvent.Pre preChangeEvent = PlatformHooks.INSTANCE.getEventHooks().callChangeEntityWorldEventPre((Entity) (Object) this, originalDestinationWorld);
if (preChangeEvent.isCancelled()) {
this.impl$onPreWorldChangeCanceled();
return null;
}
this.impl$customPortal = preChangeEvent.originalDestinationWorld() != preChangeEvent.destinationWorld();
final PortalLogic finalPortalLogic;
if (this.impl$customPortal && originalPortalLogic == originalDestinationWorld.getPortalForcer()) {
finalPortalLogic = (PortalLogic) ((ServerLevel) preChangeEvent.destinationWorld()).getPortalForcer();
} else {
finalPortalLogic = originalPortalLogic;
}
final net.minecraft.server.level.ServerLevel targetWorld = (net.minecraft.server.level.ServerLevel) preChangeEvent.destinationWorld();
final Vector3d currentPosition = VecHelper.toVector3d(this.shadow$position());
// If a player, set the fact they are changing dimensions
this.impl$onChangingDimension(targetWorld);
final net.minecraft.server.level.ServerLevel serverworld = (net.minecraft.server.level.ServerLevel) this.level;
final ResourceKey<Level> registrykey = serverworld.dimension();
if (isPlayer && registrykey == Level.END && targetWorld.dimension() == Level.OVERWORLD && finalPortalLogic.isVanilla()) {
// avoids modded dimensions
return this.impl$postProcessChangeDimension(this.impl$performGameWinLogic());
} else {
// Sponge Start: Redirect the find portal call to the teleporter.
// If this is vanilla, this will house our Reposition Event and return an appropriate
// portal info
final PortalInfo portalinfo = originalPortalLogic.getPortalInfo((Entity) (Object) this, targetWorld, // don't make this a method reference, it'll crash vanilla.
x -> this.shadow$findDimensionEntryPoint(x));
// Sponge End
if (portalinfo != null) {
if (portalinfo instanceof SpongePortalInfo) {
frame.addContext(EventContextKeys.PORTAL, ((SpongePortalInfo) portalinfo).portal());
}
// Only start teleporting if we have somewhere to go.
this.impl$prepareForPortalTeleport(serverworld, targetWorld);
try {
// Sponge Start: wrap the teleportation logic within a function to allow for modification
// of the teleporter
final Vector3d originalDestination = new Vector3d(portalinfo.pos.x, portalinfo.pos.y, portalinfo.pos.z);
// Note that impl$portalRepositioning is the lambda. As this will be different in ServerPlayer,
// we transfer it to a method instead so we can override it.
final Entity transportedEntity = originalPortalLogic.placeEntity((Entity) (Object) this, serverworld, targetWorld, this.yRot, createEndPlatform -> this.impl$portalRepositioning(createEndPlatform, serverworld, targetWorld, portalinfo));
// Make sure the right object was returned
this.impl$validateEntityAfterTeleport(transportedEntity, originalPortalLogic);
// If we need to reposition: well... reposition.
// Downside: portals won't come with us, but with how it's implemented in Forge,
// not sure how we'd do this.
//
// If this is vanilla, we've already fired and dealt with the event
final Cause cause = PhaseTracker.getCauseStackManager().currentCause();
if (transportedEntity != null && this.impl$shouldFireRepositionEvent) {
final Vector3d destination = VecHelper.toVector3d(this.shadow$position());
final ChangeEntityWorldEvent.Reposition reposition = SpongeEventFactory.createChangeEntityWorldEventReposition(cause, (org.spongepowered.api.entity.Entity) transportedEntity, (org.spongepowered.api.world.server.ServerWorld) serverworld, currentPosition, destination, (org.spongepowered.api.world.server.ServerWorld) originalDestinationWorld, originalDestination, (org.spongepowered.api.world.server.ServerWorld) targetWorld);
final Vector3d finalPosition;
if (reposition.isCancelled()) {
// send them back to the original destination
finalPosition = originalDestination;
} else if (reposition.destinationPosition() != destination) {
finalPosition = reposition.destinationPosition();
} else {
finalPosition = null;
}
if (finalPosition != null) {
// TODO: Rollback captures during phase - anything generated needs to vanish here
// Issue chunk ticket of type Portal, even if a portal isn't being created here.
final BlockPos ticketPos = VecHelper.toBlockPos(finalPosition);
targetWorld.getChunkSource().addRegionTicket(TicketType.PORTAL, new ChunkPos(ticketPos), 3, ticketPos);
this.shadow$absMoveTo(finalPosition.x(), finalPosition.y(), finalPosition.z());
}
}
// Used to perform player specific tasks.
this.impl$postPortalForceChangeTasks(transportedEntity, targetWorld, originalPortalLogic.getPortalType() instanceof NetherPortalType);
// Call post event
Sponge.eventManager().post(SpongeEventFactory.createChangeEntityWorldEventPost(cause, (org.spongepowered.api.entity.Entity) this, (ServerWorld) serverworld, (ServerWorld) originalDestinationWorld, (ServerWorld) targetWorld));
} catch (final RuntimeException e) {
// just in case a mod does something less than clever.
if ((Object) this instanceof ServerPlayer) {
this.impl$postPortalForceChangeTasks((Entity) (Object) this, (net.minecraft.server.level.ServerLevel) this.level, false);
}
throw e;
}
// Sponge End
} else {
// Didn't work out.
return null;
}
}
return this.impl$postProcessChangeDimension((Entity) (Object) this);
} finally {
// Reset for the next attempt.
this.impl$shouldFireRepositionEvent = true;
this.impl$originalDestinationWorld = null;
this.impl$customPortal = false;
}
}
use of net.minecraft.server.level.ServerPlayer in project SpongeCommon by SpongePowered.
the class AnimalMixin_Tracker method tracker$populateFrameInTickContext.
// @formatter:on
@Override
public void tracker$populateFrameInTickContext(final CauseStackManager.StackFrame frame, final EntityTickContext context) {
final ServerPlayer serverPlayer = this.shadow$getLoveCause();
if (serverPlayer != null) {
frame.addContext(EventContextKeys.PLAYER, (Player) serverPlayer);
}
super.tracker$populateFrameInTickContext(frame, context);
}
use of net.minecraft.server.level.ServerPlayer in project SpongeCommon by SpongePowered.
the class AttackEntityPacketState method unwind.
@Override
public void unwind(final BasicPacketContext context) {
if (!TrackingUtil.processBlockCaptures(context)) {
final ServerboundInteractPacket useEntityPacket = context.getPacket();
final ServerPlayer player = context.getPacketPlayer();
final net.minecraft.world.entity.@Nullable Entity entity = useEntityPacket.getTarget(player.level);
if (entity instanceof Entity) {
((Entity) entity).offer(Keys.NOTIFIER, player.getUUID());
}
}
;
}
use of net.minecraft.server.level.ServerPlayer in project SpongeCommon by SpongePowered.
the class AttackEntityPacketState method getSpawnTypeForTransaction.
@Override
public Supplier<SpawnType> getSpawnTypeForTransaction(final BasicPacketContext context, final net.minecraft.world.entity.Entity entityToSpawn) {
final ServerboundInteractPacket useEntityPacket = context.getPacket();
final ServerPlayer player = context.getPacketPlayer();
final net.minecraft.world.entity.@Nullable Entity entity = useEntityPacket.getTarget(player.level);
if (entity != null && (entity.removed || entity instanceof LivingEntity && ((LivingEntity) entity).isDeadOrDying())) {
return entityToSpawn instanceof ExperienceOrb ? SpawnTypes.EXPERIENCE : SpawnTypes.DROPPED_ITEM;
}
if (entityToSpawn instanceof ItemEntity) {
return SpawnTypes.DROPPED_ITEM;
}
return super.getSpawnTypeForTransaction(context, entityToSpawn);
}
use of net.minecraft.server.level.ServerPlayer in project SpongeCommon by SpongePowered.
the class BasicPacketState method foldContextForThread.
@Override
public void foldContextForThread(final BasicPacketContext context, final TickTaskBridge returnValue) {
@Nullable final ServerPlayer source = context.getPacketPlayer();
returnValue.bridge$contextShift((c, f) -> {
if (source != null) {
f.pushCause(source);
}
});
}
Aggregations