EpicKnarvik97 81c15b6600
All checks were successful
EpicKnarvik97/Blacksmith/pipeline/head This commit looks good
Adds information about the appropriate crafting station in ActionStartEvent
2024-08-03 16:32:44 +02:00

206 lines
7.3 KiB
Java

package net.knarcraft.blacksmith.manager;
import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.config.blacksmith.GlobalBlacksmithSettings;
import net.knarcraft.blacksmith.config.scrapper.GlobalScrapperSettings;
import net.knarcraft.blacksmith.property.SalvageMethod;
import net.knarcraft.blacksmith.util.ItemHelper;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.ServicesManager;
import org.jetbrains.annotations.NotNull;
/**
* A class which deals with everything economy
*/
public class EconomyManager {
private static Economy economy;
private EconomyManager() {
}
/**
* Sets up Vault economy support
*
* @param servicesManager <p>The services manager to use for finding a Vault provider</p>
* @return <p>True if Vault was successfully set up</p>
*/
public static boolean setUp(@NotNull ServicesManager servicesManager) {
//If already set up, there is nothing to do
if (economy != null) {
return true;
}
return setupVault(servicesManager);
}
/**
* Gets whether the given player cannot pay for re-forging their held item
*
* @param player <p>The player holding an item</p>
* @return <p>Whether the player cannot pay for the reforge</p>
*/
public static boolean cannotPayForHeldItemReforge(@NotNull Player player) {
return economy.getBalance(player) - getHeldItemCost(player) < 0;
}
/**
* Gets whether the given player cannot pay for salvaging an item
*
* @param player <p>The player holding an item</p>
* @param salvageMethod <p>The salvage method to check</p>
* @return <p>Whether the player cannot pay for the salvage</p>
*/
public static boolean cannotPayForSalvage(@NotNull Player player, @NotNull SalvageMethod salvageMethod) {
return economy.getBalance(player) - getSalvageCost(salvageMethod) < 0;
}
/**
* Gets the human-readable cost of the given player's held item
*
* @param player <p>The player holding an item</p>
* @return <p>The formatted cost</p>
*/
@NotNull
public static String formatBlacksmithCost(@NotNull Player player) {
double cost = getHeldItemCost(player);
return economy.format(cost);
}
/**
* Gets the human-readable cost of salvaging an item
*
* @param salvageMethod <p>The salvage method to get the cost for</p>
* @return <p>The formatted cost</p>
*/
@NotNull
public static String formatSalvageCost(@NotNull SalvageMethod salvageMethod) {
return economy.format(getSalvageCost(salvageMethod));
}
/**
* Gets the cost of salvaging using the specified method
*
* @param salvageMethod <p>The salvage method to get cost for</p>
* @return <p>The salvage cost</p>
*/
private static double getSalvageCost(@NotNull SalvageMethod salvageMethod) {
GlobalScrapperSettings settings = BlacksmithPlugin.getInstance().getGlobalScrapperSettings();
return switch (salvageMethod) {
case SALVAGE, EXTENDED_SALVAGE -> settings.getSalvageCost();
case NETHERITE -> settings.getNetheriteSalvageCost();
case ARMOR_TRIM -> settings.getArmorTrimSalvageCost();
};
}
/**
* Withdraws the reforging cost from the given player
*
* <p>The cost is automatically calculated from the item in the player's main hand.</p>
*
* @param player <p>The player to withdraw from</p>
*/
public static void withdrawBlacksmith(@NotNull Player player) {
double cost = getHeldItemCost(player);
if (cost > 0) {
economy.withdrawPlayer(player, cost);
}
}
/**
* Withdraws the salvaging cost from the given player
*
* @param player <p>The player to withdraw from</p>
* @param salvageMethod <p>The salvage method to withdraw for</p>
*/
public static void withdrawScrapper(@NotNull Player player, @NotNull SalvageMethod salvageMethod) {
double cost = getSalvageCost(salvageMethod);
if (cost > 0) {
economy.withdrawPlayer(player, cost);
}
}
/**
* Gets the cost of the item in the given player's main hand
*
* @param player <p>The player to calculate the cost for</p>
* @return <p>The calculated cost</p>
*/
private static double getHeldItemCost(@NotNull Player player) {
return getCost(player.getInventory().getItemInMainHand());
}
/**
* Gets the cost of repairing the given item
*
* @param item <p>The item to be repaired</p>
* @return <p>The cost of the repair</p>
*/
private static double getCost(@NotNull ItemStack item) {
GlobalBlacksmithSettings globalBlacksmithSettings = BlacksmithPlugin.getInstance().getGlobalBlacksmithSettings();
Material material = item.getType();
//Calculate the base price
double price = globalBlacksmithSettings.getBasePrice(material);
// Adjust price based on durability
double pricePerDurabilityPoint = globalBlacksmithSettings.getPricePerDurabilityPoint(material);
if (globalBlacksmithSettings.getUseNaturalCost()) {
//Cost increases with damage
price += ((double) ItemHelper.getDamage(item)) * pricePerDurabilityPoint;
} else {
//Cost decreases with damage
price += ((double) ItemHelper.getDurability(item)) * pricePerDurabilityPoint;
}
//Increase price for any enchantments
price += getEnchantmentCost(item);
//Override the cost for anvils
if (ItemHelper.isAnvil(material, true)) {
price = globalBlacksmithSettings.getAnvilCost(material);
}
return price;
}
/**
* Gets the cost resulting from all enchantments on the given item
*
* @param item <p>The item to calculate enchantment cost for</p>
* @return <p>The resulting enchantment cost</p>
*/
private static double getEnchantmentCost(@NotNull ItemStack item) {
GlobalBlacksmithSettings settings = BlacksmithPlugin.getInstance().getGlobalBlacksmithSettings();
double price = 0;
for (Enchantment enchantment : item.getEnchantments().keySet()) {
price += settings.getEnchantmentCost(enchantment) * item.getEnchantmentLevel(enchantment);
}
return price;
}
/**
* Sets up Vault for economy
*
* @param servicesManager <p>The services manager to use for finding a Vault provider</p>
* @return <p>True if Vault was successfully set up</p>
*/
private static boolean setupVault(@NotNull ServicesManager servicesManager) {
// Setup Vault
RegisteredServiceProvider<Economy> economyProvider = servicesManager.getRegistration(Economy.class);
if (economyProvider != null) {
economy = economyProvider.getProvider();
return true;
} else {
// Disable if no economy plugin was found
BlacksmithPlugin.error("Failed to load an economy plugin. Disabling...");
return false;
}
}
}