Attempts to start proper implementation of the new action cost
All checks were successful
EpicKnarvik97/Blacksmith/pipeline/head This commit looks good
All checks were successful
EpicKnarvik97/Blacksmith/pipeline/head This commit looks good
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -83,7 +83,7 @@
|
||||
<dependency>
|
||||
<groupId>net.knarcraft</groupId>
|
||||
<artifactId>knarlib</artifactId>
|
||||
<version>1.2.18</version>
|
||||
<version>1.2.19</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
||||
@@ -15,6 +15,7 @@ import net.knarcraft.blacksmith.command.scrapper.ScrapperEditCommand;
|
||||
import net.knarcraft.blacksmith.command.scrapper.ScrapperEditTabCompleter;
|
||||
import net.knarcraft.blacksmith.config.blacksmith.GlobalBlacksmithSettings;
|
||||
import net.knarcraft.blacksmith.config.scrapper.GlobalScrapperSettings;
|
||||
import net.knarcraft.blacksmith.container.ActionCost;
|
||||
import net.knarcraft.blacksmith.formatting.Translatable;
|
||||
import net.knarcraft.blacksmith.listener.NPCClickListener;
|
||||
import net.knarcraft.blacksmith.listener.PlayerListener;
|
||||
@@ -31,6 +32,7 @@ import net.knarcraft.knarlib.util.ConfigHelper;
|
||||
import net.knarcraft.knarlib.util.UpdateChecker;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerialization;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.plugin.PluginDescriptionFile;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
@@ -124,6 +126,8 @@ public class BlacksmithPlugin extends ConfigCommentPlugin {
|
||||
instance = this;
|
||||
ConfigHelper.saveDefaults(this);
|
||||
|
||||
ConfigurationSerialization.registerClass(ActionCost.class);
|
||||
|
||||
//Migrate from an earlier configuration file syntax if necessary
|
||||
if (getConfig().getString("scrapper.defaults.dropItem") == null) {
|
||||
net.knarcraft.knarlib.util.ConfigHelper.migrateConfig(this);
|
||||
|
||||
@@ -3,6 +3,7 @@ package net.knarcraft.blacksmith.config.scrapper;
|
||||
import net.knarcraft.blacksmith.BlacksmithPlugin;
|
||||
import net.knarcraft.blacksmith.config.SettingValueType;
|
||||
import net.knarcraft.blacksmith.config.Settings;
|
||||
import net.knarcraft.blacksmith.container.ActionCost;
|
||||
import net.knarcraft.blacksmith.util.ConfigHelper;
|
||||
import net.knarcraft.blacksmith.util.ItemHelper;
|
||||
import org.bukkit.Material;
|
||||
@@ -113,18 +114,6 @@ public class GlobalScrapperSettings implements Settings<ScrapperSetting> {
|
||||
return ConfigHelper.asBoolean(getValue(setting));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the given value as a double
|
||||
*
|
||||
* <p>This will throw an exception if used for a non-double setting</p>
|
||||
*
|
||||
* @param setting <p>The setting to get the value of</p>
|
||||
* @return <p>The value of the given setting as a double</p>
|
||||
*/
|
||||
public double asDouble(@NotNull ScrapperSetting setting) {
|
||||
return ConfigHelper.asDouble(getValue(setting));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value of a setting, using the default if not set
|
||||
*
|
||||
@@ -190,8 +179,9 @@ public class GlobalScrapperSettings implements Settings<ScrapperSetting> {
|
||||
*
|
||||
* @return <p>The cost of using a scrapper to salvage an item</p>
|
||||
*/
|
||||
public double getSalvageCost() {
|
||||
return asDouble(ScrapperSetting.SALVAGE_COST);
|
||||
@NotNull
|
||||
public ActionCost getSalvageCost() {
|
||||
return getCost(ScrapperSetting.SALVAGE_COST, "salvage");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -199,8 +189,9 @@ public class GlobalScrapperSettings implements Settings<ScrapperSetting> {
|
||||
*
|
||||
* @return <p>The cost of using a scrapper to remove armor trim</p>
|
||||
*/
|
||||
public double getArmorTrimSalvageCost() {
|
||||
return asDouble(ScrapperSetting.ARMOR_TRIM_SALVAGE_COST);
|
||||
@NotNull
|
||||
public ActionCost getArmorTrimSalvageCost() {
|
||||
return getCost(ScrapperSetting.ARMOR_TRIM_SALVAGE_COST, "armor trim salvage");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -208,8 +199,19 @@ public class GlobalScrapperSettings implements Settings<ScrapperSetting> {
|
||||
*
|
||||
* @return <p>The cost of using a scrapper to remove netherite from an item</p>
|
||||
*/
|
||||
public double getNetheriteSalvageCost() {
|
||||
return asDouble(ScrapperSetting.NETHERITE_SALVAGE_COST);
|
||||
@NotNull
|
||||
public ActionCost getNetheriteSalvageCost() {
|
||||
return getCost(ScrapperSetting.NETHERITE_SALVAGE_COST, "netherite salvage");
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the cost of using a scrapper to split an enchanted book
|
||||
*
|
||||
* @return <p>The cost of using a scrapper to split an enchanted book</p>
|
||||
*/
|
||||
@NotNull
|
||||
public ActionCost getEnchantedBookSalvageCost() {
|
||||
return getCost(ScrapperSetting.ENCHANTED_BOOK_SALVAGE_COST, "enchanted book salvage");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -230,6 +232,15 @@ public class GlobalScrapperSettings implements Settings<ScrapperSetting> {
|
||||
return asString(ScrapperSetting.SALVAGE_COOLDOWN_INCREASE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets whether the enchanted book salvage price should be multiplied with the number of enchantments
|
||||
*
|
||||
* @return <p>Whether the enchanted book salvage price should be multiplied with the number of enchantments</p>
|
||||
*/
|
||||
public boolean multiplyEnchantedBokSalvagePrice() {
|
||||
return asBoolean(ScrapperSetting.MULTIPLY_ENCHANTED_BOOK_SALVAGE_COST);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets trash salvage for the given material
|
||||
*
|
||||
@@ -304,4 +315,24 @@ public class GlobalScrapperSettings implements Settings<ScrapperSetting> {
|
||||
return getValue(setting).toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the action cost from the given scrapper setting
|
||||
*
|
||||
* @param setting <p>The setting to get cost from</p>
|
||||
* @param costType <p>Cost identifier for error message</p>
|
||||
* @return <p>The cost of the action</p>
|
||||
*/
|
||||
@NotNull
|
||||
private ActionCost getCost(@NotNull ScrapperSetting setting, @NotNull String costType) {
|
||||
Object value = getValue(setting);
|
||||
if (value instanceof ActionCost actionCost) {
|
||||
return actionCost;
|
||||
} else if (value instanceof Double number) {
|
||||
return new ActionCost(number);
|
||||
} else {
|
||||
BlacksmithPlugin.error("Unable to load " + costType + " cost! Cost set to free!");
|
||||
return new ActionCost();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -276,21 +276,33 @@ public enum ScrapperSetting implements Setting {
|
||||
/**
|
||||
* The setting for the salvage cost
|
||||
*/
|
||||
SALVAGE_COST("salvagePrice", SettingValueType.POSITIVE_DOUBLE, 0,
|
||||
SALVAGE_COST("salvagePrice", SettingValueType.ADVANCED_COST, 0,
|
||||
"The cost of using a scrapper to salvage an item", false, false),
|
||||
|
||||
/**
|
||||
* The setting for the armor trim salvage cost
|
||||
*/
|
||||
ARMOR_TRIM_SALVAGE_COST("armorTrimSalvagePrice", SettingValueType.POSITIVE_DOUBLE, 5,
|
||||
ARMOR_TRIM_SALVAGE_COST("armorTrimSalvagePrice", SettingValueType.ADVANCED_COST, 5,
|
||||
"The cost of using the scrapper to remove armor trim", false, false),
|
||||
|
||||
/**
|
||||
* The setting for the netherite salvage cost
|
||||
*/
|
||||
NETHERITE_SALVAGE_COST("netheriteSalvagePrice", SettingValueType.POSITIVE_DOUBLE, 15,
|
||||
NETHERITE_SALVAGE_COST("netheriteSalvagePrice", SettingValueType.ADVANCED_COST, 15,
|
||||
"The cost of using the scrapper to remove netherite from an item", false, false),
|
||||
|
||||
/**
|
||||
* The setting for the enchanted book salvage cost
|
||||
*/
|
||||
ENCHANTED_BOOK_SALVAGE_COST("enchantedBookSalvagePrice", SettingValueType.ADVANCED_COST, 15,
|
||||
"The cost of using the scrapper to split an enchanted book", false, false),
|
||||
|
||||
/**
|
||||
* The setting for whether to multiply enchanted book salvage cost
|
||||
*/
|
||||
MULTIPLY_ENCHANTED_BOOK_SALVAGE_COST("multiplyEnchantedBookSalvageCost", SettingValueType.BOOLEAN, false,
|
||||
"Whether to multiply the enchanted book salvage cost with the number of enchantments", false, false),
|
||||
|
||||
/**
|
||||
* The mathematical formula for increasing salvage cost
|
||||
*/
|
||||
|
||||
@@ -4,11 +4,9 @@ import net.knarcraft.blacksmith.BlacksmithPlugin;
|
||||
import net.knarcraft.blacksmith.manager.EconomyManager;
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.PlayerInventory;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -30,6 +28,22 @@ import java.util.Set;
|
||||
public record ActionCost(double monetaryCost, int expCost, @Nullable ItemStack itemCost,
|
||||
@NotNull Set<String> requiredPermissions) implements ConfigurationSerializable {
|
||||
|
||||
/**
|
||||
* Instantiates a new empty action cost
|
||||
*/
|
||||
public ActionCost() {
|
||||
this(0, 0, null, Set.of());
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiates a new action cost with the specified monetary cost
|
||||
*
|
||||
* @param monetaryCost <p>The monetary cost to set</p>
|
||||
*/
|
||||
public ActionCost(double monetaryCost) {
|
||||
this(monetaryCost, 0, null, Set.of());
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the monetary cost of this action
|
||||
*
|
||||
@@ -71,13 +85,13 @@ public record ActionCost(double monetaryCost, int expCost, @Nullable ItemStack i
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays this action cost as a string
|
||||
* Gets this action cost's costs as a string
|
||||
*
|
||||
* @param player <p>The player to calculate the cost for</p>
|
||||
* @return <p>The string representation of this action cost</p>
|
||||
*/
|
||||
@NotNull
|
||||
public String displayCost(@NotNull Player player) {
|
||||
public String getCostString(@NotNull Player player) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
if (monetaryCost > 0) {
|
||||
builder.append(EconomyManager.format(monetaryCost)).append(", ").append("\n");
|
||||
@@ -174,7 +188,6 @@ public record ActionCost(double monetaryCost, int expCost, @Nullable ItemStack i
|
||||
return new HashMap<>();
|
||||
}
|
||||
|
||||
PlayerInventory playerInventory = player.getInventory();
|
||||
ItemMeta targetMeta = this.itemCost.getItemMeta();
|
||||
String displayName = null;
|
||||
List<String> lore = null;
|
||||
@@ -184,9 +197,7 @@ public record ActionCost(double monetaryCost, int expCost, @Nullable ItemStack i
|
||||
}
|
||||
|
||||
Map<Integer, Integer> output = new HashMap<>();
|
||||
HashMap<Integer, ? extends ItemStack> itemsOfType = playerInventory.all(this.itemCost.getType());
|
||||
|
||||
for (Map.Entry<Integer, ? extends ItemStack> entry : itemsOfType.entrySet()) {
|
||||
for (Map.Entry<Integer, ? extends ItemStack> entry : player.getInventory().all(this.itemCost.getType()).entrySet()) {
|
||||
if (targetMeta != null) {
|
||||
// Only consider item if the name and lore is the same
|
||||
ItemMeta meta = entry.getValue().getItemMeta();
|
||||
@@ -214,14 +225,13 @@ public record ActionCost(double monetaryCost, int expCost, @Nullable ItemStack i
|
||||
|
||||
int clearedAmount = 0;
|
||||
for (Map.Entry<Integer, Integer> entry : getValidItems(player).entrySet()) {
|
||||
int inventory = entry.getKey();
|
||||
int amount = entry.getValue();
|
||||
if (amount <= this.itemCost.getAmount() - clearedAmount) {
|
||||
clearedAmount += amount;
|
||||
player.getInventory().clear(entry.getKey());
|
||||
} else {
|
||||
clearedAmount = this.itemCost.getAmount();
|
||||
ItemStack item = player.getInventory().getItem(inventory);
|
||||
ItemStack item = player.getInventory().getItem(entry.getKey());
|
||||
if (item != null) {
|
||||
item.setAmount(amount - clearedAmount);
|
||||
} else {
|
||||
@@ -236,22 +246,6 @@ public record ActionCost(double monetaryCost, int expCost, @Nullable ItemStack i
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads an action cost from a configuration section
|
||||
*
|
||||
* @param configurationSection <p>The configuration section to load from</p>
|
||||
* @param key <p>The key of the cost to load</p>
|
||||
* @return <p>The loaded cost</p>
|
||||
*/
|
||||
private static ActionCost loadActionCost(@NotNull ConfigurationSection configurationSection, @NotNull String key) {
|
||||
double cost = configurationSection.getDouble(key, Double.MIN_VALUE);
|
||||
if (cost != Double.MIN_VALUE) {
|
||||
return new ActionCost(cost, 0, null, Set.of());
|
||||
} else {
|
||||
return (ActionCost) configurationSection.get(key);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserializes an action cost
|
||||
*
|
||||
|
||||
@@ -3,8 +3,10 @@ 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.container.ActionCost;
|
||||
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;
|
||||
@@ -60,9 +62,9 @@ public class EconomyManager {
|
||||
* @param item <p>The item to be salvaged</p>
|
||||
* @return <p>Whether the player cannot pay for the salvage</p>
|
||||
*/
|
||||
public static boolean cannotPayForSalvage(@NotNull Player player, @NotNull SalvageMethod salvageMethod, @NotNull ItemStack item) {
|
||||
// TODO: Account for advanced cost options
|
||||
return economy.getBalance(player) - getSalvageCost(salvageMethod, item, player) < 0;
|
||||
public static boolean cannotPayForSalvage(@NotNull Player player, @NotNull SalvageMethod salvageMethod,
|
||||
@NotNull ItemStack item) {
|
||||
return !getSalvageCost(salvageMethod, item, player).canPay(player);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -88,7 +90,7 @@ public class EconomyManager {
|
||||
@NotNull
|
||||
public static String formatSalvageCost(@NotNull SalvageMethod salvageMethod, @NotNull ItemStack item,
|
||||
@NotNull Player player) {
|
||||
return economy.format(getSalvageCost(salvageMethod, item, player));
|
||||
return getSalvageCost(salvageMethod, item, player).getCostString(player);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -120,10 +122,11 @@ public class EconomyManager {
|
||||
* @param item <p>The item to be salvaged</p>
|
||||
* @return <p>The salvage cost</p>
|
||||
*/
|
||||
private static double getSalvageCost(@NotNull SalvageMethod salvageMethod, @NotNull ItemStack item,
|
||||
@NotNull Player player) {
|
||||
@NotNull
|
||||
private static ActionCost getSalvageCost(@NotNull SalvageMethod salvageMethod, @NotNull ItemStack item,
|
||||
@NotNull Player player) {
|
||||
GlobalScrapperSettings settings = BlacksmithPlugin.getInstance().getGlobalScrapperSettings();
|
||||
double baseCost = switch (salvageMethod) {
|
||||
ActionCost cost = switch (salvageMethod) {
|
||||
case SALVAGE, EXTENDED_SALVAGE -> settings.getSalvageCost();
|
||||
case NETHERITE -> settings.getNetheriteSalvageCost();
|
||||
case ARMOR_TRIM -> settings.getArmorTrimSalvageCost();
|
||||
@@ -131,11 +134,12 @@ public class EconomyManager {
|
||||
};
|
||||
|
||||
StringReplacer replacer = new StringReplacer(settings.getSalvageCostIncrease());
|
||||
replacer.add("{cost}", String.valueOf(baseCost));
|
||||
replacer.add("{cost}", String.valueOf(cost.monetaryCost()));
|
||||
replacer.add("{timesUsed}", String.valueOf(PlayerUsageManager.getUsages(player,
|
||||
System.currentTimeMillis() - 3600000)));
|
||||
Expression expression = new ExpressionBuilder(replacer.replace()).build();
|
||||
return Math.max(expression.evaluate(), baseCost);
|
||||
cost.changeMonetaryCost(Math.max(cost.monetaryCost(), expression.evaluate()));
|
||||
return cost;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -158,10 +162,13 @@ public class EconomyManager {
|
||||
* @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, player.getInventory().getItemInMainHand(), player);
|
||||
if (cost > 0) {
|
||||
economy.withdrawPlayer(player, cost);
|
||||
public static boolean withdrawScrapper(@NotNull Player player, @NotNull SalvageMethod salvageMethod) {
|
||||
ActionCost cost = getSalvageCost(salvageMethod, player.getInventory().getItemInMainHand(), player);
|
||||
if (cost.canPay(player)) {
|
||||
cost.takePayment(player);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -243,16 +250,15 @@ public class EconomyManager {
|
||||
* @param item <p>The enchanted book to calculate cost for</p>
|
||||
* @return <p>The cost of scrapping the enchanted book</p>
|
||||
*/
|
||||
private static double getEnchantedBookSalvageCost(@NotNull ItemStack item) {
|
||||
// TODO: Properly implement this
|
||||
/*GlobalScrapperSettings settings = BlacksmithPlugin.getInstance().getGlobalScrapperSettings();
|
||||
|
||||
double cost = settings.getEnchantedBookSalvageCost();
|
||||
if (settings.multiplyEnchantedBookSalvageCost()) {
|
||||
cost *= SalvageHelper.getEnchantmentCount(item) - 1;
|
||||
@NotNull
|
||||
private static ActionCost getEnchantedBookSalvageCost(@NotNull ItemStack item) {
|
||||
GlobalScrapperSettings settings = BlacksmithPlugin.getInstance().getGlobalScrapperSettings();
|
||||
|
||||
ActionCost cost = settings.getEnchantedBookSalvageCost();
|
||||
if (settings.multiplyEnchantedBokSalvagePrice()) {
|
||||
cost.changeMonetaryCost(cost.monetaryCost() * Math.max(SalvageHelper.getEnchantmentCount(item) - 1, 1));
|
||||
}
|
||||
return SalvageHelper.getEnchantmentCount(item) * cost;*/
|
||||
return 1000000;
|
||||
return cost;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package net.knarcraft.blacksmith.property;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* An action for modifying cost
|
||||
*/
|
||||
public enum CostModifyAction {
|
||||
|
||||
/**
|
||||
* Adds some cost to the existing cost, effectively modifying the total cost
|
||||
*/
|
||||
ADD,
|
||||
|
||||
/**
|
||||
* Clears the cost completely
|
||||
*/
|
||||
CLEAR,
|
||||
|
||||
/**
|
||||
* Sets the cost to the new cost, clearing any other existing costs
|
||||
*/
|
||||
SET,
|
||||
;
|
||||
|
||||
/**
|
||||
* Gets the action specified by the given string
|
||||
*
|
||||
* @param input <p>The input string to parse</p>
|
||||
* @return <p>The action, or null if not matched</p>
|
||||
*/
|
||||
@Nullable
|
||||
public static CostModifyAction fromString(@NotNull String input) {
|
||||
for (CostModifyAction action : CostModifyAction.values()) {
|
||||
if (input.toUpperCase().equals(action.name())) {
|
||||
return action;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -187,7 +187,10 @@ public abstract class CustomTrait<K extends Setting> extends Trait {
|
||||
if (isBlacksmith) {
|
||||
EconomyManager.withdrawBlacksmith(player);
|
||||
} else if (this.session instanceof SalvageSession salvageSession) {
|
||||
EconomyManager.withdrawScrapper(player, salvageSession.salvageMethod);
|
||||
if (!EconomyManager.withdrawScrapper(player, salvageSession.salvageMethod)) {
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
PlayerInventory playerInventory = player.getInventory();
|
||||
ItemStack heldItem = player.getInventory().getItemInMainHand();
|
||||
|
||||
@@ -73,8 +73,6 @@ public class SalvageSession extends Session implements Runnable {
|
||||
return true;
|
||||
}
|
||||
|
||||
// TODO: Check item cost. If
|
||||
|
||||
if (EconomyManager.cannotPayForSalvage(this.player, this.salvageMethod, this.itemToSalvage)) {
|
||||
NPCFormatter.sendNPCMessage(this.npc, this.player, this.config.getInsufficientFundsMessage());
|
||||
return true;
|
||||
|
||||
@@ -4,7 +4,6 @@ import net.citizensnpcs.api.util.DataKey;
|
||||
import net.knarcraft.blacksmith.BlacksmithPlugin;
|
||||
import net.knarcraft.blacksmith.config.SmithPreset;
|
||||
import net.knarcraft.blacksmith.config.SmithPresetFilter;
|
||||
import net.knarcraft.blacksmith.config.scrapper.GlobalScrapperSettings;
|
||||
import net.knarcraft.blacksmith.config.scrapper.ScrapperNPCSettings;
|
||||
import net.knarcraft.blacksmith.config.scrapper.ScrapperSetting;
|
||||
import net.knarcraft.blacksmith.container.RecipeResult;
|
||||
@@ -312,31 +311,12 @@ public class ScrapperTrait extends CustomTrait<ScrapperSetting> {
|
||||
} else if (salvageMethod == SalvageMethod.NETHERITE) {
|
||||
NPCFormatter.sendNPCMessage(this.npc, player, replacer.replace(getSettings().getNetheriteCostMessage()));
|
||||
} else if (salvageMethod == SalvageMethod.ENCHANTED_BOOK) {
|
||||
StringReplacer replacer2 = new StringReplacer();
|
||||
replacer2.add("{cost}", getEnchantedBookCost());
|
||||
NPCFormatter.sendNPCMessage(this.npc, player, replacer2.replace(getSettings().getEnchantedBookCostMessage()));
|
||||
NPCFormatter.sendNPCMessage(this.npc, player, replacer.replace(getSettings().getEnchantedBookCostMessage()));
|
||||
} else {
|
||||
BlacksmithPlugin.error("Unrecognized salvage method " + salvageMethod);
|
||||
}
|
||||
}
|
||||
|
||||
private String getEnchantedBookCost() {
|
||||
// TODO: If both an item and money is required, print both requirements
|
||||
// TODO: If only money is required, print the money requirement
|
||||
// TODO: If an item requirement is set, print the item requirement
|
||||
|
||||
GlobalScrapperSettings scrapperSettings = BlacksmithPlugin.getInstance().getGlobalScrapperSettings();
|
||||
/*String moneyCost = EconomyManager.format(scrapperSettings.getEnchantedBookSalvageCost());
|
||||
//ItemStack itemCost = scrapperSettings.
|
||||
if (scrapperSettings.requireMoneyAndItemForEnchantedBookSalvage()) {
|
||||
// TODO: Print both with a + between them (if item has a special name, use that instead of the material name)
|
||||
|
||||
} else {
|
||||
// TODO: If the item is not null, print it, otherwise print the monetary cost
|
||||
}*/
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean showExactTime() {
|
||||
return BlacksmithPlugin.getInstance().getGlobalScrapperSettings().showExactTime();
|
||||
|
||||
@@ -26,19 +26,20 @@ public final class ConfigCommandHelper {
|
||||
/**
|
||||
* Changes the value of the setting defined in the user's input
|
||||
*
|
||||
* @param args <p>The arguments given by the user</p>
|
||||
* @param arguments <p>The arguments given by the user</p>
|
||||
* @param detectedSetting <p>The setting recognized from the input, if any</p>
|
||||
* @param settings <p>The global settings object to get settings from</p>
|
||||
* @param sender <p>The command sender to display any output to</p>
|
||||
*/
|
||||
public static <K extends Setting> void changeValue(@NotNull String[] args,
|
||||
public static <K extends Setting> void changeValue(@NotNull String[] arguments,
|
||||
@NotNull K detectedSetting,
|
||||
@NotNull Settings<K> settings,
|
||||
@NotNull CommandSender sender) {
|
||||
String newValue = args[1];
|
||||
// TODO: Account for action cost
|
||||
String newValue = arguments[1];
|
||||
//This makes sure all arguments are treated as a sentence
|
||||
if (detectedSetting.getValueType() == SettingValueType.STRING) {
|
||||
newValue = String.join(" ", Arrays.asList(args).subList(1, args.length));
|
||||
newValue = String.join(" ", Arrays.asList(arguments).subList(1, arguments.length));
|
||||
}
|
||||
settings.changeValue(detectedSetting, newValue);
|
||||
getValueChangedMessage(detectedSetting.getCommandName(), newValue).success(sender);
|
||||
|
||||
@@ -2,33 +2,26 @@ package net.knarcraft.blacksmith.util;
|
||||
|
||||
import net.knarcraft.blacksmith.container.ActionCost;
|
||||
import net.knarcraft.blacksmith.formatting.Translatable;
|
||||
import net.knarcraft.blacksmith.property.CostModifyAction;
|
||||
import net.knarcraft.blacksmith.property.CostType;
|
||||
import net.knarcraft.knarlib.formatting.FormatBuilder;
|
||||
import org.bukkit.Bukkit;
|
||||
import net.knarcraft.knarlib.util.TabCompletionHelper;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.permissions.Permission;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.StringJoiner;
|
||||
|
||||
/**
|
||||
* A helper class for setting action costs
|
||||
*/
|
||||
public final class CostHelper {
|
||||
|
||||
private static List<String> plugins;
|
||||
private static Map<String, List<String>> permissions;
|
||||
|
||||
private CostHelper() {
|
||||
|
||||
}
|
||||
@@ -38,10 +31,10 @@ public final class CostHelper {
|
||||
*
|
||||
* @param commandSender <p>The command sender to notify of any problems</p>
|
||||
* @param costString <p>The string to parse into a cost</p>
|
||||
* @return <p>The parsed cost</p>
|
||||
* @throws IllegalArgumentException <p>If the specified cost is invalid</p>
|
||||
* @return <p>The parsed cost, or null if not a valid number</p>
|
||||
*/
|
||||
public static double parseSimpleCost(@NotNull CommandSender commandSender,
|
||||
@Nullable
|
||||
public static Double parseSimpleCost(@NotNull CommandSender commandSender,
|
||||
@NotNull String costString) throws IllegalArgumentException {
|
||||
try {
|
||||
double cost = Double.parseDouble(costString);
|
||||
@@ -51,7 +44,7 @@ public final class CostHelper {
|
||||
return cost;
|
||||
} catch (NumberFormatException exception) {
|
||||
new FormatBuilder(Translatable.DOUBLE_COST_REQUIRED).error(commandSender);
|
||||
throw new IllegalArgumentException("Invalid cost given");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,23 +55,19 @@ public final class CostHelper {
|
||||
* @return <p>The available tab-complete options</p>
|
||||
*/
|
||||
public static @Nullable List<String> tabCompleteCost(@NotNull String[] arguments) {
|
||||
if (plugins == null) {
|
||||
loadAvailablePermissions();
|
||||
}
|
||||
|
||||
if (arguments.length == 1) {
|
||||
return List.of("simple", "advanced");
|
||||
} else if (arguments.length == 2) {
|
||||
if (arguments[0].equalsIgnoreCase("simple")) {
|
||||
return List.of("");
|
||||
return List.of();
|
||||
} else {
|
||||
return List.of("add", "set", "clear");
|
||||
return TabCompletionHelper.filterMatchingStartsWith(TabCompletionHelper.getEnumList(CostModifyAction.class), arguments[1]);
|
||||
}
|
||||
} else if (arguments.length == 3) {
|
||||
if (arguments[0].equalsIgnoreCase("simple")) {
|
||||
return List.of("");
|
||||
return List.of();
|
||||
} else {
|
||||
return List.of("economy", "item", "exp", "permission");
|
||||
return TabCompletionHelper.filterMatchingStartsWith(TabCompletionHelper.getEnumList(CostType.class), arguments[2]);
|
||||
}
|
||||
} else if (arguments.length == 4) {
|
||||
CostType costType = CostType.parse(arguments[2]);
|
||||
@@ -86,7 +75,7 @@ public final class CostHelper {
|
||||
return null;
|
||||
}
|
||||
return switch (costType) {
|
||||
case PERMISSION -> tabCompletePermission(arguments[3]);
|
||||
case PERMISSION -> TabCompletionHelper.tabCompletePermission(arguments[3]);
|
||||
case ECONOMY -> List.of("0.5", "1", "10");
|
||||
case EXP -> List.of("1, 5, 10");
|
||||
case ITEM -> List.of();
|
||||
@@ -98,86 +87,123 @@ public final class CostHelper {
|
||||
/**
|
||||
* Modifies the cost of an action
|
||||
*
|
||||
* @param arguments <p>The arguments given by a player</p>
|
||||
* @param oldCost <p>The previous cost of the action</p>
|
||||
* @param player <p>The player attempting to alter the action cost</p>
|
||||
* @param costAction <p>The action to perform on the cost</p>
|
||||
* @param costTypeName <p>The name of the cost to modify, or null if clearing costs</p>
|
||||
* @param costValue <p>The value of the new cost, or null if clearing costs</p>
|
||||
* @param oldCost <p>The previous cost of the action</p>
|
||||
* @param player <p>The player attempting to alter the action cost</p>
|
||||
* @return <p>The modified action cost, or null if something went wrong</p>
|
||||
*/
|
||||
@Nullable
|
||||
public static ActionCost modifyActionCost(@NotNull String[] arguments, @NotNull ActionCost oldCost,
|
||||
public static ActionCost modifyActionCost(@NotNull String costAction, @Nullable String costTypeName,
|
||||
@Nullable String costValue, @NotNull ActionCost oldCost,
|
||||
@NotNull Player player) {
|
||||
CostType costType;
|
||||
if (arguments.length > 2) {
|
||||
costType = CostType.parse(arguments[2]);
|
||||
if (costTypeName != null) {
|
||||
costType = CostType.parse(costTypeName);
|
||||
} else {
|
||||
costType = null;
|
||||
}
|
||||
|
||||
switch (arguments[1].toLowerCase()) {
|
||||
case "add":
|
||||
if (costType == null) {
|
||||
player.sendMessage("Invalid cost type specified!");
|
||||
return null;
|
||||
}
|
||||
return parseCost(costType, Arrays.copyOfRange(arguments, 2, arguments.length), oldCost, player);
|
||||
case "clear":
|
||||
// Clears one or all fields of a cost
|
||||
if (costType == null) {
|
||||
return new ActionCost(0, 0, null, Set.of());
|
||||
} else {
|
||||
return switch (costType) {
|
||||
case EXP -> oldCost.changeExpCost(0);
|
||||
case ITEM -> oldCost.changeItemCost(null);
|
||||
case ECONOMY -> oldCost.changeMonetaryCost(0);
|
||||
case PERMISSION -> oldCost.changePermissionCost(Set.of());
|
||||
};
|
||||
}
|
||||
case "set":
|
||||
if (costType == null) {
|
||||
player.sendMessage("Invalid cost type specified!");
|
||||
return null;
|
||||
}
|
||||
return parseCost(costType, Arrays.copyOfRange(arguments, 2, arguments.length),
|
||||
new ActionCost(0, 0, null, Set.of()), player);
|
||||
default:
|
||||
return null;
|
||||
CostModifyAction action = CostModifyAction.fromString(costAction);
|
||||
if (action == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (action != CostModifyAction.CLEAR && (costType == null || costValue == null)) {
|
||||
player.sendMessage("Invalid cost type or value specified!");
|
||||
return null;
|
||||
}
|
||||
|
||||
return switch (action) {
|
||||
case CLEAR -> clearActionCost(costType, oldCost);
|
||||
case ADD -> addActionCost(costType, costValue, oldCost, player);
|
||||
case SET -> setActionCost(costType, costValue, player);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the cost to the specified value
|
||||
*
|
||||
* @param costType <p>The type of cost to set</p>
|
||||
* @param costValue <p>The value of the new cost</p>
|
||||
* @param player <p>The player to alert if the cost cannot be properly set</p>
|
||||
* @return <p>The new action cost</p>
|
||||
*/
|
||||
public static ActionCost setActionCost(@NotNull CostType costType, @NotNull String costValue,
|
||||
@NotNull Player player) {
|
||||
return parseCost(costType, costValue, new ActionCost(), player);
|
||||
}
|
||||
|
||||
/**
|
||||
* Modifies a value for the specified action cost
|
||||
*
|
||||
* @param costType <p>The type of cost to modify</p>
|
||||
* @param costValue <p>The new value of the cost</p>
|
||||
* @param oldCost <p>The cost to modify</p>
|
||||
* @param player <p>The player to alert if the cost cannot be properly set</p>
|
||||
* @return <p>The new action cost</p>
|
||||
*/
|
||||
public static ActionCost addActionCost(@NotNull CostType costType, @NotNull String costValue,
|
||||
@NotNull ActionCost oldCost, @NotNull Player player) {
|
||||
return parseCost(costType, costValue, oldCost, player);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears an action cost
|
||||
*
|
||||
* @param costType <p>The type of action cost to clear, or null if clearing all action costs</p>
|
||||
* @param oldCost <p>The old cost to modify, or null if clearing all action costs</p>
|
||||
* @return <p>The modified cost</p>
|
||||
*/
|
||||
public static ActionCost clearActionCost(@Nullable CostType costType, @Nullable ActionCost oldCost) {
|
||||
// Clears one or all fields of a cost
|
||||
if (costType == null || oldCost == null) {
|
||||
return new ActionCost();
|
||||
} else {
|
||||
return switch (costType) {
|
||||
case EXP -> oldCost.changeExpCost(0);
|
||||
case ITEM -> oldCost.changeItemCost(null);
|
||||
case ECONOMY -> oldCost.changeMonetaryCost(0);
|
||||
case PERMISSION -> oldCost.changePermissionCost(Set.of());
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses an advanced action cost
|
||||
*
|
||||
* @param costType <p>The type of cost to parse</p>
|
||||
* @param arguments <p>The arguments given by the player</p>
|
||||
* @param oldCost <p>The old cost to modify</p>
|
||||
* @param player <p>The player changing the value</p>
|
||||
* @param costType <p>The type of cost to parse</p>
|
||||
* @param value <p>The value to parse</p>
|
||||
* @param oldCost <p>The old cost to modify</p>
|
||||
* @param player <p>The player changing the value</p>
|
||||
* @return <p>The new action cost, or null if the process failed.</p>
|
||||
*/
|
||||
@Nullable
|
||||
private static ActionCost parseCost(@NotNull CostType costType, @NotNull String[] arguments, @NotNull ActionCost oldCost,
|
||||
private static ActionCost parseCost(@NotNull CostType costType, @NotNull String value, @NotNull ActionCost oldCost,
|
||||
@NotNull Player player) {
|
||||
switch (costType) {
|
||||
case ECONOMY:
|
||||
double economyCost = Double.parseDouble(arguments[0]);
|
||||
double economyCost = Double.parseDouble(value);
|
||||
if (economyCost < 0) {
|
||||
player.sendMessage("Cost cannot be negative!");
|
||||
new FormatBuilder("Cost cannot be negative!").error(player);
|
||||
return null;
|
||||
}
|
||||
return oldCost.changeMonetaryCost(economyCost);
|
||||
case ITEM:
|
||||
ItemStack itemCost = player.getInventory().getItemInMainHand();
|
||||
if (itemCost.getType().isAir()) {
|
||||
player.sendMessage("You have no item in your main hand");
|
||||
new FormatBuilder("You have no item in your main hand").error(player);
|
||||
return null;
|
||||
}
|
||||
return oldCost.changeItemCost(itemCost);
|
||||
case PERMISSION:
|
||||
Set<String> permissionSet = new HashSet<>(Arrays.asList(arguments[0].split(",")));
|
||||
Set<String> permissionSet = new HashSet<>(Arrays.asList(value.split(",")));
|
||||
return oldCost.changePermissionCost(permissionSet);
|
||||
case EXP:
|
||||
int expCost = Integer.parseInt(arguments[0]);
|
||||
int expCost = Integer.parseInt(value);
|
||||
if (expCost < 0) {
|
||||
player.sendMessage("Exp cost cannot be negative!");
|
||||
new FormatBuilder("Exp cost cannot be negative!").error(player);
|
||||
return null;
|
||||
}
|
||||
return oldCost.changeExpCost(expCost);
|
||||
@@ -186,103 +212,4 @@ public final class CostHelper {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the tab complete value for the permission typed
|
||||
*
|
||||
* @param typedNode <p>The full permission node typed by the player</p>
|
||||
* @return <p>All known valid auto-complete options</p>
|
||||
*/
|
||||
private static @NotNull List<String> tabCompletePermission(@NotNull String typedNode) {
|
||||
StringBuilder permissionListString = new StringBuilder();
|
||||
if (typedNode.contains(",")) {
|
||||
String[] permissionList = typedNode.split(",");
|
||||
for (int i = 0; i < permissionList.length - 1; i++) {
|
||||
permissionListString.append(permissionList[i]);
|
||||
permissionListString.append(",");
|
||||
}
|
||||
typedNode = permissionList[permissionList.length - 1];
|
||||
}
|
||||
String permissionPrefix = permissionListString.toString();
|
||||
List<String> output;
|
||||
if (typedNode.contains(".")) {
|
||||
List<String> matchingPermissions = permissions.get(typedNode.substring(0, typedNode.lastIndexOf(".")));
|
||||
if (matchingPermissions == null) {
|
||||
if (permissionPrefix.isEmpty()) {
|
||||
output = new ArrayList<>();
|
||||
} else {
|
||||
List<String> onlyPrefix = new ArrayList<>();
|
||||
onlyPrefix.add(permissionPrefix);
|
||||
output = onlyPrefix;
|
||||
}
|
||||
} else {
|
||||
//Filter by the typed text
|
||||
output = filterMatching(matchingPermissions, typedNode);
|
||||
}
|
||||
} else {
|
||||
output = plugins;
|
||||
}
|
||||
|
||||
//Add previous permissions in the comma-separated lists as a prefix
|
||||
if (permissionPrefix.isEmpty()) {
|
||||
return output;
|
||||
} else {
|
||||
List<String> prefixed = new ArrayList<>(output.size());
|
||||
for (String matchingCompletion : output) {
|
||||
prefixed.add(permissionPrefix + matchingCompletion);
|
||||
}
|
||||
return prefixed;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Find completable strings which match the text typed by the command's sender
|
||||
*
|
||||
* @param values <p>The values to filter</p>
|
||||
* @param typedText <p>The text the player has started typing</p>
|
||||
* @return <p>The given string values which start with the player's typed text</p>
|
||||
*/
|
||||
private static @NotNull List<String> filterMatching(@NotNull List<String> values, @NotNull String typedText) {
|
||||
List<String> configValues = new ArrayList<>();
|
||||
for (String value : values) {
|
||||
if (value.toLowerCase().startsWith(typedText.toLowerCase())) {
|
||||
configValues.add(value);
|
||||
}
|
||||
}
|
||||
return configValues;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads all permissions available from bukkit plugins
|
||||
*/
|
||||
private static void loadAvailablePermissions() {
|
||||
plugins = new ArrayList<>();
|
||||
permissions = new HashMap<>();
|
||||
|
||||
for (Permission permission : Bukkit.getPluginManager().getPermissions()) {
|
||||
loadPermission(permission.getName());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a given permission into the proper lists and maps
|
||||
*
|
||||
* @param permissionName <p>The permission to load</p>
|
||||
*/
|
||||
private static void loadPermission(@NotNull String permissionName) {
|
||||
String[] permissionParts = permissionName.split("\\.");
|
||||
if (permissionParts.length == 1 && !plugins.contains(permissionParts[0])) {
|
||||
plugins.add(permissionParts[0]);
|
||||
} else if (permissionParts.length > 1) {
|
||||
StringJoiner pathJoiner = new StringJoiner(".");
|
||||
for (int j = 0; j < permissionParts.length - 1; j++) {
|
||||
pathJoiner.add(permissionParts[j]);
|
||||
}
|
||||
String path = pathJoiner.toString();
|
||||
List<String> permissionList = permissions.computeIfAbsent(path, k -> new ArrayList<>());
|
||||
permissionList.add(permissionName);
|
||||
|
||||
loadPermission(path);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -142,6 +142,9 @@ scrapper:
|
||||
# The cost of using the scrapper to remove netherite from an item
|
||||
netheriteSalvagePrice: 15
|
||||
|
||||
# The cost of splitting an enchanted book into several books
|
||||
enchantedBookSalvagePrice: 15
|
||||
|
||||
# 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.
|
||||
salvageCostIncrease: "{cost}*{timesUsed}"
|
||||
@@ -149,6 +152,9 @@ scrapper:
|
||||
# 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.
|
||||
salvageCooldownIncrease: "{cooldown}*{timesUsed}"
|
||||
|
||||
# Whether to multiply the enchanted book salvage price with the number of enchantments on the book
|
||||
multiplyEnchantedBookSalvageCost: false
|
||||
|
||||
# The settings which are set to any new scrapper NPC. To change any of these settings for an existing NPC, you must
|
||||
# change the Citizens NPC file, or use the /scrapper command
|
||||
|
||||
Reference in New Issue
Block a user