From f93c4e3670986c3efec20b6236dedefeab593cea Mon Sep 17 00:00:00 2001 From: Mathijs Riezebos Date: Tue, 17 Jan 2017 23:39:36 +0100 Subject: [PATCH] Added connecting rifts Changed the functioning of and registered the ItemRiftConnectionTool item. Repaired a stupid mistake in DDTileEntityBase --- .../items/ItemRiftConnectionTool.java | 82 ++++++++++++------- .../com/zixiken/dimdoors/items/ModItems.java | 2 + .../zixiken/dimdoors/shared/RiftRegistry.java | 16 +++- .../tileentities/DDTileEntityBase.java | 2 +- 4 files changed, 69 insertions(+), 33 deletions(-) diff --git a/src/main/java/com/zixiken/dimdoors/items/ItemRiftConnectionTool.java b/src/main/java/com/zixiken/dimdoors/items/ItemRiftConnectionTool.java index c22d9911..d812aa44 100644 --- a/src/main/java/com/zixiken/dimdoors/items/ItemRiftConnectionTool.java +++ b/src/main/java/com/zixiken/dimdoors/items/ItemRiftConnectionTool.java @@ -8,8 +8,10 @@ package com.zixiken.dimdoors.items; import com.zixiken.dimdoors.DimDoors; import com.zixiken.dimdoors.shared.RiftRegistry; import com.zixiken.dimdoors.tileentities.DDTileEntityBase; +import java.util.HashSet; import java.util.Set; import net.minecraft.block.Block; +import net.minecraft.block.material.Material; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; @@ -28,50 +30,72 @@ import net.minecraft.world.World; */ public class ItemRiftConnectionTool extends ItemTool { - ItemRiftConnectionTool(float attackDamageIn, float attackSpeedIn, Item.ToolMaterial materialIn, Set effectiveBlocksIn) { - super(attackDamageIn, attackSpeedIn, materialIn, effectiveBlocksIn); + public static final String ID = "itemRiftConnectionTool"; + + ItemRiftConnectionTool() { + super(1.0F, -2.8F, ToolMaterial.WOOD, new HashSet()); //@todo add extra stuff? + setUnlocalizedName(ID); + setRegistryName(ID); } @Override public ActionResult onItemRightClick(ItemStack stack, World worldIn, EntityPlayer playerIn, EnumHand hand) { - RayTraceResult hit = ItemDoorBase.doRayTrace(worldIn, playerIn, true); - if (hit != null) { - BlockPos pos = hit.getBlockPos(); - if (worldIn.getTileEntity(pos) instanceof DDTileEntityBase) { - DDTileEntityBase rift = (DDTileEntityBase) worldIn.getTileEntity(pos); - if (!playerIn.isSneaking()) { - selectPrimaryRift(stack, rift); - } else { - selectSecondaryRiftAndTakeAction(stack, rift); - } - return new ActionResult(EnumActionResult.PASS, stack); - } + if (!stack.hasTagCompound()) { + NBTTagCompound compound = new NBTTagCompound(); + compound.setBoolean("isInConnectMode", true); + stack.setTagCompound(compound); } + + RayTraceResult hit = ItemDoorBase.doRayTrace(worldIn, playerIn, true); + if (hit != null && worldIn.getTileEntity(hit.getBlockPos()) instanceof DDTileEntityBase) { + DDTileEntityBase rift = (DDTileEntityBase) worldIn.getTileEntity(hit.getBlockPos()); + if (playerIn.isSneaking()) { + return selectRift(stack, worldIn, rift, playerIn); //new ActionResult(EnumActionResult.PASS, stack)); + } + } else { + return changeMode(stack); + } + return new ActionResult(EnumActionResult.FAIL, stack); } - private void selectPrimaryRift(ItemStack stack, DDTileEntityBase rift) { + private ActionResult selectRift(ItemStack stack, World worldIn, DDTileEntityBase rift, EntityPlayer playerIn) { + DimDoors.log(this.getClass(), "Selecting rift with ID: " + rift.riftID); NBTTagCompound compound = stack.getTagCompound(); - compound.setInteger("primaryRiftID", rift.riftID); - } - - private void selectSecondaryRiftAndTakeAction(ItemStack stack, DDTileEntityBase rift) { - NBTTagCompound compound = stack.getTagCompound(); - if (!compound.hasKey("isInConnectMode")) { - compound.setBoolean("isInConnectMode", true); - } if (compound.getBoolean("isInConnectMode")) { - if (compound.hasKey("primaryRiftID")) { - int primaryRiftID = compound.getInteger("primaryRiftID"); + if (compound.hasKey("RiftID")) { + int primaryRiftID = compound.getInteger("RiftID"); int secondaryRiftID = rift.riftID; - RiftRegistry.Instance.pair(primaryRiftID, secondaryRiftID); + if (!worldIn.isRemote) { + DimDoors.log(this.getClass(), "Pairing rifts with IDs: " + primaryRiftID + " and " + secondaryRiftID); + RiftRegistry.Instance.pair(primaryRiftID, secondaryRiftID); + } + compound.removeTag("RiftID"); + stack.damageItem(1, playerIn); } else { - DimDoors.log(this.getClass(), "Primary Rift not selected. First select a primary rift by right-clicking without sneaking."); + compound.setInteger("RiftID", rift.riftID); } } else { - int secondaryRiftID = rift.riftID; - RiftRegistry.Instance.unpair(secondaryRiftID); + if (!worldIn.isRemote) { + RiftRegistry.Instance.unpair(rift.riftID); + } + stack.damageItem(1, playerIn); } + return new ActionResult(EnumActionResult.SUCCESS, stack); + } + + private ActionResult changeMode(ItemStack stack) { + NBTTagCompound compound = stack.getTagCompound(); + if (compound.getBoolean("isInConnectMode")) { + compound.setBoolean("isInConnectMode", false); + if (compound.hasKey("RiftID")) { + compound.removeTag("RiftID"); + } + } else { + compound.setBoolean("isInConnectMode", true); + } + DimDoors.log(this.getClass(), "isInConnectMode set to: " + compound.getBoolean("isInConnectMode")); + return new ActionResult(EnumActionResult.SUCCESS, stack); } } diff --git a/src/main/java/com/zixiken/dimdoors/items/ModItems.java b/src/main/java/com/zixiken/dimdoors/items/ModItems.java index 042c74e6..67d04cf5 100644 --- a/src/main/java/com/zixiken/dimdoors/items/ModItems.java +++ b/src/main/java/com/zixiken/dimdoors/items/ModItems.java @@ -16,6 +16,7 @@ public class ModItems { public static ItemDoorQuartz itemDoorQuartz; public static ItemDimDoorPersonal itemDimDoorPersonal; public static ItemBlockDimWall itemBlockDimWall; + public static ItemRiftConnectionTool itemRiftConnectionTool; public static void registerItems() { GameRegistry.register(itemDoorQuartz = new ItemDoorQuartz()); @@ -27,6 +28,7 @@ public class ModItems { GameRegistry.register(itemStableFabric = new ItemStableFabric()); GameRegistry.register(itemDimDoorChaos = new ItemDimDoorUnstable()); GameRegistry.register(itemWorldThread = new ItemWorldThread()); + GameRegistry.register(itemRiftConnectionTool = new ItemRiftConnectionTool()); //ItemBlocks GameRegistry.register(itemBlockDimWall = new ItemBlockDimWall()); diff --git a/src/main/java/com/zixiken/dimdoors/shared/RiftRegistry.java b/src/main/java/com/zixiken/dimdoors/shared/RiftRegistry.java index d9828786..21546b94 100644 --- a/src/main/java/com/zixiken/dimdoors/shared/RiftRegistry.java +++ b/src/main/java/com/zixiken/dimdoors/shared/RiftRegistry.java @@ -42,7 +42,7 @@ public class RiftRegistry { nextRiftID = nbt.getInteger("nextUnusedID"); if (nbt.hasKey("riftData")) { NBTTagCompound riftsNBT = nbt.getCompoundTag("riftData"); - int i = 1; + int i = 0; String tag = "" + i; while (riftsNBT.hasKey(tag)) { NBTTagCompound riftNBT = riftsNBT.getCompoundTag(tag); @@ -66,13 +66,13 @@ public class RiftRegistry { public int registerNewRift(DDTileEntityBase rift) { riftList.put(nextRiftID, Location.getLocation(rift)); - //DimDoors.log("Rift registered as ID: " + nextRiftID); + DimDoors.log(this.getClass(), "Rift registered as ID: " + nextRiftID); nextRiftID++; RiftSavedData.get(DimDoors.getDefWorld()).markDirty(); //Notify that this needs to be saved on world save return nextRiftID - 1; } - public void removeRift(int riftID, World world) { + public void unregisterRift(int riftID, World world) { if (riftList.containsKey(riftID)) { riftList.remove(riftID); RiftSavedData.get(DimDoors.getDefWorld()).markDirty(); //Notify that this needs to be saved on world save @@ -84,6 +84,10 @@ public class RiftRegistry { } public void pair(int riftID, int riftID2) { + if (riftID < 0 || riftID2 < 0) { + return; + } + DimDoors.log(this.getClass(), "pairing rift with ID " + riftID + " to rift with ID " + riftID2); Location location = riftList.get(riftID); TileEntity tileEntity = location.getTileEntity(); //@todo this method might need to be in another class? if (tileEntity != null && tileEntity instanceof DDTileEntityBase) { @@ -93,7 +97,13 @@ public class RiftRegistry { } public void unpair(int riftID) { + if (riftID < 0) { + return; + } Location location = riftList.get(riftID); + if (location == null) { + DimDoors.log(this.getClass(), "riftID with null location = " + riftID); + } TileEntity tileEntity = location.getTileEntity(); if (tileEntity != null && tileEntity instanceof DDTileEntityBase) { DDTileEntityBase rift = (DDTileEntityBase) tileEntity; diff --git a/src/main/java/com/zixiken/dimdoors/tileentities/DDTileEntityBase.java b/src/main/java/com/zixiken/dimdoors/tileentities/DDTileEntityBase.java index 9fadfbf2..d0113207 100644 --- a/src/main/java/com/zixiken/dimdoors/tileentities/DDTileEntityBase.java +++ b/src/main/java/com/zixiken/dimdoors/tileentities/DDTileEntityBase.java @@ -34,8 +34,8 @@ public abstract class DDTileEntityBase extends TileEntity { } } pairedRiftID = otherRiftID; - RiftRegistry.Instance.pair(pairedRiftID, riftID); isPaired = true; + RiftRegistry.Instance.pair(pairedRiftID, riftID); this.markDirty(); }