Moves economy related code to its own class

This commit is contained in:
Kristian Knarvik 2021-09-02 01:29:44 +02:00
parent 7bfa60aeee
commit 3717e4d3b6
4 changed files with 72 additions and 43 deletions

View File

@ -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<String> firstBooks;
public static String welcomeMessage;
public static List<String> 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<Economy> 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 <p>The player which needs to pay</p>
* @param cost <p>The cost of the book printing</p>
* @param numCopies <p>The number of books the player is printing</p>
* @return <p>True if the player had the money and it has been withdrawn</p>
*/
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
*

View File

@ -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:");

View File

@ -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 <p>True if the price was changed successfully</p>
*/
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!");

View File

@ -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 <p>An economy instance, or null if it's not initialized</p>
*/
public static Economy getEconomy() {
return economy;
}
/**
* Tries to set up economy
* @return <p>True if economy is set up and enabled</p>
*/
public static boolean setupEconomy() {
if (economy != null) {
return true;
}
Server server = BooksWithoutBorders.getInstance().getServer();
Plugin plugin = server.getPluginManager().getPlugin("Vault");
if (plugin != null) {
RegisteredServiceProvider<Economy> 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 <p>The player which needs to pay</p>
* @param cost <p>The cost of the book printing</p>
* @param numCopies <p>The number of books the player is printing</p>
* @return <p>True if the player had the money and it has been withdrawn</p>
*/
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;
}
}
}