Majorly changes most plugin code

Changes the entire settings structure
Splits settings into NPC settings and global settings
Adds some command classes in preparation for a new command system
Moves a lot of code away from BlacksmithPlugin
Adds a new EconomyManager class which takes care of everything economy and pricing
Removes HyperConomy softdepend
Changes the reload command to /blacksmith reload
Adds a proper configuration file to make possible to change stuff without a bloody wiki
This commit is contained in:
2022-08-07 01:21:47 +02:00
parent e2b167e020
commit c557d969b7
14 changed files with 1076 additions and 723 deletions

View File

@ -1,33 +1,20 @@
package net.knarcraft.blacksmith;
import net.citizensnpcs.api.CitizensAPI;
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.ChatColor;
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.RegisteredServiceProvider;
import net.knarcraft.blacksmith.command.BlackSmithCommand;
import net.knarcraft.blacksmith.config.GlobalSettings;
import org.bukkit.command.PluginCommand;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import java.util.logging.Level;
import static net.knarcraft.blacksmith.util.Sanitizer.sanitizeItemName;
/**
* Blacksmith's main class
*/
public class BlacksmithPlugin extends JavaPlugin {
private static BlacksmithPlugin instance;
private Settings config;
private Economy economy;
private GlobalSettings config;
/**
* Gets an instance of the Blacksmith plugin
@ -43,14 +30,19 @@ public class BlacksmithPlugin extends JavaPlugin {
*
* @return <p>Settings for the blacksmith plugin</p>
*/
public Settings getSettings() {
public GlobalSettings getSettings() {
return config;
}
/**
* Reloads the configuration file from disk
*/
public void reload() {
config.load();
}
@Override
public void onDisable() {
// config.save();
getLogger().log(Level.INFO, " v" + getDescription().getVersion() + " disabled.");
}
@ -59,11 +51,11 @@ public class BlacksmithPlugin extends JavaPlugin {
instance = this;
//Load settings
config = new Settings(this);
config = new GlobalSettings(this);
config.load();
getLogger().log(Level.INFO, "Setting Up Vault now....");
boolean canLoad = setupVault();
boolean canLoad = EconomyManager.setUp(getServer().getServicesManager(), getLogger());
if (!canLoad) {
getLogger().log(Level.SEVERE, "Vault Integration Failed....");
getServer().getPluginManager().disablePlugin(this);
@ -73,200 +65,13 @@ public class BlacksmithPlugin extends JavaPlugin {
CitizensAPI.getTraitFactory().registerTrait(
net.citizensnpcs.api.trait.TraitInfo.create(BlacksmithTrait.class).withName("blacksmith"));
//Register the blacksmith main-command
PluginCommand blacksmithCommand = this.getCommand("blacksmith");
if (blacksmithCommand != null) {
blacksmithCommand.setExecutor(new BlackSmithCommand());
}
getLogger().log(Level.INFO, " v" + getDescription().getVersion() + " enabled.");
}
/**
* Sets up Vault for economy
*
* @return <p>True if Vault was successfully set up</p>
*/
private boolean setupVault() {
// Setup Vault
RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(
Economy.class);
if (economyProvider != null) {
economy = economyProvider.getProvider();
return true;
} else {
// Disable if no economy plugin was found
getServer().getLogger().log(Level.SEVERE, "Failed to load an economy plugin. Disabling...");
return false;
}
}
@Override
public boolean onCommand(final CommandSender sender, final @NotNull Command command, final @NotNull String label,
final String[] args) {
//Handle the reload command
config.load();
sender.sendMessage(ChatColor.GREEN + "Blacksmith config reloaded!");
return true;
}
/**
* Gets whether the given item is a type of tool
*
* @param item <p>The item to check if is tool or not</p>
* @return <p>True if the given item is a type of tool</p>
*/
public boolean isTool(ItemStack item) {
return switch (item.getType()) {
case WOODEN_PICKAXE, WOODEN_SHOVEL, WOODEN_HOE, WOODEN_SWORD, WOODEN_AXE, STONE_PICKAXE, STONE_SHOVEL,
STONE_HOE, STONE_SWORD, STONE_AXE, GOLDEN_PICKAXE, GOLDEN_SHOVEL, GOLDEN_HOE, GOLDEN_SWORD,
GOLDEN_AXE, IRON_PICKAXE, IRON_SHOVEL, IRON_HOE, IRON_SWORD, IRON_AXE, DIAMOND_PICKAXE,
DIAMOND_SHOVEL, DIAMOND_HOE, DIAMOND_SWORD, DIAMOND_AXE, NETHERITE_SWORD, NETHERITE_SHOVEL,
NETHERITE_PICKAXE, NETHERITE_AXE, NETHERITE_HOE, BOW, CROSSBOW, FLINT_AND_STEEL, FISHING_ROD,
SHEARS, TRIDENT, SHIELD -> true;
default -> false;
};
}
/**
* Gets whether the given item is a type of armor
*
* @param item <p>The item to check if is armor or not</p>
* @return <p>True if the given item is a type of armor</p>
*/
public static boolean isArmor(ItemStack item) {
return switch (item.getType()) {
case LEATHER_HELMET, LEATHER_CHESTPLATE, LEATHER_LEGGINGS, LEATHER_BOOTS, CHAINMAIL_HELMET,
CHAINMAIL_CHESTPLATE, CHAINMAIL_LEGGINGS, CHAINMAIL_BOOTS, GOLDEN_HELMET, GOLDEN_CHESTPLATE,
GOLDEN_LEGGINGS, GOLDEN_BOOTS, IRON_HELMET, IRON_CHESTPLATE, IRON_LEGGINGS, IRON_BOOTS,
DIAMOND_HELMET, DIAMOND_CHESTPLATE, DIAMOND_LEGGINGS, DIAMOND_BOOTS, TURTLE_HELMET, ELYTRA,
NETHERITE_HELMET, NETHERITE_CHESTPLATE, NETHERITE_LEGGINGS, NETHERITE_BOOTS -> true;
default -> false;
};
}
/**
* 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 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();
int maxDurability = itemStack.getType().getMaxDurability();
if (damageable != null) {
return (short) (maxDurability - damageable.getDamage());
} else {
return (short) maxDurability;
}
}
/**
* Gets the damage done to the given item
*
* @param itemStack <p>The damage done to the item</p>
* @return <p>The damage done to the item</p>
*/
public static short getDamage(ItemStack itemStack) {
Damageable damageable = (Damageable) itemStack.getItemMeta();
if (damageable != null) {
return (short) damageable.getDamage();
} else {
return 0;
}
}
/**
* 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;
if (root.keyExists("base-prices." + itemName)) {
price = root.getDouble("base-prices." + itemName);
} else {
price = Setting.BASE_PRICE.asDouble();
}
// Adjust price based on durability and enchantments
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;
}
//Add the enchantment modifier for each enchantment on the item
price += getEnchantmentCost(item, root);
return price;
}
/**
* Gets the cost resulting from all enchantments on the given item
*
* @param item <p>The item to calculate enchantment cost for</p>
* @param root <p>The data key containing the enchantment-modifiers option</p>
* @return <p>The resulting enchantment cost</p>
*/
private double getEnchantmentCost(ItemStack item, DataKey root) {
double enchantmentModifier = Setting.ENCHANTMENT_MODIFIER.asDouble();
double price = 0;
for (Enchantment enchantment : item.getEnchantments().keySet()) {
String enchantmentKey = "enchantment-modifiers." + sanitizeItemName(enchantment.getKey().toString());
if (root.keyExists(enchantmentKey)) {
price += root.getDouble(enchantmentKey) * item.getEnchantmentLevel(enchantment);
} else {
price += enchantmentModifier * item.getEnchantmentLevel(enchantment);
}
}
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());
}
}