Major changes

Removes HyperConomy support
Removes Paper dependency
Builds against Spigot 1.19.1
Adds a lot of comments
Improves some code
This commit is contained in:
2022-08-05 17:07:16 +02:00
parent 3870ead92f
commit e2b167e020
4 changed files with 125 additions and 141 deletions

View File

@ -5,28 +5,17 @@ import net.citizensnpcs.api.util.DataKey;
import net.knarcraft.blacksmith.config.Setting;
import net.knarcraft.blacksmith.config.Settings;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import regalowl.hyperconomy.HyperAPI;
import regalowl.hyperconomy.HyperConomy;
import regalowl.hyperconomy.bukkit.BukkitConnector;
import regalowl.hyperconomy.inventory.HItemStack;
import regalowl.hyperconomy.tradeobject.TradeObject;
import java.util.ArrayList;
import java.util.Objects;
import java.util.logging.Level;
import static net.knarcraft.blacksmith.util.Sanitizer.sanitizeItemName;
@ -39,9 +28,6 @@ public class BlacksmithPlugin extends JavaPlugin {
private static BlacksmithPlugin instance;
private Settings config;
private Economy economy;
private HyperAPI hyperAPI;
private BukkitConnector bukkitConnector;
private boolean useHyperAPI = false;
/**
* Gets an instance of the Blacksmith plugin
@ -75,8 +61,6 @@ public class BlacksmithPlugin extends JavaPlugin {
//Load settings
config = new Settings(this);
config.load();
//Load HyperConomy if available
setupHyperConomy();
getLogger().log(Level.INFO, "Setting Up Vault now....");
boolean canLoad = setupVault();
@ -92,25 +76,6 @@ public class BlacksmithPlugin extends JavaPlugin {
getLogger().log(Level.INFO, " v" + getDescription().getVersion() + " enabled.");
}
/**
* Sets up HyperConomy
*
* <p>Setup HyperConomy (Soft-Depend only, so this is completely optional!). HyperConomy uses your favorite
* Vault-compatible economy system and calculates prices for items based on supply and demand on the fly.
* This is only used to get the cost of a repair.</p>
*/
private void setupHyperConomy() {
if (Bukkit.getPluginManager().getPlugin("HyperConomy") != null) {
getServer().getLogger().log(Level.INFO, "Found HyperConomy! Using that for calculating prices, " +
"base-prices and price-per-durability-point in the Blacksmith config.yml will NOT be used!");
this.useHyperAPI = true;
Plugin hyperConomyPlugin = getServer().getPluginManager().getPlugin("HyperConomy");
bukkitConnector = (BukkitConnector) hyperConomyPlugin;
HyperConomy hyperConomy = Objects.requireNonNull(bukkitConnector).getHC();
this.hyperAPI = (HyperAPI) hyperConomy.getAPI();
}
}
/**
* Sets up Vault for economy
*
@ -174,28 +139,52 @@ public class BlacksmithPlugin extends JavaPlugin {
};
}
public boolean doesPlayerHaveEnough(Player player) {
return economy.getBalance(player) - getCost(player.getInventory().getItemInMainHand(), player) >= 0;
}
public String formatCost(Player player) {
double cost = getCost(player.getInventory().getItemInMainHand(), player);
return economy.format(cost);
}
public void withdraw(Player player) {
economy.withdrawPlayer(player, getCost(player.getInventory().getItemInMainHand(), player));
/**
* Gets whether the given player can pay for re-forging their held item
*
* @param player <p>The player holding an item</p>
* @return <p>Whether the player can pay for the re-forge</p>
*/
public boolean canPay(Player player) {
return economy.getBalance(player) - getHeldItemCost(player) >= 0;
}
/**
* Gets the durability of the given item
* 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>
*/
public String formatCost(Player player) {
double cost = getHeldItemCost(player);
return economy.format(cost);
}
/**
* Withdraws the re-forge 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 void withdraw(Player player) {
economy.withdrawPlayer(player, getHeldItemCost(player));
}
/**
* Gets the current durability of the given item
*
* @param itemStack <p>The item to get the durability of</p>
* @return <p>The durability of the item</p>
*/
public static short getDurability(ItemStack itemStack) {
Damageable damageable = (Damageable) itemStack.getItemMeta();
return (short) (itemStack.getType().getMaxDurability() - damageable.getDamage());
int maxDurability = itemStack.getType().getMaxDurability();
if (damageable != null) {
return (short) (maxDurability - damageable.getDamage());
} else {
return (short) maxDurability;
}
}
/**
@ -206,10 +195,20 @@ public class BlacksmithPlugin extends JavaPlugin {
*/
public static short getDamage(ItemStack itemStack) {
Damageable damageable = (Damageable) itemStack.getItemMeta();
return (short) damageable.getDamage();
if (damageable != null) {
return (short) damageable.getDamage();
} else {
return 0;
}
}
private double getCost(ItemStack item, Player player) {
/**
* 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 double getCost(ItemStack item) {
DataKey root = config.getConfig().getKey("");
String itemName = sanitizeItemName(item.getType().name());
double price;
@ -220,22 +219,18 @@ public class BlacksmithPlugin extends JavaPlugin {
}
// Adjust price based on durability and enchantments
if (this.useHyperAPI) {
return getHyperAPICost(player, item, root, price);
double pricePerDurabilityPoint;
if (root.keyExists("price-per-durability-point." + itemName)) {
pricePerDurabilityPoint = root.getDouble("price-per-durability-point." + itemName);
} else {
double pricePerDurabilityPoint;
if (root.keyExists("price-per-durability-point." + itemName)) {
pricePerDurabilityPoint = root.getDouble("price-per-durability-point." + itemName);
} else {
pricePerDurabilityPoint = Setting.PRICE_PER_DURABILITY_POINT.asDouble();
}
if (config.getNaturalCost()) {
//Cost increases with damage
price += ((double) getDamage(item)) * pricePerDurabilityPoint;
} else {
//Cost decreases with damage
price += ((double) getDurability(item)) * pricePerDurabilityPoint;
}
pricePerDurabilityPoint = Setting.PRICE_PER_DURABILITY_POINT.asDouble();
}
if (config.getNaturalCost()) {
//Cost increases with damage
price += ((double) getDamage(item)) * pricePerDurabilityPoint;
} else {
//Cost decreases with damage
price += ((double) getDurability(item)) * pricePerDurabilityPoint;
}
//Add the enchantment modifier for each enchantment on the item
@ -243,54 +238,6 @@ public class BlacksmithPlugin extends JavaPlugin {
return price;
}
private double getHyperAPICost(Player player, ItemStack item, DataKey root, double price) {
// If using hyperConomy, price is calculated like so:
// New Item Price + Enchantments Price (from hyperConomy) / maxDurability = price per durability point
// Total price would then be base_price + price per durability point * current durability
double hyperPrice = 0;
HItemStack hi = hyperAPI.getHyperPlayer(player.getName()).getItemInHand();
ItemStack item2 = player.getInventory().getItemInMainHand().clone();
for (TradeObject enchant : hyperAPI.getEnchantmentHyperObjects(hi, player.getName())) {
hyperPrice = hyperPrice + enchant.getBuyPrice(1);
Enchantment enchantment = Enchantment.getByKey(
NamespacedKey.minecraft(enchant.getEnchantment().getEnchantmentName()));
item2.removeEnchantment(Objects.requireNonNull(enchantment));
}
ArrayList<Material> leathers = new ArrayList<>();
leathers.add(Material.LEATHER_BOOTS);
leathers.add(Material.LEATHER_CHESTPLATE);
leathers.add(Material.LEATHER_HELMET);
leathers.add(Material.LEATHER_LEGGINGS);
HItemStack hi3 = null;
if (leathers.contains(player.getInventory().getItemInMainHand().getType())) {
hi3 = bukkitConnector.getBukkitCommon().getSerializableItemStack(
new ItemStack(player.getInventory().getItemInMainHand().getType()));
}
TradeObject to = this.hyperAPI.getHyperObject(hi, "default");
if (to == null) {
to = hyperAPI.getHyperObject(hi3, "default");
if (to == null) {
HItemStack hi4 = bukkitConnector.getBukkitCommon().getSerializableItemStack(
new ItemStack(player.getInventory().getItemInMainHand().getType()));
to = this.hyperAPI.getHyperObject(hi4, "default");
}
hyperPrice = hyperPrice + to.getSellPrice(1);
} else {
hyperPrice = to.getSellPrice(1);
}
double hyperPricePerDurability = hyperPrice / item.getType().getMaxDurability();
price += getDurability(item) * hyperPricePerDurability;
price += getEnchantmentCost(item2, root);
return price;
}
/**
* Gets the cost resulting from all enchantments on the given item
*
@ -302,7 +249,7 @@ public class BlacksmithPlugin extends JavaPlugin {
double enchantmentModifier = Setting.ENCHANTMENT_MODIFIER.asDouble();
double price = 0;
for (Enchantment enchantment : item.getEnchantments().keySet()) {
String enchantmentKey = "enchantment-modifiers." + sanitizeItemName(enchantment.getKey().asString());
String enchantmentKey = "enchantment-modifiers." + sanitizeItemName(enchantment.getKey().toString());
if (root.keyExists(enchantmentKey)) {
price += root.getDouble(enchantmentKey) * item.getEnchantmentLevel(enchantment);
} else {
@ -312,4 +259,14 @@ public class BlacksmithPlugin extends JavaPlugin {
return price;
}
/**
* 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 double getHeldItemCost(Player player) {
return getCost(player.getInventory().getItemInMainHand());
}
}