-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
copyboy
committed
Sep 4, 2013
1 parent
6807868
commit 05ab820
Showing
3 changed files
with
26 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |