use of org.spongepowered.common.interfaces.IMixinPlayerList 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