Skip to content

Commit

Permalink
Wait I can do this better!
Browse files Browse the repository at this point in the history
  • Loading branch information
copyboy committed Sep 4, 2013
1 parent 6807868 commit 05ab820
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/net/mcft/copy/betterstorage/item/ItemBackpack.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/net/mcft/copy/betterstorage/proxy/CommonProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 23 additions & 8 deletions src/net/mcft/copy/betterstorage/utils/EntityUtils.java
Original file line number Diff line number Diff line change
@@ -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<Class, String> propertiesLookup = new HashMap<Class, String>();

private EntityUtils() { }

public static <T extends IExtendedEntityProperties> T getProperties(Entity entity, Class<T> propertiesClass, String identifier) {
IExtendedEntityProperties properties = entity.getExtendedProperties(identifier);
public static String getIdentifier(Class<? extends IExtendedEntityProperties> 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 extends IExtendedEntityProperties> T getProperties(Entity entity, Class<T> propertiesClass) {
IExtendedEntityProperties properties = entity.getExtendedProperties(getIdentifier(propertiesClass));
return (propertiesClass.isInstance(properties) ? (T)properties : null);
}

public static <T extends IExtendedEntityProperties> T createProperties(Entity entity, Class<T> propertiesClass, String identifier) {
public static <T extends IExtendedEntityProperties> T createProperties(Entity entity, Class<T> 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 extends IExtendedEntityProperties> T getOrCreateProperties(Entity entity, Class<T> propertiesClass, String identifier) {
T properties = getProperties(entity, propertiesClass, identifier);
return ((properties != null) ? properties : createProperties(entity, propertiesClass, identifier));
public static <T extends IExtendedEntityProperties> T getOrCreateProperties(Entity entity, Class<T> propertiesClass) {
T properties = getProperties(entity, propertiesClass);
return ((properties != null) ? properties : createProperties(entity, propertiesClass));
}

}

0 comments on commit 05ab820

Please sign in to comment.