diff --git a/src/main/java/WayofTime/bloodmagic/item/sigil/ItemSigilTeleposition.java b/src/main/java/WayofTime/bloodmagic/item/sigil/ItemSigilTeleposition.java index 4a9566da..10e44dab 100644 --- a/src/main/java/WayofTime/bloodmagic/item/sigil/ItemSigilTeleposition.java +++ b/src/main/java/WayofTime/bloodmagic/item/sigil/ItemSigilTeleposition.java @@ -1,6 +1,6 @@ package WayofTime.bloodmagic.item.sigil; -import WayofTime.bloodmagic.util.Constants; +import WayofTime.bloodmagic.core.data.Binding; import WayofTime.bloodmagic.iface.ISigil; import WayofTime.bloodmagic.teleport.TeleportQueue; import WayofTime.bloodmagic.util.helper.NBTHelper; @@ -12,18 +12,23 @@ import net.minecraft.client.util.ITooltipFlag; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.tileentity.TileEntity; import net.minecraft.util.ActionResult; import net.minecraft.util.EnumActionResult; import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumHand; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; +import net.minecraftforge.common.DimensionManager; +import net.minecraftforge.common.util.INBTSerializable; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; +import javax.annotation.Nullable; import java.util.List; public class ItemSigilTeleposition extends ItemSigilBase { + public ItemSigilTeleposition() { super("teleposition"); } @@ -33,14 +38,11 @@ public class ItemSigilTeleposition extends ItemSigilBase { public void addInformation(ItemStack stack, World world, List tooltip, ITooltipFlag flag) { super.addInformation(stack, world, tooltip, flag); - if (!stack.hasTagCompound()) - return; - NBTTagCompound tag = stack.getTagCompound(); - - if (tag != null && stack.getTagCompound().hasKey(Constants.NBT.DIMENSION_ID) && stack.getTagCompound().hasKey(Constants.NBT.X_COORD) && stack.getTagCompound().hasKey(Constants.NBT.Y_COORD) && stack.getTagCompound().hasKey(Constants.NBT.Z_COORD)) { - tooltip.add(" "); - tooltip.add(TextHelper.localizeEffect("tooltip.bloodmagic.telepositionFocus.coords", getValue(tag, Constants.NBT.X_COORD), getValue(tag, Constants.NBT.Y_COORD), getValue(tag, Constants.NBT.Z_COORD))); - tooltip.add(TextHelper.localizeEffect("tooltip.bloodmagic.telepositionFocus.dimension", getValue(tag, Constants.NBT.DIMENSION_ID))); + TeleportLocation location = getTeleportLocation(stack); + if (location != null) { + tooltip.add(""); + tooltip.add(TextHelper.localizeEffect("tooltip.bloodmagic.telepositionFocus.coords", location.pos.getX(), location.pos.getY(), location.pos.getZ())); + tooltip.add(TextHelper.localizeEffect("tooltip.bloodmagic.telepositionFocus.dimension", location.dim)); } } @@ -52,15 +54,23 @@ public class ItemSigilTeleposition extends ItemSigilBase { if (PlayerHelper.isFakePlayer(player)) return ActionResult.newResult(EnumActionResult.FAIL, stack); - if (!world.isRemote && NBTHelper.checkNBT(stack) != null && stack.getTagCompound().hasKey(Constants.NBT.DIMENSION_ID) && stack.getTagCompound().hasKey(Constants.NBT.X_COORD) && stack.getTagCompound().hasKey(Constants.NBT.Y_COORD) && stack.getTagCompound().hasKey(Constants.NBT.Z_COORD)) { - BlockPos blockPos = new BlockPos(getValue(stack.getTagCompound(), Constants.NBT.X_COORD), getValue(stack.getTagCompound(), Constants.NBT.Y_COORD), getValue(stack.getTagCompound(), Constants.NBT.Z_COORD)).up(); - if (world.provider.getDimension() == getValue(stack.getTagCompound(), Constants.NBT.DIMENSION_ID)) { - TeleportQueue.getInstance().addITeleport(new Teleports.TeleportSameDim(blockPos, player, getBinding(stack).getOwnerId(), true)); + TeleportLocation location = getTeleportLocation(stack); + Binding binding = getBinding(stack); + if (!world.isRemote && location != null && binding != null) { + World teleportTo = DimensionManager.getWorld(location.dim); + if (teleportTo != null) { + TileEntity tile = teleportTo.getTileEntity(location.pos); + if (tile instanceof TileTeleposer) { + BlockPos blockPos = location.pos.up(); + if (world.provider.getDimension() == location.dim) { + TeleportQueue.getInstance().addITeleport(new Teleports.TeleportSameDim(blockPos, player, binding.getOwnerId(), true)); + } + // FIXME - Fix cross-dimension teleports causing major desync +// } else { +// TeleportQueue.getInstance().addITeleport(new Teleports.TeleportToDim(blockPos, player, getOwnerUUID(stack), world, getValue(stack.getTagCompound(), Constants.NBT.DIMENSION_ID), true)); +// } + } } - // FIXME - Fix cross-dimension teleports causing major desync -// } else { -// TeleportQueue.getInstance().addITeleport(new Teleports.TeleportToDim(blockPos, player, getOwnerUUID(stack), world, getValue(stack.getTagCompound(), Constants.NBT.DIMENSION_ID), true)); -// } } return super.onItemRightClick(world, player, hand); } @@ -75,18 +85,74 @@ public class ItemSigilTeleposition extends ItemSigilBase { if (!world.isRemote && player.isSneaking() && NBTHelper.checkNBT(stack) != null) { if (world.getTileEntity(pos) != null && world.getTileEntity(pos) instanceof TileTeleposer) { - stack.getTagCompound().setInteger(Constants.NBT.DIMENSION_ID, world.provider.getDimension()); - stack.getTagCompound().setInteger(Constants.NBT.X_COORD, pos.getX()); - stack.getTagCompound().setInteger(Constants.NBT.Y_COORD, pos.getY()); - stack.getTagCompound().setInteger(Constants.NBT.Z_COORD, pos.getZ()); - + TeleportLocation teleportLocation = new TeleportLocation(world.provider.getDimension(), pos); + updateLocation(stack, teleportLocation); return EnumActionResult.SUCCESS; } } return EnumActionResult.FAIL; } - public int getValue(NBTTagCompound tag, String key) { - return tag.getInteger(key); + @Nullable + public TeleportLocation getTeleportLocation(ItemStack stack) { + if (!(stack.getItem() instanceof ItemSigilTeleposition)) + return null; + + if (!stack.hasTagCompound()) + return null; + + NBTTagCompound locationTag = stack.getSubCompound("tplocation"); + if (locationTag == null) + return null; + + return TeleportLocation.fromTag(locationTag); + } + + public void updateLocation(ItemStack stack, TeleportLocation location) { + NBTTagCompound tagCompound; + if (!stack.hasTagCompound()) + stack.setTagCompound(tagCompound = new NBTTagCompound()); + else + tagCompound = stack.getTagCompound(); + + tagCompound.setTag("tplocation", location.serializeNBT()); + } + + public static class TeleportLocation implements INBTSerializable { + + private int dim; + private BlockPos pos; + + private TeleportLocation() { + } + + public TeleportLocation(int dim, BlockPos pos) { + this.dim = dim; + this.pos = pos; + } + + public TeleportLocation(int dim, int x, int y, int z) { + this(dim, new BlockPos(x, y, z)); + } + + @Override + public NBTTagCompound serializeNBT() { + NBTTagCompound tag = new NBTTagCompound(); + tag.setInteger("dim", dim); + tag.setLong("pos", pos.toLong()); + return tag; + } + + @Override + public void deserializeNBT(NBTTagCompound nbt) { + this.dim = nbt.getInteger("dim"); + this.pos = BlockPos.fromLong(nbt.getLong("pos")); + } + + public static TeleportLocation fromTag(NBTTagCompound tpTag) { + TeleportLocation location = new TeleportLocation(); + location.deserializeNBT(tpTag); + return location; + } } }