From a9fb3f5445ffec6673080614f10666501d5ad3d0 Mon Sep 17 00:00:00 2001
From: Mathijs Riezebos <robijn.bird@gmail.com>
Date: Thu, 19 Jan 2017 22:34:46 +0100
Subject: [PATCH] Moar Teleporting fixing

Some renaming
And moving the storage of a DimDoor's orientation over from the
BlockDoorBase to the TileEntityDimDoor
---
 .../com/zixiken/dimdoors/DDProxyCommon.java   |  3 +--
 .../dimdoors/client/RenderDimDoor.java        |  2 +-
 .../zixiken/dimdoors/items/ItemDoorBase.java  | 10 ++++++--
 .../com/zixiken/dimdoors/shared/Location.java |  2 +-
 .../zixiken/dimdoors/shared/RiftRegistry.java |  3 ++-
 .../dimdoors/shared/TeleportHelper.java       | 24 ++++++++-----------
 .../tileentities/DDTileEntityBase.java        |  1 -
 .../tileentities/TileEntityDimDoor.java       | 19 +++++++--------
 8 files changed, 31 insertions(+), 33 deletions(-)

diff --git a/src/main/java/com/zixiken/dimdoors/DDProxyCommon.java b/src/main/java/com/zixiken/dimdoors/DDProxyCommon.java
index 725bebd4..2714a980 100644
--- a/src/main/java/com/zixiken/dimdoors/DDProxyCommon.java
+++ b/src/main/java/com/zixiken/dimdoors/DDProxyCommon.java
@@ -13,7 +13,6 @@ import net.minecraft.block.state.IBlockState;
 import net.minecraft.tileentity.TileEntity;
 import net.minecraft.util.math.BlockPos;
 import net.minecraft.world.World;
-import net.minecraftforge.common.MinecraftForge;
 import net.minecraftforge.fml.common.event.FMLInitializationEvent;
 import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
 import net.minecraftforge.fml.common.registry.GameRegistry;
@@ -48,7 +47,7 @@ public abstract class DDProxyCommon implements IDDProxy {
             dimTile.orientation = state.getBlock() instanceof BlockDimDoorBase
                     ? state.getValue(BlockDoor.FACING).rotateY()
                     : ModBlocks.blockDimDoor.getDefaultState().getValue(BlockDoor.FACING);
-            dimTile.openOrClosed = door.isDoorOnRift(world, pos) && door.isUpperDoorBlock(world.getBlockState(pos));
+            dimTile.doorIsOpen = door.isDoorOnRift(world, pos) && door.isUpperDoorBlock(world.getBlockState(pos));
             dimTile.lockStatus = 0;
         }
     }
diff --git a/src/main/java/com/zixiken/dimdoors/client/RenderDimDoor.java b/src/main/java/com/zixiken/dimdoors/client/RenderDimDoor.java
index 9525c0b2..8061f106 100644
--- a/src/main/java/com/zixiken/dimdoors/client/RenderDimDoor.java
+++ b/src/main/java/com/zixiken/dimdoors/client/RenderDimDoor.java
@@ -264,7 +264,7 @@ public class RenderDimDoor extends TileEntitySpecialRenderer<TileEntityDimDoor>
         World world = te.getWorld();
         BlockPos pos = te.getPos();
         ((BlockDimDoorBase) world.getBlockState(pos).getBlock()).updateAttachedTile(world, pos);
