ATM.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. package de.Linus122.TimeIsMoney;
  2. import com.google.common.primitives.Doubles;
  3. import org.bukkit.Bukkit;
  4. import org.bukkit.Material;
  5. import org.bukkit.OfflinePlayer;
  6. import org.bukkit.block.Block;
  7. import org.bukkit.block.Sign;
  8. import org.bukkit.command.Command;
  9. import org.bukkit.command.CommandExecutor;
  10. import org.bukkit.command.CommandSender;
  11. import org.bukkit.configuration.file.YamlConfiguration;
  12. import org.bukkit.entity.Player;
  13. import org.bukkit.event.Event.Result;
  14. import org.bukkit.event.EventHandler;
  15. import org.bukkit.event.EventPriority;
  16. import org.bukkit.event.Listener;
  17. import org.bukkit.event.block.SignChangeEvent;
  18. import org.bukkit.event.inventory.InventoryClickEvent;
  19. import org.bukkit.event.inventory.InventoryDragEvent;
  20. import org.bukkit.event.inventory.InventoryMoveItemEvent;
  21. import org.bukkit.event.player.PlayerInteractEvent;
  22. import org.bukkit.inventory.Inventory;
  23. import org.bukkit.inventory.ItemStack;
  24. import org.bukkit.inventory.meta.ItemMeta;
  25. import org.bukkit.plugin.Plugin;
  26. import java.io.File;
  27. import java.io.IOException;
  28. import java.util.List;
  29. import static de.Linus122.TimeIsMoney.tools.Utils.CC;
  30. /**
  31. * ATM listener and command executor.
  32. *
  33. * @author Linus122
  34. * @since 1.9.6.1
  35. */
  36. public class ATM implements Listener, CommandExecutor {
  37. /**
  38. * The {@link Plugin}.
  39. */
  40. private final Plugin plugin;
  41. /**
  42. * The bank accounts {@link java.io.File} that stores all data.
  43. */
  44. private static final File bankAccountsFile = new File("plugins/TimeIsMoney/data.dat");
  45. /**
  46. * The bank accounts {@link org.bukkit.configuration.file.YamlConfiguration} to manage the {@link #bankAccountsFile}.
  47. */
  48. private static YamlConfiguration bankAccountsConfig;
  49. /**
  50. * The different amounts of money shown in the atm to withdraw and deposit (atm_worth_gradation).
  51. */
  52. private double[] worths = new double[4];
  53. /**
  54. * Creates a new atm instance with the {@link de.Linus122.TimeIsMoney.Main} class.
  55. *
  56. * @param plugin The {@link de.Linus122.TimeIsMoney.Main} class that implements {@link org.bukkit.plugin.java.JavaPlugin}.
  57. */
  58. public ATM(Main plugin) {
  59. this.plugin = plugin;
  60. plugin.getServer().getPluginManager().registerEvents(this, plugin);
  61. plugin.getCommand("atm").setExecutor(this);
  62. if (!bankAccountsFile.exists()) {
  63. try {
  64. bankAccountsFile.createNewFile();
  65. } catch (IOException e) {
  66. e.printStackTrace();
  67. }
  68. }
  69. bankAccountsConfig = YamlConfiguration.loadConfiguration(bankAccountsFile);
  70. worths = Doubles.toArray(Main.finalconfig.getDoubleList("atm_worth_gradation"));
  71. }
  72. /**
  73. * Withdraws the specified amount of money from the specified player's bank.
  74. *
  75. * @param p The player to withdraw money from.
  76. * @param amount The amount of money to withdraw.
  77. */
  78. private static void withdrawBank(Player p, double amount) {
  79. String bankString = getBankString(p);
  80. if (!bankAccountsConfig.contains(bankString)) bankAccountsConfig.set(bankString, 0.0);
  81. bankAccountsConfig.set(bankString, getBankBalance(p) - amount);
  82. saveBanks();
  83. }
  84. /**
  85. * Deposits the specified amount of money to the specified player's bank.
  86. *
  87. * @param p The player to deposit money to.
  88. * @param amount The amount of money to deposit.
  89. */
  90. public static void depositBank(Player p, double amount) {
  91. String bankString = getBankString(p);
  92. if (!bankAccountsConfig.contains(bankString)) bankAccountsConfig.set(bankString, 0.0);
  93. bankAccountsConfig.set(bankString, getBankBalance(p) + amount);
  94. saveBanks();
  95. }
  96. /**
  97. * Checks if the player has the specified amount of money in their bank.
  98. *
  99. * @param p The player to check the balance of.
  100. * @param amount The amount of money.
  101. * @return True if the player has the specified amount of money, false otherwise.
  102. */
  103. private static boolean bankHas(Player p, double amount) {
  104. String bankString = getBankString(p);
  105. if (!bankAccountsConfig.contains(bankString)) bankAccountsConfig.set(bankString, 0.0);
  106. return getBankBalance(p) >= amount;
  107. }
  108. /**
  109. * Gets the balance of the specified player's bank (doesn't support groups).
  110. *
  111. * @param p The offline player to get the balance of.
  112. * @return The offline player's balance in the bank.
  113. */
  114. private static double getBankBalance(OfflinePlayer p) {
  115. String bankString = p.getName() + "_TimBANK";
  116. if (!bankAccountsConfig.contains(bankString)) bankAccountsConfig.set(bankString, 0.0);
  117. return bankAccountsConfig.getDouble(bankString);
  118. }
  119. /**
  120. * Gets the balance of the specified player's bank.
  121. *
  122. * @param p The player to get the balance of.
  123. * @return The player's balance in the bank.
  124. */
  125. private static double getBankBalance(Player p) {
  126. String bankString = getBankString(p);
  127. if (!bankAccountsConfig.contains(bankString)) bankAccountsConfig.set(bankString, 0.0);
  128. return bankAccountsConfig.getDouble(bankString);
  129. }
  130. /**
  131. * Saves the banks.
  132. */
  133. private static void saveBanks() {
  134. try {
  135. bankAccountsConfig.save(bankAccountsFile);
  136. } catch (IOException e) {
  137. e.printStackTrace();
  138. }
  139. }
  140. /**
  141. * Converts the old TimeIsMoney bank data to the new format.
  142. *
  143. * @param p The player to convert data for.
  144. */
  145. private static void convertOldBank(Player p) {
  146. String bankString = getBankString(p);
  147. if (Main.economy.hasAccount(bankString)) {
  148. if (Main.economy.getBalance(bankString) > 0) {
  149. p.sendMessage(CC("&aSuccessfully converted your old TIM-Bank to new version!"));
  150. depositBank(p, Main.economy.getBalance(bankString));
  151. Main.economy.withdrawPlayer(bankString, Main.economy.getBalance(bankString));
  152. }
  153. }
  154. }
  155. /**
  156. * Gets the bank string for the specified player.
  157. *
  158. * @param p The player to get the bank string of.
  159. * @return The bank string of the specified player.
  160. */
  161. private static String getBankString(Player p) {
  162. if (!Main.finalconfig.getBoolean("group-atms")) {
  163. return p.getName() + "_TimBANK";
  164. } else {
  165. for (String key : Main.finalconfig.getConfigurationSection("atm_groups").getKeys(false)) {
  166. List<String> list = Main.finalconfig.getStringList("atm_groups." + key);
  167. if (list.contains(p.getWorld().getName())) {
  168. return p.getName() + "_TimBANK_" + key;
  169. }
  170. }
  171. }
  172. return p.getName() + "_TimBANK";
  173. /*if(!Main.finalconfig.getBoolean("group-atms")){
  174. return p.getName() + "_TimBANK";
  175. }else{
  176. for(String key : Main.finalconfig.getConfigurationSection("atm_groups").getKeys(false)){
  177. List<String> list = Main.finalconfig.getStringList("atm_groups." + key);
  178. if(list.contains(p.getWorld().getName())){
  179. return p.getName() + "_TimBANK_" + key;
  180. }
  181. }
  182. }
  183. return p.getName() + "_TimBANK";*/
  184. }
  185. @EventHandler(priority = EventPriority.HIGHEST)
  186. public void onInteract(PlayerInteractEvent e) {
  187. if (e.getClickedBlock() != null) {
  188. if (e.getClickedBlock().getType() == Material.WALL_SIGN || e.getClickedBlock().getType() == Material.SIGN || e.getClickedBlock().getType() == Material.SIGN_POST) {
  189. Sign sign = (Sign) e.getClickedBlock().getState();
  190. if (sign.getLine(0).equalsIgnoreCase(CC("&cATM"))) {
  191. if (!e.getPlayer().hasPermission("tim.atm.use")) {
  192. e.getPlayer().sendMessage(CC(Main.finalconfig.getString("message_atm_noperms")));
  193. } else {
  194. this.openGUI(e.getPlayer());
  195. }
  196. }
  197. }
  198. }
  199. }
  200. @EventHandler
  201. public void onMove(InventoryMoveItemEvent e) {
  202. if (e.getSource() == null) return;
  203. if (e.getSource().getTitle() == null) return;
  204. if (e.getSource().getTitle().equals(CC(Main.finalconfig.getString("atm_title")))) {
  205. e.setCancelled(true);
  206. }
  207. }
  208. @SuppressWarnings("deprecation")
  209. @EventHandler
  210. public void onClick(InventoryClickEvent e) {
  211. try {
  212. if (e == null) return;
  213. if (e.getInventory() == null) return;
  214. if (e.getInventory().getTitle() == null) return;
  215. if (e.getInventory().getTitle().equals(CC(Main.finalconfig.getString("atm_title")))) {
  216. e.setResult(Result.DENY);
  217. Player p = (Player) e.getWhoClicked();
  218. //e.setCancelled(true);
  219. if (e.getCurrentItem() != null) {
  220. // left side
  221. if (e.getSlot() < 4) {
  222. double amount = worths[3 - e.getSlot()];
  223. if (ATM.bankHas(p, amount)) {
  224. ATM.withdrawBank(p, amount);
  225. Main.economy.depositPlayer(p, amount);
  226. e.getWhoClicked().sendMessage(CC(Main.finalconfig.getString("atm_withdraw")) + " " + Main.economy.format(amount));
  227. } else {
  228. e.getWhoClicked().sendMessage(CC(Main.finalconfig.getString("message_atm_nomoneyinbank")));
  229. }
  230. } else
  231. // right side
  232. if (e.getSlot() > 4) {
  233. double amount = worths[3 - (3 - (e.getSlot() - 5))];
  234. if (Main.economy.has((Player) e.getWhoClicked(), amount)) {
  235. ATM.depositBank(p, amount);
  236. Main.economy.withdrawPlayer((Player) e.getWhoClicked(), amount);
  237. e.getWhoClicked().sendMessage(CC(Main.finalconfig.getString("atm_deposit")) + " " + Main.economy.format(amount));
  238. } else {
  239. e.getWhoClicked().sendMessage(CC(Main.finalconfig.getString("message_atm_nomoney")));
  240. }
  241. }
  242. ItemStack is = new ItemStack(Material.GOLD_NUGGET, 1);
  243. ItemMeta im = is.getItemMeta();
  244. im.setDisplayName(CC(Main.finalconfig.getString("atm_balance")) + " " + Main.economy.format(ATM.getBankBalance(p)));
  245. is.setItemMeta(im);
  246. e.getInventory().setItem(4, is);
  247. }
  248. }
  249. } catch (Exception ignored) {
  250. }
  251. }
  252. /**
  253. * Opens the atm gui for the specified player.
  254. *
  255. * @param player The player to open the atm gui for.
  256. */
  257. private void openGUI(Player player) {
  258. convertOldBank(player);
  259. Inventory atm_gui = Bukkit.createInventory(null, 9, CC(Main.finalconfig.getString("atm_title")));
  260. //
  261. ItemStack is = new ItemStack(Material.GOLD_NUGGET, 1);
  262. ItemMeta im = is.getItemMeta();
  263. im.setDisplayName(CC(Main.finalconfig.getString("atm_balance")) + " " + Main.economy.format(ATM.getBankBalance(player)));
  264. is.setItemMeta(im);
  265. atm_gui.setItem(4, is);
  266. //
  267. is = new ItemStack(Material.CLAY_BRICK, 1);
  268. im = is.getItemMeta();
  269. im.setDisplayName(CC(Main.finalconfig.getString("atm_withdraw") + " &a") + Main.economy.format(worths[0]));
  270. is.setItemMeta(im);
  271. atm_gui.setItem(3, is);
  272. //
  273. is = new ItemStack(Material.IRON_INGOT, 1);
  274. im = is.getItemMeta();
  275. im.setDisplayName(CC(Main.finalconfig.getString("atm_withdraw") + " &a") + Main.economy.format(worths[1]));
  276. is.setItemMeta(im);
  277. atm_gui.setItem(2, is);
  278. //
  279. is = new ItemStack(Material.GOLD_INGOT, 1);
  280. im = is.getItemMeta();
  281. im.setDisplayName(CC(Main.finalconfig.getString("atm_withdraw") + " &a") + Main.economy.format(worths[2]));
  282. is.setItemMeta(im);
  283. atm_gui.setItem(1, is);
  284. //
  285. is = new ItemStack(Material.DIAMOND, 1);
  286. im = is.getItemMeta();
  287. im.setDisplayName(CC(Main.finalconfig.getString("atm_withdraw") + " &a") + Main.economy.format(worths[3]));
  288. is.setItemMeta(im);
  289. atm_gui.setItem(0, is);
  290. // DEPOSIT
  291. //
  292. is = new ItemStack(Material.CLAY_BRICK, 1);
  293. im = is.getItemMeta();
  294. im.setDisplayName(CC(Main.finalconfig.getString("atm_deposit") + " &4") + Main.economy.format(worths[0]));
  295. is.setItemMeta(im);
  296. atm_gui.setItem(5, is);
  297. //
  298. is = new ItemStack(Material.IRON_INGOT, 1);
  299. im = is.getItemMeta();
  300. im.setDisplayName(CC(Main.finalconfig.getString("atm_deposit") + " &4") + Main.economy.format(worths[1]));
  301. is.setItemMeta(im);
  302. atm_gui.setItem(6, is);
  303. //
  304. is = new ItemStack(Material.GOLD_INGOT, 1);
  305. im = is.getItemMeta();
  306. im.setDisplayName(CC(Main.finalconfig.getString("atm_deposit") + " &4") + Main.economy.format(worths[2]));
  307. is.setItemMeta(im);
  308. atm_gui.setItem(7, is);
  309. //
  310. is = new ItemStack(Material.DIAMOND, 1);
  311. im = is.getItemMeta();
  312. im.setDisplayName(CC(Main.finalconfig.getString("atm_deposit") + " &4") + Main.economy.format(worths[3]));
  313. is.setItemMeta(im);
  314. atm_gui.setItem(8, is);
  315. player.openInventory(atm_gui);
  316. }
  317. @EventHandler
  318. public void onInventoryDrag(InventoryDragEvent e) {
  319. if (e == null) return;
  320. if (e.getInventory() == null) return;
  321. if (e.getInventory().getTitle() == null) return;
  322. if (e.getInventory().getTitle().equals(CC(Main.finalconfig.getString("atm_title")))) {
  323. e.setResult(Result.DENY);
  324. }
  325. }
  326. @EventHandler
  327. public void onSignChange(final SignChangeEvent e) {
  328. final Block b = e.getBlock();
  329. if (b.getType() == Material.WALL_SIGN || b.getType() == Material.SIGN || b.getType() == Material.SIGN_POST) {
  330. plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, () -> {
  331. if (b.getType() == Material.WALL_SIGN || b.getType() == Material.SIGN || b.getType() == Material.SIGN_POST) {
  332. Sign sign = (Sign) e.getBlock().getState();
  333. if (sign.getLine(0).equalsIgnoreCase("[atm]")) {
  334. if (!e.getPlayer().hasPermission("tim.atm.place")) {
  335. e.getPlayer().sendMessage(CC("&cYou dont have permissions to build ATM's!"));
  336. sign.setLine(0, "");
  337. } else {
  338. sign.setLine(0, CC("&cATM"));
  339. sign.update();
  340. e.getPlayer().sendMessage(CC("&2ATM created! (You can also write something in the Lines 2-4)"));
  341. }
  342. }
  343. }
  344. }, 10L);
  345. }
  346. }
  347. @Override
  348. public boolean onCommand(CommandSender cs, Command arg1, String arg2, String[] args) {
  349. if (args.length == 0) {
  350. if (cs.hasPermission("tim.use")) {
  351. openGUI((Player) cs);
  352. return true;
  353. }
  354. }
  355. if (cs.hasPermission("tim.admin")) {
  356. if (args.length > 0) {
  357. switch (args[0]) {
  358. case "balance":
  359. if (args.length > 1) {
  360. cs.sendMessage(CC("&2ATM-Balance of&c " + args[1] + "&2: &c") + getBankBalance(Bukkit.getOfflinePlayer(args[1])));
  361. } else {
  362. cs.sendMessage("/atm balance <player>");
  363. }
  364. break;
  365. default:
  366. @SuppressWarnings("deprecation")
  367. OfflinePlayer op = Bukkit.getOfflinePlayer(args[0]);
  368. if (op == null) {
  369. cs.sendMessage("Player is offline");
  370. return true;
  371. }
  372. if (op.isOnline()) {
  373. openGUI(op.getPlayer());
  374. cs.sendMessage("opened!");
  375. }
  376. break;
  377. }
  378. } else {
  379. cs.sendMessage(CC("&c/atm <player> &a- opens atm for player"));
  380. cs.sendMessage(CC("&c/atm balance <player> &a- gets balance of player"));
  381. return true;
  382. }
  383. }
  384. return true;
  385. }
  386. }