From e4e84644acedf54ccae9807391b4a6b8876a0297 Mon Sep 17 00:00:00 2001 From: SenseiKiwi Date: Wed, 25 Jun 2014 14:56:59 -0400 Subject: [PATCH] Changed Door Item Mapping Code 1. Changed EventHookContainer to remove a check against BaseItemDoor.getDoorToPlace(). The checks performed there can be done in BaseItemDoor.tryToPlaceDoor(), which removes the need for callers to know more internal details about how doors are handled. I moved the checks inside. 2. Renamed vanillaDoorMapping to doorItemMapping. It now maps dim door items to themselves to remove the need for various checks we were performing. Updated BaseItemDoor's constructor to reflect this change. 3. Removed BaseItemDoor.getDoorToPlace() and integrated its functionality into BaseItemDoor.tryToPlaceDoor(). 4. Changed BaseItemDoor.tryToPlaceDoor() so that it simply returns false if a given item stack cannot be used to place any doors. We don't need to check if the item is an ItemDoor or anything like that now. --- .../mod_pocketDim/EventHookContainer.java | 11 ++---- .../mod_pocketDim/items/BaseItemDoor.java | 39 ++++++++----------- 2 files changed, 20 insertions(+), 30 deletions(-) diff --git a/src/main/java/StevenDimDoors/mod_pocketDim/EventHookContainer.java b/src/main/java/StevenDimDoors/mod_pocketDim/EventHookContainer.java index 773877bb..4130aaf8 100644 --- a/src/main/java/StevenDimDoors/mod_pocketDim/EventHookContainer.java +++ b/src/main/java/StevenDimDoors/mod_pocketDim/EventHookContainer.java @@ -88,14 +88,11 @@ public class EventHookContainer ItemStack stack = event.entityPlayer.inventory.getCurrentItem(); if (stack != null && stack.getItem() instanceof ItemDoor) { - if (BaseItemDoor.getDoorToPlace(stack.getItem()) != null) + if (BaseItemDoor.tryToPlaceDoor(stack, event.entityPlayer, world, + event.x, event.y, event.z, event.face)) { - if (BaseItemDoor.tryToPlaceDoor(stack, event.entityPlayer, world, - event.x, event.y, event.z, event.face)) - { - // Cancel the event so that we don't get two doors from vanilla doors - event.setCanceled(true); - } + // Cancel the event so that we don't get two doors from vanilla doors + event.setCanceled(true); } } } diff --git a/src/main/java/StevenDimDoors/mod_pocketDim/items/BaseItemDoor.java b/src/main/java/StevenDimDoors/mod_pocketDim/items/BaseItemDoor.java index ddd25027..538d0592 100644 --- a/src/main/java/StevenDimDoors/mod_pocketDim/items/BaseItemDoor.java +++ b/src/main/java/StevenDimDoors/mod_pocketDim/items/BaseItemDoor.java @@ -24,9 +24,9 @@ import StevenDimDoors.mod_pocketDim.tileentities.TileEntityDimDoor; public abstract class BaseItemDoor extends ItemDoor { - // maps non-dimensional door items to their corresponding dimensional door - // item - private static HashMap vanillaDoorMapping = new HashMap(); + // Maps non-dimensional door items to their corresponding dimensional door item + // Also maps dimensional door items to themselves for simplicity + private static HashMap doorItemMapping = new HashMap(); private static DDProperties properties = null; /** @@ -35,7 +35,7 @@ public abstract class BaseItemDoor extends ItemDoor * @param material * @param door */ - public BaseItemDoor(int itemID, Material material, ItemDoor door) + public BaseItemDoor(int itemID, Material material, ItemDoor vanillaDoor) { super(itemID, material); this.setMaxStackSize(64); @@ -43,9 +43,10 @@ public abstract class BaseItemDoor extends ItemDoor if (properties == null) properties = DDProperties.instance(); - if(door!=null) + doorItemMapping.put(this, this); + if (vanillaDoor != null) { - vanillaDoorMapping.put(door, this); + doorItemMapping.put(vanillaDoor, this); } } @@ -77,19 +78,6 @@ public abstract class BaseItemDoor extends ItemDoor return false; } - public static BaseDimDoor getDoorToPlace(Item item) - { - if (!(item instanceof BaseItemDoor)) - { - item = BaseItemDoor.vanillaDoorMapping.get(item); - } - if(item == null) - { - return null; - } - return ((BaseItemDoor) item).getDoorBlock(); - } - /** * Tries to place a door block, called in EventHookContainer * @@ -111,15 +99,20 @@ public abstract class BaseItemDoor extends ItemDoor { return false; } - if (!(stack.getItem() instanceof ItemDoor)) + // Retrieve the actual door type that we want to use here. + // It's okay if stack isn't an ItemDoor. In that case, the lookup will + // return null, just as if the item was an unrecognized door type. + BaseItemDoor mappedItem = doorItemMapping.get(stack.getItem()); + if (mappedItem == null) { - throw new IllegalArgumentException("The itemstack must correspond to some type of door"); + return false; } - if (BaseItemDoor.placeDoorOnBlock(getDoorToPlace(stack.getItem()), stack, player, world, x, y, z, side)) + BaseDimDoor doorBlock = mappedItem.getDoorBlock(); + if (BaseItemDoor.placeDoorOnBlock(doorBlock, stack, player, world, x, y, z, side)) { return true; } - return BaseItemDoor.placeDoorOnRift(getDoorToPlace(stack.getItem()), world, player, stack); + return BaseItemDoor.placeDoorOnRift(doorBlock, world, player, stack); } /**