diff --git a/src/net/mcft/copy/betterstorage/item/ItemBackpack.java b/src/net/mcft/copy/betterstorage/item/ItemBackpack.java index a6331c14..5223b656 100644 --- a/src/net/mcft/copy/betterstorage/item/ItemBackpack.java +++ b/src/net/mcft/copy/betterstorage/item/ItemBackpack.java @@ -245,10 +245,10 @@ public static IInventory getBackpackItems(EntityLivingBase carrier) { } public static void initBackpackData(EntityLivingBase entity) { - EntityUtils.createProperties(entity, PropertiesBackpack.class, PropertiesBackpack.identifier); + EntityUtils.createProperties(entity, PropertiesBackpack.class); } public static PropertiesBackpack getBackpackData(EntityLivingBase entity) { - PropertiesBackpack backpackData = EntityUtils.getOrCreateProperties(entity, PropertiesBackpack.class, PropertiesBackpack.identifier); + PropertiesBackpack backpackData = EntityUtils.getOrCreateProperties(entity, PropertiesBackpack.class); if (!backpackData.initialized) { ItemBackpack.initBackpackOpen(entity); updateHasItems(entity, backpackData); diff --git a/src/net/mcft/copy/betterstorage/proxy/CommonProxy.java b/src/net/mcft/copy/betterstorage/proxy/CommonProxy.java index 2bf23ef8..0506a404 100644 --- a/src/net/mcft/copy/betterstorage/proxy/CommonProxy.java +++ b/src/net/mcft/copy/betterstorage/proxy/CommonProxy.java @@ -233,7 +233,7 @@ public void onLivingUpdate(LivingUpdateEvent event) { PropertiesBackpack backpackData; if (backpack == null) { - backpackData = EntityUtils.getProperties(entity, PropertiesBackpack.class, PropertiesBackpack.identifier); + backpackData = EntityUtils.getProperties(entity, PropertiesBackpack.class); if (backpackData == null) return; // If the entity is supposed to spawn with diff --git a/src/net/mcft/copy/betterstorage/utils/EntityUtils.java b/src/net/mcft/copy/betterstorage/utils/EntityUtils.java index 27113835..770d01d8 100644 --- a/src/net/mcft/copy/betterstorage/utils/EntityUtils.java +++ b/src/net/mcft/copy/betterstorage/utils/EntityUtils.java @@ -1,28 +1,43 @@ package net.mcft.copy.betterstorage.utils; +import java.util.HashMap; +import java.util.Map; + import net.minecraft.entity.Entity; import net.minecraftforge.common.IExtendedEntityProperties; public final class EntityUtils { + private static Map propertiesLookup = new HashMap(); + private EntityUtils() { } - public static T getProperties(Entity entity, Class propertiesClass, String identifier) { - IExtendedEntityProperties properties = entity.getExtendedProperties(identifier); + public static String getIdentifier(Class propertiesClass) { + String identifier = propertiesLookup.get(propertiesClass); + if (identifier == null) { + try { identifier = (String)propertiesClass.getField("identifier").get(null); } + catch (Exception e) { throw new Error(e); } + propertiesLookup.put(propertiesClass, identifier); + } + return identifier; + } + + public static T getProperties(Entity entity, Class propertiesClass) { + IExtendedEntityProperties properties = entity.getExtendedProperties(getIdentifier(propertiesClass)); return (propertiesClass.isInstance(properties) ? (T)properties : null); } - public static T createProperties(Entity entity, Class propertiesClass, String identifier) { + public static T createProperties(Entity entity, Class propertiesClass) { try { T properties = propertiesClass.getConstructor().newInstance(); - entity.registerExtendedProperties(identifier, properties); + entity.registerExtendedProperties(getIdentifier(propertiesClass), properties); return properties; - } catch (Exception e) { throw new RuntimeException(e); } + } catch (Exception e) { throw new Error(e); } } - public static T getOrCreateProperties(Entity entity, Class propertiesClass, String identifier) { - T properties = getProperties(entity, propertiesClass, identifier); - return ((properties != null) ? properties : createProperties(entity, propertiesClass, identifier)); + public static T getOrCreateProperties(Entity entity, Class propertiesClass) { + T properties = getProperties(entity, propertiesClass); + return ((properties != null) ? properties : createProperties(entity, propertiesClass)); } }