use of org.spongepowered.common.mixin.core.entity.MixinEntity in project SpongeCommon by SpongePowered.
the class EntityUtil method transferEntityToDimension.
/**
* Called specifically from {@link MixinEntity#changeDimension(int)} to overwrite
* {@link Entity#changeDimension(int)}. This is mostly for debugging
* purposes, but as well as ensuring that the phases are entered and exited correctly.
*
* @param mixinEntity The mixin entity being called
* @param toSuggestedDimension The target dimension id suggested by mods and vanilla alike. The suggested
* dimension id can be erroneous and Vanilla will re-assign the variable to the overworld for
* silly things like entering an end portal while in the end.
* @return The entity, if the teleport was not cancelled or something.
*/
@Nullable
public static Entity transferEntityToDimension(IMixinEntity mixinEntity, int toSuggestedDimension) {
final Entity entity = toNative(mixinEntity);
// handle portal event
MoveEntityEvent.Teleport.Portal event = handleDisplaceEntityPortalEvent(entity, toSuggestedDimension, null);
if (event == null || event.isCancelled()) {
return null;
}
entity.world.profiler.startSection("changeDimension");
// use the world from event
final Transform<World> toTransform = event.getToTransform();
WorldServer toWorld = (WorldServer) toTransform.getExtent();
entity.world.removeEntity(entity);
entity.isDead = false;
entity.world.profiler.startSection("reposition");
final Vector3i toChunkPosition = toTransform.getLocation().getChunkPosition();
toWorld.getChunkProvider().loadChunk(toChunkPosition.getX(), toChunkPosition.getZ());
// Only need to update the entity location here as the portal is handled in SpongeCommonEventFactory
final Vector3d toPosition = toTransform.getPosition();
entity.setLocationAndAngles(toPosition.getX(), toPosition.getY(), toPosition.getZ(), (float) toTransform.getYaw(), (float) toTransform.getPitch());
entity.world = toWorld;
toWorld.spawnEntity(entity);
toWorld.updateEntityWithOptionalForce(entity, false);
entity.world.profiler.endSection();
entity.world.profiler.endSection();
entity.world.profiler.endSection();
return entity;
}
Aggregations