Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main.java.me.BlackBeltPanda.PandaTowny;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.Properties;
- import net.milkbowl.vault.economy.EconomyResponse;
- import org.bukkit.ChatColor;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandExecutor;
- import org.bukkit.command.CommandSender;
- import org.bukkit.entity.Player;
- public class Commands implements CommandExecutor {
- @SuppressWarnings("deprecation")
- public void withdrawFromBank(Player player, String name, double amount, String prefix){
- EconomyResponse r = Main.econ.withdrawPlayer(prefix+name, amount);
- if (r.transactionSuccess()){
- player.sendMessage(ChatColor.GREEN + "Successfully withdrew " + String.valueOf(r.amount) +" from " + name + "'s bank");
- Main.econ.depositPlayer(player, amount);
- }
- else {
- player.sendMessage(ChatColor.RED + "Failed to withdraw " + String.valueOf(amount) + " from " + name + "'s bank.");
- }
- player.sendMessage(ChatColor.GREEN + name + "'s remaining balance: " + ChatColor.GOLD + String.valueOf(r.balance));
- }
- public boolean onCommand(final CommandSender sender, final Command cmd, final String label, final String[] args) {
- if (sender instanceof Player) {
- final Player player = (Player)sender;
- String bank = cmd.getName();
- //Make sure the command is the one we want
- if ((bank.equalsIgnoreCase("nation") || bank.equalsIgnoreCase("town")) && args.length == 2 && args[0].equals("withdraw")) {
- //Try and parse the argument to an integer
- double amount = 0;
- try {
- amount = Double.parseDouble(args[1]);
- } catch (NumberFormatException e){
- player.sendMessage(ChatColor.RED + "That's not a valid number");
- return true;
- }
- if (amount <= 0){
- player.sendMessage(ChatColor.RED + "Please specify a number greater than 0.");
- return true;
- }
- //Load the player's resident file
- String pPath = "/./home/customer/multicraft/servers/towny/plugins/Towny/data/residents/" + player.getName() + ".txt";
- File pFile = new File(pPath);
- if (pFile.exists() && pFile.isFile()){
- //Convert to properties data
- Properties pProps = new Properties();
- try {
- pProps.load(new FileReader(pPath));
- } catch (IOException e) {
- System.out.print("Failed to load resident properties for " + player.getName());
- player.sendMessage(ChatColor.RED + "Failed to load resident properties.");
- return true;
- }
- //Grab their town if it exists
- String town = pProps.getProperty("town");
- if (town != null){
- String tPath = "/./home/customer/multicraft/servers/towny/plugins/Towny/data/towns/" + town + ".txt";
- File tFile = new File(tPath);
- if (tFile.exists() && tFile.isFile()){
- Properties tProps = new Properties();
- try {
- tProps.load(new FileReader(tPath));
- } catch (IOException e) {
- System.out.print("Failed to load town properties for " + town);
- player.sendMessage(ChatColor.RED + "Failed to load town properties.");
- return true;
- }
- //Withdrawing from a town
- String mayor = tProps.getProperty("mayor");
- if (bank.equalsIgnoreCase("town")){
- //Make sure the player is mayor or assistant
- if (!mayor.equals(player.getName())){
- if (tProps.getProperty("assistants") == null){
- player.sendMessage(ChatColor.RED + "You must be the town owner or an assistant to withdraw money from a town.");
- return true;
- }
- //If not mayor, make sure the player is assistant
- String[] assistants = tProps.getProperty("assistants").split(",");
- for (String assistant : assistants){
- if (!assistant.isEmpty() && assistant.equals(player.getName())){
- withdrawFromBank(player, town, amount, "Town-");
- return true;
- }
- else {
- player.sendMessage(ChatColor.RED + "You must be the town owner or an assistant to withdraw money from a town.");
- return true;
- }
- }
- }
- //If mayor then withdraw
- else{
- withdrawFromBank(player, town, amount, "Town-");
- return true;
- }
- }
- //Withdrawing from a nation
- else if (bank.equalsIgnoreCase("nation")){
- String nation = tProps.getProperty("nation");
- if (nation != null){
- String nPath = "/./home/customer/multicraft/servers/towny/plugins/Towny/data/nations/" + nation + ".txt";
- File nFile = new File(nPath);
- if (nFile.exists() && nFile.isFile()){
- Properties nProps = new Properties();
- try {
- nProps.load(new FileReader(nPath));
- } catch (IOException e) {
- System.out.print("Failed to load nation properties for " + nation);
- player.sendMessage(ChatColor.RED + "Failed to load nation properties.");
- return true;
- }
- String capital = nProps.getProperty("capital");
- //If the player's town is the nation's capital
- if (capital.equals(town)){
- //If they're not the mayor of the capital, check if they're a nation assistant
- if (!mayor.equals(player.getName())){
- String[] assistants = nProps.getProperty("assistants").split(",");
- for (String assistant : assistants){
- if (!assistant.isEmpty() && assistant.equals(player.getName())){
- withdrawFromBank(player, nation, amount, "Nation-");
- return true;
- }
- else {
- player.sendMessage(ChatColor.RED + "You must be the nation owner or an assistant to withdraw money from a nation.");
- return true;
- }
- }
- }
- //If they are the mayor, then withdraw
- else{
- withdrawFromBank(player, nation, amount, "Nation-");
- return true;
- }
- }
- player.sendMessage(ChatColor.RED + "You must be the nation owner or an assistant to withdraw money from a nation.");
- return true;
- }
- player.sendMessage(ChatColor.RED + "You do not have a nation.");
- return true;
- }
- player.sendMessage(ChatColor.RED + "You do not have a nation.");
- return true;
- }
- player.sendMessage(ChatColor.RED + "You do not have a nation.");
- return true;
- }
- player.sendMessage(ChatColor.RED + "That town doesn't exist.");
- return true;
- }
- player.sendMessage(ChatColor.RED + "You do not have a town.");
- return true;
- }
- player.sendMessage(ChatColor.RED + "You do not have a town.");
- return true;
- }
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement