import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleMessage;
import com.seodisparate.TurnBasedMinecraft.common.networking.PacketEditingMessage;
+import com.seodisparate.TurnBasedMinecraft.common.networking.PacketGeneralMessage;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraftforge.event.entity.living.LivingAttackEvent;
import net.minecraftforge.event.entity.living.LivingSetAttackTargetEvent;
{
editingInfo.isPendingEntitySelection = false;
event.setCanceled(true);
- editingInfo.entityInfo = config.getMatchingEntityInfo(event.getEntity());
- if(editingInfo.entityInfo == null)
+ if(editingInfo.isEditingCustomName)
{
- editingInfo.entityInfo = new EntityInfo();
- editingInfo.entityInfo.classType = event.getEntity().getClass();
- TurnBasedMinecraftMod.NWINSTANCE.sendTo(new PacketEditingMessage(PacketEditingMessage.Type.PICK_EDIT, editingInfo.entityInfo), (EntityPlayerMP)editingInfo.editor);
- return;
+ if(event.getEntity().getCustomNameTag().isEmpty())
+ {
+ TurnBasedMinecraftMod.logger.error("Cannot edit custom name from entity without custom name");
+ TurnBasedMinecraftMod.NWINSTANCE.sendTo(new PacketGeneralMessage("Cannot edit custom name from entity without custom name"), (EntityPlayerMP) editingInfo.editor);
+ return;
+ }
+ editingInfo.entityInfo = config.getCustomEntityInfo(event.getEntity().getCustomNameTag());
+ if(editingInfo.entityInfo == null)
+ {
+ editingInfo.entityInfo = new EntityInfo();
+ editingInfo.entityInfo.customName = event.getEntity().getCustomNameTag();
+ }
+ TurnBasedMinecraftMod.NWINSTANCE.sendTo(new PacketGeneralMessage("Editing custom name \"" + event.getEntity().getCustomNameTag() + "\""), (EntityPlayerMP) editingInfo.editor);
+ TurnBasedMinecraftMod.logger.info("Begin editing custom \"" + event.getEntity().getCustomNameTag() + "\"");
+ TurnBasedMinecraftMod.NWINSTANCE.sendTo(new PacketEditingMessage(PacketEditingMessage.Type.PICK_EDIT, editingInfo.entityInfo), (EntityPlayerMP) editingInfo.editor);
+ }
+ else
+ {
+ editingInfo.entityInfo = config.getMatchingEntityInfo(event.getEntity()).clone();
+ if(editingInfo.entityInfo == null)
+ {
+ editingInfo.entityInfo = new EntityInfo();
+ editingInfo.entityInfo.classType = event.getEntity().getClass();
+ }
+ TurnBasedMinecraftMod.NWINSTANCE.sendTo(new PacketGeneralMessage("Editing entity \"" + editingInfo.entityInfo.classType.getName() + "\""), (EntityPlayerMP) editingInfo.editor);
+ TurnBasedMinecraftMod.logger.info("Begin editing \"" + editingInfo.entityInfo.classType.getName() + "\"");
+ TurnBasedMinecraftMod.NWINSTANCE.sendTo(new PacketEditingMessage(PacketEditingMessage.Type.PICK_EDIT, editingInfo.entityInfo), (EntityPlayerMP) editingInfo.editor);
}
- TurnBasedMinecraftMod.NWINSTANCE.sendTo(new PacketEditingMessage(PacketEditingMessage.Type.PICK_EDIT, editingInfo.entityInfo), (EntityPlayerMP)editingInfo.editor);
return;
}
}
}
- if((event.getEntity() != null && battleManager.isRecentlyLeftBattle(event.getEntity().getEntityId()))
- || (event.getSource().getTrueSource() != null && battleManager.isRecentlyLeftBattle(event.getSource().getTrueSource().getEntityId())))
+ if(event.getEntity() != null && event.getSource().getTrueSource() != null && (battleManager.isRecentlyLeftBattle(event.getEntity().getEntityId()) || battleManager.isRecentlyLeftBattle(event.getSource().getTrueSource().getEntityId())))
{
event.setCanceled(true);
return;
public static final long BATTLE_DECISION_DURATION_NANO_DEFAULT = 15000000000L;
private long battleDecisionDurationNanos = BATTLE_DECISION_DURATION_NANO_DEFAULT;
private Map<String, EntityInfo> entityInfoMap;
+ private Map<String, EntityInfo> customEntityInfoMap;
private Set<String> ignoreBattleTypes;
private Logger logger;
private int playerSpeed = 50;
public Config(Logger logger)
{
entityInfoMap = new HashMap<String, EntityInfo>();
+ customEntityInfoMap = new HashMap<String, EntityInfo>();
ignoreBattleTypes = new HashSet<String>();
this.logger = logger;
musicBattleTypes = new HashSet<String>();
{
TomlTable entity = entityArray.getTable(i);
EntityInfo eInfo = new EntityInfo();
- try
+ String name = null;
+ if(entity.contains("name") && entity.contains("custom_name"))
{
- eInfo.classType = Class.forName(entity.getString("name"));
+ logger.error("Entity cannot have both \"name\" and \"custom_name\" entries");
+ continue;
}
- catch (ClassNotFoundException e)
+ else if(entity.contains("name"))
{
- logger.error("Entity with class name \"" + entity.getString("name") + "\" not found, skipping...");
- continue;
+ try
+ {
+ eInfo.classType = Class.forName(entity.getString("name"));
+ name = eInfo.classType.getName();
+ } catch(ClassNotFoundException e)
+ {
+ logger.error("Entity with class name \"" + entity.getString("name") + "\" not found, skipping...");
+ continue;
+ }
}
- catch (NullPointerException e)
+ else if(entity.contains("custom_name"))
{
- logger.error("Entity does not have \"name\", skipping...");
+ eInfo.customName = entity.getString("custom_name");
+ name = eInfo.customName;
+ }
+ else
+ {
+ logger.error("Entity must have \"name\" or \"custom_name\" entry");
continue;
}
if(eInfo.attackPower < 0)
{
eInfo.attackPower = 0;
- logEntityInvalidValue("attack_power", eInfo.classType.getName(), "0");
+ logEntityInvalidValue("attack_power", name, "0");
}
}
catch (NullPointerException e)
{
- logEntityMissingRequiredValue("attack_power", eInfo.classType.getName());
+ logEntityMissingRequiredValue("attack_power", name);
continue;
}
if(eInfo.attackProbability < 0 || eInfo.attackProbability > 100)
{
eInfo.attackProbability = 35;
- logEntityInvalidValue("attack_probability", eInfo.classType.getName(), "35");
+ logEntityInvalidValue("attack_probability", name, "35");
}
}
catch (NullPointerException e)
{
- logEntityMissingRequiredValue("attack_probability", eInfo.classType.getName());
+ logEntityMissingRequiredValue("attack_probability", name);
continue;
}
if(eInfo.attackEffectProbability < 0 || eInfo.attackEffectProbability > 100)
{
eInfo.attackEffectProbability = 35;
- logEntityInvalidValue("attack_effect", eInfo.classType.getName(), "35");
+ logEntityInvalidValue("attack_effect", name, "35");
}
}
}
catch (NullPointerException e)
{
eInfo.attackEffect = EntityInfo.Effect.UNKNOWN;
+ logEntityMissingOptionalValue("attack_effect", name, "unknown");
}
try
if(eInfo.attackVariance < 0)
{
eInfo.attackVariance = 0;
- logEntityInvalidValue("attack_variance", eInfo.classType.getName(), "0");
+ logEntityInvalidValue("attack_variance", name, "0");
}
}
catch (NullPointerException e)
{
eInfo.attackVariance = 0;
+ logEntityMissingOptionalValue("attack_variance", name, "0");
}
try
if(eInfo.defenseDamage < 0)
{
eInfo.defenseDamage = 0;
- logEntityInvalidValue("defense_damage", eInfo.classType.getName(), "0");
+ logEntityInvalidValue("defense_damage", name, "0");
}
else
{
if(eInfo.defenseDamageProbability < 0 || eInfo.defenseDamageProbability > 100)
{
eInfo.defenseDamageProbability = 35;
- logEntityInvalidValue("defense_damage_probability", eInfo.classType.getName(), "35");
+ logEntityInvalidValue("defense_damage_probability", name, "35");
}
}
}
catch (NullPointerException e)
{
eInfo.defenseDamage = 0;
+ logEntityMissingOptionalValue("defense_damage", name, "0");
}
try
if(eInfo.evasion < 0 || eInfo.evasion > 100)
{
eInfo.evasion = 20;
- logEntityInvalidValue("evasion", eInfo.classType.getName(), "20");
+ logEntityInvalidValue("evasion", name, "20");
}
}
catch (NullPointerException e)
{
- logEntityMissingRequiredValue("evasion", eInfo.classType.getName());
+ logEntityMissingRequiredValue("evasion", name);
continue;
}
}
catch (NullPointerException e)
{
- logEntityMissingRequiredValue("speed", eInfo.classType.getName());
+ logEntityMissingRequiredValue("speed", name);
+ continue;
}
try
catch (NullPointerException e)
{
eInfo.ignoreBattle = false;
+ logEntityMissingOptionalValue("ignore_battle", name, "false");
}
eInfo.category = entity.getString("category");
if(eInfo.category == null)
{
- logEntityMissingRequiredValue("category", eInfo.classType.getName());
+ logEntityMissingRequiredValue("category", name);
continue;
}
}
catch (NullPointerException e)
{
- logEntityMissingRequiredValue("decision_attack_probability", eInfo.classType.getName());
+ logEntityMissingRequiredValue("decision_attack_probability", name);
continue;
}
}
catch (NullPointerException e)
{
- logEntityMissingRequiredValue("decision_defend_probability", eInfo.classType.getName());
+ logEntityMissingRequiredValue("decision_defend_probability", name);
continue;
}
}
catch (NullPointerException e)
{
- logEntityMissingRequiredValue("decision_flee_probability", eInfo.classType.getName());
+ logEntityMissingRequiredValue("decision_flee_probability", name);
continue;
}
- entityInfoMap.put(eInfo.classType.getName(), eInfo);
+ if(eInfo.classType != null)
+ {
+ entityInfoMap.put(eInfo.classType.getName(), eInfo);
+ }
+ else if(!eInfo.customName.isEmpty())
+ {
+ customEntityInfoMap.put(eInfo.customName, eInfo);
+ }
+ else
+ {
+ logger.error("Cannot add entity to internal config, no \"name\" or \"custom_name\"");
+ }
}
}
return true;
logger.error("Entity \"" + name + "\" does not have option \"" + option + "\", skipping...");
}
+ private void logEntityMissingOptionalValue(String option, String name, String defaultValue)
+ {
+ logger.info("Entity \"" + name + "\" does not have optional option \"" + option + "\", defaulting to \"" + defaultValue + "\"...");
+ }
+
private String getRegexEntityName(String name)
{
String regex = "^\\s*name\\s*=\\s*";
regex += "(\"" + name + "\"";
- regex += "|'" + name + "')";
+ regex += "|'" + name + "'";
+ regex += "|\"\"\"" + name + "\"\"\"";
+ regex += "|'''" + name + "''')";
+ return regex;
+ }
+
+ private String getRegexCustomEntityName(String name)
+ {
+ String regex = "^\\s*custom_name\\s*=\\s*";
+ regex += "(\"" + name + "\"";
+ regex += "|'" + name + "'";
+ regex += "|\"\"\"" + name + "\"\"\"";
+ regex += "|'''" + name + "''')";
return regex;
}
private boolean addEntityEntry(EntityInfo eInfo)
{
+ if(eInfo.classType == null && eInfo.customName.isEmpty())
+ {
+ logger.error("addEntityEntry: Got invalid eInfo, no name of any type");
+ return false;
+ }
try
{
File config = new File(TurnBasedMinecraftMod.CONFIG_FILE_PATH);
FileWriter fw = new FileWriter(config, true);
fw.write("[[server_config.entity]]\n");
- fw.write("name = \"" + eInfo.classType.getName() + "\"\n");
+ if(eInfo.classType != null)
+ {
+ fw.write("name = \"" + eInfo.classType.getName() + "\"\n");
+ }
+ else
+ {
+ fw.write("custom_name = \"" + eInfo.customName + "\"\n");
+ }
fw.write("attack_power = " + eInfo.attackPower + "\n");
fw.write("attack_probability = " + eInfo.attackProbability + "\n");
if(eInfo.attackVariance > 0)
fw.write("speed = " + eInfo.speed + "\n");
if(eInfo.ignoreBattle)
{
- fw.write("ignoreBattle = true\n");
+ fw.write("ignore_battle = true\n");
}
fw.write("category = \"" + eInfo.category + "\"\n");
fw.write("decision_attack_probability = " + eInfo.decisionAttack + "\n");
fw.write("decision_flee_probability = " + eInfo.decisionFlee + "\n");
fw.close();
- entityInfoMap.put(eInfo.classType.getName(), eInfo);
+ if(eInfo.classType != null)
+ {
+ entityInfoMap.put(eInfo.classType.getName(), eInfo);
+ }
+ else
+ {
+ customEntityInfoMap.put(eInfo.customName, eInfo);
+ }
}
catch (Throwable t)
{
- logger.error("Failed to add entity entry (name = \"" + eInfo.classType.getName() + "\")");
+ if(eInfo.classType != null)
+ {
+ logger.error("Failed to add entity entry (name = \"" + eInfo.classType.getName() + "\")");
+ }
+ else
+ {
+ logger.error("Failed to add custom entity entry (custom_name = \"" + eInfo.customName + "\")");
+ }
return false;
}
return true;
}
int nameIndex = -1;
+ if(eInfo.classType != null)
{
Pattern p = Pattern.compile(getRegexEntityName(eInfo.classType.getName()), Pattern.MULTILINE);
Matcher m = p.matcher(cached);
nameIndex = m.start();
}
}
+ else if(!eInfo.customName.isEmpty())
+ {
+ Pattern p = Pattern.compile(getRegexCustomEntityName(eInfo.customName), Pattern.MULTILINE);
+ Matcher m = p.matcher(cached);
+ if(m.find())
+ {
+ nameIndex = m.start();
+ }
+ }
+ else
+ {
+ logger.error("EntityInfo does not have classType or customName, cannot edit/add");
+ return false;
+ }
int entryIndex = -1;
int nextIndex = -1;
if(nameIndex != -1)
}
else
{
- logger.warn("editEntityEntry: could not find entry for \"" + eInfo.classType.getName() + "\", skipping to adding it...");
+ if(eInfo.classType != null)
+ {
+ logger.warn("editEntityEntry: could not find entry for \"" + eInfo.classType.getName() + "\", skipping to adding it...");
+ }
+ else if(!eInfo.customName.isEmpty())
+ {
+ logger.warn("editEntityEntry: could not find entry for \"" + eInfo.customName + "\", skipping to adding it...");
+ }
return addEntityEntry(eInfo);
}
*/
public EntityInfo getEntityInfo(String classFullName)
{
- return entityInfoMap.get(classFullName).clone();
+ EntityInfo eInfo = entityInfoMap.get(classFullName);
+ if(eInfo != null)
+ {
+ eInfo = eInfo.clone();
+ }
+ return eInfo;
}
protected EntityInfo getEntityInfoReference(String classFullName)
return null;
}
+ /**
+ * Returns a clone of an EntityInfo (to prevent editing it).
+ * @param customName
+ * @return a clone of the stored custom EntityInfo or null if invalid String
+ */
+ public EntityInfo getCustomEntityInfo(String customName)
+ {
+ EntityInfo eInfo = customEntityInfoMap.get(customName);
+ if(eInfo != null)
+ {
+ eInfo = eInfo.clone();
+ }
+ return eInfo;
+ }
+
+ protected EntityInfo getCustomEntityInfoReference(String customName)
+ {
+ return customEntityInfoMap.get(customName);
+ }
+
private int getConfigFileVersion(InputStream io)
{
int version = 0;