TurnBasedMinecraftMod/src/main/java/com/seodisparate/TurnBasedMinecraft/common/Config.java

1063 lines
34 KiB
Java
Raw Normal View History

package com.seodisparate.TurnBasedMinecraft.common;
2018-10-20 08:54:32 +00:00
import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
2018-10-20 08:54:32 +00:00
import java.util.regex.Matcher;
import java.util.regex.Pattern;
2018-10-20 08:54:32 +00:00
import net.consensys.cava.toml.*;
import org.apache.logging.log4j.Logger;
public class Config
{
public static final long BATTLE_DECISION_DURATION_NANO_MIN = 5000000000L;
public static final long BATTLE_DECISION_DURATION_NANO_MAX = 60000000000L;
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;
2018-09-13 05:12:04 +00:00
private Set<String> ignoreBattleTypes;
private Logger logger;
2018-09-06 08:08:36 +00:00
private int playerSpeed = 50;
private int playerHasteSpeed = 80;
private int playerSlowSpeed = 20;
private int playerAttackProbability = 100;
private int playerEvasion = 10;
private int defenseDuration = 1;
private int fleeGoodProbability = 90;
2018-10-20 08:54:32 +00:00
private int fleeBadProbability = 35;
private int minimumHitPercentage = 4;
private int maxInBattle = 8;
private Set<String> musicBattleTypes;
private Set<String> musicSillyTypes;
private boolean freezeCombatantsInBattle = false;
private int sillyMusicThreshold = 40;
private int configVersion = 0;
private Set<Integer> battleIgnoringPlayers = null;
private boolean onlyOPsSelfDisableTB = true;
private boolean battleDisabledForAll = false;
private boolean oldBattleBehaviorEnabled = false;
private int leaveBattleCooldownSeconds = 5;
private int aggroStartBattleDistance = 8;
2018-10-20 08:54:32 +00:00
public Config(Logger logger)
{
entityInfoMap = new HashMap<String, EntityInfo>();
customEntityInfoMap = new HashMap<String, EntityInfo>();
2018-09-13 05:12:04 +00:00
ignoreBattleTypes = new HashSet<String>();
this.logger = logger;
musicBattleTypes = new HashSet<String>();
musicSillyTypes = new HashSet<String>();
battleIgnoringPlayers = new HashSet<Integer>();
2018-10-20 08:54:32 +00:00
int internalVersion = getConfigFileVersion(getClass().getResourceAsStream(TurnBasedMinecraftMod.CONFIG_INTERNAL_PATH));
if(internalVersion == 0)
{
logger.error("Failed to check version of internal config file");
}
else
{
configVersion = internalVersion;
}
2018-10-20 08:54:32 +00:00
try
{
File testLoad = new File(TurnBasedMinecraftMod.CONFIG_FILE_PATH);
if(!testLoad.exists())
{
writeConfig();
}
}
catch (Throwable t)
{
logger.error("Failed to check/create-new config file");
}
2018-10-20 08:54:32 +00:00
// parse config
File configFile = new File(TurnBasedMinecraftMod.CONFIG_FILE_PATH);
if(!configFile.exists() || !configFile.canRead())
{
logger.error("Failed to read/parse config file " + TurnBasedMinecraftMod.CONFIG_FILE_PATH);
return;
}
2018-10-20 08:54:32 +00:00
int configVersion = getConfigFileVersion(configFile);
if(configVersion < this.configVersion)
{
logger.warn("Config file " + TurnBasedMinecraftMod.CONFIG_FILENAME + " is older version, renaming...");
moveOldConfig();
try
{
writeConfig();
} catch (Throwable t)
{
logger.error("Failed to write config file!");
}
}
try
{
parseConfig(configFile);
} catch (Throwable t)
{
logger.error("Failed to parse config file!");
}
}
2018-10-20 08:54:32 +00:00
private void writeConfig() throws IOException
{
File configFile = new File(TurnBasedMinecraftMod.CONFIG_FILE_PATH);
File dirs = configFile.getParentFile();
dirs.mkdirs();
InputStream configStream = this.getClass().getResourceAsStream(TurnBasedMinecraftMod.CONFIG_INTERNAL_PATH);
FileOutputStream configOutput = new FileOutputStream(configFile);
byte[] buf = new byte[4096];
int read = 0;
while(read != -1)
{
read = configStream.read(buf);
if(read > 0)
{
configOutput.write(buf, 0, read);
}
}
configStream.close();
configOutput.close();
}
2018-10-20 08:54:32 +00:00
private void moveOldConfig()
{
File configFile = new File(TurnBasedMinecraftMod.CONFIG_FILE_PATH);
if(configFile.exists())
{
configFile.renameTo(new File(TurnBasedMinecraftMod.CONFIG_DIRECTORY
+ "TBM_Config_"
+ DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(LocalDateTime.now())
2018-10-20 08:54:32 +00:00
+ ".toml"));
}
}
2018-10-20 08:54:32 +00:00
private boolean parseConfig(File configFile) throws IOException
{
2018-10-20 08:54:32 +00:00
TomlParseResult parseResult = Toml.parse(configFile.toPath(), TomlVersion.V0_5_0);
// client_config
{
TomlArray battleMusicCategories = parseResult.getArray("client_config.battle_music");
if(battleMusicCategories != null)
{
for (int i = 0; i < battleMusicCategories.size(); ++i)
{
musicBattleTypes.add(battleMusicCategories.getString(i));
}
}
else
{
musicBattleTypes.add("monster");
musicBattleTypes.add("animal");
musicBattleTypes.add("boss");
musicBattleTypes.add("player");
logNotFound("client_config.battle_music");
}
}
{
TomlArray sillyMusicCategories = parseResult.getArray("client_config.silly_music");
if(sillyMusicCategories != null)
{
for (int i = 0; i < sillyMusicCategories.size(); ++i)
{
musicSillyTypes.add(sillyMusicCategories.getString(i));
}
}
else
{
musicSillyTypes.add("passive");
logNotFound("client_config.silly_music");
}
}
try
{
sillyMusicThreshold = parseResult.getLong("client_config.silly_music_threshold").intValue();
}
catch (NullPointerException e)
{
sillyMusicThreshold = 40;
logNotFound("client_config.silly_music_threshold", "40");
}
// server_config
try
{
leaveBattleCooldownSeconds = parseResult.getLong("server_config.leave_battle_cooldown").intValue();
if (leaveBattleCooldownSeconds < 1)
{
leaveBattleCooldownSeconds = 1;
} else if (leaveBattleCooldownSeconds > 10)
{
leaveBattleCooldownSeconds = 10;
}
}
catch (NullPointerException e)
{
leaveBattleCooldownSeconds = 5;
logNotFound("server_config.leave_battle_cooldown", "5");
}
try
{
aggroStartBattleDistance = parseResult.getLong("server_config.aggro_start_battle_max_distance").intValue();
if (aggroStartBattleDistance < 5)
{
aggroStartBattleDistance = 5;
} else if (aggroStartBattleDistance > 50)
{
aggroStartBattleDistance = 50;
}
}
catch (NullPointerException e)
{
aggroStartBattleDistance = 8;
logNotFound("server_config.aggro_start_battle_max_distance", "8");
}
try
{
oldBattleBehaviorEnabled = parseResult.getBoolean("server_config.old_battle_behavior");
}
catch (NullPointerException e)
{
oldBattleBehaviorEnabled = false;
logNotFound("server_config.old_battle_behavior", "false");
}
try
{
onlyOPsSelfDisableTB = !parseResult.getBoolean("server_config.anyone_can_disable_tbm_for_self");
}
catch (NullPointerException e)
{
onlyOPsSelfDisableTB = true;
logNotFound("server_config.anyone_can_disable_tbm_for_self", "false");
}
try
{
maxInBattle = parseResult.getLong("server_config.max_in_battle").intValue();
if (maxInBattle < 2)
{
maxInBattle = 2;
}
}
catch (NullPointerException e)
{
maxInBattle = 8;
logNotFound("server_config.max_in_battle", "8");
}
try
{
freezeCombatantsInBattle = parseResult.getBoolean("server_config.freeze_battle_combatants");
}
catch (NullPointerException e)
{
2018-10-20 08:54:32 +00:00
freezeCombatantsInBattle = false;
logNotFound("server_config.freeze_battle_combatants", "false");
}
try
{
TomlArray ignoreTypes = parseResult.getArray("server_config.ignore_battle_types");
for(int i = 0; i < ignoreTypes.size(); ++i)
{
ignoreBattleTypes.add(ignoreTypes.getString(i));
}
}
catch (NullPointerException e)
{
ignoreBattleTypes.add("passive");
ignoreBattleTypes.add("boss");
logNotFound("server_config.ignore_battle_types");
}
try
{
playerSpeed = parseResult.getLong("server_config.player_speed").intValue();
}
catch (NullPointerException e)
{
playerSpeed = 50;
logNotFound("server_config.player_speed", "50");
}
try
{
playerHasteSpeed = parseResult.getLong("server_config.player_haste_speed").intValue();
}
catch (NullPointerException e)
{
playerHasteSpeed = 80;
logNotFound("server_config.player_haste_speed", "80");
}
try
{
playerSlowSpeed = parseResult.getLong("server_config.player_slow_speed").intValue();
}
catch (NullPointerException e)
{
playerSlowSpeed = 20;
logNotFound("server_config.player_slow_speed", "20");
}
try
{
playerAttackProbability = parseResult.getLong("server_config.player_attack_probability").intValue();
}
catch (NullPointerException e)
{
playerAttackProbability = 90;
logNotFound("server_config.player_attack_probability", "90");
}
try
{
playerEvasion = parseResult.getLong("server_config.player_evasion").intValue();
}
catch (NullPointerException e)
{
playerEvasion = 10;
logNotFound("server_config.player_evasion", "10");
}
try
{
defenseDuration = parseResult.getLong("server_config.defense_duration").intValue();
}
catch (NullPointerException e)
{
defenseDuration = 1;
logNotFound("server_config.defense_duration", "1");
}
try
{
fleeGoodProbability = parseResult.getLong("server_config.flee_good_probability").intValue();
}
catch (NullPointerException e)
{
fleeGoodProbability = 90;
logNotFound("server_config.flee_good_probability", "90");
}
try
{
fleeBadProbability = parseResult.getLong("server_config.flee_bad_probability").intValue();
}
catch (NullPointerException e)
{
fleeBadProbability = 35;
logNotFound("server_config.flee_bad_probability", "35");
}
try
{
minimumHitPercentage = parseResult.getLong("server_config.minimum_hit_percentage").intValue();
if (minimumHitPercentage < 1)
{
minimumHitPercentage = 1;
}
}
catch (NullPointerException e)
{
minimumHitPercentage = 4;
logNotFound("server_config.minimum_hit_percentage", "4");
}
try
{
battleDecisionDurationNanos = parseResult.getLong("server_config.battle_turn_time_seconds") * 1000000000L;
if(battleDecisionDurationNanos < BATTLE_DECISION_DURATION_NANO_MIN)
{
2018-10-20 08:54:32 +00:00
battleDecisionDurationNanos = BATTLE_DECISION_DURATION_NANO_MIN;
logger.warn("Config \"server_config.battle_turn_time_seconds\" too low, defaulting to minimum \"5\"");
}
else if(battleDecisionDurationNanos > BATTLE_DECISION_DURATION_NANO_MAX)
{
battleDecisionDurationNanos = BATTLE_DECISION_DURATION_NANO_MAX;
logger.warn("Config \"server_config.battle_turn_time_seconds\" too high, defaulting to maximum \"60\"");
}
}
catch (NullPointerException e)
{
battleDecisionDurationNanos = BATTLE_DECISION_DURATION_NANO_DEFAULT;
logNotFound("server_config.battle_turn_time_seconds", "15");
}
// entities
TomlArray entityArray = parseResult.getArray("server_config.entity");
if(entityArray != null)
{
for(int i = 0; i < entityArray.size(); ++i)
{
TomlTable entity = entityArray.getTable(i);
EntityInfo eInfo = new EntityInfo();
String name = null;
if(entity.contains("name") && entity.contains("custom_name"))
2018-10-20 08:54:32 +00:00
{
logger.error("Entity cannot have both \"name\" and \"custom_name\" entries");
continue;
2018-10-20 08:54:32 +00:00
}
else if(entity.contains("name"))
{
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;
}
}
else if(entity.contains("custom_name"))
{
eInfo.customName = entity.getString("custom_name");
name = eInfo.customName;
}
else
{
logger.error("Entity must have \"name\" or \"custom_name\" entry");
continue;
}
2018-10-20 08:54:32 +00:00
try
{
2018-10-20 08:54:32 +00:00
eInfo.attackPower = entity.getLong("attack_power").intValue();
if(eInfo.attackPower < 0)
{
2018-10-20 08:54:32 +00:00
eInfo.attackPower = 0;
logEntityInvalidValue("attack_power", name, "0");
}
2018-10-20 08:54:32 +00:00
}
catch (NullPointerException e)
{
logEntityMissingRequiredValue("attack_power", name);
2018-10-20 08:54:32 +00:00
continue;
}
try
{
eInfo.attackProbability = entity.getLong("attack_probability").intValue();
if(eInfo.attackProbability < 0 || eInfo.attackProbability > 100)
{
2018-10-20 08:54:32 +00:00
eInfo.attackProbability = 35;
logEntityInvalidValue("attack_probability", name, "35");
}
}
2018-10-20 08:54:32 +00:00
catch (NullPointerException e)
{
logEntityMissingRequiredValue("attack_probability", name);
2018-10-20 08:54:32 +00:00
continue;
}
try
{
2018-10-20 08:54:32 +00:00
eInfo.attackEffect = EntityInfo.Effect.fromString(entity.getString("attack_effect"));
if(eInfo.attackEffect != EntityInfo.Effect.UNKNOWN)
{
2018-10-20 08:54:32 +00:00
eInfo.attackEffectProbability = entity.getLong("attack_effect_probability").intValue();
if(eInfo.attackEffectProbability < 0 || eInfo.attackEffectProbability > 100)
{
eInfo.attackEffectProbability = 35;
logEntityInvalidValue("attack_effect", name, "35");
2018-10-20 08:54:32 +00:00
}
}
2018-10-20 08:54:32 +00:00
}
catch (NullPointerException e)
{
eInfo.attackEffect = EntityInfo.Effect.UNKNOWN;
logEntityMissingOptionalValue("attack_effect", name, "unknown");
2018-10-20 08:54:32 +00:00
}
try
{
eInfo.attackVariance = entity.getLong("attack_variance").intValue();
if(eInfo.attackVariance < 0)
{
2018-10-20 08:54:32 +00:00
eInfo.attackVariance = 0;
logEntityInvalidValue("attack_variance", name, "0");
}
}
2018-10-20 08:54:32 +00:00
catch (NullPointerException e)
{
eInfo.attackVariance = 0;
logEntityMissingOptionalValue("attack_variance", name, "0");
2018-10-20 08:54:32 +00:00
}
try
{
2018-10-20 08:54:32 +00:00
eInfo.defenseDamage = entity.getLong("defense_damage").intValue();
if(eInfo.defenseDamage < 0)
{
2018-10-20 08:54:32 +00:00
eInfo.defenseDamage = 0;
logEntityInvalidValue("defense_damage", name, "0");
}
else
{
2018-10-20 08:54:32 +00:00
eInfo.defenseDamageProbability = entity.getLong("defense_damage_probability").intValue();
if(eInfo.defenseDamageProbability < 0 || eInfo.defenseDamageProbability > 100)
{
eInfo.defenseDamageProbability = 35;
logEntityInvalidValue("defense_damage_probability", name, "35");
2018-10-20 08:54:32 +00:00
}
}
}
2018-10-20 08:54:32 +00:00
catch (NullPointerException e)
{
2018-10-20 08:54:32 +00:00
eInfo.defenseDamage = 0;
logEntityMissingOptionalValue("defense_damage", name, "0");
2018-10-20 08:54:32 +00:00
}
try
{
eInfo.evasion = entity.getLong("evasion").intValue();
if(eInfo.evasion < 0 || eInfo.evasion > 100)
{
2018-10-20 08:54:32 +00:00
eInfo.evasion = 20;
logEntityInvalidValue("evasion", name, "20");
}
}
2018-10-20 08:54:32 +00:00
catch (NullPointerException e)
{
logEntityMissingRequiredValue("evasion", name);
2018-10-20 08:54:32 +00:00
continue;
}
2018-10-20 08:54:32 +00:00
try
{
2018-10-20 08:54:32 +00:00
eInfo.speed = entity.getLong("speed").intValue();
}
2018-10-20 08:54:32 +00:00
catch (NullPointerException e)
{
logEntityMissingRequiredValue("speed", name);
continue;
}
2018-10-20 08:54:32 +00:00
try
{
2018-10-20 08:54:32 +00:00
eInfo.ignoreBattle = entity.getBoolean("ignore_battle");
}
2018-10-20 08:54:32 +00:00
catch (NullPointerException e)
{
2018-10-20 08:54:32 +00:00
eInfo.ignoreBattle = false;
logEntityMissingOptionalValue("ignore_battle", name, "false");
}
2018-10-20 08:54:32 +00:00
eInfo.category = entity.getString("category");
if(eInfo.category == null)
{
logEntityMissingRequiredValue("category", name);
2018-10-20 08:54:32 +00:00
continue;
}
2018-10-20 08:54:32 +00:00
try
{
2018-10-20 08:54:32 +00:00
eInfo.decisionAttack = entity.getLong("decision_attack_probability").intValue();
}
2018-10-20 08:54:32 +00:00
catch (NullPointerException e)
{
logEntityMissingRequiredValue("decision_attack_probability", name);
2018-10-20 08:54:32 +00:00
continue;
}
2018-10-20 08:54:32 +00:00
try
{
2018-10-20 08:54:32 +00:00
eInfo.decisionDefend = entity.getLong("decision_defend_probability").intValue();
}
2018-10-20 08:54:32 +00:00
catch (NullPointerException e)
{
logEntityMissingRequiredValue("decision_defend_probability", name);
2018-10-20 08:54:32 +00:00
continue;
}
try
{
eInfo.decisionFlee = entity.getLong("decision_flee_probability").intValue();
}
catch (NullPointerException e)
{
logEntityMissingRequiredValue("decision_flee_probability", name);
2018-10-20 08:54:32 +00:00
continue;
}
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\"");
}
2018-10-20 08:54:32 +00:00
}
}
return true;
}
private void logNotFound(String option)
{
logger.warn("Config option \"" + option + "\" not found, setting defaults");
}
private void logNotFound(String option, String defaultValue)
{
logger.warn("Config option \"" + option + "\" not found, defaulting to \"" + defaultValue + "\"");
}
private void logEntityInvalidValue(String option, String name, String defaultValue)
{
logger.warn("Invalid \"" + option + "\" for \"" + name + "\", defaulting to " + defaultValue);
}
private void logEntityMissingRequiredValue(String option, String name)
{
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 + "\"...");
}
2018-10-20 08:54:32 +00:00
private String getRegexEntityName(String name)
{
String regex = "^\\s*name\\s*=\\s*";
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 + "''')";
2018-10-20 08:54:32 +00:00
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;
}
2018-10-20 08:54:32 +00:00
try
{
File config = new File(TurnBasedMinecraftMod.CONFIG_FILE_PATH);
FileWriter fw = new FileWriter(config, true);
fw.write("[[server_config.entity]]\n");
if(eInfo.classType != null)
{
fw.write("name = \"" + eInfo.classType.getName() + "\"\n");
}
else
{
fw.write("custom_name = \"" + eInfo.customName + "\"\n");
}
2018-10-20 08:54:32 +00:00
fw.write("attack_power = " + eInfo.attackPower + "\n");
fw.write("attack_probability = " + eInfo.attackProbability + "\n");
if(eInfo.attackVariance > 0)
{
fw.write("attack_variance = " + eInfo.attackVariance + "\n");
}
if(eInfo.attackEffect != EntityInfo.Effect.UNKNOWN && eInfo.attackEffectProbability > 0)
{
fw.write("attack_effect = \"" + eInfo.attackEffect.toString() + "\"\n");
fw.write("attack_effect_probability = " + eInfo.attackEffectProbability + "\n");
}
if(eInfo.defenseDamage > 0 && eInfo.defenseDamageProbability > 0)
{
fw.write("defense_damage = " + eInfo.defenseDamage + "\n");
fw.write("defense_damage_probability = " + eInfo.defenseDamageProbability + "\n");
}
fw.write("evasion = " + eInfo.evasion + "\n");
fw.write("speed = " + eInfo.speed + "\n");
if(eInfo.ignoreBattle)
{
fw.write("ignore_battle = true\n");
2018-10-20 08:54:32 +00:00
}
fw.write("category = \"" + eInfo.category + "\"\n");
fw.write("decision_attack_probability = " + eInfo.decisionAttack + "\n");
fw.write("decision_defend_probability = " + eInfo.decisionDefend + "\n");
fw.write("decision_flee_probability = " + eInfo.decisionFlee + "\n");
fw.close();
if(eInfo.classType != null)
{
entityInfoMap.put(eInfo.classType.getName(), eInfo);
}
else
{
customEntityInfoMap.put(eInfo.customName, eInfo);
}
2018-10-20 08:54:32 +00:00
}
catch (Throwable t)
{
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 + "\")");
}
2018-10-20 08:54:32 +00:00
return false;
}
return true;
}
protected boolean editEntityEntry(EntityInfo eInfo)
{
try
{
String cached = new String();
char buf[] = new char[1024];
int read = 0;
File config = new File(TurnBasedMinecraftMod.CONFIG_FILE_PATH);
{
FileReader fr = new FileReader(config);
read = fr.read(buf);
while (read != -1)
{
cached += String.valueOf(buf, 0, read);
read = fr.read(buf);
}
fr.close();
}
int nameIndex = -1;
if(eInfo.classType != null)
2018-10-20 08:54:32 +00:00
{
Pattern p = Pattern.compile(getRegexEntityName(eInfo.classType.getName()), Pattern.MULTILINE);
Matcher m = p.matcher(cached);
if(m.find())
{
nameIndex = m.start();
}
2018-10-20 08:54:32 +00:00
}
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;
}
2018-10-20 08:54:32 +00:00
int entryIndex = -1;
int nextIndex = -1;
if(nameIndex != -1)
{
{
2018-10-20 08:54:32 +00:00
Pattern p = Pattern.compile("^\\s*\\[\\[\\s*server_config\\s*\\.\\s*entity\\s*]]", Pattern.MULTILINE);
Matcher m = p.matcher(cached.substring(0, nameIndex));
while(m.find())
{
2018-10-20 08:54:32 +00:00
entryIndex = m.start();
}
2018-10-20 08:54:32 +00:00
if(entryIndex == -1)
{
2018-10-20 08:54:32 +00:00
logger.warn("editEntityEntry: could not find header for entry \"" + eInfo.classType.getName() + "\", skipping to adding it...");
return addEntityEntry(eInfo);
}
}
{
2018-10-20 08:54:32 +00:00
Pattern p = Pattern.compile("^\\s*\\[", Pattern.MULTILINE);
Matcher m = p.matcher(cached.substring(nameIndex));
if(m.find())
{
2018-10-20 08:54:32 +00:00
nextIndex = m.start() + nameIndex;
}
}
}
2018-10-20 08:54:32 +00:00
else
{
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...");
}
2018-10-20 08:54:32 +00:00
return addEntityEntry(eInfo);
}
String cut = null;
if(nextIndex != -1)
{
cut = cached.substring(0, entryIndex) + cached.substring(nextIndex);
}
else
{
cut = cached.substring(0, entryIndex);
}
{
FileWriter fw = new FileWriter(config);
fw.write(cut);
fw.close();
}
return addEntityEntry(eInfo);
}
catch (Throwable t)
{
return false;
}
}
2018-10-20 08:54:32 +00:00
public int getPlayerSpeed()
{
return playerSpeed;
}
public int getPlayerHasteSpeed()
{
return playerHasteSpeed;
}
public int getPlayerSlowSpeed()
{
return playerSlowSpeed;
}
2018-10-20 08:54:32 +00:00
public int getPlayerAttackProbability()
{
return playerAttackProbability;
}
public int getPlayerEvasion()
{
return playerEvasion;
}
2018-10-20 08:54:32 +00:00
public int getDefenseDuration()
{
return defenseDuration;
}
2018-10-20 08:54:32 +00:00
public int getFleeGoodProbability()
{
return fleeGoodProbability;
}
2018-10-20 08:54:32 +00:00
public int getFleeBadProbability()
{
return fleeBadProbability;
}
2018-10-20 08:54:32 +00:00
/**
* Returns a clone of an EntityInfo (to prevent editing it).
* @param classFullName
* @return a clone of the stored EntityInfo or null if invalid String
*/
public EntityInfo getEntityInfo(String classFullName)
{
EntityInfo eInfo = entityInfoMap.get(classFullName);
if(eInfo != null)
{
eInfo = eInfo.clone();
}
return eInfo;
}
2018-10-20 08:54:32 +00:00
protected EntityInfo getEntityInfoReference(String classFullName)
{
return entityInfoMap.get(classFullName);
}
2018-10-20 08:54:32 +00:00
protected EntityInfo getMatchingEntityInfo(Object entity)
{
2018-09-06 08:08:36 +00:00
if(entity == null)
{
return null;
}
EntityInfo matching = entityInfoMap.get(entity.getClass().getName());
if(matching != null && matching.classType.isInstance(entity))
{
return matching;
}
return null;
}
2018-10-20 08:54:32 +00:00
/**
* 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);
}
2018-10-20 08:54:32 +00:00
private int getConfigFileVersion(InputStream io)
{
2018-10-20 08:54:32 +00:00
int version = 0;
try
{
2018-10-20 08:54:32 +00:00
TomlParseResult result = Toml.parse(io, TomlVersion.V0_5_0);
version = result.getLong("version").intValue();
}
catch (Throwable t)
{
2018-10-20 08:54:32 +00:00
// ignored
}
2018-10-20 08:54:32 +00:00
return version;
}
2018-10-20 08:54:32 +00:00
private int getConfigFileVersion(File configFile)
{
int version = 0;
try
{
2018-10-20 08:54:32 +00:00
TomlParseResult result = Toml.parse(configFile.toPath(), TomlVersion.V0_5_0);
version = result.getLong("version").intValue();
}
catch (Throwable t)
{
2018-10-20 08:54:32 +00:00
// ignored
}
2018-10-20 08:54:32 +00:00
return version;
}
2018-10-20 08:54:32 +00:00
2018-09-13 05:12:04 +00:00
public boolean isIgnoreBattleType(String type)
{
return ignoreBattleTypes.contains(type);
}
2018-10-20 08:54:32 +00:00
public int getMinimumHitPercentage()
{
return minimumHitPercentage;
}
2018-10-20 08:54:32 +00:00
public int getMaxInBattle()
{
return maxInBattle;
}
2018-10-20 08:54:32 +00:00
public boolean isBattleMusicType(String type)
{
return musicBattleTypes.contains(type.toLowerCase());
}
2018-10-20 08:54:32 +00:00
public boolean isSillyMusicType(String type)
{
return musicSillyTypes.contains(type.toLowerCase());
}
2018-10-20 08:54:32 +00:00
public boolean isFreezeCombatantsEnabled()
{
return freezeCombatantsInBattle;
}
2018-10-20 08:54:32 +00:00
public int getSillyMusicThreshold()
{
return sillyMusicThreshold;
}
2018-10-20 08:54:32 +00:00
public int getConfigVersion()
{
return configVersion;
}
2018-10-20 08:54:32 +00:00
public long getDecisionDurationNanos()
{
return battleDecisionDurationNanos;
}
2018-10-20 08:54:32 +00:00
public int getDecisionDurationSeconds()
{
return (int)(battleDecisionDurationNanos / 1000000000L);
}
2018-10-20 08:54:32 +00:00
protected void addBattleIgnoringPlayer(int id)
{
battleIgnoringPlayers.add(id);
}
2018-10-20 08:54:32 +00:00
protected void removeBattleIgnoringPlayer(int id)
{
battleIgnoringPlayers.remove(id);
}
2018-10-20 08:54:32 +00:00
protected void clearBattleIgnoringPlayers()
{
battleIgnoringPlayers.clear();
}
protected Set<Integer> getBattleIgnoringPlayers()
{
return battleIgnoringPlayers;
}
2018-10-20 08:54:32 +00:00
public boolean getIfOnlyOPsCanDisableTurnBasedForSelf()
{
return onlyOPsSelfDisableTB;
}
2018-10-20 08:54:32 +00:00
protected void setBattleDisabledForAll(boolean isDisabled)
{
battleDisabledForAll = isDisabled;
}
2018-10-20 08:54:32 +00:00
protected boolean getBattleDisabledForAll()
{
return battleDisabledForAll;
}
2018-10-20 08:54:32 +00:00
public boolean isOldBattleBehaviorEnabled()
{
return oldBattleBehaviorEnabled;
}
2018-10-20 08:54:32 +00:00
public int getLeaveBattleCooldownSeconds()
{
return leaveBattleCooldownSeconds;
}
2018-10-20 08:54:32 +00:00
public long getLeaveBattleCooldownNanos()
{
return (long)leaveBattleCooldownSeconds * 1000000000L;
}
2018-10-20 08:54:32 +00:00
public int getAggroStartBattleDistance()
{
return aggroStartBattleDistance;
}
}