Skip to content

Instantly share code, notes, and snippets.

@NeatMonster
Created March 21, 2012 19:54
Show Gist options
  • Save NeatMonster/2152082 to your computer and use it in GitHub Desktop.
Save NeatMonster/2152082 to your computer and use it in GitHub Desktop.
EasyBank
package fr.madlaine.easybank;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.logging.Logger;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.ChatColor;
import org.bukkit.block.Sign;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
public class EasyBank extends JavaPlugin implements Listener {
private static String PREFIX = ChatColor.GRAY + "[" + ChatColor.WHITE + "Easy" + ChatColor.GOLD + "Bank"
+ ChatColor.GRAY + "] " + ChatColor.WHITE;
private static Economy economy = null;
private static Logger logger = Logger.getLogger("Minecraft");
private static String logTag = "[EasyBank] ";
@Override
public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args) {
if (command.getName().equalsIgnoreCase("easybank")) {
if (!(sender instanceof Player))
sender.sendMessage(PREFIX + "Commands can only be used by in-game players!");
else if (args.length == 2 && args[0].equalsIgnoreCase("deposit"))
if (sender.hasPermission("easybank.deposit")) {
final NumberFormat formatter = NumberFormat.getInstance();
try {
final double deposit = formatter.parse(args[1]).doubleValue();
if (economy.has(sender.getName(), deposit)) {
economy.withdrawPlayer(sender.getName(), deposit);
final double amount = getConfig().getDouble("Accounts." + sender.getName(), 0D);
getConfig().set("Accounts." + sender.getName(), amount + deposit);
saveConfig();
sender.sendMessage(PREFIX + "You've successfully deposited " + ChatColor.GREEN
+ economy.format(deposit) + ChatColor.WHITE + " on your bank account.");
sender.sendMessage(PREFIX + "You've now " + ChatColor.GREEN + economy.format(amount)
+ ChatColor.WHITE + " on your bank account.");
} else
sender.sendMessage(PREFIX + ChatColor.RED + "You don't have enough money to do that!");
} catch (final ParseException e) {
sender.sendMessage(PREFIX + ChatColor.RED + "Please enter a valid amount!");
}
} else
sender.sendMessage(PREFIX + ChatColor.RED + "You're not allowed to do that!");
else if (args.length == 2 && args[0].equalsIgnoreCase("debit")) {
if (sender.hasPermission("easybank.debit")) {
final NumberFormat formatter = NumberFormat.getInstance();
try {
final double debit = formatter.parse(args[1]).doubleValue();
final double amount = getConfig().getDouble("Accounts." + sender.getName(), 0D);
if (debit <= amount) {
economy.depositPlayer(sender.getName(), debit);
getConfig().set("Accounts." + sender.getName(), amount - debit);
saveConfig();
sender.sendMessage(PREFIX + "You've successfully debited " + ChatColor.GREEN
+ economy.format(debit) + ChatColor.WHITE + " from your bank account.");
sender.sendMessage(PREFIX + "You've now " + ChatColor.GREEN + economy.format(amount)
+ ChatColor.WHITE + " on your bank account.");
} else
sender.sendMessage(PREFIX + ChatColor.RED + "You don't have enough money to do that!");
} catch (final ParseException e) {
sender.sendMessage(PREFIX + ChatColor.RED + "Please enter a valid amount!");
}
} else
sender.sendMessage(PREFIX + ChatColor.RED + "You're not allowed to do that!");
} else {
sender.sendMessage(PREFIX + ChatColor.GREEN + "EasyBank's commands:");
sender.sendMessage(PREFIX + ChatColor.GREEN + "/bank deposit <amount>: " + ChatColor.WHITE
+ "deposits money on your bank account");
sender.sendMessage(PREFIX + ChatColor.GREEN + "/bank debit <amount>: " + ChatColor.WHITE
+ "debits money from your bank account");
}
return true;
}
return false;
}
@Override
public void onEnable() {
super.onEnable();
if (!setupEconomy()) {
logger.severe(logTag + "Vault wasn't found, the plugin is going to disable itself!");
getServer().getPluginManager().disablePlugin(this);
} else
getServer().getPluginManager().registerEvents(this, this);
}
@EventHandler(ignoreCancelled = true)
public void onPlayerInteract(final PlayerInteractEvent event) {
if (event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getClickedBlock().getState() instanceof Sign
&& ((Sign) event.getClickedBlock().getState()).getLine(0).equalsIgnoreCase(PREFIX))
if (event.getPlayer().hasPermission("easybank.update")) {
final Sign sign = (Sign) event.getClickedBlock().getState();
sign.setLine(0, PREFIX);
sign.setLine(1, event.getPlayer().getName());
sign.setLine(2, "" + getConfig().getDouble("Accounts." + event.getPlayer().getName(), 0D));
sign.setLine(3, "");
sign.update();
} else {
event.getPlayer().sendMessage(PREFIX + ChatColor.RED + "You're not allowed to do that!");
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerJoin(final PlayerJoinEvent event) {
if (event.getPlayer().hasPermission("easybank.message")) {
final double amount = getConfig().getDouble("Accounts." + event.getPlayer().getName(), 0D);
event.getPlayer().sendMessage(
PREFIX + "You've " + ChatColor.GREEN + economy.format(amount) + ChatColor.WHITE
+ " on your bank account.");
}
}
@EventHandler(ignoreCancelled = true)
public void onSignChange(final SignChangeEvent event) {
if (event.getLine(0).equalsIgnoreCase("[EasyBank]"))
if (event.getPlayer().hasPermission("easybank.create") && event.getPlayer().isOp()) {
event.setLine(0, PREFIX);
event.setLine(1, event.getPlayer().getName());
event.setLine(2, "" + getConfig().getDouble("Accounts." + event.getPlayer().getName(), 0D));
event.setLine(3, "");
} else {
event.getPlayer().sendMessage(PREFIX + ChatColor.RED + "You're not allowed to do that!");
event.setCancelled(true);
}
}
private boolean setupEconomy() {
final RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(
net.milkbowl.vault.economy.Economy.class);
if (economyProvider != null)
economy = economyProvider.getProvider();
return economy != null;
}
}
name: EasyBank
author: Madlaine
version: 0.2
main: fr.madlaine.easybank.EasyBank
depend: [Vault]
commands:
bank:
description: Main command of EasyBank.
usage: /<command> <deposit|debit> [amount]
permissions:
easybank.message:
description: Allows the player to see the welcome message.
easybank.deposit:
description: Allows the player to deposit money.
easybank.debit:
description: Allows the player to debit money.
easybank.create:
description: Allows the player to create an EasyBank sign.
easybank.update:
description: Allows the player to update an EasyBank sign.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment