Advertisement
drazisil

Untitled

Jan 27th, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.62 KB | None | 0 0
  1. package com.drazisil.creativedimensions.world;
  2.  
  3. import net.minecraft.entity.Entity;
  4. import net.minecraft.entity.player.EntityPlayerMP;
  5. import net.minecraft.network.play.server.SPacketEffect;
  6. import net.minecraft.network.play.server.SPacketEntityEffect;
  7. import net.minecraft.network.play.server.SPacketPlayerAbilities;
  8. import net.minecraft.network.play.server.SPacketRespawn;
  9. import net.minecraft.potion.PotionEffect;
  10. import net.minecraft.util.math.BlockPos;
  11. import net.minecraft.util.math.MathHelper;
  12. import net.minecraft.world.Teleporter;
  13. import net.minecraft.world.WorldServer;
  14.  
  15. import javax.annotation.Nullable;
  16. import java.util.Random;
  17.  
  18. /**
  19.  * Created by drazisil on 2/11/2017.
  20.  */
  21. public class TeleporterCreative extends Teleporter
  22.     {
  23.         private final WorldServer worldServerInstance;
  24.         /** A private Random() function in Teleporter */
  25.         private final Random random;
  26.         // private final Long2ObjectMap<Teleporter.PortalPosition> destinationCoordinateCache = new Long2ObjectOpenHashMap(4096);
  27.  
  28.     public TeleporterCreative(WorldServer worldIn)
  29.         {
  30.             super(worldIn);
  31.             this.worldServerInstance = worldIn;
  32.             this.random = new Random(worldIn.getSeed());
  33.         }
  34.  
  35.         @Nullable
  36.         public static Entity changeDimension(Entity entityIn, int dimensionIn)
  37.         {
  38.             if (!entityIn.worldObj.isRemote && !entityIn.isDead && entityIn instanceof EntityPlayerMP)
  39.             {
  40.                 if (!net.minecraftforge.common.ForgeHooks.onTravelToDimension(entityIn, dimensionIn)) return entityIn;
  41.  
  42.                 if (entityIn.dimension != dimensionIn) {
  43.                     entityIn.dimension = dimensionIn;
  44.                 }
  45.  
  46.                 TeleporterCreative.transferPlayerToDimension((EntityPlayerMP) entityIn, dimensionIn, new TeleporterCreative(((EntityPlayerMP) entityIn).getServerWorld()));
  47.                 ((EntityPlayerMP) entityIn).connection.sendPacket(new SPacketEffect(1032, BlockPos.ORIGIN, 0, false));
  48.                 return entityIn;
  49.             } else {
  50.                 return null;
  51.             }
  52.         }
  53.  
  54.         public static void transferPlayerToDimension(EntityPlayerMP player, int dimensionIn, TeleporterCreative teleporter)
  55.         {
  56.             int i = player.dimension;
  57.             WorldServer worldserver = player.getServer().getPlayerList().getServerInstance().worldServerForDimension(player.dimension);
  58.             player.dimension = dimensionIn;
  59.             WorldServer worldserver1 = player.getServer().getPlayerList().getServerInstance().worldServerForDimension(player.dimension);
  60.             player.connection.sendPacket(new SPacketRespawn(player.dimension, worldserver1.getDifficulty(), worldserver1.getWorldInfo().getTerrainType(), player.interactionManager.getGameType()));
  61.             player.getServer().getPlayerList().updatePermissionLevel(player);
  62.             worldserver.removeEntityDangerously(player);
  63.             player.isDead = false;
  64.             TeleporterCreative.transferEntityToWorld(player, i, worldserver, worldserver1, teleporter);
  65.             player.getServer().getPlayerList().preparePlayer(player, worldserver);
  66.  
  67.             BlockPos safePos = player.getServer().getPlayerList().getServerInstance().worldServerForDimension(dimensionIn).getTopSolidOrLiquidBlock(new BlockPos(player.posX, player.posY, player.posZ));
  68.  
  69.             player.connection.setPlayerLocation(player.posX, safePos.getY(), player.posZ, player.rotationYaw, player.rotationPitch);
  70.             player.interactionManager.setWorld(worldserver1);
  71.             player.connection.sendPacket(new SPacketPlayerAbilities(player.capabilities));
  72.             player.getServer().getPlayerList().updateTimeAndWeatherForPlayer(player, worldserver1);
  73.             player.getServer().getPlayerList().syncPlayerInventory(player);
  74.  
  75.             for (PotionEffect potioneffect : player.getActivePotionEffects())
  76.             {
  77.                 player.connection.sendPacket(new SPacketEntityEffect(player.getEntityId(), potioneffect));
  78.             }
  79.             net.minecraftforge.fml.common.FMLCommonHandler.instance().firePlayerChangedDimensionEvent(player, i, dimensionIn);
  80.         }
  81.  
  82.         public static void transferEntityToWorld(Entity entityIn, int lastDimension, WorldServer oldWorldIn, WorldServer toWorldIn, TeleporterCreative teleporter)
  83.         {
  84.             net.minecraft.world.WorldProvider pOld = oldWorldIn.provider;
  85.             net.minecraft.world.WorldProvider pNew = toWorldIn.provider;
  86.             double moveFactor = pOld.getMovementFactor() / pNew.getMovementFactor();
  87.             double d0 = entityIn.posX * moveFactor;
  88.             double d1 = entityIn.posZ * moveFactor;
  89.             double d2 = 8.0D;
  90.             float f = entityIn.rotationYaw;
  91.             oldWorldIn.theProfiler.startSection("moving");
  92.  
  93.             if (false && entityIn.dimension == -1)
  94.             {
  95.                 d0 = MathHelper.clamp_double(d0 / 8.0D, toWorldIn.getWorldBorder().minX() + 16.0D, toWorldIn.getWorldBorder().maxX() - 16.0D);
  96.                 d1 = MathHelper.clamp_double(d1 / 8.0D, toWorldIn.getWorldBorder().minZ() + 16.0D, toWorldIn.getWorldBorder().maxZ() - 16.0D);
  97.                 entityIn.setLocationAndAngles(d0, entityIn.posY, d1, entityIn.rotationYaw, entityIn.rotationPitch);
  98.  
  99.                 if (entityIn.isEntityAlive())
  100.                 {
  101.                     oldWorldIn.updateEntityWithOptionalForce(entityIn, false);
  102.                 }
  103.             }
  104.             else if (false && entityIn.dimension == 0)
  105.             {
  106.                 d0 = MathHelper.clamp_double(d0 * 8.0D, toWorldIn.getWorldBorder().minX() + 16.0D, toWorldIn.getWorldBorder().maxX() - 16.0D);
  107.                 d1 = MathHelper.clamp_double(d1 * 8.0D, toWorldIn.getWorldBorder().minZ() + 16.0D, toWorldIn.getWorldBorder().maxZ() - 16.0D);
  108.                 entityIn.setLocationAndAngles(d0, entityIn.posY, d1, entityIn.rotationYaw, entityIn.rotationPitch);
  109.  
  110.                 if (entityIn.isEntityAlive())
  111.                 {
  112.                     oldWorldIn.updateEntityWithOptionalForce(entityIn, false);
  113.                 }
  114.             }
  115.  
  116.             if (entityIn.dimension == 1)
  117.             {
  118.                 BlockPos blockpos;
  119.  
  120.                 if (lastDimension == 1)
  121.                 {
  122.                     blockpos = toWorldIn.getSpawnPoint();
  123.                 }
  124.                 else
  125.                 {
  126.                     blockpos = toWorldIn.getSpawnCoordinate();
  127.                 }
  128.  
  129.                 d0 = (double)blockpos.getX();
  130.                 entityIn.posY = (double)blockpos.getY();
  131.                 d1 = (double)blockpos.getZ();
  132.                 entityIn.setLocationAndAngles(d0, entityIn.posY, d1, 90.0F, 0.0F);
  133.  
  134.                 if (entityIn.isEntityAlive())
  135.                 {
  136.                     oldWorldIn.updateEntityWithOptionalForce(entityIn, false);
  137.                 }
  138.             }
  139.  
  140.             oldWorldIn.theProfiler.endSection();
  141.  
  142.             if (lastDimension != 1)
  143.             {
  144.                 oldWorldIn.theProfiler.startSection("placing");
  145.                 d0 = (double)MathHelper.clamp_int((int)d0, -29999872, 29999872);
  146.                 d1 = (double)MathHelper.clamp_int((int)d1, -29999872, 29999872);
  147.  
  148.                 if (entityIn.isEntityAlive())
  149.                 {
  150.                     entityIn.setLocationAndAngles(d0, entityIn.posY, d1, entityIn.rotationYaw, entityIn.rotationPitch);
  151.  
  152.                     toWorldIn.spawnEntityInWorld(entityIn);
  153.                     toWorldIn.updateEntityWithOptionalForce(entityIn, false);
  154.                 }
  155.  
  156.                 oldWorldIn.theProfiler.endSection();
  157.             }
  158.  
  159.             entityIn.setWorld(toWorldIn);
  160.         }
  161.  
  162.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement