Skip to content

Commit

Permalink
Add money command. Again. fixes #1291
Browse files Browse the repository at this point in the history
  • Loading branch information
Fortifier42 committed Feb 1, 2016
1 parent 29ba77d commit 4fadff7
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
Expand Up @@ -1950,6 +1950,42 @@ public void registerCoreMembers() {
"MIDI", "midi (cancel) [<file>] (<location>/<entity>|...) (tempo:<#.#>) (volume:<#.#>)", 1);


// <--[command]
// @Name Money
// @Syntax money [give/take/set] (quantity:<#.#>) (to/from:<player>|...)
// @Required 1
// @Stable stable
// @Short Manage a player's money.
// @Author Fortifier42
// @Group player
// @Plugin Vault
// @Description
// Give money to, take money from, and set the balance of a player.
// If no quantity is specified it defaults to '1'. You can specify a list of
// players to give to or take from. If no player(s) are specified defaults to the attached player.
// NOTE: This requires an economy plugin. May work for offline players depending on economy plugin.
// @Tags
// <p@player.money>
// @Usage
// Use to give 1 money to the player.
// - money give
// @Usage
// Use to take 10 money from a player.
// - money take quantity:10 from:p@mcmonkey4eva
// @Usage
// Use to give all players on the server 100 money.
// - money give quantity:100 to:<server.list_players>

// @Usage
// Use to set the money of all online players to 250.
// - money set quantity:250 to:<server.list_online_players>
// -->
if (Depends.economy != null) {
registerCoreMember(MoneyCommand.class,
"MONEY", "money [give/take] (quantity:<#.#>) (to/from:<player>|...)", 1);
}


// <--[command]
// @Name Mount
// @Syntax mount (cancel) [<entity>|...] (<location>)
Expand Down
@@ -0,0 +1,96 @@
package net.aufdemrand.denizen.scripts.commands.player;

import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizen.utilities.depends.Depends;
import net.aufdemrand.denizencore.exceptions.CommandExecutionException;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.objects.dList;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
import net.aufdemrand.denizencore.scripts.commands.AbstractCommand;
import net.aufdemrand.denizencore.utilities.debugging.dB;
import net.milkbowl.vault.economy.Economy;

import java.util.Arrays;
import java.util.List;

public class MoneyCommand extends AbstractCommand {

enum Action {
GIVE,
TAKE,
SET
}

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (!scriptEntry.hasObject("action") && arg.matchesEnum(Action.values())) {
scriptEntry.addObject("action", arg.asElement());
}
else if (!scriptEntry.hasObject("quantity") && arg.matchesPrefix("quantity", "qty", "q")
&& arg.matchesPrimitive(aH.PrimitiveType.Double)) {
scriptEntry.addObject("quantity", arg.asElement());
}
else if (!scriptEntry.hasObject("players") && arg.matchesPrefix("to", "from") &&
arg.matchesArgumentList(dPlayer.class)) {
scriptEntry.addObject("players", arg.asType(dList.class).filter(dPlayer.class));
}
else {
arg.reportUnhandled();
}
}

scriptEntry.defaultObject("quantity", new Element(1));

if (!scriptEntry.hasObject("players")) {
if (!((BukkitScriptEntryData) scriptEntry.entryData).hasPlayer()) {
throw new InvalidArgumentsException("This command must have a player attached!");
}
else {
scriptEntry.addObject("players",
Arrays.asList(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer()));
}
}
else if (!scriptEntry.hasObject("action")) {
throw new InvalidArgumentsException("Must specify a valid action!");
}
}

@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
Element action = scriptEntry.getElement("action");
Element quantity = scriptEntry.getElement("quantity");
List<dPlayer> players = (List<dPlayer>) scriptEntry.getObject("players");

dB.report(scriptEntry, getName(), aH.debugList("Player(s)", players) + action.debug() + quantity.debug());
Economy eco = Depends.economy;
double amt = quantity.asDouble();
switch (Action.valueOf(action.asString().toUpperCase())) {
case GIVE:
for (dPlayer player : players) {
eco.depositPlayer(player.getOfflinePlayer(), amt);
}
break;

case TAKE:
for (dPlayer player : players) {
eco.withdrawPlayer(player.getOfflinePlayer(), amt);
}
break;

case SET:
for (dPlayer player : players) {
double balance = eco.getBalance(player.getOfflinePlayer());
if (amt > balance) {
eco.depositPlayer(player.getOfflinePlayer(), amt - balance);
}
else {
eco.withdrawPlayer(player.getOfflinePlayer(), balance - amt);
}
}
}
}
}

0 comments on commit 4fadff7

Please sign in to comment.