The command sender to notify of any problems
+ * @param costStringThe string to parse into a cost
+ * @returnThe parsed cost
+ * @throws IllegalArgumentExceptionIf the specified cost is invalid
+ */ + private double parseSimpleCost(@NotNull CommandSender commandSender, + @NotNull String costString) throws IllegalArgumentException { + try { + double cost = Double.parseDouble(costString); + if (cost < 0) { + throw new NumberFormatException(); + } + return cost; + } catch (NumberFormatException exception) { + BlacksmithPlugin.getStringFormatter().displayErrorMessage(commandSender, + BlacksmithTranslatableMessage.DOUBLE_COST_REQUIRED); + throw new IllegalArgumentException("Invalid cost given"); + } + } + } diff --git a/src/main/java/net/knarcraft/blacksmith/config/scrapper/GlobalScrapperSettings.java b/src/main/java/net/knarcraft/blacksmith/config/scrapper/GlobalScrapperSettings.java index ce53805..a94df02 100644 --- a/src/main/java/net/knarcraft/blacksmith/config/scrapper/GlobalScrapperSettings.java +++ b/src/main/java/net/knarcraft/blacksmith/config/scrapper/GlobalScrapperSettings.java @@ -213,30 +213,21 @@ public class GlobalScrapperSettings implements SettingsThe enchanted book salvage base cost
+ * @returnThe salvage cost increase formula
*/ - public double getEnchantedBookSalvageCost() { - return asDouble(ScrapperSetting.ENCHANTED_BOOK_SALVAGE_BASE_COST); + public String getSalvageCostIncrease() { + return asString(ScrapperSetting.SALVAGE_COST_INCREASE); } /** - * Whether to multiply the cost of salvaging enchanted books based on the number of enchantments + * Gets the math formula for the increase in salvage cooldown * - * @returnWhether to multiply the cost of salvaging enchanted books based on the number of enchantments
+ * @returnThe salvage cooldown increase formula
*/ - public boolean multiplyEnchantedBookSalvageCost() { - return asBoolean(ScrapperSetting.ENCHANTED_BOOK_SALVAGE_MULTIPLY); - } - - /** - * Whether to require both a monetary and item-based cost when salvaging enchanted books - * - * @returnWhether to require both a monetary and item-based cost when salvaging enchanted books
- */ - public boolean requireMoneyAndItemForEnchantedBookSalvage() { - return asBoolean(ScrapperSetting.ENCHANTED_BOOK_SALVAGE_REQUIRE_BOTH); + public String getSalvageCooldownIncrease() { + return asString(ScrapperSetting.SALVAGE_COOLDOWN_INCREASE); } /** @@ -302,4 +293,15 @@ public class GlobalScrapperSettings implements SettingsThe setting to get the value of
+ * @returnThe value of the given setting as a string
+ */ + @NotNull + private String asString(@NotNull ScrapperSetting setting) { + return getValue(setting).toString(); + } + } diff --git a/src/main/java/net/knarcraft/blacksmith/config/scrapper/ScrapperSetting.java b/src/main/java/net/knarcraft/blacksmith/config/scrapper/ScrapperSetting.java index 7225a85..3f1d849 100644 --- a/src/main/java/net/knarcraft/blacksmith/config/scrapper/ScrapperSetting.java +++ b/src/main/java/net/knarcraft/blacksmith/config/scrapper/ScrapperSetting.java @@ -292,28 +292,18 @@ public enum ScrapperSetting implements Setting { "The cost of using the scrapper to remove netherite from an item", false, false), /** - * The setting for the enchanted book salvage cost + * The mathematical formula for increasing salvage cost */ - ENCHANTED_BOOK_SALVAGE_BASE_COST("enchantedBookSalvageBasePrice", SettingValueType.POSITIVE_DOUBLE, 10, - "The per-enchantment cost of splitting an enchanted book", false, false), + SALVAGE_COST_INCREASE("salvageCostIncrease", SettingValueType.STRING, "{cost}*{timesUsed}", + "The mathematical formula for salvage cost increase when continually used within the same hour. " + + "This is necessary for some servers where items can be easily farmed. Set to {cost} to disable behavior.", false, false), /** - * The setting for the enchanted book item cost + * The mathematical formula for increasing salvage cooldown */ - ENCHANTED_BOOK_SALVAGE_ITEM_COST("enchantedBookSalvageCost", SettingValueType.ADVANCED_COST, null, - "The cost to pay in order to salvage an enchanted book", false, false), - - /** - * The setting for whether to multiply enchanted book cost - */ - ENCHANTED_BOOK_SALVAGE_MULTIPLY("enchantedBookSalvageMultiplyByEnchantmentNumber", SettingValueType.BOOLEAN, - true, "Whether to multiply the cost of salvaging an enchanted book by the number of enchantments on the book", false, false), - - /** - * The setting for whether to require both monetary and item payment for salvaging enchanted books - */ - ENCHANTED_BOOK_SALVAGE_REQUIRE_BOTH("enchantedBookSalvageRequireMoneyAndItem", SettingValueType.BOOLEAN, - true, "Whether salvaging an enchanted book will cost both the configured money amount and the configured item", false, false), + SALVAGE_COOLDOWN_INCREASE("salvageCooldownIncrease", SettingValueType.STRING, "{cooldown}*{timesUsed}", + "The mathematical formula for salvage cooldown increase when continually used within the same hour. " + + "This is necessary for some servers where items can be easily farmed. Set to {cooldown} to disable behavior.", false, false), /** * Whether to display exact time in minutes and seconds when displaying a remaining cool-down diff --git a/src/main/java/net/knarcraft/blacksmith/formatting/BlacksmithTranslatableMessage.java b/src/main/java/net/knarcraft/blacksmith/formatting/BlacksmithTranslatableMessage.java index dfd2844..f31647f 100644 --- a/src/main/java/net/knarcraft/blacksmith/formatting/BlacksmithTranslatableMessage.java +++ b/src/main/java/net/knarcraft/blacksmith/formatting/BlacksmithTranslatableMessage.java @@ -143,7 +143,12 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage { /** * The format to use for formatting any message spoken by a blacksmith NPC */ - NPC_TALK_FORMAT; + NPC_TALK_FORMAT, + + /** + * The text to display when explaining that a cost must be a positive double + */ + DOUBLE_COST_REQUIRED; /** * Gets the message to display when displaying the raw value of messages diff --git a/src/main/java/net/knarcraft/blacksmith/manager/EconomyManager.java b/src/main/java/net/knarcraft/blacksmith/manager/EconomyManager.java index 7bbce34..9e137ee 100644 --- a/src/main/java/net/knarcraft/blacksmith/manager/EconomyManager.java +++ b/src/main/java/net/knarcraft/blacksmith/manager/EconomyManager.java @@ -5,8 +5,10 @@ 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.knarcraft.blacksmith.util.SalvageHelper; +import net.knarcraft.knarlib.formatting.StringReplacer; import net.milkbowl.vault.economy.Economy; +import net.objecthunter.exp4j.Expression; +import net.objecthunter.exp4j.ExpressionBuilder; import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Player; @@ -59,7 +61,8 @@ public class EconomyManager { * @returnWhether the player cannot pay for the salvage
*/ public static boolean cannotPayForSalvage(@NotNull Player player, @NotNull SalvageMethod salvageMethod, @NotNull ItemStack item) { - return economy.getBalance(player) - getSalvageCost(salvageMethod, item) < 0; + // TODO: Account for advanced cost options + return economy.getBalance(player) - getSalvageCost(salvageMethod, item, player) < 0; } /** @@ -79,11 +82,13 @@ public class EconomyManager { * * @param salvageMethodThe salvage method to get the cost for
* @param itemThe item to be salvaged
+ * @param playerThe player to provide the cost to
* @returnThe formatted cost
*/ @NotNull - public static String formatSalvageCost(@NotNull SalvageMethod salvageMethod, @NotNull ItemStack item) { - return economy.format(getSalvageCost(salvageMethod, item)); + public static String formatSalvageCost(@NotNull SalvageMethod salvageMethod, @NotNull ItemStack item, + @NotNull Player player) { + return economy.format(getSalvageCost(salvageMethod, item, player)); } /** @@ -112,16 +117,25 @@ public class EconomyManager { * Gets the cost of salvaging using the specified method * * @param salvageMethodThe salvage method to get cost for
+ * @param itemThe item to be salvaged
* @returnThe salvage cost
*/ - private static double getSalvageCost(@NotNull SalvageMethod salvageMethod, @NotNull ItemStack item) { + private static double getSalvageCost(@NotNull SalvageMethod salvageMethod, @NotNull ItemStack item, + @NotNull Player player) { GlobalScrapperSettings settings = BlacksmithPlugin.getInstance().getGlobalScrapperSettings(); - return switch (salvageMethod) { + double baseCost = switch (salvageMethod) { case SALVAGE, EXTENDED_SALVAGE -> settings.getSalvageCost(); case NETHERITE -> settings.getNetheriteSalvageCost(); case ARMOR_TRIM -> settings.getArmorTrimSalvageCost(); case ENCHANTED_BOOK -> getEnchantedBookSalvageCost(item); }; + + StringReplacer replacer = new StringReplacer(settings.getSalvageCostIncrease()); + replacer.add("{cost}", String.valueOf(baseCost)); + replacer.add("{timesUsed}", String.valueOf(PlayerUsageManager.getUsages(player, + System.currentTimeMillis() - 3600000))); + Expression expression = new ExpressionBuilder(replacer.replace()).build(); + return Math.max(expression.evaluate(), baseCost); } /** @@ -145,7 +159,7 @@ public class EconomyManager { * @param salvageMethodThe salvage method to withdraw for
*/ public static void withdrawScrapper(@NotNull Player player, @NotNull SalvageMethod salvageMethod) { - double cost = getSalvageCost(salvageMethod, player.getInventory().getItemInMainHand()); + double cost = getSalvageCost(salvageMethod, player.getInventory().getItemInMainHand(), player); if (cost > 0) { economy.withdrawPlayer(player, cost); } @@ -230,12 +244,15 @@ public class EconomyManager { * @returnThe cost of scrapping the enchanted book
*/ private static double getEnchantedBookSalvageCost(@NotNull ItemStack item) { - GlobalScrapperSettings settings = BlacksmithPlugin.getInstance().getGlobalScrapperSettings(); + // TODO: Properly implement this + /*GlobalScrapperSettings settings = BlacksmithPlugin.getInstance().getGlobalScrapperSettings(); + double cost = settings.getEnchantedBookSalvageCost(); if (settings.multiplyEnchantedBookSalvageCost()) { cost *= SalvageHelper.getEnchantmentCount(item) - 1; } - return SalvageHelper.getEnchantmentCount(item) * cost; + return SalvageHelper.getEnchantmentCount(item) * cost;*/ + return 1000000; } /** diff --git a/src/main/java/net/knarcraft/blacksmith/manager/PlayerUsageManager.java b/src/main/java/net/knarcraft/blacksmith/manager/PlayerUsageManager.java new file mode 100644 index 0000000..b38070d --- /dev/null +++ b/src/main/java/net/knarcraft/blacksmith/manager/PlayerUsageManager.java @@ -0,0 +1,75 @@ +package net.knarcraft.blacksmith.manager; + +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +/** + * A manager for keeping track of players using scrappers + */ +public class PlayerUsageManager { + + private static final MapThe player using a scrapper
+ */ + public static void registerUsage(@NotNull Player player) { + playerUsages.putIfAbsent(player, new HashSet<>()); + SetThe player to check usages for
+ * @param afterTimestampThe timestamp of when to start looking for usages
+ * @returnThe number of scrapper uses for the given player
+ */ + public static int getUsages(@NotNull Player player, @NotNull Long afterTimestamp) { + SetThe timestamp for the earliest time of valid timestamps
+ */ + public static void removeExpiredData(@NotNull Long afterTimestamp) { + for (Set