Removes some redundant code

This commit is contained in:
Kristian Knarvik 2022-07-19 02:38:35 +02:00
parent 781999c995
commit 555d405bcc
6 changed files with 83 additions and 30 deletions

View File

@ -1,7 +1,7 @@
package net.apunch.blacksmith;
import net.apunch.blacksmith.util.Setting;
import net.apunch.blacksmith.util.Settings;
import net.apunch.blacksmith.config.Setting;
import net.apunch.blacksmith.config.Settings;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.util.DataKey;
import net.milkbowl.vault.economy.Economy;
@ -29,6 +29,8 @@ import java.util.ArrayList;
import java.util.Objects;
import java.util.logging.Level;
import static net.apunch.blacksmith.util.Sanitizer.sanitizeItemName;
/**
* Blacksmith's main class
*/
@ -209,7 +211,7 @@ public class BlacksmithPlugin extends JavaPlugin {
private double getCost(ItemStack item, Player player) {
DataKey root = config.getConfig().getKey("");
String itemName = item.getType().name().toLowerCase().replace('_', '-');
String itemName = sanitizeItemName(item.getType().name());
double price;
if (root.keyExists("base-prices." + itemName)) {
price = root.getDouble("base-prices." + itemName);
@ -237,16 +239,7 @@ public class BlacksmithPlugin extends JavaPlugin {
}
//Add the enchantment modifier for each enchantment on the item
double enchantmentModifier = Setting.ENCHANTMENT_MODIFIER.asDouble();
for (Enchantment enchantment : item.getEnchantments().keySet()) {
String enchantmentKey = "enchantment-modifiers." +
enchantment.getKey().asString().toLowerCase().replace('_', '-');
if (root.keyExists(enchantmentKey)) {
price += root.getDouble(enchantmentKey) * item.getEnchantmentLevel(enchantment);
} else {
price += enchantmentModifier * item.getEnchantmentLevel(enchantment);
}
}
price += getEnchantmentCost(item, root);
return price;
}
@ -293,16 +286,30 @@ public class BlacksmithPlugin extends JavaPlugin {
double hyperPricePerDurability = hyperPrice / item.getType().getMaxDurability();
price += getDurability(item) * hyperPricePerDurability;
double enchantmentModifier = Setting.ENCHANTMENT_MODIFIER.asDouble();
for (Enchantment enchantment : item2.getEnchantments().keySet()) {
if (root.keyExists("enchantment-modifiers." + enchantment.getKey().asString().toLowerCase().replace('_', '-'))) {
enchantmentModifier = root.getDouble("enchantment-modifiers."
+ enchantment.getKey().asString().toLowerCase().replace('_', '-'));
}
price += enchantmentModifier * item2.getEnchantmentLevel(enchantment);
}
price += getEnchantmentCost(item2, 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().asString());
if (root.keyExists(enchantmentKey)) {
price += root.getDouble(enchantmentKey) * item.getEnchantmentLevel(enchantment);
} else {
price += enchantmentModifier * item.getEnchantmentLevel(enchantment);
}
}
return price;
}
}

View File

@ -1,6 +1,7 @@
package net.apunch.blacksmith;
import net.apunch.blacksmith.util.Settings;
import net.apunch.blacksmith.config.Settings;
import net.apunch.blacksmith.util.Sanitizer;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.trait.Trait;
@ -26,6 +27,8 @@ import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import static net.apunch.blacksmith.util.Sanitizer.sanitizedToItemName;
/**
* The class representing the Blacksmith NPC trait
*/
@ -67,8 +70,9 @@ public class BlacksmithTrait extends Trait {
@Override
public void load(DataKey key) {
for (DataKey sub : key.getRelative("reforge-able-items").getIntegerSubKeys()) {
if (Material.getMaterial(sub.getString("").toUpperCase().replace('-', '_')) != null) {
reforgeAbleItems.add(Material.getMaterial(sub.getString("").toUpperCase().replace('-', '_')));
Material material = Material.getMaterial(sanitizedToItemName(sub.getString("")));
if (material != null) {
reforgeAbleItems.add(material);
}
}
config.loadVariables(key);
@ -78,8 +82,8 @@ public class BlacksmithTrait extends Trait {
public void save(DataKey key) {
//Save all items the blacksmith knows how to reforge
for (int i = 0; i < reforgeAbleItems.size(); i++) {
key.getRelative("reforge-able-items").setString(String.valueOf(i),
reforgeAbleItems.get(i).name().toLowerCase().replace('_', '-'));
key.getRelative("reforge-able-items").setString(String.valueOf(i), Sanitizer.sanitizeItemName(
reforgeAbleItems.get(i).name()));
}
//Save all other config values

View File

@ -1,6 +1,6 @@
package net.apunch.blacksmith;
import net.apunch.blacksmith.util.Settings;
import net.apunch.blacksmith.config.Settings;
import net.citizensnpcs.api.npc.NPC;
import org.bukkit.NamespacedKey;
import org.bukkit.enchantments.Enchantment;
@ -14,7 +14,11 @@ import java.util.Calendar;
import java.util.Objects;
import java.util.Random;
class ReforgeSession implements Runnable {
/**
* A representation of the session between a player and a blacksmith
*/
public class ReforgeSession implements Runnable {
private final BlacksmithTrait blacksmithTrait;
private final Player player;
private final NPC npc;
@ -23,6 +27,14 @@ class ReforgeSession implements Runnable {
private final Settings config;
private static final String[] enchantments = new String[Enchantment.values().length];
/**
* Instantiates a new session
*
* @param blacksmithTrait <p>A reference to the blacksmith trait</p>
* @param player <p>The player initiating the session</p>
* @param npc <p>The Blacksmith NPC involved in the session</p>
* @param config <p>The config to use for the session</p>
*/
ReforgeSession(BlacksmithTrait blacksmithTrait, Player player, NPC npc, Settings config) {
this.blacksmithTrait = blacksmithTrait;
this.player = player;
@ -150,4 +162,5 @@ class ReforgeSession implements Runnable {
public Player getPlayer() {
return player;
}
}

View File

@ -1,4 +1,4 @@
package net.apunch.blacksmith.util;
package net.apunch.blacksmith.config;
import java.util.Arrays;

View File

@ -1,4 +1,4 @@
package net.apunch.blacksmith.util;
package net.apunch.blacksmith.config;
import net.apunch.blacksmith.BlacksmithPlugin;
import net.citizensnpcs.api.util.DataKey;

View File

@ -0,0 +1,29 @@
package net.apunch.blacksmith.util;
public final class Sanitizer {
private Sanitizer() {
}
/**
* Sanitizes an item name to the format used by this plugin
*
* @param itemName <p>The item name to sanitize</p>
* @return <p>The sanitized name</p>
*/
public static String sanitizeItemName(String itemName) {
return itemName.toLowerCase().replace('_', '-');
}
/**
* Converts a sanitized item name to the original name
*
* @param itemName <p>The item name to convert</p>
* @return <p>The un-sanitized name</p>
*/
public static String sanitizedToItemName(String itemName) {
return itemName.toUpperCase().replace('-', '_');
}
}