Search in sources :

Example 1 with MixinEntity

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;
}
Also used : MixinEntity(org.spongepowered.common.mixin.core.entity.MixinEntity) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) Entity(net.minecraft.entity.Entity) Vector3d(com.flowpowered.math.vector.Vector3d) Vector3i(com.flowpowered.math.vector.Vector3i) WorldServer(net.minecraft.world.WorldServer) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) World(org.spongepowered.api.world.World) Nullable(javax.annotation.Nullable)

Aggregations

Vector3d (com.flowpowered.math.vector.Vector3d)1 Vector3i (com.flowpowered.math.vector.Vector3i)1 Nullable (javax.annotation.Nullable)1 Entity (net.minecraft.entity.Entity)1 WorldServer (net.minecraft.world.WorldServer)1 World (org.spongepowered.api.world.World)1 IMixinEntity (org.spongepowered.common.interfaces.entity.IMixinEntity)1 IMixinWorldServer (org.spongepowered.common.interfaces.world.IMixinWorldServer)1 MixinEntity (org.spongepowered.common.mixin.core.entity.MixinEntity)1