Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 37 additions & 12 deletions src/main/java/codechicken/nei/ItemMobSpawner.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package codechicken.nei;

import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -76,26 +77,49 @@ public void addInformation(ItemStack itemstack, EntityPlayer par2EntityPlayer, L
meta = idPig;
}
Entity e = getEntity(meta);
list.add(
(e instanceof IMob ? EnumChatFormatting.DARK_RED : EnumChatFormatting.DARK_AQUA)
+ IDtoNameMap.get(meta));
if (e != null) {
list.add(
(e instanceof IMob ? EnumChatFormatting.DARK_RED : EnumChatFormatting.DARK_AQUA)
+ IDtoNameMap.get(meta));
}
}

public static EntityLiving getEntity(int ID) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could reduce nesting for this method to improve code readability

public static EntityLiving getEntity(int ID) {
    EntityLiving cached = entityHashMap.get(ID);
    if (cached != null) {
        return cached;
    }

    loadSpawners();

    Class<?> clazz = (Class<?>) EntityList.IDtoClassMapping.get(ID);
    World world = NEIClientUtils.mc() != null ? NEIClientUtils.mc().theWorld : null;

    if (isInvalidEntityClass(clazz)) {
        return cacheFallback(ID);
    }

    EntityLiving entity = createEntityInstance(clazz, world, ID);
    if (entity != null) {
        entityHashMap.put(ID, entity);
    }

    return entity;
}

private static boolean isInvalidEntityClass(Class<?> clazz) {
    if (clazz == null) {
        return false;
    }

    int modifiers = clazz.getModifiers();
    if (Modifier.isAbstract(modifiers) || Modifier.isInterface(modifiers)) {
        NEIClientConfig.logger.warn(
            "Skipping abstract entity class: " + clazz.getName()
        );
        return true;
    }

    return false;
}

private static EntityLiving createEntityInstance(Class<?> clazz, World world, int id) {
    if (clazz == null || world == null) {
        return getEntity(idPig);
    }

    try {
        return (EntityLiving) clazz
            .getConstructor(World.class)
            .newInstance(world);
    } catch (Throwable t) {
        if (clazz == null) {
            NEIClientConfig.logger.error(
                "Null class for entity (" + id + ", " + IDtoNameMap.get(id) + ")"
            );
        } else {
            NEIClientConfig.logger.error(
                "Error creating instance of entity: " + clazz.getName(), t
            );
        }
        return getEntity(idPig);
    }
}

private static EntityLiving cacheFallback(int id) {
    EntityLiving fallback = getEntity(idPig);
    if (fallback != null) {
        entityHashMap.put(id, fallback);
    }
    return fallback;
}

EntityLiving e = entityHashMap.get(ID);
if (e == null) {
loadSpawners();
Class<?> clazz = EntityList.IDtoClassMapping.get(ID);
World world = NEIClientUtils.mc().theWorld;
Class<?> clazz = (Class<?>) EntityList.IDtoClassMapping.get(ID);
World world = NEIClientUtils.mc() != null ? NEIClientUtils.mc().theWorld : null;

if (clazz != null) {
int modifiers = clazz.getModifiers();
if (Modifier.isAbstract(modifiers) || Modifier.isInterface(modifiers)) {
NEIClientConfig.logger.warn("Skipping abstract entity class: " + clazz.getName());
e = getEntity(idPig);
if (e != null) {
entityHashMap.put(ID, e);
}
return e;
}
}

try {
e = (EntityLiving) clazz.getConstructor(new Class[] { World.class }).newInstance(world);
if (clazz != null && world != null) {
e = (EntityLiving) clazz.getConstructor(new Class[] { World.class }).newInstance(world);
} else {
e = getEntity(idPig);
}
} catch (Throwable t) {
if (clazz == null)
if (clazz == null) {
NEIClientConfig.logger.error("Null class for entity (" + ID + ", " + IDtoNameMap.get(ID));
else NEIClientConfig.logger.error("Error creating instance of entity: " + clazz.getName(), t);
} else {
NEIClientConfig.logger.error("Error creating instance of entity: " + clazz.getName(), t);
}
e = getEntity(idPig);
}
entityHashMap.put(ID, e);
if (e != null) {
entityHashMap.put(ID, e);
}
}
return e;
}
Expand All @@ -111,12 +135,13 @@ private void setDefaultTag(ItemStack itemstack) {
public static void loadSpawners() {
if (loaded) return;
loaded = true;
for (Map.Entry<Class<? extends Entity>, String> entry : EntityList.classToStringMapping.entrySet()) {
final Class<? extends Entity> clazz = entry.getKey();
for (Object entry : EntityList.classToStringMapping.entrySet()) {
Map.Entry<Class<? extends Entity>, String> mapEntry = (Map.Entry<Class<? extends Entity>, String>) entry;
final Class<? extends Entity> clazz = mapEntry.getKey();
if (EntityLiving.class.isAssignableFrom(clazz)) {
Integer id = (Integer) EntityList.classToIDMapping.get(clazz);
if (id == null) continue;
String name = entry.getValue();
String name = mapEntry.getValue();
if (name == null) continue;
if (name.equals("EnderDragon")) continue;
if (name.equals("Pig")) idPig = id;
Expand Down