-        if (te.openOrClosed) {
+        if (te.doorIsOpen) {
             renderDimDoorTileEntity(te, x, y, z);
             if (te.lockStatus >= 1) {
                 for (int i = 0; i < 1 + te.lockStatus; i++) {
diff --git a/src/main/java/com/zixiken/dimdoors/items/ItemDoorBase.java b/src/main/java/com/zixiken/dimdoors/items/ItemDoorBase.java
index e8d74b1a..18287267 100644
--- a/src/main/java/com/zixiken/dimdoors/items/ItemDoorBase.java
+++ b/src/main/java/com/zixiken/dimdoors/items/ItemDoorBase.java
@@ -8,6 +8,7 @@ import com.zixiken.dimdoors.blocks.BlockDimDoor;
 import com.zixiken.dimdoors.blocks.BlockDimDoorBase;
 import com.zixiken.dimdoors.blocks.ModBlocks;
 import com.zixiken.dimdoors.tileentities.DDTileEntityBase;
+import com.zixiken.dimdoors.tileentities.TileEntityDimDoor;
 import net.minecraft.block.Block;
 import net.minecraft.block.state.IBlockState;
 import net.minecraft.entity.player.EntityPlayer;
@@ -135,8 +136,13 @@ public abstract class ItemDoorBase extends ItemDoor {
                 }
                 DimDoors.log(this.getClass(), "New Door rift-ID after placement: " + newTileEntityDimDoor.getRiftID());
                 DimDoors.log(this.getClass(), "Facing direction of Door-block at pos of this Rift tile entity is: "
-                + newTileEntityDimDoor.getWorld().getBlockState(newTileEntityDimDoor.getPos()).getValue(BlockDimDoor.FACING));
-
+                        + newTileEntityDimDoor.getWorld().getBlockState(newTileEntityDimDoor.getPos()).getValue(BlockDimDoor.FACING));
+                if (newTileEntityDimDoor instanceof TileEntityDimDoor) {
+                    TileEntityDimDoor tileEntityDimDoor = (TileEntityDimDoor) newTileEntityDimDoor;
+                    tileEntityDimDoor.orientation
+                            = newTileEntityDimDoor.getWorld().getBlockState(newTileEntityDimDoor.getPos()).getValue(BlockDimDoor.FACING).getOpposite();
+                    //storing the orientation inside the tile-entity, because that thing can actually save the orientation in the worldsave, unlike the block itself, which fucks up somehow
+                }
                 return EnumActionResult.SUCCESS;
             } else {
                 return EnumActionResult.FAIL;
diff --git a/src/main/java/com/zixiken/dimdoors/shared/Location.java b/src/main/java/com/zixiken/dimdoors/shared/Location.java
index 00b0c363..2210b4bf 100644
--- a/src/main/java/com/zixiken/dimdoors/shared/Location.java
+++ b/src/main/java/com/zixiken/dimdoors/shared/Location.java
@@ -32,7 +32,7 @@ public class Location {
 
     public Location(int dimID, BlockPos pos) {
         this.dimensionID = dimID;
-        this.pos = pos.up(0); //copyOf
+        this.pos = pos; //copyOf
     }
 
     public TileEntity getTileEntity() {
diff --git a/src/main/java/com/zixiken/dimdoors/shared/RiftRegistry.java b/src/main/java/com/zixiken/dimdoors/shared/RiftRegistry.java
index 970691bd..8d287482 100644
--- a/src/main/java/com/zixiken/dimdoors/shared/RiftRegistry.java
+++ b/src/main/java/com/zixiken/dimdoors/shared/RiftRegistry.java
@@ -125,7 +125,8 @@ public class RiftRegistry {
         if (pairedRiftID < 0) {
             return false;
         }
-        DDTileEntityBase destinationRift = (DDTileEntityBase) getRiftLocation(pairedRiftID).getTileEntity();
+        Location destinationRiftLocation = getRiftLocation(pairedRiftID);
+        DDTileEntityBase destinationRift = (DDTileEntityBase) destinationRiftLocation.getTileEntity();
         return TeleportHelper.teleport(entity, destinationRift.getTeleportTargetLocation());
     }
 }
diff --git a/src/main/java/com/zixiken/dimdoors/shared/TeleportHelper.java b/src/main/java/com/zixiken/dimdoors/shared/TeleportHelper.java
index 9d08e6f3..31b8f027 100644
--- a/src/main/java/com/zixiken/dimdoors/shared/TeleportHelper.java
+++ b/src/main/java/com/zixiken/dimdoors/shared/TeleportHelper.java
@@ -1,14 +1,10 @@
 package com.zixiken.dimdoors.shared;
 
 import com.zixiken.dimdoors.DimDoors;
-import net.minecraft.client.Minecraft;
 import net.minecraft.client.entity.EntityPlayerSP;
-import net.minecraft.client.multiplayer.WorldClient;
 import net.minecraft.entity.Entity;
 import net.minecraft.entity.player.EntityPlayer;
 import net.minecraft.entity.player.EntityPlayerMP;
-import net.minecraft.network.play.server.SPacketUpdateHealth;
-import net.minecraft.server.management.PlayerList;
 import net.minecraft.util.math.BlockPos;
 import net.minecraft.world.Teleporter;
 import net.minecraft.world.WorldServer;
@@ -28,21 +24,21 @@ public class TeleportHelper extends Teleporter {
         entityIn.setPositionAndUpdate(pos.getX() + .5, pos.getY() + .05, pos.getZ() + .5);
     }
 
-    public static boolean teleport(Entity entity, Location location) {
+    public static boolean teleport(Entity entity, Location newLocation) {
         if (entity instanceof EntityPlayerSP) {
-            DimDoors.log(location.getClass(), "Not teleporting, because EntityPlayerSP.");
+            DimDoors.log(TeleportHelper.class, "Not teleporting, because EntityPlayerSP.");
             return false;
         }
 
-        BlockPos newPos = location.getPos();
+        BlockPos newPos = newLocation.getPos();
         int oldDimID = entity.dimension;
-        int newDimID = location.getDimensionID();
+        int newDimID = newLocation.getDimensionID();
         WorldServer oldWorldServer = DimDoors.proxy.getWorldServer(oldDimID);
         WorldServer newWorldServer = DimDoors.proxy.getWorldServer(newDimID);
-        DimDoors.log(location.getClass(), "Starting teleporting now:");
+        DimDoors.log(TeleportHelper.class, "Starting teleporting now:");
         if (oldDimID == newDimID) {
             if (entity instanceof EntityPlayer) {
-                DimDoors.log(location.getClass(), "Using teleport method 1");
+                DimDoors.log(TeleportHelper.class, "Using teleport method 1");
                 EntityPlayerMP player = (EntityPlayerMP) entity;
 
                 player.setPositionAndUpdate(newPos.getX() + 0.5, newPos.getY() + 0.5, newPos.getZ() + 0.5);
@@ -50,7 +46,7 @@ public class TeleportHelper extends Teleporter {
                 //player.connection.sendPacket(new SPacketUpdateHealth(player.getHealth(), player.getFoodStats().getFoodLevel(), player.getFoodStats().getSaturationLevel()));
                 player.timeUntilPortal = 150;
             } else {
-                DimDoors.log(location.getClass(), "Using teleport method 2");
+                DimDoors.log(TeleportHelper.class, "Using teleport method 2");
                 WorldServer world = (WorldServer) entity.world;
 
                 entity.setPosition(newPos.getX() + 0.5, newPos.getY() + 0.5, newPos.getZ() + 0.5);
@@ -59,22 +55,22 @@ public class TeleportHelper extends Teleporter {
             }
         } else {
             if (entity instanceof EntityPlayer) {
-                DimDoors.log(location.getClass(), "Using teleport method 3");
+                DimDoors.log(TeleportHelper.class, "Using teleport method 3");
                 EntityPlayerMP player = (EntityPlayerMP) entity;
                 player.changeDimension(newDimID); //@todo, this only works for Vanilla dimensions, I've heard?
                 player.setPositionAndUpdate(newPos.getX() + 0.5, newPos.getY() + 0.5, newPos.getZ() + 0.5);
                 player.world.updateEntityWithOptionalForce(player, false);
                 //player.connection.sendPacket(new SPacketUpdateHealth(player.getHealth(), player.getFoodStats().getFoodLevel(), player.getFoodStats().getSaturationLevel()));
             } else if (!entity.world.isRemote) {
-                DimDoors.log(location.getClass(), "Using teleport method 4");
+                DimDoors.log(TeleportHelper.class, "Using teleport method 4");
                 entity.changeDimension(newDimID);
                 entity.setPosition(newPos.getX() + 0.5, newPos.getY() + 0.5, newPos.getZ() + 0.5);
                 oldWorldServer.resetUpdateEntityTick();
                 newWorldServer.resetUpdateEntityTick();
             } else {
                 //does this statement ever get reached though?
+                return false;
             }
-            return false;
         }
         entity.timeUntilPortal = 150;
         return true;
diff --git a/src/main/java/com/zixiken/dimdoors/tileentities/DDTileEntityBase.java b/src/main/java/com/zixiken/dimdoors/tileentities/DDTileEntityBase.java
index eeff9021..ca92533e 100644
--- a/src/main/java/com/zixiken/dimdoors/tileentities/DDTileEntityBase.java
+++ b/src/main/java/com/zixiken/dimdoors/tileentities/DDTileEntityBase.java
@@ -1,7 +1,6 @@
 package com.zixiken.dimdoors.tileentities;
 
 import com.zixiken.dimdoors.DimDoors;
-import com.zixiken.dimdoors.blocks.BlockDimDoor;
 import com.zixiken.dimdoors.shared.Location;
 import com.zixiken.dimdoors.shared.RiftRegistry;
 import java.util.Random;
diff --git a/src/main/java/com/zixiken/dimdoors/tileentities/TileEntityDimDoor.java b/src/main/java/com/zixiken/dimdoors/tileentities/TileEntityDimDoor.java
index a6bcc606..80b2abab 100644
--- a/src/main/java/com/zixiken/dimdoors/tileentities/TileEntityDimDoor.java
+++ b/src/main/java/com/zixiken/dimdoors/tileentities/TileEntityDimDoor.java
@@ -1,6 +1,5 @@
 package com.zixiken.dimdoors.tileentities;
 
-import com.zixiken.dimdoors.blocks.BlockDimDoor;
 import com.zixiken.dimdoors.shared.Location;
 import com.zixiken.dimdoors.shared.RiftRegistry;
 import java.util.Random;
@@ -10,11 +9,11 @@ import net.minecraft.util.EnumFacing;
 
 public class TileEntityDimDoor extends DDTileEntityBase {
 
-    public boolean openOrClosed;
+    public boolean doorIsOpen = false;
     public EnumFacing orientation = EnumFacing.SOUTH;
-    public boolean hasExit;
-    public byte lockStatus;
-    public boolean isDungeonChainLink;
+    public boolean hasExit = false;
+    public byte lockStatus = 1;
+    public boolean isDungeonChainLink = false;
     public boolean hasGennedPair = false;
 
     @Override
@@ -22,7 +21,7 @@ public class TileEntityDimDoor extends DDTileEntityBase {
         super.readFromNBT(nbt);
 
         try {
-            this.openOrClosed = nbt.getBoolean("openOrClosed");
+            this.doorIsOpen = nbt.getBoolean("doorIsOpen");
             this.orientation = EnumFacing.getFront(nbt.getInteger("orientation"));
             this.hasExit = nbt.getBoolean("hasExit");
             this.isDungeonChainLink = nbt.getBoolean("isDungeonChainLink");
@@ -33,9 +32,9 @@ public class TileEntityDimDoor extends DDTileEntityBase {
 
     @Override
     public NBTTagCompound writeToNBT(NBTTagCompound nbt) {
-          super.writeToNBT(nbt);
+        super.writeToNBT(nbt);
 
-        nbt.setBoolean("openOrClosed", this.openOrClosed);
+        nbt.setBoolean("doorIsOpen", this.doorIsOpen);
         nbt.setBoolean("hasExit", this.hasExit);
         nbt.setInteger("orientation", this.orientation.getIndex());
         nbt.setBoolean("isDungeonChainLink", isDungeonChainLink);
@@ -61,9 +60,7 @@ public class TileEntityDimDoor extends DDTileEntityBase {
 
     @Override
     public Location getTeleportTargetLocation() {
-        EnumFacing facing = getWorld().getBlockState(getPos()).getValue(BlockDimDoor.FACING).getOpposite(); //@todo this will allways return South after world-load?
-
-        return new Location(this.getWorld().provider.getDimension(), this.getPos().offset(facing));
+        return new Location(this.getWorld().provider.getDimension(), this.getPos().offset(orientation));
     }
 
     @Override