Search in sources :

Example 1 with IMixinNetHandlerPlayServer

use of org.spongepowered.common.interfaces.network.IMixinNetHandlerPlayServer in project SpongeCommon by SpongePowered.

the class EntityUtil method changeWorld.

public static boolean changeWorld(net.minecraft.entity.Entity entity, Location<World> location, int currentDim, int targetDim) {
    final MinecraftServer mcServer = SpongeImpl.getServer();
    final WorldServer fromWorld = mcServer.getWorld(currentDim);
    final WorldServer toWorld = mcServer.getWorld(targetDim);
    if (entity instanceof EntityPlayer) {
        fromWorld.getEntityTracker().removePlayerFromTrackers((EntityPlayerMP) entity);
        fromWorld.getPlayerChunkMap().removePlayer((EntityPlayerMP) entity);
        mcServer.getPlayerList().getPlayers().remove(entity);
    } else {
        fromWorld.getEntityTracker().untrack(entity);
    }
    entity.dismountRidingEntity();
    entity.world.removeEntityDangerously(entity);
    entity.isDead = false;
    entity.dimension = targetDim;
    entity.setPositionAndRotation(location.getX(), location.getY(), location.getZ(), 0, 0);
    while (!toWorld.getCollisionBoxes(entity, entity.getEntityBoundingBox()).isEmpty() && entity.posY < 256.0D) {
        entity.setPosition(entity.posX, entity.posY + 1.0D, entity.posZ);
    }
    toWorld.getChunkProvider().loadChunk((int) entity.posX >> 4, (int) entity.posZ >> 4);
    if (entity instanceof EntityPlayerMP && ((EntityPlayerMP) entity).connection != null) {
        EntityPlayerMP entityPlayerMP = (EntityPlayerMP) entity;
        // Support vanilla clients going into custom dimensions
        final int toDimensionId = WorldManager.getClientDimensionId(entityPlayerMP, toWorld);
        if (((IMixinEntityPlayerMP) entityPlayerMP).usesCustomClient()) {
            WorldManager.sendDimensionRegistration(entityPlayerMP, toWorld.provider);
        } else {
            // Force vanilla client to refresh its chunk cache if same dimension type
            if (fromWorld != toWorld && fromWorld.provider.getDimensionType() == toWorld.provider.getDimensionType()) {
                entityPlayerMP.connection.sendPacket(new SPacketRespawn(toDimensionId >= 0 ? -1 : 0, toWorld.getDifficulty(), toWorld.getWorldInfo().getTerrainType(), entityPlayerMP.interactionManager.getGameType()));
            }
        }
        // Prevent 'lastMoveLocation' from being set to the previous world.
        ((IMixinNetHandlerPlayServer) entityPlayerMP.connection).setLastMoveLocation(null);
        entityPlayerMP.connection.sendPacket(new SPacketRespawn(toDimensionId, toWorld.getDifficulty(), toWorld.getWorldInfo().getTerrainType(), entityPlayerMP.interactionManager.getGameType()));
        entity.setWorld(toWorld);
        entityPlayerMP.connection.setPlayerLocation(entityPlayerMP.posX, entityPlayerMP.posY, entityPlayerMP.posZ, entityPlayerMP.rotationYaw, entityPlayerMP.rotationPitch);
        entityPlayerMP.setSneaking(false);
        mcServer.getPlayerList().updateTimeAndWeatherForPlayer(entityPlayerMP, toWorld);
        toWorld.getPlayerChunkMap().addPlayer(entityPlayerMP);
        toWorld.spawnEntity(entityPlayerMP);
        mcServer.getPlayerList().getPlayers().add(entityPlayerMP);
        entityPlayerMP.interactionManager.setWorld(toWorld);
        entityPlayerMP.addSelfToInternalCraftingInventory();
        ((IMixinEntityPlayerMP) entityPlayerMP).refreshXpHealthAndFood();
        for (Object effect : entityPlayerMP.getActivePotionEffects()) {
            entityPlayerMP.connection.sendPacket(new SPacketEntityEffect(entityPlayerMP.getEntityId(), (PotionEffect) effect));
        }
        entityPlayerMP.sendPlayerAbilities();
    } else {
        entity.setWorld(toWorld);
        toWorld.spawnEntity(entity);
    }
    fromWorld.resetUpdateEntityTick();
    toWorld.resetUpdateEntityTick();
    return true;
}
Also used : PotionEffect(net.minecraft.potion.PotionEffect) IMixinEntityPlayer(org.spongepowered.common.interfaces.entity.player.IMixinEntityPlayer) EntityPlayer(net.minecraft.entity.player.EntityPlayer) WorldServer(net.minecraft.world.WorldServer) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) IMixinEntityPlayerMP(org.spongepowered.common.interfaces.entity.player.IMixinEntityPlayerMP) IMixinEntityPlayerMP(org.spongepowered.common.interfaces.entity.player.IMixinEntityPlayerMP) SPacketRespawn(net.minecraft.network.play.server.SPacketRespawn) SPacketEntityEffect(net.minecraft.network.play.server.SPacketEntityEffect) IMixinNetHandlerPlayServer(org.spongepowered.common.interfaces.network.IMixinNetHandlerPlayServer) MinecraftServer(net.minecraft.server.MinecraftServer)

Example 2 with IMixinNetHandlerPlayServer

use of org.spongepowered.common.interfaces.network.IMixinNetHandlerPlayServer in project SpongeCommon by SpongePowered.

the class MixinEntity method setLocation.

@Override
public boolean setLocation(Location<World> location) {
    checkNotNull(location, "The location was null!");
    if (isRemoved()) {
        return false;
    }
    try (final BasicPluginContext context = PluginPhase.State.TELEPORT.createPhaseContext().buildAndSwitch()) {
        // TODO Add a 'Move' plugin phase or just keep it under Teleport?
        try (final CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
            if (!Sponge.getCauseStackManager().getCurrentContext().containsKey(EventContextKeys.TELEPORT_TYPE)) {
                Sponge.getCauseStackManager().addContext(EventContextKeys.TELEPORT_TYPE, TeleportTypes.PLUGIN);
            }
            // TODO These methods need a Cause (maybe wait till Cause PR)
            // TODO Need to not fire Teleport in all cases (especially when movement is local)
            MoveEntityEvent.Teleport event = EntityUtil.handleDisplaceEntityTeleportEvent((net.minecraft.entity.Entity) (Object) this, location);
            if (event.isCancelled()) {
                return false;
            }
            location = event.getToTransform().getLocation();
            this.rotationPitch = (float) event.getToTransform().getPitch();
            this.rotationYaw = (float) event.getToTransform().getYaw();
        }
        final IMixinChunkProviderServer chunkProviderServer = (IMixinChunkProviderServer) ((WorldServer) this.world).getChunkProvider();
        chunkProviderServer.setForceChunkRequests(true);
        final net.minecraft.entity.Entity thisEntity = (net.minecraft.entity.Entity) (Object) this;
        final List<net.minecraft.entity.Entity> passengers = thisEntity.getPassengers();
        boolean isTeleporting = true;
        if (location.getExtent().getUniqueId() != ((World) this.world).getUniqueId()) {
            final net.minecraft.world.World nmsWorld = (net.minecraft.world.World) location.getExtent();
            if ((net.minecraft.entity.Entity) (Object) this instanceof EntityPlayerMP) {
                // Close open containers
                final EntityPlayerMP entityPlayerMP = (EntityPlayerMP) (Object) this;
                if (entityPlayerMP.openContainer != entityPlayerMP.inventoryContainer) {
                    // Call API method to make sure we capture it
                    ((Player) entityPlayerMP).closeInventory();
                }
            }
            EntityUtil.changeWorld((net.minecraft.entity.Entity) (Object) this, location, ((IMixinWorldServer) this.world).getDimensionId(), ((IMixinWorldServer) nmsWorld).getDimensionId());
        } else {
            double distance = location.getPosition().distance(this.getPosition());
            if (distance <= 4) {
                isTeleporting = false;
            }
            if (thisEntity instanceof EntityPlayerMP && ((EntityPlayerMP) thisEntity).connection != null) {
                final EntityPlayerMP entityPlayerMP = (EntityPlayerMP) thisEntity;
                // No reason to attempt to load chunks unless we're teleporting
                if (isTeleporting) {
                    // Close open containers
                    if (entityPlayerMP.openContainer != entityPlayerMP.inventoryContainer) {
                        // Call API method to make sure we capture it
                        ((Player) entityPlayerMP).closeInventory();
                    }
                    ((WorldServer) location.getExtent()).getChunkProvider().loadChunk(location.getChunkPosition().getX(), location.getChunkPosition().getZ());
                }
                entityPlayerMP.connection.setPlayerLocation(location.getX(), location.getY(), location.getZ(), thisEntity.rotationYaw, thisEntity.rotationPitch);
                // Set last move to teleport target
                ((IMixinNetHandlerPlayServer) entityPlayerMP.connection).setLastMoveLocation(null);
            } else {
                setPosition(location.getPosition().getX(), location.getPosition().getY(), location.getPosition().getZ());
            }
        }
        if (isTeleporting) {
            // Re-attach passengers
            for (net.minecraft.entity.Entity passenger : passengers) {
                passenger.startRiding(thisEntity, true);
            }
        }
        chunkProviderServer.setForceChunkRequests(false);
        return true;
    }
}
Also used : Entity(org.spongepowered.api.entity.Entity) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) Player(org.spongepowered.api.entity.living.player.Player) MoveEntityEvent(org.spongepowered.api.event.entity.MoveEntityEvent) World(org.spongepowered.api.world.World) IMixinChunkProviderServer(org.spongepowered.common.interfaces.world.gen.IMixinChunkProviderServer) BasicPluginContext(org.spongepowered.common.event.tracking.phase.plugin.BasicPluginContext) CauseStackManager(org.spongepowered.api.event.CauseStackManager) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) IMixinNetHandlerPlayServer(org.spongepowered.common.interfaces.network.IMixinNetHandlerPlayServer)

Example 3 with IMixinNetHandlerPlayServer

use of org.spongepowered.common.interfaces.network.IMixinNetHandlerPlayServer in project SpongeCommon by SpongePowered.

the class ResourcePackState method unwind.

@Override
public void unwind(BasicPacketContext phaseContext) {
    final EntityPlayerMP player = phaseContext.getPacketPlayer();
    final NetHandlerPlayServer connection = player.connection;
    final IMixinNetHandlerPlayServer mixinHandler = (IMixinNetHandlerPlayServer) connection;
    final CPacketResourcePackStatus resource = phaseContext.getPacket();
    final ResourcePackStatusEvent.ResourcePackStatus status;
    ResourcePack pack;
    switch(resource.action) {
        case ACCEPTED:
            pack = mixinHandler.popReceivedResourcePack(true);
            status = ResourcePackStatusEvent.ResourcePackStatus.ACCEPTED;
            break;
        case DECLINED:
            pack = mixinHandler.popReceivedResourcePack(false);
            status = ResourcePackStatusEvent.ResourcePackStatus.DECLINED;
            break;
        case SUCCESSFULLY_LOADED:
            pack = mixinHandler.popAcceptedResourcePack();
            status = ResourcePackStatusEvent.ResourcePackStatus.SUCCESSFULLY_LOADED;
            break;
        case FAILED_DOWNLOAD:
            pack = mixinHandler.popAcceptedResourcePack();
            status = ResourcePackStatusEvent.ResourcePackStatus.FAILED;
            break;
        default:
            throw new AssertionError();
    }
    if (pack == null) {
        return;
    }
    SpongeImpl.postEvent(SpongeEventFactory.createResourcePackStatusEvent(Sponge.getCauseStackManager().getCurrentCause(), pack, (Player) player, status));
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) ResourcePack(org.spongepowered.api.resourcepack.ResourcePack) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) CPacketResourcePackStatus(net.minecraft.network.play.client.CPacketResourcePackStatus) IMixinNetHandlerPlayServer(org.spongepowered.common.interfaces.network.IMixinNetHandlerPlayServer) IMixinNetHandlerPlayServer(org.spongepowered.common.interfaces.network.IMixinNetHandlerPlayServer) NetHandlerPlayServer(net.minecraft.network.NetHandlerPlayServer) ResourcePackStatusEvent(org.spongepowered.api.event.entity.living.humanoid.player.ResourcePackStatusEvent)

Aggregations

EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)3 IMixinNetHandlerPlayServer (org.spongepowered.common.interfaces.network.IMixinNetHandlerPlayServer)3 Player (org.spongepowered.api.entity.living.player.Player)2 EntityPlayer (net.minecraft.entity.player.EntityPlayer)1 NetHandlerPlayServer (net.minecraft.network.NetHandlerPlayServer)1 CPacketResourcePackStatus (net.minecraft.network.play.client.CPacketResourcePackStatus)1 SPacketEntityEffect (net.minecraft.network.play.server.SPacketEntityEffect)1 SPacketRespawn (net.minecraft.network.play.server.SPacketRespawn)1 PotionEffect (net.minecraft.potion.PotionEffect)1 MinecraftServer (net.minecraft.server.MinecraftServer)1 WorldServer (net.minecraft.world.WorldServer)1 Entity (org.spongepowered.api.entity.Entity)1 CauseStackManager (org.spongepowered.api.event.CauseStackManager)1 MoveEntityEvent (org.spongepowered.api.event.entity.MoveEntityEvent)1 ResourcePackStatusEvent (org.spongepowered.api.event.entity.living.humanoid.player.ResourcePackStatusEvent)1 ResourcePack (org.spongepowered.api.resourcepack.ResourcePack)1 World (org.spongepowered.api.world.World)1 BasicPluginContext (org.spongepowered.common.event.tracking.phase.plugin.BasicPluginContext)1 IMixinEntity (org.spongepowered.common.interfaces.entity.IMixinEntity)1 IMixinEntityPlayer (org.spongepowered.common.interfaces.entity.player.IMixinEntityPlayer)1