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

View File

@ -61,6 +61,6 @@ public class DatabaseManagerFactory {
}
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();
formulaManager = new FormulaManager(this);
formulaManager = new FormulaManager();
for (Player player : getServer().getOnlinePlayers()) {
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
public void run() {
for (String playerName : mcMMO.getDatabaseManager().getStoredUsers()) {
McMMOPlayer mcMMOPlayer = UserManager.getPlayer(playerName);
McMMOPlayer mcMMOPlayer = UserManager.getPlayer(playerName, true);
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.
@ -53,21 +53,18 @@ public class FormulaConversionTask extends BukkitRunnable {
private void editValues(PlayerProfile profile) {
mcMMO.p.debug("========================================================================");
mcMMO.p.debug("Conversion report for " + profile.getPlayerName() + ":");
for (SkillType skillType : SkillType.values()) {
if (skillType.isChildSkill()) {
continue;
}
for (SkillType skillType : SkillType.nonChildSkills()) {
int oldLevel = profile.getSkillLevel(skillType);
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) {
continue;
}
double modifier = ExperienceConfig.getInstance().getExpModifier();
//TODO: Why not validate like the other configs?
if (modifier <= 0) {
modifier = 1;
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(" OLD:");
mcMMO.p.debug(" Level: " + oldExperienceValues[0]);
mcMMO.p.debug(" XP " + oldExperienceValues[1]);
mcMMO.p.debug(" Level: " + oldLevel);
mcMMO.p.debug(" XP " + oldXPLevel);
mcMMO.p.debug(" Total XP " + totalOldXP);
mcMMO.p.debug(" NEW:");

View File

@ -297,7 +297,7 @@ public final class CommandRegistrationManager {
command.setDescription(LocaleLoader.getString("Commands.Description.mcconvert"));
command.setPermission("mcmmo.commands.mcconvert");
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());
}

View File

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