Fix minor Exp discrepancy

Add 'l<level>' syntax to /exp command
EG: /exp set l20
This commit is contained in:
KHobbits 2012-08-08 02:06:45 +01:00
parent 101ae201d6
commit ba6cc5a9ee
2 changed files with 44 additions and 14 deletions

View File

@ -3,6 +3,7 @@ package com.earth2me.essentials.commands;
import static com.earth2me.essentials.I18n._; import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.User; import com.earth2me.essentials.User;
import com.earth2me.essentials.craftbukkit.SetExpFix; import com.earth2me.essentials.craftbukkit.SetExpFix;
import java.util.Locale;
import org.bukkit.Server; import org.bukkit.Server;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -103,7 +104,7 @@ public class Commandexp extends EssentialsCommand
} }
} }
private void expMatch(final Server server, final CommandSender sender, final String match, final String amount, final boolean toggle) throws NotEnoughArgumentsException private void expMatch(final Server server, final CommandSender sender, final String match, String amount, final boolean toggle) throws NotEnoughArgumentsException
{ {
boolean foundUser = false; boolean foundUser = false;
for (Player matchPlayer : server.matchPlayer(match)) for (Player matchPlayer : server.matchPlayer(match))
@ -124,9 +125,25 @@ public class Commandexp extends EssentialsCommand
sender.sendMessage(_("exp", target.getDisplayName(), SetExpFix.getTotalExperience(target), target.getLevel(), SetExpFix.getExpUntilNextLevel(target))); sender.sendMessage(_("exp", target.getDisplayName(), SetExpFix.getTotalExperience(target), target.getLevel(), SetExpFix.getExpUntilNextLevel(target)));
} }
private void setExp(final CommandSender sender, final User target, final String strAmount, final boolean give) private void setExp(final CommandSender sender, final User target, String strAmount, final boolean give)
{ {
Long amount = Long.parseLong(strAmount); Long amount;
strAmount = strAmount.toLowerCase(Locale.ENGLISH);
if (strAmount.startsWith("l"))
{
strAmount = strAmount.substring(1);
int neededLevel = Integer.parseInt(strAmount);
if (give)
{
neededLevel += target.getLevel();
}
amount = (long)SetExpFix.getExpToLevel(neededLevel);
SetExpFix.setTotalExperience(target, 0);
}
else {
amount = Long.parseLong(strAmount);
}
if (give) if (give)
{ {
amount += SetExpFix.getTotalExperience(target); amount += SetExpFix.getTotalExperience(target);

View File

@ -22,7 +22,7 @@ public class SetExpFix
int amount = exp; int amount = exp;
while (amount > 0) while (amount > 0)
{ {
final int expToLevel = getExpToLevel(player); final int expToLevel = getExpAtLevel(player);
amount -= expToLevel; amount -= expToLevel;
if (amount >= 0) if (amount >= 0)
{ {
@ -39,43 +39,56 @@ public class SetExpFix
} }
} }
private static int getExpToLevel(final Player player) private static int getExpAtLevel(final Player player)
{ {
return getExpToLevel(player.getLevel()); return getExpAtLevel(player.getLevel());
} }
private static int getExpToLevel(final int level) public static int getExpAtLevel(final int level)
{ {
if (level >= 30) if (level > 29)
{ {
return 62 + (level - 30) * 7; return 62 + (level - 30) * 7;
} }
if (level >= 15) if (level > 15)
{ {
return 17 + (level - 15) * 3; return 17 + (level - 15) * 3;
} }
return 17; return 17;
} }
public static int getExpToLevel(final int level)
{
int currentLevel = 0;
int exp = 0;
while (currentLevel < level)
{
exp += getExpAtLevel(currentLevel);
currentLevel++;
}
return exp;
}
//This method is required because the bukkit player.getTotalExperience() method, shows exp that has been 'spent'. //This method is required because the bukkit player.getTotalExperience() method, shows exp that has been 'spent'.
//Without this people would be able to use exp and then still sell it. //Without this people would be able to use exp and then still sell it.
public static int getTotalExperience(final Player player) public static int getTotalExperience(final Player player)
{ {
int exp = (int)Math.round(getExpToLevel(player) * player.getExp()); int exp = (int)Math.round(getExpAtLevel(player) * player.getExp());
int currentLevel = player.getLevel(); int currentLevel = player.getLevel();
while (currentLevel > 0) while (currentLevel > 0)
{ {
currentLevel--; currentLevel--;
exp += getExpToLevel(currentLevel); exp += getExpAtLevel(currentLevel);
} }
return exp; return exp;
} }
public static int getExpUntilNextLevel(final Player player) public static int getExpUntilNextLevel(final Player player)
{ {
int exp = (int)Math.round(getExpToLevel(player) * player.getExp()); int exp = (int)Math.round(getExpAtLevel(player) * player.getExp());
int nextLevel = player.getLevel() + 1; int nextLevel = player.getLevel();
return getExpToLevel(nextLevel) - exp; return getExpAtLevel(nextLevel) - exp;
} }
} }