pagesbydustin

DimTeleporter.java

Apr 7th, 2019
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.51 KB | None | 0 0
  1. package com.crashdbo.firstmod.util.commands;
  2.  
  3. import javax.annotation.Nonnull;
  4.  
  5. import net.minecraft.entity.Entity;
  6. import net.minecraft.entity.player.EntityPlayer;
  7. import net.minecraft.entity.player.EntityPlayerMP;
  8. import net.minecraft.server.MinecraftServer;
  9. import net.minecraft.util.math.BlockPos;
  10. import net.minecraft.world.Teleporter;
  11. import net.minecraft.world.WorldServer;
  12.  
  13. public class DimTeleporter extends Teleporter{
  14.  
  15.     public DimTeleporter(WorldServer worldIn, double x, double y, double z) {
  16.         super(worldIn);
  17.         this.worldServer = world;
  18.         this.x = x;
  19.         this.y = y;
  20.         this.z = z;
  21.     }
  22.  
  23.     private final WorldServer worldServer;
  24.     private double x;
  25.     private double y;
  26.     private double z;
  27.    
  28.     @Override
  29.     public void placeInPortal(@Nonnull Entity entity, float rotationYaw) {
  30.         // The main purpose of this function is to *not* create a nether portal
  31.         this.worldServer.getBlockState(new BlockPos((int) this.x, (int) this.y, (int) this.z));
  32.  
  33.         entity.setPosition(this.x, this.y, this.z);
  34.         entity.motionX = 0.0f;
  35.         entity.motionY = 0.0f;
  36.         entity.motionZ = 0.0f;
  37.     }
  38.    
  39.     public static void teleportToDimension(EntityPlayer player, int dimension, double x, double y, double z) {
  40.         int oldDimension = player.getEntityWorld().provider.getDimension();
  41.        
  42.         if (player.world.isRemote) {
  43.             EntityPlayerMP entityPlayerMP = (EntityPlayerMP) player; //Game crashes here can't cast EntityPlayerSP to EntityPlayerMP
  44.            
  45.             MinecraftServer server = player.getEntityWorld().getMinecraftServer();
  46.             WorldServer worldServer = server.getWorld(dimension);
  47.             player.addExperienceLevel(0);
  48.  
  49.             if (worldServer == null || worldServer.getMinecraftServer() == null){ //Dimension doesn't exist
  50.                 throw new IllegalArgumentException("Dimension: "+dimension+" doesn't exist!");
  51.             }
  52.  
  53.             worldServer.getMinecraftServer().getPlayerList().transferPlayerToDimension(entityPlayerMP, dimension, new DimTeleporter(worldServer, x, y, z));
  54.             player.setPositionAndUpdate(x, y, z);
  55.             if (oldDimension == 1) {
  56.                 // For some reason teleporting out of the end does weird things. Compensate for that
  57.                 player.setPositionAndUpdate(x, y, z);
  58.                 worldServer.spawnEntity(player);
  59.                 worldServer.updateEntityWithOptionalForce(player, false);
  60.             }
  61.         }
  62.        
  63.        
  64.     }
  65.    
  66. }
Advertisement
Add Comment
Please, Sign In to add comment