From 111f2f9b51258b0b1f2657da61dd24211fb776fe Mon Sep 17 00:00:00 2001
From: Mathijs Riezebos <robijn.bird@gmail.com>
Date: Thu, 19 Jan 2017 04:06:13 +0100
Subject: [PATCH] Restructured Teleportation

Reshaped the teleportation implementation in a way so that it fits my
view on the structure. :P
Removed redundant methods
---
 .../zixiken/dimdoors/blocks/BlockDimDoor.java | 16 ----------
 .../dimdoors/blocks/BlockDimDoorBase.java     | 30 ++++++-------------
 .../dimdoors/blocks/BlockDimDoorGold.java     |  7 -----
 .../dimdoors/blocks/BlockDimDoorPersonal.java |  7 -----
 .../blocks/BlockDimDoorTransient.java         |  6 ----
 .../dimdoors/blocks/BlockDimDoorUnstable.java | 10 +------
 .../dimdoors/blocks/BlockDimDoorWarp.java     |  7 -----
 .../dimdoors/blocks/BlockTransTrapdoor.java   | 13 +-------
 .../com/zixiken/dimdoors/blocks/IDimDoor.java |  9 ------
 .../com/zixiken/dimdoors/shared/Location.java |  4 +--
 .../zixiken/dimdoors/shared/RiftRegistry.java |  5 ++++
 .../tileentities/DDTileEntityBase.java        |  3 ++
 .../tileentities/TileEntityDimDoor.java       | 16 ++++++----
 .../dimdoors/tileentities/TileEntityRift.java |  5 ++++
 .../tileentities/TileEntityTransTrapdoor.java |  7 +++++
 15 files changed, 44 insertions(+), 101 deletions(-)

diff --git a/src/main/java/com/zixiken/dimdoors/blocks/BlockDimDoor.java b/src/main/java/com/zixiken/dimdoors/blocks/BlockDimDoor.java
index abf8728e..4634dccf 100644
--- a/src/main/java/com/zixiken/dimdoors/blocks/BlockDimDoor.java
+++ b/src/main/java/com/zixiken/dimdoors/blocks/BlockDimDoor.java
@@ -1,14 +1,8 @@
 package com.zixiken.dimdoors.blocks;
 
 import com.zixiken.dimdoors.items.ModItems;
-import com.zixiken.dimdoors.shared.Location;
-import com.zixiken.dimdoors.shared.RiftRegistry;
-import com.zixiken.dimdoors.tileentities.DDTileEntityBase;
 import net.minecraft.block.material.Material;
 import net.minecraft.item.Item;
