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;
}
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;
}
}
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));
}
Aggregations