Use class names to better find custom entities.

This commit is contained in:
GJ 2013-09-24 12:24:59 -04:00 committed by TfT_02
parent cf90236e57
commit 4a0fee5796
5 changed files with 76 additions and 70 deletions

View File

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

View File

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

View File

@ -44,18 +44,25 @@ public final class ModUtils {
} }
public static CustomEntity getCustomEntity(Entity entity) { public static CustomEntity getCustomEntity(Entity entity) {
if (!CustomEntityConfig.getInstance().customEntityIds.contains(entity.getEntityId()) && !CustomEntityConfig.getInstance().customEntityTypes.contains(entity.getType())) { CustomEntity customEntity = CustomEntityConfig.getInstance().customEntityTypeMap.get(entity.getType().toString());
if (customEntity == null) {
try {
customEntity = CustomEntityConfig.getInstance().customEntityClassMap.get(((Class<?>) entity.getClass().getDeclaredField("entityClass").get(entity)).getName());
}
catch (NoSuchFieldException e){
return null; return null;
} }
catch (IllegalArgumentException e) {
return null;
}
catch (IllegalAccessException e) {
return null;
}
}
for (CustomEntity customEntity : CustomEntityConfig.getInstance().customEntities) {
if ((customEntity.getEntityID() == entity.getEntityId()) && (customEntity.getEntityType() == entity.getType())) {
return customEntity; return customEntity;
} }
}
return null;
}
/** /**
* Check if a custom block is a woodcutting block. * Check if a custom block is a woodcutting block.
@ -158,7 +165,26 @@ public final class ModUtils {
} }
public static boolean isCustomEntity(Entity entity) { 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;
}
} }
/** /**

View File

@ -453,7 +453,7 @@ public final class CombatUtils {
* @param target The defending entity * @param target The defending entity
* @param skillType The skill being used * @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; double baseXP = 0;
if (target instanceof Player) { if (target instanceof Player) {
@ -468,14 +468,12 @@ public final class CombatUtils {
} }
} }
else { else {
if (target instanceof Animals) {
if (ModUtils.isCustomEntity(target)) { if (ModUtils.isCustomEntity(target)) {
baseXP = ModUtils.getCustomEntity(target).getXpMultiplier(); baseXP = ModUtils.getCustomEntity(target).getXpMultiplier();
} }
else { else if (target instanceof Animals) {
baseXP = ExperienceConfig.getInstance().getAnimalsXP(); baseXP = ExperienceConfig.getInstance().getAnimalsXP();
} }
}
else { else {
EntityType type = target.getType(); EntityType type = target.getType();
@ -500,7 +498,6 @@ public final class CombatUtils {
baseXP = ExperienceConfig.getInstance().getCombatXP(type); baseXP = ExperienceConfig.getInstance().getCombatXP(type);
break; break;
// Temporary workaround for custom entities
case UNKNOWN: case UNKNOWN:
baseXP = 1.0; baseXP = 1.0;
break; break;
@ -523,9 +520,6 @@ public final class CombatUtils {
break; break;
default: default:
if (ModUtils.isCustomEntity(target)) {
baseXP = ModUtils.getCustomEntity(target).getXpMultiplier();
}
break; break;
} }
} }

View File

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