In this small tutorial we show you how you can make your own console commands (server side in this case). The command we add here is a simple 'tp' command that allows the player to teleport to any dimension. We will use this command later to test our custom dimension.

So first lets make a TeleportCommand class which will implement the teleportation functionality. This is actually very simple. You just have to implement the methods from CommandBase:

To do the actual teleportation we need a CustomTeleporter class which is implemented like this. This clas extends Teleporter to make sure that teleporting doesn't create a nether portal. The default vanilla implementation of Teleporter does this and for our purposes we don't want that. In addition we also add a 'teleportToDimension' convenience method that we will call from our command above:

publicclassCustomTeleporterextendsTeleporter{publicCustomTeleporter(WorldServerworld,doublex,doubley,doublez){super(world);this.worldServer=world;this.x=x;this.y=y;this.z=z;}privatefinalWorldServerworldServer;privatedoublex;privatedoubley;privatedoublez;@OverridepublicvoidplaceInPortal(@NonnullEntityentity,floatrotationYaw){// The main purpose of this function is to *not* create a nether portalthis.worldServer.getBlockState(newBlockPos((int)this.x,(int)this.y,(int)this.z));entity.setPosition(this.x,this.y,this.z);entity.motionX=0.0f;entity.motionY=0.0f;entity.motionZ=0.0f;}publicstaticvoidteleportToDimension(EntityPlayerplayer,intdimension,doublex,doubley,doublez){intoldDimension=player.worldObj.provider.getDimension();EntityPlayerMPentityPlayerMP=(EntityPlayerMP)player;MinecraftServerserver=((EntityPlayerMP)player).worldObj.getMinecraftServer();WorldServerworldServer=server.worldServerForDimension(dimension);player.addExperienceLevel(0);if(worldServer==null||worldServer.getMinecraftServer()==null){//Dimension doesn't existthrownewIllegalArgumentException("Dimension: "+dimension+" doesn't exist!");}worldServer.getMinecraftServer().getPlayerList().transferPlayerToDimension(entityPlayerMP,dimension,newCustomTeleporter(worldServer,x,y,z));player.setPositionAndUpdate(x,y,z);if(oldDimension==1){// For some reason teleporting out of the end does weird things. Compensate for thatplayer.setPositionAndUpdate(x,y,z);worldServer.spawnEntityInWorld(player);worldServer.updateEntityWithOptionalForce(player,false);}}}

To actually make this command work we need to register it in our main mod class: