Advertisement
BlackBeltPanda

TownyWithdraw

Apr 2nd, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.71 KB | None | 0 0
  1. package main.java.me.BlackBeltPanda.PandaTowny;
  2.  
  3. import java.io.File;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.util.Properties;
  7.  
  8. import net.milkbowl.vault.economy.EconomyResponse;
  9.  
  10. import org.bukkit.ChatColor;
  11. import org.bukkit.command.Command;
  12. import org.bukkit.command.CommandExecutor;
  13. import org.bukkit.command.CommandSender;
  14. import org.bukkit.entity.Player;
  15.  
  16. public class Commands implements CommandExecutor {
  17.  
  18. @SuppressWarnings("deprecation")
  19. public void withdrawFromBank(Player player, String name, double amount, String prefix){
  20. EconomyResponse r = Main.econ.withdrawPlayer(prefix+name, amount);
  21. if (r.transactionSuccess()){
  22. player.sendMessage(ChatColor.GREEN + "Successfully withdrew " + String.valueOf(r.amount) +" from " + name + "'s bank");
  23. Main.econ.depositPlayer(player, amount);
  24. }
  25. else {
  26. player.sendMessage(ChatColor.RED + "Failed to withdraw " + String.valueOf(amount) + " from " + name + "'s bank.");
  27. }
  28. player.sendMessage(ChatColor.GREEN + name + "'s remaining balance: " + ChatColor.GOLD + String.valueOf(r.balance));
  29. }
  30.  
  31. public boolean onCommand(final CommandSender sender, final Command cmd, final String label, final String[] args) {
  32. if (sender instanceof Player) {
  33. final Player player = (Player)sender;
  34. String bank = cmd.getName();
  35. //Make sure the command is the one we want
  36. if ((bank.equalsIgnoreCase("nation") || bank.equalsIgnoreCase("town")) && args.length == 2 && args[0].equals("withdraw")) {
  37.  
  38. //Try and parse the argument to an integer
  39. double amount = 0;
  40. try {
  41. amount = Double.parseDouble(args[1]);
  42. } catch (NumberFormatException e){
  43. player.sendMessage(ChatColor.RED + "That's not a valid number");
  44. return true;
  45. }
  46. if (amount <= 0){
  47. player.sendMessage(ChatColor.RED + "Please specify a number greater than 0.");
  48. return true;
  49. }
  50.  
  51. //Load the player's resident file
  52. String pPath = "/./home/customer/multicraft/servers/towny/plugins/Towny/data/residents/" + player.getName() + ".txt";
  53. File pFile = new File(pPath);
  54. if (pFile.exists() && pFile.isFile()){
  55. //Convert to properties data
  56. Properties pProps = new Properties();
  57. try {
  58. pProps.load(new FileReader(pPath));
  59. } catch (IOException e) {
  60. System.out.print("Failed to load resident properties for " + player.getName());
  61. player.sendMessage(ChatColor.RED + "Failed to load resident properties.");
  62. return true;
  63. }
  64. //Grab their town if it exists
  65. String town = pProps.getProperty("town");
  66. if (town != null){
  67. String tPath = "/./home/customer/multicraft/servers/towny/plugins/Towny/data/towns/" + town + ".txt";
  68. File tFile = new File(tPath);
  69. if (tFile.exists() && tFile.isFile()){
  70. Properties tProps = new Properties();
  71. try {
  72. tProps.load(new FileReader(tPath));
  73. } catch (IOException e) {
  74. System.out.print("Failed to load town properties for " + town);
  75. player.sendMessage(ChatColor.RED + "Failed to load town properties.");
  76. return true;
  77. }
  78. //Withdrawing from a town
  79. String mayor = tProps.getProperty("mayor");
  80. if (bank.equalsIgnoreCase("town")){
  81. //Make sure the player is mayor or assistant
  82. if (!mayor.equals(player.getName())){
  83. if (tProps.getProperty("assistants") == null){
  84. player.sendMessage(ChatColor.RED + "You must be the town owner or an assistant to withdraw money from a town.");
  85. return true;
  86. }
  87. //If not mayor, make sure the player is assistant
  88. String[] assistants = tProps.getProperty("assistants").split(",");
  89. for (String assistant : assistants){
  90. if (!assistant.isEmpty() && assistant.equals(player.getName())){
  91. withdrawFromBank(player, town, amount, "Town-");
  92. return true;
  93. }
  94. else {
  95. player.sendMessage(ChatColor.RED + "You must be the town owner or an assistant to withdraw money from a town.");
  96. return true;
  97. }
  98. }
  99. }
  100. //If mayor then withdraw
  101. else{
  102. withdrawFromBank(player, town, amount, "Town-");
  103. return true;
  104. }
  105. }
  106. //Withdrawing from a nation
  107. else if (bank.equalsIgnoreCase("nation")){
  108. String nation = tProps.getProperty("nation");
  109. if (nation != null){
  110. String nPath = "/./home/customer/multicraft/servers/towny/plugins/Towny/data/nations/" + nation + ".txt";
  111. File nFile = new File(nPath);
  112. if (nFile.exists() && nFile.isFile()){
  113. Properties nProps = new Properties();
  114. try {
  115. nProps.load(new FileReader(nPath));
  116. } catch (IOException e) {
  117. System.out.print("Failed to load nation properties for " + nation);
  118. player.sendMessage(ChatColor.RED + "Failed to load nation properties.");
  119. return true;
  120. }
  121. String capital = nProps.getProperty("capital");
  122. //If the player's town is the nation's capital
  123. if (capital.equals(town)){
  124. //If they're not the mayor of the capital, check if they're a nation assistant
  125. if (!mayor.equals(player.getName())){
  126. String[] assistants = nProps.getProperty("assistants").split(",");
  127. for (String assistant : assistants){
  128. if (!assistant.isEmpty() && assistant.equals(player.getName())){
  129. withdrawFromBank(player, nation, amount, "Nation-");
  130. return true;
  131. }
  132. else {
  133. player.sendMessage(ChatColor.RED + "You must be the nation owner or an assistant to withdraw money from a nation.");
  134. return true;
  135. }
  136. }
  137. }
  138. //If they are the mayor, then withdraw
  139. else{
  140. withdrawFromBank(player, nation, amount, "Nation-");
  141. return true;
  142. }
  143. }
  144. player.sendMessage(ChatColor.RED + "You must be the nation owner or an assistant to withdraw money from a nation.");
  145. return true;
  146. }
  147. player.sendMessage(ChatColor.RED + "You do not have a nation.");
  148. return true;
  149. }
  150. player.sendMessage(ChatColor.RED + "You do not have a nation.");
  151. return true;
  152. }
  153. player.sendMessage(ChatColor.RED + "You do not have a nation.");
  154. return true;
  155. }
  156. player.sendMessage(ChatColor.RED + "That town doesn't exist.");
  157. return true;
  158. }
  159. player.sendMessage(ChatColor.RED + "You do not have a town.");
  160. return true;
  161. }
  162. player.sendMessage(ChatColor.RED + "You do not have a town.");
  163. return true;
  164. }
  165. }
  166. return true;
  167. }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement