Refactor/rework

This commit is contained in:
Len
2022-08-22 00:21:36 +02:00
parent 658837814a
commit 3aa8414c5b
17 changed files with 885 additions and 12 deletions
@@ -0,0 +1,58 @@
package com.alttd.playershops.utils;
import com.alttd.playershops.PlayerShops;
import com.alttd.playershops.shop.PlayerShop;
import net.milkbowl.vault.economy.EconomyResponse;
import org.bukkit.entity.Player;
// TODO document
public class EconomyUtils {
//check to see if the player has enough funds to take out [amount]
//return false if they do not
public static boolean hasSufficientFunds(Player player, double amount) {
double balance = PlayerShops.getInstance().getEconomy().getBalance(player);
return (balance >= amount);
}
public static boolean hasSufficientFunds(PlayerShop shop, double amount) {
double balance = shop.getBalance();
return (balance >= amount);
}
//gets the current funds of the player
public static double getFunds(Player player) {
return PlayerShops.getInstance().getEconomy().getBalance(player);
}
//removes [amount] of funds from the player
//return false if the player did not have sufficient funds or if something went wrong
public static boolean removeFunds(Player player, double amount) {
EconomyResponse response = PlayerShops.getInstance().getEconomy().withdrawPlayer(player, amount);
return response.transactionSuccess();
}
//adds [amount] of funds to the player
//return false if the player is offline
public static boolean addFunds(Player player, double amount) {
if(player.isOnline()) {
EconomyResponse response = PlayerShops.getInstance().getEconomy().depositPlayer(player, amount);
return response.transactionSuccess();
} else {
return false;
// int temp = PlayerShops.getInstance().Earnings.getInt(player.getUniqueId() + ".earnings");
// PlayerShops.getInstance().Earnings.set(player.getUniqueId() + ".earnings", temp + amount);
// PlayerShops.getInstance().Earnings.save();
}
}
public static boolean canAcceptFunds(Player player, double price) {
// if we ever need to limit the maximum balance a player can have this is the place
return true;
}
public static boolean canAcceptFunds(PlayerShop shop, double price) {
// if we ever need to limit the maximum balance a shop can have this is the place
return true;
}
}
@@ -0,0 +1,119 @@
package com.alttd.playershops.utils;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.HashMap;
public class InventoryUtils {
/**
* Counts the number of items in the given inventory where
* Util.matches(inventory item, item) is true.
*
* @param inv
* The inventory to search
* @param item
* The ItemStack to search for
* @return The number of items that match in this inventory.
*/
public static int countItems(Inventory inv, ItemStack item) {
int items = 0;
for (ItemStack iStack : inv.getContents()) {
if (iStack == null)
continue;
if (ShopUtil.matches(item, iStack)) {
items += iStack.getAmount();
}
}
return items;
}
// TODO DOCS
//removes itemstack from inventory
//returns the amount of items it could not remove
public static int removeItem(Inventory inventory, ItemStack itemStack) {
if (inventory == null)
return itemStack.getAmount();
if (itemStack == null || itemStack.getAmount() <= 0)
return 0;
ItemStack[] contents = inventory.getContents();
int amount = itemStack.getAmount();
for (ItemStack stack : contents) {
if (stack != null) {
if (ShopUtil.matches(stack, itemStack)) {
if (stack.getAmount() > amount) {
stack.setAmount(stack.getAmount() - amount);
inventory.setContents(contents);
return 0;
} else if (stack.getAmount() == amount) {
stack.setType(Material.AIR);
inventory.setContents(contents);
return 0;
} else {
amount -= stack.getAmount();
stack.setType(Material.AIR);
}
}
}
}
inventory.setContents(contents);
return amount;
}
//takes an ItemStack and splits it up into multiple ItemStacks with correct stack sizes
//then adds those items to the given inventory
public static int addItem(Inventory inventory, ItemStack itemStack) {
if (inventory == null)
return itemStack.getAmount();
if (itemStack.getAmount() <= 0)
return 0;
ArrayList<ItemStack> itemStacksAdding = new ArrayList<>();
//break up the itemstack into multiple ItemStacks with correct stack size
int fullStacks = itemStack.getAmount() / itemStack.getMaxStackSize();
int partialStack = itemStack.getAmount() % itemStack.getMaxStackSize();
for (int i = 0; i < fullStacks; i++) {
ItemStack is = itemStack.clone();
is.setAmount(is.getMaxStackSize());
itemStacksAdding.add(is);
}
ItemStack is = itemStack.clone();
is.setAmount(partialStack);
if (partialStack > 0)
itemStacksAdding.add(is);
//try adding all items from itemStacksAdding and return number of ones you couldn't add
int amount = 0;
for (ItemStack addItem : itemStacksAdding) {
HashMap<Integer, ItemStack> noAdd = inventory.addItem(addItem);
for (ItemStack noAddItemstack : noAdd.values()) {
amount += noAddItemstack.getAmount();
}
}
return amount;
}
public static boolean hasRoom(Inventory inventory, ItemStack itemStack) {
if (inventory == null)
return false;
if (itemStack.getAmount() <= 0)
return true;
int overflow = addItem(inventory, itemStack);
//revert back if inventory cannot hold all of the items
if (overflow > 0) {
ItemStack revert = itemStack.clone();
revert.setAmount(revert.getAmount() - overflow);
InventoryUtils.removeItem(inventory, revert);
return false;
}
removeItem(inventory, itemStack);
return true;
}
}
@@ -0,0 +1,83 @@
package com.alttd.playershops.utils;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.bukkit.Bukkit;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import java.util.HashMap;
public class Util {
private static MiniMessage miniMessage = null;
public static HashMap<String, String> colors;
static { // this might be in minimessage already?
colors = new HashMap<>();
colors.put("&0", "<black>");
colors.put("&1", "<dark_blue>");
colors.put("&2", "<dark_green>");
colors.put("&3", "<dark_aqua>");
colors.put("&4", "<dark_red>");
colors.put("&5", "<dark_purple>");
colors.put("&6", "<gold>");
colors.put("&7", "<gray>");
colors.put("&8", "<dark_gray>");
colors.put("&9", "<blue>");
colors.put("&a", "<green>");
colors.put("&b", "<aqua>");
colors.put("&c", "<red>");
colors.put("&d", "<light_purple>");
colors.put("&e", "<yellow>");
colors.put("&f", "<white>");
colors.put("&l", "<bold>");
colors.put("&o", "<italic>");
}
public static String parseColors(String message) {
for (String key : colors.keySet()) {
if (message.contains(key)) {
message = message.replace(key, colors.get(key));
}
}
return message;
}
private static MiniMessage miniMessage() {
if (miniMessage == null)
miniMessage = MiniMessage.miniMessage();
return miniMessage;
}
public static Component parseMiniMessage(String message, TagResolver tagResolver) {
message = parseColors(message);
if (tagResolver == null) {
return miniMessage().deserialize(message);
} else {
return miniMessage().deserialize(message, tagResolver);
}
}
public static String capitalize(String string) {
if (string.length() <= 1)
return string.toUpperCase();
string = string.toLowerCase();
return string.substring(0, 1).toUpperCase() + string.toLowerCase().substring(1);
}
/**
* Call an event and check it is cancelled.
*
* @param event The event implement the Cancellable interface.
* @return The event is cancelled.
*/
public static boolean callCancellableEvent(Cancellable event) {
if (!(event instanceof Event)) {
throw new IllegalArgumentException("Cancellable must is event implement");
}
Bukkit.getPluginManager().callEvent((Event) event);
return event.isCancelled();
}
}