Skip to content

Commit

Permalink
add 'xp' input option to 'take' command
Browse files Browse the repository at this point in the history
Why does bukkit not have a takeExp? Luckily giving a negative just kinda works.
  • Loading branch information
mcmonkey4eva committed Mar 5, 2020
1 parent 5ad509d commit 49a497b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
Expand Up @@ -201,7 +201,7 @@ public void registerCommands() {
registerCoreMember(StatisticCommand.class, "STATISTIC", "statistic [<statistic>] [add/take/set] (<#>) (qualifier:<material>/<entity>) (players:<player>|...)", 2);
registerCoreMember(StrikeCommand.class, "STRIKE", "strike (no_damage) [<location>]", 1);
registerCoreMember(SwitchCommand.class, "SWITCH", "switch [<location>|...] (state:[{toggle}/on/off]) (duration:<value>)", 1);
registerCoreMember(TakeCommand.class, "TAKE", "take [money/iteminhand/scriptname:<name>/bydisplay:<name>/bycover:<title>|<author>/slot:<slot>/nbt:<key>/<item>|...] (quantity:<#>) (from:<inventory>)", 1);
registerCoreMember(TakeCommand.class, "TAKE", "take [money/xp/iteminhand/scriptname:<name>/bydisplay:<name>/bycover:<title>|<author>/slot:<slot>/nbt:<key>/<item>|...] (quantity:<#>) (from:<inventory>)", 1);
registerCoreMember(TeamCommand.class, "TEAM", "team (id:<scoreboard>/{main}) [name:<team>] (add:<entry>|...) (remove:<entry>|...) (prefix:<prefix>) (suffix:<suffix>)", 2);
registerCoreMember(TeleportCommand.class, "TELEPORT", "teleport (<entity>|...) [<location>]", 1);
registerCoreMember(TimeCommand.class, "TIME", "time ({global}/player) [<time-duration>/reset] (<world>) (reset:<duration>) (freeze)", 1);
Expand Down
Expand Up @@ -23,7 +23,7 @@ public class TakeCommand extends AbstractCommand {

// <--[command]
// @Name Take
// @Syntax take [money/iteminhand/scriptname:<name>/bydisplay:<name>/bycover:<title>|<author>/slot:<slot>/nbt:<key>/<item>|...] (quantity:<#>) (from:<inventory>)
// @Syntax take [money/xp/iteminhand/scriptname:<name>/bydisplay:<name>/bycover:<title>|<author>/slot:<slot>/nbt:<key>/<item>|...] (quantity:<#>) (from:<inventory>)
// @Required 1
// @Short Takes an item from the player.
// @Group item
Expand All @@ -45,6 +45,8 @@ public class TakeCommand extends AbstractCommand {
//
// Using 'bycover:' will take a written book by the specified book title + author pair.
//
// Using 'xp' will take experience from the player.
//
// If an economy is registered, using 'money' instead of an item will take money from the player's economy balance.
//
// If no quantity is specified, exactly 1 item will be taken.
Expand All @@ -69,7 +71,7 @@ public class TakeCommand extends AbstractCommand {
// - take emerald quantity:5
// -->

private enum Type {MONEY, ITEMINHAND, ITEM, INVENTORY, BYDISPLAY, SLOT, BYCOVER, SCRIPTNAME, NBT}
private enum Type {MONEY, XP, ITEMINHAND, ITEM, INVENTORY, BYDISPLAY, SLOT, BYCOVER, SCRIPTNAME, NBT}

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
Expand All @@ -80,6 +82,10 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException
&& arg.matches("money", "coins")) {
scriptEntry.addObject("type", Type.MONEY);
}
else if (!scriptEntry.hasObject("type")
&& arg.matches("xp", "exp")) {
scriptEntry.addObject("type", Type.XP);
}
else if (!scriptEntry.hasObject("type")
&& arg.matches("item_in_hand", "iteminhand")) {
scriptEntry.addObject("type", Type.ITEMINHAND);
Expand Down Expand Up @@ -234,6 +240,11 @@ public void execute(ScriptEntry scriptEntry) {
break;
}

case XP: {
Utilities.getEntryPlayer(scriptEntry).getPlayerEntity().giveExp(-qty.asInt());
break;
}

case ITEM: {
for (ItemTag item : items) {
ItemStack is = item.getItemStack();
Expand Down
Expand Up @@ -54,14 +54,6 @@ public static void setTotalExperience(Player player, int exp) {
player.setTotalExperience(exp);
}

public static void setLevel(Player player, int level) {
player.setLevel(level);
}

public static void giveExperience(Player player, int exp) {
player.giveExp(exp);
}

public static void takeExperience(Player player, int toTake) {
int pastLevelStart = (int) (player.getExp() * player.getExpToLevel());
while (toTake >= pastLevelStart) {
Expand Down

0 comments on commit 49a497b

Please sign in to comment.