Search in sources :

Example 1 with TeleportEntityEvent

use of crazypants.enderio.api.teleport.TeleportEntityEvent in project EnderIO by SleepyTrousers.

the class TeleportUtil method serverTeleport.

public static boolean serverTeleport(@Nonnull Entity entity, @Nonnull BlockPos pos, int targetDim, boolean conserveMotion, @Nonnull TravelSource source) {
    TeleportEntityEvent evt = new TeleportEntityEvent(entity, source, pos, targetDim);
    if (MinecraftForge.EVENT_BUS.post(evt)) {
        return false;
    }
    EntityPlayerMP player = null;
    if (entity instanceof EntityPlayerMP) {
        player = (EntityPlayerMP) entity;
    }
    int x = pos.getX();
    int y = pos.getY();
    int z = pos.getZ();
    int from = entity.dimension;
    if (from != targetDim) {
        MinecraftServer server = FMLCommonHandler.instance().getMinecraftServerInstance();
        WorldServer fromDim = server.getWorld(from);
        WorldServer toDim = server.getWorld(targetDim);
        Teleporter teleporter = new TeleporterEIO(toDim);
        // play sound at the dimension we are leaving for others to hear
        SoundHelper.playSound(server.getWorld(entity.dimension), entity, source.sound, 1.0F, 1.0F);
        if (player != null) {
            server.getPlayerList().transferPlayerToDimension(player, targetDim, teleporter);
            if (from == 1 && entity.isEntityAlive()) {
                // get around vanilla End
                // hacks
                toDim.spawnEntity(entity);
                toDim.updateEntityWithOptionalForce(entity, false);
            }
        } else {
            NBTTagCompound tagCompound = new NBTTagCompound();
            float rotationYaw = entity.rotationYaw;
            float rotationPitch = entity.rotationPitch;
            entity.writeToNBT(tagCompound);
            Class<? extends Entity> entityClass = entity.getClass();
            fromDim.removeEntity(entity);
            try {
                Entity newEntity = entityClass.getConstructor(World.class).newInstance(toDim);
                newEntity.readFromNBT(tagCompound);
                newEntity.setLocationAndAngles(x, y, z, rotationYaw, rotationPitch);
                newEntity.forceSpawn = true;
                toDim.spawnEntity(newEntity);
                // necessary?
                newEntity.forceSpawn = false;
            } catch (Exception e) {
                // Throwables.propagate(e);
                Log.error("serverTeleport: Error creating a entity to be created in new dimension.");
                return false;
            }
        }
    }
    // Force the chunk to load
    if (!entity.world.isBlockLoaded(pos)) {
        entity.world.getChunkFromBlockCoords(pos);
    }
    if (player != null) {
        player.connection.setPlayerLocation(x + 0.5, y + 1.1, z + 0.5, player.rotationYaw, player.rotationPitch);
    } else {
        entity.setPositionAndUpdate(x + 0.5, y + 1.1, z + 0.5);
    }
    entity.fallDistance = 0;
    SoundHelper.playSound(entity.world, entity, source.sound, 1.0F, 1.0F);
    if (player != null) {
        if (conserveMotion) {
            Vector3d velocityVex = Util.getLookVecEio(player);
            SPacketEntityVelocity p = new SPacketEntityVelocity(entity.getEntityId(), velocityVex.x, velocityVex.y, velocityVex.z);
            player.connection.sendPacket(p);
        }
    }
    return true;
}
Also used : Entity(net.minecraft.entity.Entity) TeleportEntityEvent(crazypants.enderio.api.teleport.TeleportEntityEvent) Teleporter(net.minecraft.world.Teleporter) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) WorldServer(net.minecraft.world.WorldServer) World(net.minecraft.world.World) MinecraftServer(net.minecraft.server.MinecraftServer) Vector3d(com.enderio.core.common.vecmath.Vector3d) SPacketEntityVelocity(net.minecraft.network.play.server.SPacketEntityVelocity) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP)

Example 2 with TeleportEntityEvent

use of crazypants.enderio.api.teleport.TeleportEntityEvent in project EnderIO by SleepyTrousers.

the class TravelController method doClientTeleport.

// Note: This is restricted to the current player
public boolean doClientTeleport(@Nonnull Entity entity, @Nonnull EnumHand hand, @Nonnull BlockPos bc, @Nonnull TravelSource source, int powerUse, boolean conserveMomentum) {
    TeleportEntityEvent evt = new TeleportEntityEvent(entity, source, bc, entity.dimension);
    if (MinecraftForge.EVENT_BUS.post(evt)) {
        return false;
    }
    PacketTravelEvent p = new PacketTravelEvent(evt.getTarget(), powerUse, conserveMomentum, source, hand);
    PacketHandler.INSTANCE.sendToServer(p);
    return true;
}
Also used : TeleportEntityEvent(crazypants.enderio.api.teleport.TeleportEntityEvent) PacketTravelEvent(crazypants.enderio.base.teleport.packet.PacketTravelEvent)

Aggregations

TeleportEntityEvent (crazypants.enderio.api.teleport.TeleportEntityEvent)2 Vector3d (com.enderio.core.common.vecmath.Vector3d)1 PacketTravelEvent (crazypants.enderio.base.teleport.packet.PacketTravelEvent)1 Entity (net.minecraft.entity.Entity)1 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)1 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)1 SPacketEntityVelocity (net.minecraft.network.play.server.SPacketEntityVelocity)1 MinecraftServer (net.minecraft.server.MinecraftServer)1 Teleporter (net.minecraft.world.Teleporter)1 World (net.minecraft.world.World)1 WorldServer (net.minecraft.world.WorldServer)1