Browse Source

Use class names to better find custom entities.

GJ 11 years ago
parent
commit
4a0fee5796

+ 29 - 22
src/main/java/com/gmail/nossr50/config/mods/CustomEntityConfig.java

@@ -1,11 +1,12 @@
 package com.gmail.nossr50.config.mods;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Set;
 
+import org.apache.commons.lang.ClassUtils;
 import org.bukkit.configuration.ConfigurationSection;
-import org.bukkit.entity.EntityType;
 import org.bukkit.inventory.ItemStack;
 
 import com.gmail.nossr50.config.ConfigLoader;
@@ -14,13 +15,13 @@ import com.gmail.nossr50.datatypes.mods.CustomEntity;
 public class CustomEntityConfig extends ConfigLoader {
     private static CustomEntityConfig instance;
 
-    public List<Integer> customEntityIds        = new ArrayList<Integer>();
-    public List<Integer> customHostileEntityIds = new ArrayList<Integer>();
-    public List<Integer> customNeutralEntityIds = new ArrayList<Integer>();
-    public List<Integer> customPassiveEntityIds = new ArrayList<Integer>();
+    public List<String> customHostileEntityTypes = new ArrayList<String>();
+    public List<String> customNeutralEntityTypes = new ArrayList<String>();
+    public List<String> customPassiveEntityTypes = new ArrayList<String>();
+    public List<String> customEntityTypes        = new ArrayList<String>();
 
-    public List<EntityType> customEntityTypes = new ArrayList<EntityType>();
-    public List<CustomEntity> customEntities = new ArrayList<CustomEntity>();
+    public HashMap<String, CustomEntity> customEntityClassMap = new HashMap<String, CustomEntity>();
+    public HashMap<String, CustomEntity> customEntityTypeMap  = new HashMap<String, CustomEntity>();
 
     public CustomEntityConfig() {
         super("ModConfigs", "entities.yml");
@@ -37,12 +38,12 @@ public class CustomEntityConfig extends ConfigLoader {
 
     @Override
     protected void loadKeys() {
-        loadMobs("Hostile", customHostileEntityIds);
-        loadMobs("Neutral", customNeutralEntityIds);
-        loadMobs("Passive", customPassiveEntityIds);
+        loadMobs("Hostile", customHostileEntityTypes);
+        loadMobs("Neutral", customNeutralEntityTypes);
+        loadMobs("Passive", customPassiveEntityTypes);
     }
 
-    private void loadMobs(String entityType, List<Integer> entityIdList) {
+    private void loadMobs(String entityType, List<String> entityTypeList) {
         ConfigurationSection entitySection = config.getConfigurationSection(entityType);
 
         if (entitySection == null) {
@@ -52,8 +53,18 @@ public class CustomEntityConfig extends ConfigLoader {
         Set<String> entityConfigSet = entitySection.getKeys(false);
 
         for (String entityName : entityConfigSet) {
-            int id = config.getInt(entityType + "." + entityName + ".ID", 0);
-            EntityType type = EntityType.fromId(id);
+            Class<?> clazz = null;
+            String className = config.getString(entityType + "." + entityName + ".Class", "");
+
+            try {
+                clazz = ClassUtils.getClass(className);
+            }
+            catch (ClassNotFoundException e) {
+                plugin.getLogger().warning("Invalid class (" + className + ") detected for " + entityName + ".");
+                plugin.getLogger().warning("This custom entity may not function properly.");
+            }
+
+            String entityTypeName = entityName.replace("_", ".");
             double xpMultiplier = config.getDouble(entityType + "." + entityName + ".XP_Multiplier", 1.0D);
             boolean canBeTamed = config.getBoolean(entityType + "." + entityName + ".Tameable", false);
             int tamingXp = config.getInt(entityType + "." + entityName + "Taming_XP", 0);
@@ -64,21 +75,17 @@ public class CustomEntityConfig extends ConfigLoader {
 
             CustomEntity entity;
 
-            if (id == 0) {
-                plugin.getLogger().warning("Missing ID. This entity will be skipped.");
-                continue;
-            }
-
             if (canBeSummoned && (callOfTheWildId == 0 || callOfTheWildAmount == 0)) {
                 plugin.getLogger().warning("Incomplete Call of the Wild information. This enitity will not be able to be summoned by Call of the Wild.");
                 canBeSummoned = false;
             }
 
-            entity = new CustomEntity(id, type, xpMultiplier, canBeTamed, tamingXp, canBeSummoned, new ItemStack(callOfTheWildId, callOfTheWildData), callOfTheWildAmount);
+            entity = new CustomEntity(xpMultiplier, canBeTamed, tamingXp, canBeSummoned, new ItemStack(callOfTheWildId, callOfTheWildData), callOfTheWildAmount);
 
-            entityIdList.add(id);
-            customEntityTypes.add(type);
-            customEntities.add(entity);
+            entityTypeList.add(entityTypeName);
+            customEntityTypeMap.put(entityTypeName, entity);
+            customEntityClassMap.put(clazz == null ? null : clazz.getName(), entity);
+            customEntityTypes.add(entityTypeName);
         }
     }
 }

+ 1 - 22
src/main/java/com/gmail/nossr50/datatypes/mods/CustomEntity.java

@@ -1,11 +1,8 @@
 package com.gmail.nossr50.datatypes.mods;
 
-import org.bukkit.entity.EntityType;
 import org.bukkit.inventory.ItemStack;
 
 public class CustomEntity {
-    private int entityID;
-    private EntityType entityType;
     private double xpMultiplier;
     private boolean canBeTamed;
     private int tamingXP;
@@ -13,9 +10,7 @@ public class CustomEntity {
     private ItemStack callOfTheWildItem;
     private int callOfTheWildAmount;
 
-    public CustomEntity(int entityID, EntityType entityType, double xpMultiplier, boolean canBeTamed, int tamingXP, boolean canBeSummoned, ItemStack callOfTheWildItem, int callOfTheWildAmount) {
-        this.entityID = entityID;
-        this.entityType = entityType;
+    public CustomEntity(double xpMultiplier, boolean canBeTamed, int tamingXP, boolean canBeSummoned, ItemStack callOfTheWildItem, int callOfTheWildAmount) {
         this.xpMultiplier = xpMultiplier;
         this.canBeTamed = canBeTamed;
         this.tamingXP = tamingXP;
@@ -24,22 +19,6 @@ public class CustomEntity {
         this.callOfTheWildAmount = callOfTheWildAmount;
     }
 
-    public int getEntityID() {
-        return entityID;
-    }
-
-    public void setEntityID(int entityID) {
-        this.entityID = entityID;
-    }
-
-    public EntityType getEntityType() {
-        return entityType;
-    }
-
-    public void setEntityType(EntityType entityType) {
-        this.entityType = entityType;
-    }
-
     public double getXpMultiplier() {
         return xpMultiplier;
     }

+ 34 - 8
src/main/java/com/gmail/nossr50/util/ModUtils.java

@@ -44,17 +44,24 @@ public final class ModUtils {
     }
 
     public static CustomEntity getCustomEntity(Entity entity) {
-        if (!CustomEntityConfig.getInstance().customEntityIds.contains(entity.getEntityId()) && !CustomEntityConfig.getInstance().customEntityTypes.contains(entity.getType())) {
-            return null;
-        }
+        CustomEntity customEntity = CustomEntityConfig.getInstance().customEntityTypeMap.get(entity.getType().toString());
 
-        for (CustomEntity customEntity : CustomEntityConfig.getInstance().customEntities) {
-            if ((customEntity.getEntityID() == entity.getEntityId()) && (customEntity.getEntityType() == entity.getType())) {
-                return customEntity;
+        if (customEntity == null) {
+            try {
+                customEntity = CustomEntityConfig.getInstance().customEntityClassMap.get(((Class<?>) entity.getClass().getDeclaredField("entityClass").get(entity)).getName());
+            }
+            catch (NoSuchFieldException e){
+                return null;
+            }
+            catch (IllegalArgumentException e) {
+                return null;
+            }
+            catch (IllegalAccessException e) {
+                return null;
             }
         }
 
-        return null;
+        return customEntity;
     }
 
     /**
@@ -158,7 +165,26 @@ public final class ModUtils {
     }
 
     public static boolean isCustomEntity(Entity entity) {
-        return customEntitiesEnabled && CustomEntityConfig.getInstance().customEntityIds.contains(entity.getEntityId());
+        if (!customEntitiesEnabled) {
+            return false;
+        }
+
+        if (CustomEntityConfig.getInstance().customEntityTypeMap.containsKey(entity.getType().toString())) {
+            return true;
+        }
+
+        try {
+            return CustomEntityConfig.getInstance().customEntityClassMap.containsKey(((Class<?>) entity.getClass().getDeclaredField("entityClass").get(entity)).getName());
+        }
+        catch (NoSuchFieldException e){
+            return false;
+        }
+        catch (IllegalArgumentException e) {
+            return false;
+        }
+        catch (IllegalAccessException e) {
+            return false;
+        }
     }
 
     /**

+ 6 - 12
src/main/java/com/gmail/nossr50/util/skills/CombatUtils.java

@@ -453,7 +453,7 @@ public final class CombatUtils {
      * @param target The defending entity
      * @param skillType The skill being used
      */
-    public static void startGainXp(McMMOPlayer mcMMOPlayer, LivingEntity target, SkillType skillType, double multiplier) {
+    private static void startGainXp(McMMOPlayer mcMMOPlayer, LivingEntity target, SkillType skillType, double multiplier) {
         double baseXP = 0;
 
         if (target instanceof Player) {
@@ -468,13 +468,11 @@ public final class CombatUtils {
             }
         }
         else {
-            if (target instanceof Animals) {
-                if (ModUtils.isCustomEntity(target)) {
-                    baseXP = ModUtils.getCustomEntity(target).getXpMultiplier();
-                }
-                else {
-                    baseXP = ExperienceConfig.getInstance().getAnimalsXP();
-                }
+            if (ModUtils.isCustomEntity(target)) {
+                baseXP = ModUtils.getCustomEntity(target).getXpMultiplier();
+            }
+            else if (target instanceof Animals) {
+                baseXP = ExperienceConfig.getInstance().getAnimalsXP();
             }
             else {
                 EntityType type = target.getType();
@@ -500,7 +498,6 @@ public final class CombatUtils {
                         baseXP = ExperienceConfig.getInstance().getCombatXP(type);
                         break;
 
-                    // Temporary workaround for custom entities
                     case UNKNOWN:
                         baseXP = 1.0;
                         break;
@@ -523,9 +520,6 @@ public final class CombatUtils {
                         break;
 
                     default:
-                        if (ModUtils.isCustomEntity(target)) {
-                            baseXP = ModUtils.getCustomEntity(target).getXpMultiplier();
-                        }
                         break;
                 }
             }

+ 6 - 6
src/main/resources/entities.yml

@@ -3,7 +3,7 @@
 ###
 Hostile:
     Mob_1:
-        ID: 9999
+        Class: CLASS_NAME
         XP_Multiplier: 1.0
         Tameable: false
         Taming_XP: 250
@@ -12,7 +12,7 @@ Hostile:
         COTW_Material_Data: 9
         COTW_Material_Amount: 99
     Mob_2:
-        ID: 9999
+        Class: CLASS_NAME
         XP_Multiplier: 1.0
         Tameable: false
         Taming_XP: 250
@@ -25,7 +25,7 @@ Hostile:
 ###
 Neutral:
     Mob_1:
-        ID: 9999
+        Class: CLASS_NAME
         XP_Multiplier: 1.0
         Tameable: false
         Taming_XP: 250
@@ -34,7 +34,7 @@ Neutral:
         COTW_Material_Data: 9
         COTW_Material_Amount: 99
     Mob_2:
-        ID: 9999
+        Class: CLASS_NAME
         XP_Multiplier: 1.0
         Tameable: false
         Taming_XP: 250
@@ -47,7 +47,7 @@ Neutral:
 ###
 Passive:
     Mob_1:
-        ID: 9999
+        Class: CLASS_NAME
         XP_Multiplier: 1.0
         Tameable: false
         Taming_XP: 250
@@ -56,7 +56,7 @@ Passive:
         COTW_Material_Data: 9
         COTW_Material_Amount: 99
     Mob_2:
-        ID: 9999
+        Class: CLASS_NAME
         XP_Multiplier: 1.0
         Tameable: false
         Taming_XP: 250