Adds vanilla syntax to /exp command

This commit is contained in:
GunfighterJ 2013-01-18 18:20:05 -06:00
parent 313fd30ce9
commit f8ce80c18a

View File

@ -2,6 +2,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.Util;
import com.earth2me.essentials.craftbukkit.SetExpFix; import com.earth2me.essentials.craftbukkit.SetExpFix;
import java.util.Locale; import java.util.Locale;
import org.bukkit.Server; import org.bukkit.Server;
@ -23,6 +24,33 @@ public class Commandexp extends EssentialsCommand
{ {
showExp(user, user); showExp(user, user);
} }
else if (Util.isInt(args[0].toLowerCase().replace("l", ""))) // check vanilla syntax
{
int lvl = Integer.parseInt(args[0].toLowerCase().replace("l", ""));
if (args.length > 1 && user.isAuthorized("essentials.exp.give.others"))
{
if (args[0].toLowerCase(Locale.ENGLISH).contains("l"))
{
addLevel(server, user, lvl, args[1]);
}
else
{
expMatch(server, user, args[1], args[0], true);
}
}
else
{
if (args[0].toLowerCase(Locale.ENGLISH).contains("l"))
{
addLevel(server, user, lvl, user.getName());
}
else
{
expMatch(server, user, user.getName(), args[0], true);
}
}
}
else if (args[0].equalsIgnoreCase("set") && user.isAuthorized("essentials.exp.set")) else if (args[0].equalsIgnoreCase("set") && user.isAuthorized("essentials.exp.set"))
{ {
if (args.length == 3 && user.isAuthorized("essentials.exp.set.others")) if (args.length == 3 && user.isAuthorized("essentials.exp.set.others"))
@ -66,26 +94,43 @@ public class Commandexp extends EssentialsCommand
@Override @Override
public void run(final Server server, final CommandSender sender, final String commandLabel, final String[] args) throws Exception public void run(final Server server, final CommandSender sender, final String commandLabel, final String[] args) throws Exception
{ {
if (args.length < 1) String parseLevel = args[0].toLowerCase().replace("l", "");
int lvl = Integer.parseInt(parseLevel);
if (Util.isInt(parseLevel))
{ {
throw new NotEnoughArgumentsException(); if (args[0].toLowerCase(Locale.ENGLISH).contains("l"))
} {
else if (args.length > 2 && args[0].equalsIgnoreCase("set")) addLevel(server, sender, lvl, args[1]);
{ }
expMatch(server, sender, args[1], args[2], false); else
} {
else if (args.length > 2 && args[0].equalsIgnoreCase("give")) expMatch(server, sender, args[1], args[0], true);
{ }
expMatch(server, sender, args[1], args[2], true);
} }
else else
{ {
String match = args[0].trim(); if (args.length < 1)
if (args.length == 2)
{ {
match = args[1].trim(); throw new NotEnoughArgumentsException();
}
else if (args.length > 2 && args[0].equalsIgnoreCase("set"))
{
expMatch(server, sender, args[1], args[2], false);
}
else if (args.length > 2 && args[0].equalsIgnoreCase("give"))
{
expMatch(server, sender, args[1], args[2], true);
}
else
{
String match = args[0].trim();
if (args.length == 2)
{
match = args[1].trim();
}
showMatch(server, sender, match);
} }
showMatch(server, sender, match);
} }
} }
@ -119,6 +164,31 @@ public class Commandexp extends EssentialsCommand
} }
} }
private void addLevel(final Server server, final CommandSender sender, final int level, final String target) throws NotEnoughArgumentsException
{
boolean foundUser = false;
for (Player matchPlayer : server.matchPlayer(target))
{
final User user = ess.getUser(matchPlayer);
final int curLevel = user.getLevel();
final int fLevel = curLevel + level;
if (fLevel < 0)
{
user.setLevel(0);
user.setExp(0F);
}
else
{
user.setLevel(fLevel);
}
foundUser = true;
}
if (!foundUser)
{
throw new NotEnoughArgumentsException(_("playerNotFound"));
}
}
private void showExp(final CommandSender sender, final User target) private void showExp(final CommandSender sender, final User target)
{ {
final int totalExp = SetExpFix.getTotalExperience(target); final int totalExp = SetExpFix.getTotalExperience(target);
@ -131,7 +201,7 @@ public class Commandexp extends EssentialsCommand
strAmount = strAmount.toLowerCase(Locale.ENGLISH); strAmount = strAmount.toLowerCase(Locale.ENGLISH);
if (strAmount.startsWith("l") || strAmount.endsWith("l")) if (strAmount.startsWith("l") || strAmount.endsWith("l"))
{ {
strAmount = strAmount.replaceAll("l",""); strAmount = strAmount.replaceAll("l", "");
int neededLevel = Integer.parseInt(strAmount); int neededLevel = Integer.parseInt(strAmount);
if (give) if (give)
{ {
@ -140,7 +210,8 @@ public class Commandexp extends EssentialsCommand
amount = (long)SetExpFix.getExpToLevel(neededLevel); amount = (long)SetExpFix.getExpToLevel(neededLevel);
SetExpFix.setTotalExperience(target, 0); SetExpFix.setTotalExperience(target, 0);
} }
else { else
{
amount = Long.parseLong(strAmount); amount = Long.parseLong(strAmount);
if (amount < 0 || amount > Integer.MAX_VALUE) if (amount < 0 || amount > Integer.MAX_VALUE)
{ {