-import net.minecraft.tileentity.TileEntity;
-import net.minecraft.util.math.BlockPos;
-import net.minecraft.world.World;
 
 public class BlockDimDoor extends BlockDimDoorBase {
 
@@ -22,16 +16,6 @@ public class BlockDimDoor extends BlockDimDoorBase {
         setRegistryName(ID);
     }
 
-    @Override
-    public void placeLink(Location location) {
-        TileEntity te = location.getTileEntity();
-
-        if(te != null && te instanceof DDTileEntityBase) {
-            int id = ((DDTileEntityBase) te).riftID;
-            RiftRegistry.Instance.pair(id, id);
-        }
-    }
-
     @Override
     public Item getItemDoor() {
         return ModItems.itemDimDoor;
diff --git a/src/main/java/com/zixiken/dimdoors/blocks/BlockDimDoorBase.java b/src/main/java/com/zixiken/dimdoors/blocks/BlockDimDoorBase.java
index 4ca42083..9466ef16 100644
--- a/src/main/java/com/zixiken/dimdoors/blocks/BlockDimDoorBase.java
+++ b/src/main/java/com/zixiken/dimdoors/blocks/BlockDimDoorBase.java
@@ -3,9 +3,7 @@ package com.zixiken.dimdoors.blocks;
 import java.util.Random;
 
 import com.zixiken.dimdoors.DimDoors;
-import com.zixiken.dimdoors.shared.Location;
 import com.zixiken.dimdoors.shared.RiftRegistry;
-import com.zixiken.dimdoors.shared.TeleportHelper;
 import com.zixiken.dimdoors.tileentities.DDTileEntityBase;
 import com.zixiken.dimdoors.tileentities.TileEntityDimDoor;
 import javax.annotation.Nullable;
@@ -24,7 +22,6 @@ import net.minecraft.util.EnumFacing;
 import net.minecraft.util.EnumHand;
 import net.minecraft.util.math.BlockPos;
 import net.minecraft.util.math.RayTraceResult;
-import net.minecraft.world.Teleporter;
 import net.minecraft.world.World;
 import net.minecraftforge.fml.relauncher.Side;
 import net.minecraftforge.fml.relauncher.SideOnly;
@@ -42,9 +39,8 @@ public abstract class BlockDimDoorBase extends BlockDoor implements IDimDoor, IT
             if (down.getValue(BlockDoor.OPEN)
                     && entity instanceof EntityPlayer
                     && isEntityFacingDoor(down, (EntityLivingBase) entity)) {
-                enterDimDoor(world, pos, entity);
                 this.toggleDoor(world, pos, false);
-                DimDoors.log(BlockDimDoorBase.class, "RiftID = " + getRiftTile(world, pos, world.getBlockState(pos)).riftID + " Derp");
+                enterDimDoor(world, pos, entity);
             }
         } else {
             BlockPos up = pos.up();
@@ -128,26 +124,17 @@ public abstract class BlockDimDoorBase extends BlockDoor implements IDimDoor, IT
 
     @Override
     public TileEntity createNewTileEntity(World world, int metadata) {
-        TileEntityDimDoor dimDoor = new TileEntityDimDoor();
-        updateAttachedTile(world, dimDoor.getPos());
-        placeLink(new Location(world, dimDoor.getPos()));
-
-        return dimDoor;
+        return new TileEntityDimDoor();
     }
 
     @Override
     public void enterDimDoor(World world, BlockPos pos, Entity entity) {
-        TileEntity te = world.getTileEntity(pos);
-
-        if(te != null && te instanceof DDTileEntityBase) {
-            DDTileEntityBase base = (DDTileEntityBase) te;
-            Location loc = RiftRegistry.Instance.getRiftLocation(base.pairedRiftID);
-
-            if(loc != null && loc.getTileEntity() instanceof DDTileEntityBase) {
-                loc = ((DDTileEntityBase) loc.getTileEntity()).getTeleportTarget();
-
-                TeleportHelper.teleport(entity, loc);
-            }
+        DDTileEntityBase riftTile = getRiftTile(world, pos, world.getBlockState(pos));
+        if (riftTile.tryTeleport(entity)) {
+            //player is succesfully teleported
+        } else {
+            //@todo some kind of message that teleporting wasn't successfull
+            //probably should only happen on personal dimdoors
         }
     }
 
@@ -183,6 +170,7 @@ public abstract class BlockDimDoorBase extends BlockDoor implements IDimDoor, IT
         }
     }
 
+    //returns the DDTileEntityBase that is the tile entity belonging to the door block "state" at this "pos" in the "world"
     public DDTileEntityBase getRiftTile(World world, BlockPos pos, IBlockState state) {
         TileEntity tileEntity;
         if (state.getValue(BlockDoor.HALF) == EnumDoorHalf.LOWER) {
diff --git a/src/main/java/com/zixiken/dimdoors/blocks/BlockDimDoorGold.java b/src/main/java/com/zixiken/dimdoors/blocks/BlockDimDoorGold.java
index f33dc5df..8eaaa901 100644
--- a/src/main/java/com/zixiken/dimdoors/blocks/BlockDimDoorGold.java
+++ b/src/main/java/com/zixiken/dimdoors/blocks/BlockDimDoorGold.java
@@ -1,13 +1,10 @@
 package com.zixiken.dimdoors.blocks;
 
 import com.zixiken.dimdoors.items.ModItems;
-import com.zixiken.dimdoors.shared.Location;
 import com.zixiken.dimdoors.tileentities.TileEntityDimDoorGold;
-
 import net.minecraft.block.material.Material;
 import net.minecraft.item.Item;
 import net.minecraft.tileentity.TileEntity;
-import net.minecraft.util.math.BlockPos;
 import net.minecraft.world.World;
 
 public class BlockDimDoorGold extends BlockDimDoorBase {
@@ -21,10 +18,6 @@ public class BlockDimDoorGold extends BlockDimDoorBase {
         setRegistryName(ID);
     }
 
-    @Override
-    public void placeLink(Location location) {
-    }
-
     @Override
     public Item getItemDoor() {
         return ModItems.itemDimDoorGold;
diff --git a/src/main/java/com/zixiken/dimdoors/blocks/BlockDimDoorPersonal.java b/src/main/java/com/zixiken/dimdoors/blocks/BlockDimDoorPersonal.java
index 244fcc5e..c34146bd 100644
--- a/src/main/java/com/zixiken/dimdoors/blocks/BlockDimDoorPersonal.java
+++ b/src/main/java/com/zixiken/dimdoors/blocks/BlockDimDoorPersonal.java
@@ -1,11 +1,8 @@
 package com.zixiken.dimdoors.blocks;
 
 import com.zixiken.dimdoors.items.ModItems;
-import com.zixiken.dimdoors.shared.Location;
 import net.minecraft.block.material.Material;
 import net.minecraft.item.Item;
-import net.minecraft.util.math.BlockPos;
-import net.minecraft.world.World;
 
 public class BlockDimDoorPersonal extends BlockDimDoorBase {
 
@@ -18,10 +15,6 @@ public class BlockDimDoorPersonal extends BlockDimDoorBase {
         setRegistryName(ID);
     }
 
-    @Override
-    public void placeLink(Location location) {
-    }
-
     @Override
     public Item getItemDoor() {
         return ModItems.itemDimDoorPersonal;
diff --git a/src/main/java/com/zixiken/dimdoors/blocks/BlockDimDoorTransient.java b/src/main/java/com/zixiken/dimdoors/blocks/BlockDimDoorTransient.java
index d42c56a2..cc087a34 100644
--- a/src/main/java/com/zixiken/dimdoors/blocks/BlockDimDoorTransient.java
+++ b/src/main/java/com/zixiken/dimdoors/blocks/BlockDimDoorTransient.java
@@ -1,13 +1,11 @@
 package com.zixiken.dimdoors.blocks;
 
-import com.zixiken.dimdoors.shared.Location;
 import net.minecraft.block.material.Material;
 import net.minecraft.block.state.IBlockState;
 import net.minecraft.entity.Entity;
 import net.minecraft.entity.EntityLivingBase;
 import net.minecraft.entity.player.EntityPlayer;
 import net.minecraft.item.Item;
-import net.minecraft.util.EnumBlockRenderType;
 import net.minecraft.util.math.BlockPos;
 import net.minecraft.world.World;
 
@@ -41,10 +39,6 @@ public class BlockDimDoorTransient extends BlockDimDoorBase {
         }
     }
 
-    @Override
-    public void placeLink(Location location) {
-    }
-
     @Override
     public Item getItemDoor() {
         return null;
diff --git a/src/main/java/com/zixiken/dimdoors/blocks/BlockDimDoorUnstable.java b/src/main/java/com/zixiken/dimdoors/blocks/BlockDimDoorUnstable.java
index 523c967d..d712672f 100644
--- a/src/main/java/com/zixiken/dimdoors/blocks/BlockDimDoorUnstable.java
+++ b/src/main/java/com/zixiken/dimdoors/blocks/BlockDimDoorUnstable.java
@@ -1,15 +1,11 @@
 package com.zixiken.dimdoors.blocks;
 
 import com.zixiken.dimdoors.items.ModItems;
-import com.zixiken.dimdoors.shared.Location;
+import java.util.Random;
 import net.minecraft.block.material.Material;
 import net.minecraft.block.state.IBlockState;
 import net.minecraft.init.Items;
 import net.minecraft.item.Item;
-import net.minecraft.util.math.BlockPos;
-import net.minecraft.world.World;
-
-import java.util.Random;
 
 public class BlockDimDoorUnstable extends BlockDimDoorBase {
 
@@ -23,10 +19,6 @@ public class BlockDimDoorUnstable extends BlockDimDoorBase {
         setLightLevel(.0F);
     }
 
-    @Override
-    public void placeLink(Location location) {
-    }
-
     @Override
     public Item getItemDoor() {
         return ModItems.itemDimDoorChaos;
diff --git a/src/main/java/com/zixiken/dimdoors/blocks/BlockDimDoorWarp.java b/src/main/java/com/zixiken/dimdoors/blocks/BlockDimDoorWarp.java
index da7ab04e..36107f84 100644
--- a/src/main/java/com/zixiken/dimdoors/blocks/BlockDimDoorWarp.java
+++ b/src/main/java/com/zixiken/dimdoors/blocks/BlockDimDoorWarp.java
@@ -1,11 +1,8 @@
 package com.zixiken.dimdoors.blocks;
 
 import com.zixiken.dimdoors.items.ModItems;
-import com.zixiken.dimdoors.shared.Location;
 import net.minecraft.block.material.Material;
 import net.minecraft.item.Item;
-import net.minecraft.util.math.BlockPos;
-import net.minecraft.world.World;
 
 public class BlockDimDoorWarp extends BlockDimDoorBase {
 
@@ -18,10 +15,6 @@ public class BlockDimDoorWarp extends BlockDimDoorBase {
         setRegistryName(ID);
     }
 
-    @Override
-    public void placeLink(Location location) {
-    }
-
     @Override
     public Item getItemDoor() {
         return ModItems.itemDimDoorWarp;
diff --git a/src/main/java/com/zixiken/dimdoors/blocks/BlockTransTrapdoor.java b/src/main/java/com/zixiken/dimdoors/blocks/BlockTransTrapdoor.java
index dbce359b..b39d2c6f 100644
--- a/src/main/java/com/zixiken/dimdoors/blocks/BlockTransTrapdoor.java
+++ b/src/main/java/com/zixiken/dimdoors/blocks/BlockTransTrapdoor.java
@@ -3,7 +3,7 @@ package com.zixiken.dimdoors.blocks;
 import java.util.Random;
 
 import com.zixiken.dimdoors.DimDoors;
-import com.zixiken.dimdoors.shared.Location;
+import com.zixiken.dimdoors.tileentities.TileEntityTransTrapdoor;
 import net.minecraft.block.Block;
 import net.minecraft.block.BlockTrapDoor;
 import net.minecraft.block.ITileEntityProvider;
@@ -21,7 +21,6 @@ import net.minecraft.util.EnumHand;
 import net.minecraft.util.math.BlockPos;
 import net.minecraft.util.math.RayTraceResult;
 import net.minecraft.world.World;
-import com.zixiken.dimdoors.tileentities.TileEntityTransTrapdoor;
 import net.minecraftforge.fml.relauncher.Side;
 import net.minecraftforge.fml.relauncher.SideOnly;
 
@@ -76,21 +75,11 @@ public class BlockTransTrapdoor extends BlockTrapDoor implements IDimDoor, ITile
         }
     }
 
-    @Override
-    public void onBlockAdded(World world, BlockPos pos, IBlockState state) {
-        this.placeLink(new Location(world, pos));
-        world.setTileEntity(pos, createNewTileEntity(world, getMetaFromState(state)));
-    }
-
     @Override
     public TileEntity createNewTileEntity(World world, int metadata) {
         return new TileEntityTransTrapdoor();
     }
 
-    @Override
-    public void placeLink(Location location) {
-    }
-
     @Override
     @SideOnly(Side.CLIENT)
     public ItemStack getPickBlock(IBlockState state, RayTraceResult target, World world, BlockPos pos, EntityPlayer player) {
diff --git a/src/main/java/com/zixiken/dimdoors/blocks/IDimDoor.java b/src/main/java/com/zixiken/dimdoors/blocks/IDimDoor.java
index 532606eb..e6510563 100644
--- a/src/main/java/com/zixiken/dimdoors/blocks/IDimDoor.java
+++ b/src/main/java/com/zixiken/dimdoors/blocks/IDimDoor.java
@@ -1,9 +1,7 @@
 package com.zixiken.dimdoors.blocks;
 
-import com.zixiken.dimdoors.shared.Location;
 import net.minecraft.entity.Entity;
 import net.minecraft.item.Item;
-import net.minecraft.tileentity.TileEntity;
 import net.minecraft.util.math.BlockPos;
 import net.minecraft.world.World;
 
@@ -21,13 +19,6 @@ public interface IDimDoor {
      */
     public void enterDimDoor(World world, BlockPos pos, Entity entity);
 
-    /**
-     * called when a door is placed to determine how it will place a link
-     *
-     * @param location
-     */
-    public void placeLink(Location location);
-
     public Item getItemDoor();
 
     /**
diff --git a/src/main/java/com/zixiken/dimdoors/shared/Location.java b/src/main/java/com/zixiken/dimdoors/shared/Location.java
index fee26e83..ee9191f7 100644
--- a/src/main/java/com/zixiken/dimdoors/shared/Location.java
+++ b/src/main/java/com/zixiken/dimdoors/shared/Location.java
@@ -47,14 +47,14 @@ public class Location {
     }
 
     public World getWorld() {
-        return DimensionManager.getWorld(dimensionID); //@todo HOW?
+        return DimensionManager.getWorld(dimensionID);
     }
 
     public int getDimensionID() {
         return dimensionID;
     }
 
-    static Location getLocation(TileEntity tileEntity) {
+    static Location getLocation(TileEntity tileEntity) {//@todo Location is not yet comparable, so a Location begotten by this method, can not be used to find a rift ID in the RiftRegistry
         World world = tileEntity.getWorld();
         int dimID = world.provider.getDimension();
         BlockPos blockPos = tileEntity.getPos();
diff --git a/src/main/java/com/zixiken/dimdoors/shared/RiftRegistry.java b/src/main/java/com/zixiken/dimdoors/shared/RiftRegistry.java
index 21546b94..dc8fff7c 100644
--- a/src/main/java/com/zixiken/dimdoors/shared/RiftRegistry.java
+++ b/src/main/java/com/zixiken/dimdoors/shared/RiftRegistry.java
@@ -9,6 +9,7 @@ import com.zixiken.dimdoors.DimDoors;
 import com.zixiken.dimdoors.tileentities.DDTileEntityBase;
 import java.util.HashMap;
 import java.util.Map;
+import net.minecraft.entity.Entity;
 import net.minecraft.nbt.NBTTagCompound;
 import net.minecraft.tileentity.TileEntity;
 import net.minecraft.world.World;
@@ -118,4 +119,8 @@ public class RiftRegistry {
     public DDTileEntityBase getLastChangedRift() {
         return lastBrokenRift;
     }
+
+    public void teleportEntityToRift(Entity entity, int pairedRiftID) {
+        TeleportHelper.teleport(entity, getRiftLocation(pairedRiftID));
+    }
 }
diff --git a/src/main/java/com/zixiken/dimdoors/tileentities/DDTileEntityBase.java b/src/main/java/com/zixiken/dimdoors/tileentities/DDTileEntityBase.java
index 97490d32..00d8c28c 100644
--- a/src/main/java/com/zixiken/dimdoors/tileentities/DDTileEntityBase.java
+++ b/src/main/java/com/zixiken/dimdoors/tileentities/DDTileEntityBase.java
@@ -4,6 +4,7 @@ import com.zixiken.dimdoors.shared.Location;
 import com.zixiken.dimdoors.shared.RiftRegistry;
 import java.util.Random;
 import net.minecraft.block.state.IBlockState;
+import net.minecraft.entity.Entity;
 import net.minecraft.nbt.NBTTagCompound;
 import net.minecraft.tileentity.TileEntity;
 import net.minecraft.util.math.BlockPos;
@@ -89,4 +90,6 @@ public abstract class DDTileEntityBase extends TileEntity {
     public Location getTeleportTarget() {
         return new Location(this.getWorld().provider.getDimension(), this.getPos());
     }
+
+    public abstract boolean tryTeleport(Entity entity);
 }
diff --git a/src/main/java/com/zixiken/dimdoors/tileentities/TileEntityDimDoor.java b/src/main/java/com/zixiken/dimdoors/tileentities/TileEntityDimDoor.java
index bc8130f4..e2a029eb 100644
--- a/src/main/java/com/zixiken/dimdoors/tileentities/TileEntityDimDoor.java
+++ b/src/main/java/com/zixiken/dimdoors/tileentities/TileEntityDimDoor.java
@@ -1,12 +1,9 @@
 package com.zixiken.dimdoors.tileentities;
 
-import java.util.Random;
-
-import com.zixiken.dimdoors.blocks.BlockDimDoor;
-import com.zixiken.dimdoors.shared.Location;
 import com.zixiken.dimdoors.shared.RiftRegistry;
+import java.util.Random;
+import net.minecraft.entity.Entity;
 import net.minecraft.nbt.NBTTagCompound;
-import net.minecraft.tileentity.TileEntity;
 import net.minecraft.util.EnumFacing;
 
 public class TileEntityDimDoor extends DDTileEntityBase {
@@ -66,4 +63,13 @@ public class TileEntityDimDoor extends DDTileEntityBase {
 
         return new Location(world, pos.offset(facing));
     }*/
+
+    @Override
+    public boolean tryTeleport(Entity entity) {
+        if (!isPaired) {
+            //@todo try to automatically pair this door somehow
+        }
+        RiftRegistry.Instance.teleportEntityToRift(entity, pairedRiftID);
+        return true;
+    }
 }
diff --git a/src/main/java/com/zixiken/dimdoors/tileentities/TileEntityRift.java b/src/main/java/com/zixiken/dimdoors/tileentities/TileEntityRift.java
index 6fab7b95..9ecce440 100644
--- a/src/main/java/com/zixiken/dimdoors/tileentities/TileEntityRift.java
+++ b/src/main/java/com/zixiken/dimdoors/tileentities/TileEntityRift.java
@@ -142,4 +142,9 @@ public class TileEntityRift extends DDTileEntityBase implements ITickable {
     public float[] getRenderColor(Random rand) {
         return null;
     }
+
+    @Override
+    public boolean tryTeleport(Entity entity) {
+        return false; //@todo, rift blade functionality?
+    }
 }
diff --git a/src/main/java/com/zixiken/dimdoors/tileentities/TileEntityTransTrapdoor.java b/src/main/java/com/zixiken/dimdoors/tileentities/TileEntityTransTrapdoor.java
index 0e639d25..82c6f5ea 100644
--- a/src/main/java/com/zixiken/dimdoors/tileentities/TileEntityTransTrapdoor.java
+++ b/src/main/java/com/zixiken/dimdoors/tileentities/TileEntityTransTrapdoor.java
@@ -1,6 +1,7 @@
 package com.zixiken.dimdoors.tileentities;
 
 import java.util.Random;
+import net.minecraft.entity.Entity;
 
 public class TileEntityTransTrapdoor extends DDTileEntityBase {
 
@@ -18,4 +19,10 @@ public class TileEntityTransTrapdoor extends DDTileEntityBase {
         }
         return rgbaColor;
     }
+
+    @Override
+    public boolean tryTeleport(Entity entity) {
+        //@todo teleport the player somewhere to the Overworld?
+        return false;
+    }
 }