use of net.minecraft.world.WorldProviderEnd in project SpongeCommon by SpongePowered.
the class EntityUtil method adjustEntityPostionForTeleport.
public static void adjustEntityPostionForTeleport(IMixinPlayerList playerList, Entity entity, WorldServer fromWorld, WorldServer toWorld) {
fromWorld.profiler.startSection("moving");
WorldProvider pOld = fromWorld.provider;
WorldProvider pNew = toWorld.provider;
double moveFactor = playerList.getMovementFactor(pOld) / playerList.getMovementFactor(pNew);
double x = entity.posX * moveFactor;
double y = entity.posY;
double z = entity.posZ * moveFactor;
if (pNew instanceof WorldProviderEnd) {
BlockPos blockpos;
if (pOld instanceof WorldProviderEnd) {
blockpos = toWorld.getSpawnPoint();
} else {
blockpos = toWorld.getSpawnCoordinate();
}
x = blockpos.getX();
y = blockpos.getY();
z = blockpos.getZ();
entity.setLocationAndAngles(x, y, z, 90.0F, 0.0F);
}
if (!(pOld instanceof WorldProviderEnd)) {
fromWorld.profiler.startSection("placing");
x = MathHelper.clamp((int) x, -29999872, 29999872);
z = MathHelper.clamp((int) z, -29999872, 29999872);
if (entity.isEntityAlive()) {
entity.setLocationAndAngles(x, y, z, entity.rotationYaw, entity.rotationPitch);
}
fromWorld.profiler.endSection();
}
if (entity.isEntityAlive()) {
fromWorld.updateEntityWithOptionalForce(entity, false);
}
fromWorld.profiler.endSection();
}
use of net.minecraft.world.WorldProviderEnd in project SpongeCommon by SpongePowered.
the class EntityUtil method teleportPlayerToDimension.
/**
* A relative copy paste of {@link EntityPlayerMP#changeDimension(int)} where instead we direct all processing
* to the appropriate areas for throwing events and capturing world changes during the transfer.
*
* @param mixinEntityPlayerMP The player being teleported
* @param suggestedDimensionId The suggested dimension
* @return The player object, not re-created
*/
@Nullable
public static Entity teleportPlayerToDimension(EntityPlayerMP entityPlayerMP, int suggestedDimensionId) {
// Fire teleport event here to support Forge's EntityTravelDimensionEvent
// This also prevents sending client wrong data if event is cancelled
WorldServer toWorld = SpongeImpl.getServer().getWorld(suggestedDimensionId);
MoveEntityEvent.Teleport.Portal event = EntityUtil.handleDisplaceEntityPortalEvent(entityPlayerMP, suggestedDimensionId, toWorld.getDefaultTeleporter());
if (event == null || event.isCancelled()) {
return entityPlayerMP;
}
entityPlayerMP.invulnerableDimensionChange = true;
boolean sameDimension = entityPlayerMP.dimension == suggestedDimensionId;
// If leaving The End via End's Portal
// Sponge Start - Check the provider, not the world's dimension
final WorldServer fromWorldServer = (WorldServer) event.getFromTransform().getExtent();
if (fromWorldServer.provider instanceof WorldProviderEnd && suggestedDimensionId == 1) {
// if (this.dimension == 1 && dimensionIn == 1)
// Sponge End
fromWorldServer.removeEntity(entityPlayerMP);
if (!entityPlayerMP.queuedEndExit) {
entityPlayerMP.queuedEndExit = true;
entityPlayerMP.connection.sendPacket(new SPacketChangeGameState(4, entityPlayerMP.seenCredits ? 0.0F : 1.0F));
entityPlayerMP.seenCredits = true;
}
return entityPlayerMP;
}
// else { // Sponge - Remove unecessary
final WorldServer toWorldServer = (WorldServer) event.getToTransform().getExtent();
// not being loaded then short-circuit to prevent unnecessary logic from running
if (!sameDimension && fromWorldServer == toWorldServer) {
return entityPlayerMP;
}
transferPlayerToDimension(event, entityPlayerMP);
entityPlayerMP.connection.sendPacket(new SPacketEffect(1032, BlockPos.ORIGIN, 0, false));
// Sponge End
return entityPlayerMP;
}
use of net.minecraft.world.WorldProviderEnd in project SpongeCommon by SpongePowered.
the class MixinPlayerList method prepareEntityForPortal.
// copy of transferEntityToWorld but only contains code to apply the location on entity before being placed into a portal
@Override
public void prepareEntityForPortal(Entity entityIn, WorldServer oldWorldIn, WorldServer toWorldIn) {
oldWorldIn.profiler.startSection("moving");
WorldProvider pOld = oldWorldIn.provider;
WorldProvider pNew = toWorldIn.provider;
double moveFactor = getMovementFactor(pOld) / getMovementFactor(pNew);
double x = entityIn.posX * moveFactor;
double y = entityIn.posY;
double z = entityIn.posZ * moveFactor;
if (pNew instanceof WorldProviderEnd) {
BlockPos blockpos;
if (pOld instanceof WorldProviderEnd) {
blockpos = toWorldIn.getSpawnPoint();
} else {
blockpos = toWorldIn.getSpawnCoordinate();
}
x = blockpos.getX();
y = blockpos.getY();
z = blockpos.getZ();
entityIn.setLocationAndAngles(x, y, z, 90.0F, 0.0F);
}
if (!(pOld instanceof WorldProviderEnd)) {
oldWorldIn.profiler.startSection("placing");
x = MathHelper.clamp((int) x, -29999872, 29999872);
z = MathHelper.clamp((int) z, -29999872, 29999872);
if (entityIn.isEntityAlive()) {
entityIn.setLocationAndAngles(x, y, z, entityIn.rotationYaw, entityIn.rotationPitch);
}
oldWorldIn.profiler.endSection();
}
if (entityIn.isEntityAlive()) {
oldWorldIn.updateEntityWithOptionalForce(entityIn, false);
}
oldWorldIn.profiler.endSection();
}
use of net.minecraft.world.WorldProviderEnd in project SpongeCommon by SpongePowered.
the class EntityUtil method handleDisplaceEntityPortalEvent.
@Nullable
public static MoveEntityEvent.Teleport.Portal handleDisplaceEntityPortalEvent(Entity entityIn, int targetDimensionId, @Nullable Teleporter teleporter) {
SpongeImplHooks.registerPortalAgentType(teleporter);
final MinecraftServer mcServer = SpongeImpl.getServer();
final IMixinPlayerList mixinPlayerList = (IMixinPlayerList) mcServer.getPlayerList();
final IMixinEntity mixinEntity = (IMixinEntity) entityIn;
final Transform<World> fromTransform = mixinEntity.getTransform();
final WorldServer fromWorld = ((WorldServer) entityIn.world);
final IMixinWorldServer fromMixinWorld = (IMixinWorldServer) fromWorld;
boolean sameDimension = entityIn.dimension == targetDimensionId;
// handle the end
if (targetDimensionId == 1 && fromWorld.provider instanceof WorldProviderEnd) {
targetDimensionId = 0;
}
WorldServer toWorld = mcServer.getWorld(targetDimensionId);
// not being loaded then short-circuit to prevent unnecessary logic from running
if (!sameDimension && fromWorld == toWorld) {
return null;
}
if (teleporter == null) {
teleporter = toWorld.getDefaultTeleporter();
}
final Map<String, String> portalAgents = fromMixinWorld.getActiveConfig().getConfig().getWorld().getPortalAgents();
String worldName = "";
String teleporterClassName = teleporter.getClass().getName();
// check for new destination in config
if (teleporterClassName.equals("net.minecraft.world.Teleporter")) {
worldName = portalAgents.get("minecraft:default_" + toWorld.provider.getDimensionType().getName().toLowerCase(Locale.ENGLISH));
if (worldName == null && toWorld.provider instanceof WorldProviderHell) {
worldName = portalAgents.get("minecraft:default_nether");
}
} else {
// custom
worldName = portalAgents.get("minecraft:" + teleporter.getClass().getSimpleName());
}
if (worldName != null && !worldName.equals("")) {
for (WorldProperties worldProperties : Sponge.getServer().getAllWorldProperties()) {
if (worldProperties.getWorldName().equalsIgnoreCase(worldName)) {
Optional<World> spongeWorld = Sponge.getServer().loadWorld(worldProperties);
if (spongeWorld.isPresent()) {
toWorld = (WorldServer) spongeWorld.get();
teleporter = toWorld.getDefaultTeleporter();
if (fromWorld.provider.isNether() || toWorld.provider.isNether()) {
((IMixinTeleporter) teleporter).setNetherPortalType(true);
} else {
((IMixinTeleporter) teleporter).setNetherPortalType(false);
}
}
}
}
}
adjustEntityPostionForTeleport(mixinPlayerList, entityIn, fromWorld, toWorld);
try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame();
TeleportingContext context = EntityPhase.State.CHANGING_DIMENSION.createPhaseContext().setTargetWorld(toWorld).buildAndSwitch()) {
Sponge.getCauseStackManager().pushCause(teleporter);
Sponge.getCauseStackManager().pushCause(mixinEntity);
Sponge.getCauseStackManager().addContext(EventContextKeys.TELEPORT_TYPE, TeleportTypes.PORTAL);
if (entityIn.isEntityAlive() && !(fromWorld.provider instanceof WorldProviderEnd)) {
fromWorld.profiler.startSection("placing");
// Note: We must always use placeInPortal to support mods.
if (!((IMixinTeleporter) teleporter).isVanilla() || entityIn.getLastPortalVec() != null) {
teleporter.placeInPortal(entityIn, entityIn.rotationYaw);
}
fromWorld.profiler.endSection();
}
// Complete phases, just because we need to. The phases don't actually do anything, because the processing resides here.
// Grab the exit location of entity after being placed into portal
final Transform<World> portalExitTransform = mixinEntity.getTransform().setExtent((World) toWorld);
// Use setLocationAndAngles to avoid firing MoveEntityEvent to plugins
mixinEntity.setLocationAndAngles(fromTransform);
final MoveEntityEvent.Teleport.Portal event = SpongeEventFactory.createMoveEntityEventTeleportPortal(Sponge.getCauseStackManager().getCurrentCause(), fromTransform, portalExitTransform, (PortalAgent) teleporter, mixinEntity, true);
SpongeImpl.postEvent(event);
final Vector3i chunkPosition = mixinEntity.getLocation().getChunkPosition();
final IMixinTeleporter toMixinTeleporter = (IMixinTeleporter) teleporter;
final List<BlockSnapshot> capturedBlocks = context.getCapturedBlocks();
final Transform<World> toTransform = event.getToTransform();
if (event.isCancelled()) {
// We need to make sure to only restore the location if
if (!portalExitTransform.getExtent().getUniqueId().equals(mixinEntity.getLocation().getExtent().getUniqueId())) {
// update cache
((IMixinTeleporter) teleporter).removePortalPositionFromCache(ChunkPos.asLong(chunkPosition.getX(), chunkPosition.getZ()));
if (!capturedBlocks.isEmpty()) {
for (BlockSnapshot original : Lists.reverse(capturedBlocks)) {
original.restore(true, BlockChangeFlags.NONE);
}
capturedBlocks.clear();
}
mixinEntity.setLocationAndAngles(fromTransform);
} else {
// Call setTransform to let plugins know mods changed the position
// Guarantees plugins such as Nucleus can track changed locations properly
mixinEntity.setTransform(mixinEntity.getTransform());
}
return event;
}
if (!portalExitTransform.equals(toTransform)) {
// if plugin set to same world, just set the transform
if (fromWorld == toTransform.getExtent()) {
// force cancel so we know to skip remaining logic
event.setCancelled(true);
// update cache
toMixinTeleporter.removePortalPositionFromCache(ChunkPos.asLong(chunkPosition.getX(), chunkPosition.getZ()));
// Undo created portal
if (!capturedBlocks.isEmpty()) {
for (BlockSnapshot original : Lists.reverse(capturedBlocks)) {
original.restore(true, BlockChangeFlags.NONE);
}
}
capturedBlocks.clear();
mixinEntity.setLocationAndAngles(toTransform);
return event;
}
} else {
if (toWorld.provider instanceof WorldProviderEnd) {
BlockPos blockpos = entityIn.world.getTopSolidOrLiquidBlock(toWorld.getSpawnPoint());
entityIn.moveToBlockPosAndAngles(blockpos, entityIn.rotationYaw, entityIn.rotationPitch);
}
}
if (!capturedBlocks.isEmpty() && !TrackingUtil.processBlockCaptures(capturedBlocks, EntityPhase.State.CHANGING_DIMENSION, context)) {
toMixinTeleporter.removePortalPositionFromCache(ChunkPos.asLong(chunkPosition.getX(), chunkPosition.getZ()));
}
if (!event.getKeepsVelocity()) {
entityIn.motionX = 0;
entityIn.motionY = 0;
entityIn.motionZ = 0;
}
return event;
}
}
Aggregations