Skip to content

Commit

Permalink
Remove lag machine EntityUtils.getIdentifier
Browse files Browse the repository at this point in the history
  • Loading branch information
copyboy committed Sep 4, 2013
1 parent 7ddc314 commit 6807868
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 17 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);
EntityUtils.createProperties(entity, PropertiesBackpack.class, PropertiesBackpack.identifier);
}
public static PropertiesBackpack getBackpackData(EntityLivingBase entity) {
PropertiesBackpack backpackData = EntityUtils.getOrCreateProperties(entity, PropertiesBackpack.class);
PropertiesBackpack backpackData = EntityUtils.getOrCreateProperties(entity, PropertiesBackpack.class, PropertiesBackpack.identifier);
if (!backpackData.initialized) {
ItemBackpack.initBackpackOpen(entity);
updateHasItems(entity, backpackData);
Expand Down
3 changes: 1 addition & 2 deletions src/net/mcft/copy/betterstorage/misc/PropertiesBackpack.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public class PropertiesBackpack implements IExtendedEntityProperties {

public static final String identifier = Constants.modId + ".backpack";

// public EntityLiving entity;
public ItemStack[] contents = null;

/** If the backpack has been initialized for the entity yet. */
Expand All @@ -30,7 +29,7 @@ public class PropertiesBackpack implements IExtendedEntityProperties {
public float prevLidAngle = 0;

@Override
public void init(Entity entity, World world) {}
public void init(Entity entity, World world) { }

@Override
public void saveNBTData(NBTTagCompound compound) {
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);
backpackData = EntityUtils.getProperties(entity, PropertiesBackpack.class, PropertiesBackpack.identifier);
if (backpackData == null) return;

// If the entity is supposed to spawn with
Expand Down
18 changes: 6 additions & 12 deletions src/net/mcft/copy/betterstorage/utils/EntityUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,22 @@ public final class EntityUtils {

private EntityUtils() { }

private static String getIdentifier(Class<? extends IExtendedEntityProperties> propertiesClass) {
try { return (String)propertiesClass.getField("identifier").get(null); }
catch (Exception e) { throw new RuntimeException(e); }
}

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

public static <T extends IExtendedEntityProperties> T createProperties(Entity entity, Class<T> propertiesClass) {
public static <T extends IExtendedEntityProperties> T createProperties(Entity entity, Class<T> propertiesClass, String identifier) {
try {
T properties = propertiesClass.getConstructor().newInstance();
entity.registerExtendedProperties(getIdentifier(propertiesClass), properties);
entity.registerExtendedProperties(identifier, properties);
return properties;
} catch (Exception e) { throw new RuntimeException(e); }
}

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

}

0 comments on commit 6807868

Please sign in to comment.