From 3717e4d3b6c44d225db519db4887acfe55b5984d Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Thu, 2 Sep 2021 01:29:44 +0200 Subject: [PATCH] Moves economy related code to its own class --- .../BooksWithoutBorders.java | 43 +------------ .../command/CommandBooksWithoutBorders.java | 3 +- .../command/CommandSetBookPrice.java | 5 +- .../utility/EconomyHelper.java | 64 +++++++++++++++++++ 4 files changed, 72 insertions(+), 43 deletions(-) create mode 100644 src/main/java/net/knarcraft/bookswithoutborders/utility/EconomyHelper.java diff --git a/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java b/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java index 9d6bb0a..4dad153 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java @@ -23,8 +23,8 @@ import net.knarcraft.bookswithoutborders.command.GiveTabCompleter; import net.knarcraft.bookswithoutborders.listener.PlayerEventListener; import net.knarcraft.bookswithoutborders.listener.SignEventListener; import net.knarcraft.bookswithoutborders.utility.BookToFromTextHelper; +import net.knarcraft.bookswithoutborders.utility.EconomyHelper; import net.knarcraft.bookswithoutborders.utility.FileHelper; -import net.milkbowl.vault.economy.Economy; import org.bukkit.Material; import org.bukkit.OfflinePlayer; import org.bukkit.command.CommandExecutor; @@ -38,9 +38,7 @@ import org.bukkit.inventory.ItemFactory; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; import org.bukkit.inventory.meta.BookMeta; -import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginManager; -import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.java.JavaPlugin; import java.io.BufferedReader; @@ -69,7 +67,6 @@ public class BooksWithoutBorders extends JavaPlugin { public static List firstBooks; public static String welcomeMessage; public static List existingPlayers; - public static Economy eco; public static Material bookPriceType = null; public static double bookPriceQuantity; public static boolean authorOnlyCopy; @@ -264,7 +261,7 @@ public class BooksWithoutBorders extends JavaPlugin { //Converts string into material String sMaterial = config.getString("Options.Price_to_create_book.Item_type", " "); if (sMaterial.equalsIgnoreCase("Economy")) { - if (setupEconomy()) { + if (EconomyHelper.setupEconomy()) { bookPriceType = Material.AIR; } else { sendErrorMessage(consoleSender, "BooksWithoutBorders failed to hook into Vault! Book price not set!"); @@ -295,19 +292,6 @@ public class BooksWithoutBorders extends JavaPlugin { return true; } - public boolean setupEconomy() { - Plugin plugin = getServer().getPluginManager().getPlugin("Vault"); - - if (plugin != null) { - RegisteredServiceProvider economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class); - if (economyProvider != null) { - eco = economyProvider.getProvider(); - } - } - - return (eco != null); - } - public boolean loadExistingPlayers() { File fTest = new File(this.getDataFolder().getAbsolutePath() + getSlash() + "Existing Players.txt"); existingPlayers = new ArrayList<>(); @@ -446,7 +430,7 @@ public class BooksWithoutBorders extends JavaPlugin { int itemCost = (int) cost; if (BooksWithoutBorders.bookPriceType == Material.AIR) { - return !payForBookPrintingEconomy(player, cost, numCopies); + return !EconomyHelper.payForBookPrintingEconomy(player, cost, numCopies); } else { if (player.getInventory().contains(BooksWithoutBorders.bookPriceType, itemCost)) { payForBookPrintingItem(player, itemCost); @@ -459,27 +443,6 @@ public class BooksWithoutBorders extends JavaPlugin { } } - /** - * Uses economy to take payment for printing a number of books - * - * @param player

The player which needs to pay

- * @param cost

The cost of the book printing

- * @param numCopies

The number of books the player is printing

- * @return

True if the player had the money and it has been withdrawn

- */ - private boolean payForBookPrintingEconomy(Player player, double cost, int numCopies) { - Economy economy = BooksWithoutBorders.eco; - if ((economy.getBalance(player) - cost) >= 0) { - economy.withdrawPlayer(player, cost); - BooksWithoutBorders.sendSuccessMessage(player, economy.format(cost) + " withdrawn to create " + numCopies + " book(s)"); - BooksWithoutBorders.sendSuccessMessage(player, "New balance: " + economy.format(economy.getBalance(player))); - return true; - } else { - BooksWithoutBorders.sendErrorMessage(player, economy.format(cost) + " is required for this command!"); - return false; - } - } - /** * Takes payment for printing a number of books by withdrawing the correct item * diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandBooksWithoutBorders.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandBooksWithoutBorders.java index 182334e..6b1894d 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandBooksWithoutBorders.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandBooksWithoutBorders.java @@ -1,6 +1,7 @@ package net.knarcraft.bookswithoutborders.command; import net.knarcraft.bookswithoutborders.BooksWithoutBorders; +import net.knarcraft.bookswithoutborders.utility.EconomyHelper; import org.bukkit.Material; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -63,7 +64,7 @@ public class CommandBooksWithoutBorders implements CommandExecutor { if (bookPriceType != Material.AIR) { sendErrorMessage(sender, "[" + (int) bookPriceQuantity + " " + bookPriceType.toString() + "(s) are required to create a book]"); } else { - sendErrorMessage(sender, "[" + BooksWithoutBorders.eco.format(bookPriceQuantity) + " is required to create a book]"); + sendErrorMessage(sender, "[" + EconomyHelper.getEconomy().format(bookPriceQuantity) + " is required to create a book]"); } } sender.sendMessage(getCommandColor() + "Commands:"); diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetBookPrice.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetBookPrice.java index af366ba..3bd289b 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetBookPrice.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetBookPrice.java @@ -1,6 +1,7 @@ package net.knarcraft.bookswithoutborders.command; import net.knarcraft.bookswithoutborders.BooksWithoutBorders; +import net.knarcraft.bookswithoutborders.utility.EconomyHelper; import net.knarcraft.bookswithoutborders.utility.InventoryHelper; import org.bukkit.Material; import org.bukkit.command.Command; @@ -109,7 +110,7 @@ public class CommandSetBookPrice implements CommandExecutor { * @return

True if the price was changed successfully

*/ private boolean setEconomyPrice(CommandSender sender, double price) { - if (booksWithoutBorders.setupEconomy()) { + if (EconomyHelper.setupEconomy()) { BooksWithoutBorders.bookPriceQuantity = price; BooksWithoutBorders.bookPriceType = Material.AIR; booksWithoutBorders.getConfig().set("Options.Price_to_create_book.Item_type", "Economy"); @@ -118,7 +119,7 @@ public class CommandSetBookPrice implements CommandExecutor { booksWithoutBorders.saveConfig(); BooksWithoutBorders.sendSuccessMessage(sender, "Book creation price set to " + - BooksWithoutBorders.eco.format(BooksWithoutBorders.bookPriceQuantity) + "!"); + EconomyHelper.getEconomy().format(BooksWithoutBorders.bookPriceQuantity) + "!"); return true; } else { BooksWithoutBorders.sendErrorMessage(sender, "BooksWithoutBorders failed to hook into Vault! Book price not set!"); diff --git a/src/main/java/net/knarcraft/bookswithoutborders/utility/EconomyHelper.java b/src/main/java/net/knarcraft/bookswithoutborders/utility/EconomyHelper.java new file mode 100644 index 0000000..f580da7 --- /dev/null +++ b/src/main/java/net/knarcraft/bookswithoutborders/utility/EconomyHelper.java @@ -0,0 +1,64 @@ +package net.knarcraft.bookswithoutborders.utility; + +import net.knarcraft.bookswithoutborders.BooksWithoutBorders; +import net.milkbowl.vault.economy.Economy; +import org.bukkit.Server; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.RegisteredServiceProvider; + +public class EconomyHelper { + + private static Economy economy; + + /** + * Gets an economy instance for making transactions + * @return

An economy instance, or null if it's not initialized

+ */ + public static Economy getEconomy() { + return economy; + } + + /** + * Tries to set up economy + * @return

True if economy is set up and enabled

+ */ + public static boolean setupEconomy() { + if (economy != null) { + return true; + } + + Server server = BooksWithoutBorders.getInstance().getServer(); + Plugin plugin = server.getPluginManager().getPlugin("Vault"); + + if (plugin != null) { + RegisteredServiceProvider economyProvider = server.getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class); + if (economyProvider != null) { + economy = economyProvider.getProvider(); + } + } + + return (economy != null); + } + + /** + * Uses economy to take payment for printing a number of books + * + * @param player

The player which needs to pay

+ * @param cost

The cost of the book printing

+ * @param numCopies

The number of books the player is printing

+ * @return

True if the player had the money and it has been withdrawn

+ */ + public static boolean payForBookPrintingEconomy(Player player, double cost, int numCopies) { + if ((economy.getBalance(player) - cost) >= 0) { + economy.withdrawPlayer(player, cost); + BooksWithoutBorders.sendSuccessMessage(player, economy.format(cost) + " withdrawn to create " + numCopies + " book(s)"); + BooksWithoutBorders.sendSuccessMessage(player, "New balance: " + economy.format(economy.getBalance(player))); + return true; + } else { + BooksWithoutBorders.sendErrorMessage(player, economy.format(cost) + " is required for this command!"); + return false; + } + } + +}