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:
Kristian Knarvik 2022-08-05 17:07:16 +02:00
parent 3870ead92f
commit e2b167e020
4 changed files with 125 additions and 141 deletions

24
pom.xml
View File

@ -6,7 +6,7 @@
<groupId>net.knarcraft</groupId>
<artifactId>blacksmith</artifactId>
<version>1.18.1-SNAPSHOT</version>
<version>1.19.1-SNAPSHOT</version>
<name>Blacksmith</name>
<description>Blacksmith Character for the CitizensAPI</description>
@ -15,16 +15,11 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<citizensapi.version>2.0.26-SNAPSHOT</citizensapi.version>
<vault.version>1.7.3</vault.version>
<hyperconomy.version>0.975.7-SNAPSHOT</hyperconomy.version>
<build.number>Unknown</build.number>
</properties>
<!-- Repositories -->
<repositories>
<repository>
<id>papermc-repo</id>
<url>https://papermc.io/repo/repository/maven-public/</url>
</repository>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
@ -48,16 +43,10 @@
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.destroystokyo.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.16.5-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.18.1-R0.1-SNAPSHOT</version>
<version>1.19.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
@ -67,11 +56,10 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>regalowl.hyperconomy</groupId>
<artifactId>hyperconomy</artifactId>
<version>${hyperconomy.version}</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/HyperConomy.jar</systemPath>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>23.0.0</version>
<scope>compile</scope>
</dependency>
</dependencies>

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());
}
}

View File

@ -97,7 +97,7 @@ public class BlacksmithTrait extends Trait {
return;
}
//If the player is not looking at a blacksmith, there is no need to do anything
Entity target = getTarget(event.getPlayer(), event.getPlayer().getLocation().getNearbyEntities(15, 10, 15));
Entity target = getTarget(event.getPlayer(), event.getPlayer().getNearbyEntities(15, 10, 15));
if (!CitizensAPI.getNPCRegistry().isNPC(target) ||
!CitizensAPI.getNPCRegistry().getNPC(target).hasTrait(BlacksmithTrait.class)) {
return;

View File

@ -13,6 +13,7 @@ import org.bukkit.inventory.meta.ItemMeta;
import java.util.Calendar;
import java.util.Objects;
import java.util.Random;
import java.util.logging.Level;
/**
* A representation of the session between a player and a blacksmith
@ -44,7 +45,7 @@ public class ReforgeSession implements Runnable {
int i = 0;
for (Enchantment enchantment : Enchantment.values()) {
enchantments[i++] = enchantment.getKey().asString();
enchantments[i++] = enchantment.getKey().toString();
}
}
@ -118,47 +119,85 @@ public class ReforgeSession implements Runnable {
}
}
/**
* Updates the damage done to an item
*
* @param item <p>The item to update damage for</p>
* @param newDamage <p>The new damage done</p>
*/
private void updateDamage(ItemStack item, int newDamage) {
ItemMeta meta = item.getItemMeta();
Damageable damageable = (Damageable) meta;
damageable.setDamage(newDamage);
if (damageable != null) {
damageable.setDamage(newDamage);
} else {
BlacksmithPlugin.getInstance().getLogger().log(Level.WARNING, "Unable to change damage of " + item);
}
item.setItemMeta(meta);
}
// Return if the session should end
boolean endSession() {
/**
* Gets whether to end the current session
*
* <p>If the player has switched their item, or the player cannot pay, this returns true.</p>
*
* @return <p>True if the current session should end</p>
*/
public boolean endSession() {
// Prevent player from switching items during session
ItemStack itemInHand = player.getInventory().getItemInMainHand();
if (!itemToReforge.equals(itemInHand)) {
player.sendMessage(config.getItemChangedMessage());
return true;
}
if (!BlacksmithPlugin.getInstance().doesPlayerHaveEnough(player)) {
// The player is unable to pay
if (!BlacksmithPlugin.getInstance().canPay(player)) {
player.sendMessage(config.getInsufficientFundsMessage());
return true;
}
return false;
}
boolean isRunning() {
/**
* Gets whether the current session is still running
*
* @return <p>True if the current session is still running</p>
*/
public boolean isRunning() {
return BlacksmithPlugin.getInstance().getServer().getScheduler().isQueued(taskId);
}
boolean isInSession(Player other) {
/**
* Gets whether the given player is currently in a re-forge session
*
* @param other <p>The player to check if is in session</p>
* @return <p>True if the given player is in a re-forge session</p>
*/
public boolean isInSession(Player other) {
return player.getName().equals(other.getName());
}
void beginReforge() {
/**
* Begins the actual item reforging
*/
public void beginReforge() {
if (!config.getDisableCoolDown()) {
//Finish the re-forge after a random delay between the max and min
taskId = BlacksmithPlugin.getInstance().getServer().getScheduler().scheduleSyncDelayedTask(
BlacksmithPlugin.getInstance(), this, (new Random().nextInt(config.getMaxReforgeDelay()) +
config.getMinReforgeDelay()) * 20L);
} else {
//Finish the re-forge as soon as possible
taskId = BlacksmithPlugin.getInstance().getServer().getScheduler().scheduleSyncDelayedTask(
BlacksmithPlugin.getInstance(), this, 0);
}
}
/**
* Gets the player currently in this reforge session
*
* @return <p>The player currently in this reforge session</p>
*/
public Player getPlayer() {
return player;
}