Formatting and localizing.

This commit is contained in:
GJ 2013-08-21 13:18:50 -04:00 committed by TfT_02
parent 68e433b3b7
commit 223649ec28
7 changed files with 81 additions and 70 deletions

View File

@ -1,11 +1,14 @@
package com.gmail.nossr50.commands.database; package com.gmail.nossr50.commands.database;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor; import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.util.StringUtil;
import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.datatypes.experience.FormulaType; import com.gmail.nossr50.datatypes.experience.FormulaType;
@ -15,6 +18,18 @@ import com.gmail.nossr50.util.player.UserManager;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
public class McconvertCommand implements TabExecutor { public class McconvertCommand implements TabExecutor {
private static final List<String> FORMULA_TYPES;
static {
ArrayList<String> types = new ArrayList<String>();
for (FormulaType type : FormulaType.values()) {
types.add(type.toString());
}
Collections.sort(types);
FORMULA_TYPES = ImmutableList.copyOf(types);
}
/* /*
* Do this later; Use mcconvert instead of mmoupdate: * Do this later; Use mcconvert instead of mmoupdate:
@ -28,16 +43,12 @@ public class McconvertCommand implements TabExecutor {
@Override @Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
switch (args.length) { switch (args.length) {
case 0:
sender.sendMessage("Usage is /mcconvert <linear | exponential>");
return true;
case 1: case 1:
FormulaType previousType = mcMMO.getFormulaManager().getPreviousFormulaType(); FormulaType previousType = mcMMO.getFormulaManager().getPreviousFormulaType();
FormulaType newType = FormulaType.getFormulaType(args[0].toUpperCase()); FormulaType newType = FormulaType.getFormulaType(args[0].toUpperCase());
if (newType == FormulaType.UNKNOWN) { if (newType == FormulaType.UNKNOWN) {
sender.sendMessage("Unknown formula type! Valid types are: LINEAR and EXPONENTIAL"); sender.sendMessage(LocaleLoader.getString("Commands.mcconvert.Invalid"));
return true; return true;
} }
@ -47,23 +58,30 @@ public class McconvertCommand implements TabExecutor {
} }
sender.sendMessage(LocaleLoader.getString("Commands.mcconvert.Start", previousType.toString(), newType.toString())); sender.sendMessage(LocaleLoader.getString("Commands.mcconvert.Start", previousType.toString(), newType.toString()));
UserManager.saveAll(); UserManager.saveAll();
UserManager.clearAll(); UserManager.clearAll();
new FormulaConversionTask(sender, newType).runTaskLater(mcMMO.p, 1); new FormulaConversionTask(sender, newType).runTaskLater(mcMMO.p, 1);
for (Player player : mcMMO.p.getServer().getOnlinePlayers()) { for (Player player : mcMMO.p.getServer().getOnlinePlayers()) {
UserManager.addUser(player); UserManager.addUser(player);
} }
return true; return true;
default: default:
break;
}
return false; return false;
} }
}
@Override @Override
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) { public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
switch (args.length) {
case 1:
return StringUtil.copyPartialMatches(args[0], FORMULA_TYPES, new ArrayList<String>(FORMULA_TYPES.size()));
default:
return ImmutableList.of(); return ImmutableList.of();
} }
} }
}

View File

@ -61,6 +61,6 @@ public class DatabaseManagerFactory {
} }
public static DatabaseManager createCustomDatabaseManager(Class<? extends DatabaseManager> clazz) throws Throwable { public static DatabaseManager createCustomDatabaseManager(Class<? extends DatabaseManager> clazz) throws Throwable {
return customManager.getConstructor((Class<?>) null).newInstance((Object[]) null); return customManager.getConstructor((Class<?>) clazz).newInstance((Object[]) null);
} }
} }

View File

@ -127,7 +127,7 @@ public class mcMMO extends JavaPlugin {
PartyManager.loadParties(); PartyManager.loadParties();
formulaManager = new FormulaManager(this); formulaManager = new FormulaManager();
for (Player player : getServer().getOnlinePlayers()) { for (Player player : getServer().getOnlinePlayers()) {
UserManager.addUser(player); // In case of reload add all users back into UserManager UserManager.addUser(player); // In case of reload add all users back into UserManager

View File

@ -25,7 +25,7 @@ public class FormulaConversionTask extends BukkitRunnable {
@Override @Override
public void run() { public void run() {
for (String playerName : mcMMO.getDatabaseManager().getStoredUsers()) { for (String playerName : mcMMO.getDatabaseManager().getStoredUsers()) {
McMMOPlayer mcMMOPlayer = UserManager.getPlayer(playerName); McMMOPlayer mcMMOPlayer = UserManager.getPlayer(playerName, true);
PlayerProfile profile; PlayerProfile profile;
// If the mcMMOPlayer doesn't exist, create a temporary profile and check if it's present in the database. If it's not, abort the process. // If the mcMMOPlayer doesn't exist, create a temporary profile and check if it's present in the database. If it's not, abort the process.
@ -53,21 +53,18 @@ public class FormulaConversionTask extends BukkitRunnable {
private void editValues(PlayerProfile profile) { private void editValues(PlayerProfile profile) {
mcMMO.p.debug("========================================================================"); mcMMO.p.debug("========================================================================");
mcMMO.p.debug("Conversion report for " + profile.getPlayerName() + ":"); mcMMO.p.debug("Conversion report for " + profile.getPlayerName() + ":");
for (SkillType skillType : SkillType.values()) { for (SkillType skillType : SkillType.nonChildSkills()) {
if (skillType.isChildSkill()) { int oldLevel = profile.getSkillLevel(skillType);
continue; int oldXPLevel = profile.getSkillXpLevel(skillType);
} int totalOldXP = mcMMO.getFormulaManager().calculateTotalExperience(oldLevel, oldXPLevel);
int[] oldExperienceValues = new int[2];
oldExperienceValues[0] = profile.getSkillLevel(skillType);
oldExperienceValues[1] = profile.getSkillXpLevel(skillType);
int totalOldXP = mcMMO.getFormulaManager().calculateTotalExperience(oldExperienceValues);
if (totalOldXP == 0) { if (totalOldXP == 0) {
continue; continue;
} }
double modifier = ExperienceConfig.getInstance().getExpModifier(); double modifier = ExperienceConfig.getInstance().getExpModifier();
//TODO: Why not validate like the other configs?
if (modifier <= 0) { if (modifier <= 0) {
modifier = 1; modifier = 1;
mcMMO.p.getLogger().warning("Invalid value found for Conversion.Exp_Modifier! Skipping using the modifier..."); mcMMO.p.getLogger().warning("Invalid value found for Conversion.Exp_Modifier! Skipping using the modifier...");
@ -80,8 +77,8 @@ public class FormulaConversionTask extends BukkitRunnable {
mcMMO.p.debug(" Skill: " + skillType.toString()); mcMMO.p.debug(" Skill: " + skillType.toString());
mcMMO.p.debug(" OLD:"); mcMMO.p.debug(" OLD:");
mcMMO.p.debug(" Level: " + oldExperienceValues[0]); mcMMO.p.debug(" Level: " + oldLevel);
mcMMO.p.debug(" XP " + oldExperienceValues[1]); mcMMO.p.debug(" XP " + oldXPLevel);
mcMMO.p.debug(" Total XP " + totalOldXP); mcMMO.p.debug(" Total XP " + totalOldXP);
mcMMO.p.debug(" NEW:"); mcMMO.p.debug(" NEW:");

View File

@ -297,7 +297,7 @@ public final class CommandRegistrationManager {
command.setDescription(LocaleLoader.getString("Commands.Description.mcconvert")); command.setDescription(LocaleLoader.getString("Commands.Description.mcconvert"));
command.setPermission("mcmmo.commands.mcconvert"); command.setPermission("mcmmo.commands.mcconvert");
command.setPermissionMessage(permissionsMessage); command.setPermissionMessage(permissionsMessage);
command.setUsage(LocaleLoader.getString("Commands.Usage.0", "mcconvert")); command.setUsage(LocaleLoader.getString("Commands.Usage.1", "mcconvert", "<linear | exponential>"));
command.setExecutor(new McconvertCommand()); command.setExecutor(new McconvertCommand());
} }

View File

@ -13,7 +13,6 @@ import com.gmail.nossr50.datatypes.skills.SkillType;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
public class FormulaManager { public class FormulaManager {
private final mcMMO plugin;
private static String formulaFilePath = mcMMO.getFlatFileDirectory() + "formula.yml"; private static String formulaFilePath = mcMMO.getFlatFileDirectory() + "formula.yml";
private static File formulaFile = new File(formulaFilePath); private static File formulaFile = new File(formulaFilePath);
@ -23,9 +22,7 @@ public class FormulaManager {
private FormulaType previousFormula; private FormulaType previousFormula;
public FormulaManager(final mcMMO plugin) { public FormulaManager() {
this.plugin = plugin;
loadFormula(); loadFormula();
} }
@ -52,18 +49,17 @@ public class FormulaManager {
* the amount of levels and experience, using the previously * the amount of levels and experience, using the previously
* used formula type. * used formula type.
* *
* @param oldExperienceValues level and experience amount * @param skillLevel Amount of levels
* @param skillXPLevel Amount of experience
* @return The total amount of experience * @return The total amount of experience
*/ */
public int calculateTotalExperience(int[] oldExperienceValues) { public int calculateTotalExperience(int skillLevel, int skillXPLevel) {
int totalXP = 0; int totalXP = 0;
int skillLevel = oldExperienceValues[0];
int skillXPLevel = oldExperienceValues[1];
for (int level = 0; level < skillLevel; level++) { for (int level = 0; level < skillLevel; level++) {
totalXP += getCachedXpToLevel(level, previousFormula); totalXP += getCachedXpToLevel(level, previousFormula);
} }
totalXP += skillXPLevel; totalXP += skillXPLevel;
return totalXP; return totalXP;
@ -79,25 +75,23 @@ public class FormulaManager {
* @return the amount of levels and experience * @return the amount of levels and experience
*/ */
public int[] calculateNewLevel(SkillType skillType, int experience, FormulaType formulaType) { public int[] calculateNewLevel(SkillType skillType, int experience, FormulaType formulaType) {
int[] newExperienceValues = new int[2];
int newLevel = 0; int newLevel = 0;
int remainder = 0; int remainder = 0;
int maxLevel = Config.getInstance().getLevelCap(skillType); int maxLevel = Config.getInstance().getLevelCap(skillType);
while (experience > 0 && newLevel < maxLevel) { while (experience > 0 && newLevel < maxLevel) {
int experienceToNextLevel = getCachedXpToLevel(newLevel, formulaType); int experienceToNextLevel = getCachedXpToLevel(newLevel, formulaType);
if (experience - experienceToNextLevel >= 0) {
newLevel++; if (experience - experienceToNextLevel < 0) {
experience -= experienceToNextLevel;
}
else {
remainder = experience; remainder = experience;
break; break;
} }
newLevel++;
experience -= experienceToNextLevel;
} }
newExperienceValues[0] = newLevel;
newExperienceValues[1] = remainder; return new int[]{newLevel, remainder};
return newExperienceValues;
} }
/** /**
@ -111,45 +105,46 @@ public class FormulaManager {
*/ */
public int getCachedXpToLevel(int level, FormulaType formulaType) { public int getCachedXpToLevel(int level, FormulaType formulaType) {
int experience; int experience;
double multiplier;
switch (formulaType) { switch (formulaType) {
case UNKNOWN: case UNKNOWN:
case LINEAR: case LINEAR:
if (experienceNeededLinear.containsKey(level)) { if (!experienceNeededLinear.containsKey(level)) {
experience = experienceNeededLinear.get(level); multiplier = ExperienceConfig.getInstance().getLinearMultiplier();
return experience;
}
double multiplier = ExperienceConfig.getInstance().getLinearMultiplier(); //TODO: Validate at load?
if (multiplier <= 0) { if (multiplier <= 0) {
multiplier = 20; multiplier = 20;
} }
experience = (int) Math.floor(ExperienceConfig.getInstance().getLinearBase() + level * multiplier); experience = (int) Math.floor(ExperienceConfig.getInstance().getLinearBase() + level * multiplier);
experienceNeededLinear.put(level, experience); experienceNeededLinear.put(level, experience);
return experience;
case EXPONENTIAL:
if (experienceNeededExponential.containsKey(level)) {
experience = experienceNeededExponential.get(level);
return experience;
} }
return experienceNeededLinear.get(level);
case EXPONENTIAL:
if (!experienceNeededExponential.containsKey(level)) {
multiplier = ExperienceConfig.getInstance().getExponentialMultiplier(); multiplier = ExperienceConfig.getInstance().getExponentialMultiplier();
double exponent = ExperienceConfig.getInstance().getExponentialExponent(); double exponent = ExperienceConfig.getInstance().getExponentialExponent();
int base = ExperienceConfig.getInstance().getExponentialBase(); int base = ExperienceConfig.getInstance().getExponentialBase();
//TODO: Validate at load?
if (multiplier <= 0) { if (multiplier <= 0) {
multiplier = 0.1; multiplier = 0.1;
} }
//TODO: Validate at load?
if (exponent <= 0) { if (exponent <= 0) {
exponent = 1.80; exponent = 1.80;
} }
experience = (int) Math.floor(multiplier * Math.pow(level, exponent) + base); experience = (int) Math.floor(multiplier * Math.pow(level, exponent) + base);
experienceNeededExponential.put(level, experience); experienceNeededExponential.put(level, experience);
return experience; }
return experienceNeededExponential.get(level);
default: default:
return 0; return 0;

View File

@ -447,6 +447,7 @@ Commands.mmoupdate.InvalidType=[[RED]]{0} is not a valid database type.
Commands.mmoupdate.Start=[[GRAY]]Starting conversion from {0} to {1}... Commands.mmoupdate.Start=[[GRAY]]Starting conversion from {0} to {1}...
Commands.mmoupdate.Finish=[[GRAY]]Database migration complete; the {1} database now has all data from the {0} database. Commands.mmoupdate.Finish=[[GRAY]]Database migration complete; the {1} database now has all data from the {0} database.
Commands.mmoshowdb=[[YELLOW]]The currently used database is [[GREEN]]{0} Commands.mmoshowdb=[[YELLOW]]The currently used database is [[GREEN]]{0}
Commands.mcconvert.Invalid=[[RED]]Unknown formula type! Valid types are: [[GREEN]]LINEAR [[RED]]and [[GREEN]]EXPONENTIAL.
Commands.mcconvert.Same=[[RED]]Already using formula type {0} Commands.mcconvert.Same=[[RED]]Already using formula type {0}
Commands.mcconvert.Start=[[GRAY]]Starting conversion from {0} to {1} curve Commands.mcconvert.Start=[[GRAY]]Starting conversion from {0} to {1} curve
Commands.mcconvert.Finish=[[GRAY]]Formula conversion complete; now using an {0} XP curve. Commands.mcconvert.Finish=[[GRAY]]Formula conversion complete; now using an {0} XP curve.