Updates some terrible code structure and fixes the durability updating

This commit is contained in:
Kristian Knarvik 2022-02-01 22:24:39 +01:00
parent a65a58fb10
commit d82121a3ef
5 changed files with 439 additions and 314 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.util.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.util.DataKey;
import net.milkbowl.vault.economy.Economy;
@ -30,12 +30,22 @@ import java.util.Objects;
import java.util.logging.Level;
public class BlacksmithPlugin extends JavaPlugin {
private static BlacksmithPlugin instance;
private Settings config;
private Economy economy;
private HyperAPI hyperAPI;
private BukkitConnector bukCon;
private boolean useHyperAPI = false;
public static BlacksmithPlugin getInstance() {
return instance;
}
public Settings getSettings() {
return config;
}
@Override
public void onDisable() {
// config.save();
@ -45,6 +55,7 @@ public class BlacksmithPlugin extends JavaPlugin {
@Override
public void onEnable() {
instance = this;
config = new Settings(this);
config.load();
/* Setup Hyperconomy (Soft-Depend only, so this is completely optional!)

View File

@ -1,18 +1,15 @@
package net.apunch.blacksmith;
import net.apunch.blacksmith.util.Settings.Setting;
import net.apunch.blacksmith.util.Settings;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.util.DataKey;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import java.util.ArrayList;
import java.util.Calendar;
@ -20,44 +17,28 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Random;
import java.util.UUID;
public class BlacksmithTrait extends Trait {
private static final String[] enchantments = new String[Enchantment.values().length];
private final BlacksmithPlugin plugin;
private final List<Material> reforgeAbleItems = new ArrayList<>();
private final Map<String, Calendar> coolDowns = new HashMap<>();
private final Map<UUID, Calendar> coolDowns = new HashMap<>();
private ReforgeSession session;
// Defaults
private String busyWithPlayerMsg = Setting.BUSY_WITH_PLAYER_MESSAGE.asString();
private String busyReforgingMsg = Setting.BUSY_WITH_REFORGE_MESSAGE.asString();
private String costMsg = Setting.COST_MESSAGE.asString();
private String invalidItemMsg = Setting.INVALID_ITEM_MESSAGE.asString();
private String startReforgeMsg = Setting.START_REFORGE_MESSAGE.asString();
private String successMsg = Setting.SUCCESS_MESSAGE.asString();
private String failMsg = Setting.FAIL_MESSAGE.asString();
private String insufficientFundsMsg = Setting.INSUFFICIENT_FUNDS_MESSAGE.asString();
private String coolDownUnexpiredMessage = Setting.COOLDOWN_UNEXPIRED_MESSAGE.asString();
private String itemChangedMsg = Setting.ITEM_UNEXPECTEDLY_CHANGED_MESSAGE.asString();
private int minReforgeDelay = Setting.MIN_REFORGE_DELAY.asInt();
private int maxReforgeDelay = Setting.MAX_REFORGE_DELAY.asInt();
private int reforgeCoolDown = Setting.REFORGE_COOLDOWN.asInt();
private int failChance = Setting.FAIL_CHANCE.asInt();
private int extraEnchantmentChance = Setting.EXTRA_ENCHANTMENT_CHANCE.asInt();
private int maxEnchantments = Setting.MAX_ENCHANTMENTS.asInt();
private boolean dropItem = Setting.DROP_ITEM.asBoolean();
private boolean disableCoolDown = Setting.DISABLE_COOLDOWN.asBoolean();
private boolean disableDelay = Setting.DISABLE_DELAY.asBoolean();
private final Settings config;
public BlacksmithTrait() {
super("blacksmith");
plugin = (BlacksmithPlugin) Bukkit.getServer().getPluginManager().getPlugin("Blacksmith");
int i = 0;
for (Enchantment enchantment : Enchantment.values()) {
enchantments[i++] = enchantment.getKey().asString();
}
this.config = BlacksmithPlugin.getInstance().getSettings();
}
public void addCoolDown(UUID playerUUID, Calendar waitUntil) {
coolDowns.put(playerUUID, waitUntil);
}
public void unsetSession() {
this.session = null;
}
@Override
@ -67,65 +48,7 @@ public class BlacksmithTrait extends Trait {
reforgeAbleItems.add(Material.getMaterial(sub.getString("").toUpperCase().replace('-', '_')));
}
}
// Override defaults if they exist
if (key.keyExists("messages.busy-with-player")) {
busyWithPlayerMsg = key.getString("messages.busy-with-player");
}
if (key.keyExists("messages.busy-with-reforge")) {
busyReforgingMsg = key.getString("messages.busy-with-reforge");
}
if (key.keyExists("messages.cost")) {
costMsg = key.getString("messages.cost");
}
if (key.keyExists("messages.invalid-item")) {
invalidItemMsg = key.getString("messages.invalid-item");
}
if (key.keyExists("messages.start-reforge")) {
startReforgeMsg = key.getString("messages.start-reforge");
}
if (key.keyExists("messages.successful-reforge")) {
successMsg = key.getString("messages.successful-reforge");
}
if (key.keyExists("messages.fail-reforge")) {
failMsg = key.getString("messages.fail-reforge");
}
if (key.keyExists("messages.insufficient-funds")) {
insufficientFundsMsg = key.getString("messages.insufficient-funds");
}
if (key.keyExists("messages.cooldown-not-expired")) {
coolDownUnexpiredMessage = key.getString("messages.cooldown-not-expired");
}
if (key.keyExists("messages.item-changed-during-reforge")) {
itemChangedMsg = key.getString("messages.item-changed-during-reforge");
}
if (key.keyExists("delays-in-seconds.minimum")) {
minReforgeDelay = key.getInt("delays-in-seconds.minimum");
}
if (key.keyExists("delays-in-seconds.maximum")) {
maxReforgeDelay = key.getInt("delays-in-seconds.maximum");
}
if (key.keyExists("delays-in-seconds.reforge-cooldown")) {
reforgeCoolDown = key.getInt("delays-in-seconds.reforge-cooldown");
}
if (key.keyExists("percent-chance-to-fail-reforge")) {
failChance = key.getInt("percent-chance-to-fail-reforge");
}
if (key.keyExists("maximum-enchantments")) {
maxEnchantments = key.getInt("maximum-enchantments");
}
if (key.keyExists("extra-enchantments-chance")) {
extraEnchantmentChance = key.getInt("extra-enchantment-chance");
}
if (key.keyExists("dropitem")) {
dropItem = key.getBoolean("dropitem");
}
if (key.keyExists("disable-cooldown")) {
disableCoolDown = key.getBoolean("disable-cooldown");
}
if (key.keyExists("disable-delay")) {
disableDelay = key.getBoolean("disable-delay");
}
config.loadVariables(key);
}
@EventHandler
@ -135,19 +58,19 @@ public class BlacksmithTrait extends Trait {
}
Player player = event.getClicker();
if ((disableCoolDown & (coolDowns.get(player.getName()) != (null)))) {
coolDowns.remove(player.getName());
if ((config.getDisableCoolDown() & (coolDowns.get(player.getUniqueId()) != (null)))) {
coolDowns.remove(player.getUniqueId());
}
if (!player.hasPermission("blacksmith.reforge")) {
return;
}
if (coolDowns.get(player.getName()) != null) {
if (!Calendar.getInstance().after(coolDowns.get(player.getName()))) {
player.sendMessage(coolDownUnexpiredMessage);
if (coolDowns.get(player.getUniqueId()) != null) {
if (!Calendar.getInstance().after(coolDowns.get(player.getUniqueId()))) {
player.sendMessage(config.getCoolDownUnexpiredMessage());
return;
}
coolDowns.remove(player.getName());
coolDowns.remove(player.getUniqueId());
}
@ -155,7 +78,7 @@ public class BlacksmithTrait extends Trait {
if (session != null) {
//timeout
if (System.currentTimeMillis() > _sessionstart + 10 * 1000 || this.npc.getEntity().getLocation().distance(session.player.getLocation()) > 20) {
if (System.currentTimeMillis() > _sessionStart + 10 * 1000 || this.npc.getEntity().getLocation().distance(session.getPlayer().getLocation()) > 20) {
session = null;
}
}
@ -164,13 +87,13 @@ public class BlacksmithTrait extends Trait {
if (session != null) {
if (!session.isInSession(player)) {
player.sendMessage(busyWithPlayerMsg);
player.sendMessage(config.getBusyWithPlayerMessage());
return;
}
if (session.isRunning()) {
player.sendMessage(busyReforgingMsg);
player.sendMessage(config.getBusyReforgingMessage());
return;
}
if (session.handleClick()) {
@ -181,21 +104,21 @@ public class BlacksmithTrait extends Trait {
} else {
if ((!plugin.isTool(hand) && !plugin.isArmor(hand))
|| (!reforgeAbleItems.isEmpty() && !reforgeAbleItems.contains(hand.getType()))) {
player.sendMessage(invalidItemMsg);
player.sendMessage(config.getInvalidItemMessage());
return;
}
String cost = plugin.formatCost(player);
_sessionstart = System.currentTimeMillis();
session = new ReforgeSession(player, npc);
player.sendMessage(costMsg.replace("<price>", cost).replace("<item>",
_sessionStart = System.currentTimeMillis();
session = new ReforgeSession(this, player, npc, config);
player.sendMessage(config.getCostMessage().replace("<price>", cost).replace("<item>",
hand.getType().name().toLowerCase().replace('_', ' ')));
}
}
private long _sessionstart = System.currentTimeMillis();
private long _sessionStart = System.currentTimeMillis();
@Override
public void save(DataKey key) {
@ -204,29 +127,11 @@ public class BlacksmithTrait extends Trait {
reforgeAbleItems.get(i).name().toLowerCase().replace('_', '-'));
}
key.setString("messages.busy-with-player", busyWithPlayerMsg);
key.setString("messages.busy-with-reforge", busyReforgingMsg);
key.setString("messages.cost", costMsg);
key.setString("messages.invalid-item", invalidItemMsg);
key.setString("messages.start-reforge", startReforgeMsg);
key.setString("messages.successful-reforge", successMsg);
key.setString("messages.fail-reforge", failMsg);
key.setString("messages.insufficient-funds", insufficientFundsMsg);
key.setString("messages.cooldown-not-expired", coolDownUnexpiredMessage);
key.setString("messages.item-changed-during-reforge", itemChangedMsg);
key.setInt("delays-in-seconds.minimum", minReforgeDelay);
key.setInt("delays-in-seconds.maximum", maxReforgeDelay);
key.setInt("delays-in-seconds.reforge-cooldown", reforgeCoolDown);
key.setInt("percent-chance-to-fail-reforge", failChance);
key.setInt("percent-chance-for-extra-enchantment", extraEnchantmentChance);
key.setInt("maximum-enchantments", maxEnchantments);
key.setBoolean("drop-item", dropItem);
key.setBoolean("disable-delay", disableDelay);
key.setBoolean("disable-cooldown", disableCoolDown);
config.saveVariables(key);
}
private void reforge(NPC npc, Player player) {
player.sendMessage(startReforgeMsg);
player.sendMessage(config.getStartReforgeMessage());
//plugin.deposit(npc, player); // CitiTrader dependency outdated and broken
@ -240,127 +145,4 @@ public class BlacksmithTrait extends Trait {
player.getInventory().setItemInMainHand(null);
}
private class ReforgeSession implements Runnable {
private final Player player;
private final NPC npc;
private final ItemStack reforge;
private int taskId;
private ReforgeSession(Player player, NPC npc) {
this.player = player;
this.npc = npc;
reforge = player.getInventory().getItemInMainHand();
}
@Override
public void run() {
player.sendMessage(reforgeItemInHand() ? successMsg : failMsg);
if (npc.getEntity() instanceof Player) {
((Player) npc.getEntity()).getInventory().setItemInMainHand(null);
} else {
Objects.requireNonNull(((LivingEntity) npc.getEntity()).getEquipment()).setItemInMainHand(null);
}
if (!disableDelay) {
if (dropItem) {
player.getWorld().dropItemNaturally(npc.getEntity().getLocation(), reforge);
} else {
player.getInventory().addItem(reforge);
}
} else {
player.getInventory().setItemInMainHand(reforge);
}
session = null;
// Start cooldown
Calendar wait = Calendar.getInstance();
wait.add(Calendar.SECOND, reforgeCoolDown);
coolDowns.put(player.getName(), wait);
}
private boolean reforgeItemInHand() {
Random random = new Random();
if (random.nextInt(100) < failChance) {
for (Enchantment enchantment : reforge.getEnchantments().keySet()) {
// Remove or downgrade enchantments
if (random.nextBoolean()) {
reforge.removeEnchantment(enchantment);
} else {
if (reforge.getEnchantmentLevel(enchantment) > 1) {
reforge.removeEnchantment(enchantment);
reforge.addEnchantment(enchantment, 1);
}
}
}
// Damage the item
short reforgeDurability = BlacksmithPlugin.getDurability(reforge);
short durability = (short) (reforgeDurability + reforgeDurability * random.nextInt(8));
short maxDurability = reforge.getType().getMaxDurability();
if (durability <= 0) {
durability = (short) (maxDurability / 3);
} else if (reforgeDurability + durability > maxDurability) {
durability = (short) (maxDurability - random.nextInt(maxDurability - 25));
}
((Damageable) reforge.getItemMeta()).setDamage(durability);
return false;
}
((Damageable) reforge.getItemMeta()).setDamage((short) 0);
// Add random enchantments
// If durability is full, chance is multiplied by 4. Seems unbalanced, so disabled for now.
/*if (reforge.getDurability() == 0)
chance *= 4;
else */
int roll = random.nextInt(100);
if (roll < extraEnchantmentChance && reforge.getEnchantments().keySet().size() < maxEnchantments) {
Enchantment enchantment = Enchantment.getByKey(NamespacedKey.fromString(enchantments[random.nextInt(enchantments.length)]));
if (Objects.requireNonNull(enchantment).canEnchantItem(reforge)) {
reforge.addEnchantment(enchantment, random.nextInt(enchantment.getMaxLevel() - enchantment.getStartLevel()) + enchantment.getStartLevel());
}
}
return true;
}
// Return if the session should end
private boolean handleClick() {
// Prevent player from switching items during session
if (!reforge.equals(player.getInventory().getItemInMainHand())) {
player.sendMessage(itemChangedMsg);
return true;
}
if (!plugin.doesPlayerHaveEnough(player)) {
player.sendMessage(insufficientFundsMsg);
return true;
}
return false;
}
private boolean isRunning() {
return plugin.getServer().getScheduler().isQueued(taskId);
}
private boolean isInSession(Player other) {
return player.getName().equals(other.getName());
}
private void beginReforge() {
if (!disableCoolDown) {
taskId = plugin
.getServer()
.getScheduler()
.scheduleSyncDelayedTask(plugin, this,
(new Random().nextInt(maxReforgeDelay) + minReforgeDelay) * 20L);
} else {
taskId = plugin
.getServer()
.getScheduler()
.scheduleSyncDelayedTask(plugin, this, 0);
}
}
}
}

View File

@ -0,0 +1,147 @@
package net.apunch.blacksmith;
import net.apunch.blacksmith.util.Settings;
import net.citizensnpcs.api.npc.NPC;
import org.bukkit.NamespacedKey;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.Calendar;
import java.util.Objects;
import java.util.Random;
class ReforgeSession implements Runnable {
private final BlacksmithTrait blacksmithTrait;
private final Player player;
private final NPC npc;
private final ItemStack itemToReforge;
private int taskId;
private final Settings config;
private static final String[] enchantments = new String[Enchantment.values().length];
ReforgeSession(BlacksmithTrait blacksmithTrait, Player player, NPC npc, Settings config) {
this.blacksmithTrait = blacksmithTrait;
this.player = player;
this.npc = npc;
itemToReforge = player.getInventory().getItemInMainHand();
this.config = config;
int i = 0;
for (Enchantment enchantment : Enchantment.values()) {
enchantments[i++] = enchantment.getKey().asString();
}
}
@Override
public void run() {
player.sendMessage(reforgeItemInHand() ? config.getSuccessMessage() : config.getFailMessage());
if (npc.getEntity() instanceof Player) {
((Player) npc.getEntity()).getInventory().setItemInMainHand(null);
} else {
Objects.requireNonNull(((LivingEntity) npc.getEntity()).getEquipment()).setItemInMainHand(null);
}
if (!config.getDisableDelay()) {
if (config.getDropItem()) {
player.getWorld().dropItemNaturally(npc.getEntity().getLocation(), itemToReforge);
} else {
player.getInventory().addItem(itemToReforge);
}
} else {
player.getInventory().setItemInMainHand(itemToReforge);
}
blacksmithTrait.unsetSession();
// Start cool down
Calendar wait = Calendar.getInstance();
wait.add(Calendar.SECOND, config.getReforgeCoolDown());
blacksmithTrait.addCoolDown(player.getUniqueId(), wait);
}
private boolean reforgeItemInHand() {
Random random = new Random();
if (random.nextInt(100) < config.getFailChance()) {
for (Enchantment enchantment : itemToReforge.getEnchantments().keySet()) {
// Remove or downgrade enchantments
if (random.nextBoolean()) {
itemToReforge.removeEnchantment(enchantment);
} else {
if (itemToReforge.getEnchantmentLevel(enchantment) > 1) {
itemToReforge.removeEnchantment(enchantment);
itemToReforge.addEnchantment(enchantment, 1);
}
}
}
// Damage the item
short reforgeDurability = BlacksmithPlugin.getDurability(itemToReforge);
short durability = (short) (reforgeDurability + reforgeDurability * random.nextInt(8));
short maxDurability = itemToReforge.getType().getMaxDurability();
if (durability <= 0) {
durability = (short) (maxDurability / 3);
} else if (reforgeDurability + durability > maxDurability) {
durability = (short) (maxDurability - random.nextInt(maxDurability - 25));
}
updateDamage(itemToReforge, maxDurability - durability);
return false;
} else {
updateDamage(itemToReforge, 0);
// Add random enchantments
int roll = random.nextInt(100);
if (roll < config.getExtraEnchantmentChance() && itemToReforge.getEnchantments().keySet().size() < config.getMaxEnchantments()) {
Enchantment enchantment = Enchantment.getByKey(NamespacedKey.fromString(enchantments[random.nextInt(enchantments.length)]));
if (Objects.requireNonNull(enchantment).canEnchantItem(itemToReforge)) {
itemToReforge.addEnchantment(enchantment, random.nextInt(enchantment.getMaxLevel() - enchantment.getStartLevel()) + enchantment.getStartLevel());
}
}
return true;
}
}
private void updateDamage(ItemStack item, int newDamage) {
ItemMeta meta = item.getItemMeta();
Damageable damageable = (Damageable) meta;
damageable.setDamage(newDamage);
item.setItemMeta(meta);
}
// Return if the session should end
boolean handleClick() {
// Prevent player from switching items during session
if (!itemToReforge.equals(player.getInventory().getItemInMainHand())) {
player.sendMessage(config.getItemChangedMessage());
return true;
}
if (!BlacksmithPlugin.getInstance().doesPlayerHaveEnough(player)) {
player.sendMessage(config.getInsufficientFundsMessage());
return true;
}
return false;
}
boolean isRunning() {
return BlacksmithPlugin.getInstance().getServer().getScheduler().isQueued(taskId);
}
boolean isInSession(Player other) {
return player.getName().equals(other.getName());
}
void beginReforge() {
if (!config.getDisableCoolDown()) {
taskId = BlacksmithPlugin.getInstance().getServer().getScheduler().scheduleSyncDelayedTask(
BlacksmithPlugin.getInstance(), this, (new Random().nextInt(config.getMaxReforgeDelay()) +
config.getMinReforgeDelay()) * 20L);
} else {
taskId = BlacksmithPlugin.getInstance().getServer().getScheduler().scheduleSyncDelayedTask(
BlacksmithPlugin.getInstance(), this, 0);
}
}
public Player getPlayer() {
return player;
}
}

View File

@ -0,0 +1,76 @@
package net.apunch.blacksmith.util;
public enum Setting {
BASE_PRICE("base-prices.default", 10),
PRICE_PER_DURABILITY_POINT("price-per-durability-point.default", 1),
BUSY_WITH_PLAYER_MESSAGE("defaults.messages.busy-with-player", "§cI'm busy at the moment. Come back later!"),
BUSY_WITH_REFORGE_MESSAGE("defaults.messages.busy-with-reforge", "§cI'm working on it. Be patient!"),
COOL_DOWN_UNEXPIRED_MESSAGE(
"defaults.messages.cooldown-not-expired",
"§cYou've already had your chance! Give me a break!"),
COST_MESSAGE(
"defaults.messages.cost",
"§eIt will cost §a<price> §eto reforge that §a<item>§e! Click again to reforge!"),
DROP_ITEM("defaults.dropitem", true),
DISABLE_COOL_DOWN("defaults.disablecooldown", false),
DISABLE_DELAY("defaults.disabledelay", false),
ENCHANTMENT_MODIFIER("enchantment-modifiers.default", 5),
FAIL_CHANCE("defaults.percent-chance-to-fail-reforge", 10),
FAIL_MESSAGE("defaults.messages.fail-reforge", "§cWhoops! Didn't mean to do that! Maybe next time?"),
INSUFFICIENT_FUNDS_MESSAGE(
"defaults.messages.insufficient-funds",
"§cYou don't have enough money to reforge that item!"),
INVALID_ITEM_MESSAGE("defaults.messages.invalid-item", "§cI'm sorry, but I don't know how to reforge that!"),
ITEM_UNEXPECTEDLY_CHANGED_MESSAGE(
"defaults.messages.item-changed-during-reforge",
"§cThat's not the item you wanted to reforge before!"),
EXTRA_ENCHANTMENT_CHANCE("defaults.percent-chance-for-extra-enchantment", 5),
MAX_ENCHANTMENTS("defaults.maximum-enchantments", 3),
MAX_REFORGE_DELAY("defaults.delays-in-seconds.maximum", 30),
MIN_REFORGE_DELAY("defaults.delays-in-seconds.minimum", 5),
REFORGE_COOL_DOWN("defaults.delays-in-seconds.reforge-cooldown", 60),
START_REFORGE_MESSAGE("defaults.messages.start-reforge", "§eOk, let's see what I can do..."),
SUCCESS_MESSAGE("defaults.messages.successful-reforge", "There you go! All better!");
private final String path;
private Object value;
Setting(String path, Object value) {
this.path = path;
this.value = value;
}
public String getPath() {
return path;
}
public boolean asBoolean() {
return (Boolean) value;
}
public double asDouble() {
if (value instanceof String) {
return Double.parseDouble((String) value);
}
if (value instanceof Integer) {
return (Integer) value;
}
return (Double) value;
}
public int asInt() {
return (Integer) value;
}
public String asString() {
return value.toString();
}
Object get() {
return value;
}
void set(Object value) {
this.value = value;
}
}

View File

@ -7,6 +7,26 @@ import net.citizensnpcs.api.util.YamlStorage;
import java.io.File;
public class Settings {
private String busyWithPlayerMessage = Setting.BUSY_WITH_PLAYER_MESSAGE.asString();
private String busyReforgingMessage = Setting.BUSY_WITH_REFORGE_MESSAGE.asString();
private String costMessage = Setting.COST_MESSAGE.asString();
private String invalidItemMessage = Setting.INVALID_ITEM_MESSAGE.asString();
private String startReforgeMessage = Setting.START_REFORGE_MESSAGE.asString();
private String successMessage = Setting.SUCCESS_MESSAGE.asString();
private String failMessage = Setting.FAIL_MESSAGE.asString();
private String insufficientFundsMessage = Setting.INSUFFICIENT_FUNDS_MESSAGE.asString();
private String coolDownUnexpiredMessage = Setting.COOL_DOWN_UNEXPIRED_MESSAGE.asString();
private String itemChangedMessage = Setting.ITEM_UNEXPECTEDLY_CHANGED_MESSAGE.asString();
private int minReforgeDelay = Setting.MIN_REFORGE_DELAY.asInt();
private int maxReforgeDelay = Setting.MAX_REFORGE_DELAY.asInt();
private int reforgeCoolDown = Setting.REFORGE_COOL_DOWN.asInt();
private int failChance = Setting.FAIL_CHANCE.asInt();
private int extraEnchantmentChance = Setting.EXTRA_ENCHANTMENT_CHANCE.asInt();
private int maxEnchantments = Setting.MAX_ENCHANTMENTS.asInt();
private boolean dropItem = Setting.DROP_ITEM.asBoolean();
private boolean disableCoolDown = Setting.DISABLE_COOL_DOWN.asBoolean();
private boolean disableDelay = Setting.DISABLE_DELAY.asBoolean();
private final YamlStorage config;
public Settings(BlacksmithPlugin plugin) {
@ -17,10 +37,10 @@ public class Settings {
config.load();
DataKey root = config.getKey("");
for (Setting setting : Setting.values()) {
if (!root.keyExists(setting.path)) {
root.setRaw(setting.path, setting.get());
if (!root.keyExists(setting.getPath())) {
root.setRaw(setting.getPath(), setting.get());
} else {
setting.set(root.getRaw(setting.path));
setting.set(root.getRaw(setting.getPath()));
}
}
@ -31,74 +51,163 @@ public class Settings {
return config;
}
public enum Setting {
BASE_PRICE("base-prices.default", 10),
PRICE_PER_DURABILITY_POINT("price-per-durability-point.default", 1),
BUSY_WITH_PLAYER_MESSAGE("defaults.messages.busy-with-player", "§cI'm busy at the moment. Come back later!"),
BUSY_WITH_REFORGE_MESSAGE("defaults.messages.busy-with-reforge", "§cI'm working on it. Be patient!"),
COOLDOWN_UNEXPIRED_MESSAGE(
"defaults.messages.cooldown-not-expired",
"§cYou've already had your chance! Give me a break!"),
COST_MESSAGE(
"defaults.messages.cost",
"§eIt will cost §a<price> §eto reforge that §a<item>§e! Click again to reforge!"),
DROP_ITEM("defaults.dropitem", true),
DISABLE_COOLDOWN("defaults.disablecooldown", false),
DISABLE_DELAY("defaults.disabledelay", false),
ENCHANTMENT_MODIFIER("enchantment-modifiers.default", 5),
FAIL_CHANCE("defaults.percent-chance-to-fail-reforge", 10),
FAIL_MESSAGE("defaults.messages.fail-reforge", "§cWhoops! Didn't mean to do that! Maybe next time?"),
INSUFFICIENT_FUNDS_MESSAGE(
"defaults.messages.insufficient-funds",
"§cYou don't have enough money to reforge that item!"),
INVALID_ITEM_MESSAGE("defaults.messages.invalid-item", "§cI'm sorry, but I don't know how to reforge that!"),
ITEM_UNEXPECTEDLY_CHANGED_MESSAGE(
"defaults.messages.item-changed-during-reforge",
"§cThat's not the item you wanted to reforge before!"),
EXTRA_ENCHANTMENT_CHANCE("defaults.percent-chance-for-extra-enchantment", 5),
MAX_ENCHANTMENTS("defaults.maximum-enchantments", 3),
MAX_REFORGE_DELAY("defaults.delays-in-seconds.maximum", 30),
MIN_REFORGE_DELAY("defaults.delays-in-seconds.minimum", 5),
REFORGE_COOLDOWN("defaults.delays-in-seconds.reforge-cooldown", 60),
START_REFORGE_MESSAGE("defaults.messages.start-reforge", "§eOk, let's see what I can do..."),
SUCCESS_MESSAGE("defaults.messages.successful-reforge", "There you go! All better!");
private final String path;
private Object value;
Setting(String path, Object value) {
this.path = path;
this.value = value;
public void loadVariables(DataKey key) {
// Override defaults if they exist
if (key.keyExists("messages.busy-with-player")) {
busyWithPlayerMessage = key.getString("messages.busy-with-player");
}
public boolean asBoolean() {
return (Boolean) value;
if (key.keyExists("messages.busy-with-reforge")) {
busyReforgingMessage = key.getString("messages.busy-with-reforge");
}
public double asDouble() {
if (value instanceof String) {
return Double.parseDouble((String) value);
}
if (value instanceof Integer) {
return (Integer) value;
}
return (Double) value;
if (key.keyExists("messages.cost")) {
costMessage = key.getString("messages.cost");
}
public int asInt() {
return (Integer) value;
if (key.keyExists("messages.invalid-item")) {
invalidItemMessage = key.getString("messages.invalid-item");
}
public String asString() {
return value.toString();
if (key.keyExists("messages.start-reforge")) {
startReforgeMessage = key.getString("messages.start-reforge");
}
private Object get() {
return value;
if (key.keyExists("messages.successful-reforge")) {
successMessage = key.getString("messages.successful-reforge");
}
private void set(Object value) {
this.value = value;
if (key.keyExists("messages.fail-reforge")) {
failMessage = key.getString("messages.fail-reforge");
}
if (key.keyExists("messages.insufficient-funds")) {
insufficientFundsMessage = key.getString("messages.insufficient-funds");
}
if (key.keyExists("messages.cooldown-not-expired")) {
coolDownUnexpiredMessage = key.getString("messages.cooldown-not-expired");
}
if (key.keyExists("messages.item-changed-during-reforge")) {
itemChangedMessage = key.getString("messages.item-changed-during-reforge");
}
if (key.keyExists("delays-in-seconds.minimum")) {
minReforgeDelay = key.getInt("delays-in-seconds.minimum");
}
if (key.keyExists("delays-in-seconds.maximum")) {
maxReforgeDelay = key.getInt("delays-in-seconds.maximum");
}
if (key.keyExists("delays-in-seconds.reforge-cooldown")) {
reforgeCoolDown = key.getInt("delays-in-seconds.reforge-cooldown");
}
if (key.keyExists("percent-chance-to-fail-reforge")) {
failChance = key.getInt("percent-chance-to-fail-reforge");
}
if (key.keyExists("maximum-enchantments")) {
maxEnchantments = key.getInt("maximum-enchantments");
}
if (key.keyExists("extra-enchantments-chance")) {
extraEnchantmentChance = key.getInt("extra-enchantment-chance");
}
if (key.keyExists("dropitem")) {
dropItem = key.getBoolean("dropitem");
}
if (key.keyExists("disable-cooldown")) {
disableCoolDown = key.getBoolean("disable-cooldown");
}
if (key.keyExists("disable-delay")) {
disableDelay = key.getBoolean("disable-delay");
}
}
public void saveVariables(DataKey key) {
key.setString("messages.busy-with-player", getBusyWithPlayerMessage());
key.setString("messages.busy-with-reforge", getBusyReforgingMessage());
key.setString("messages.cost", getCostMessage());
key.setString("messages.invalid-item", getInvalidItemMessage());
key.setString("messages.start-reforge", getStartReforgeMessage());
key.setString("messages.successful-reforge", getSuccessMessage());
key.setString("messages.fail-reforge", getFailMessage());
key.setString("messages.insufficient-funds", getInsufficientFundsMessage());
key.setString("messages.cooldown-not-expired", getCoolDownUnexpiredMessage());
key.setString("messages.item-changed-during-reforge", getItemChangedMessage());
key.setInt("delays-in-seconds.minimum", getMinReforgeDelay());
key.setInt("delays-in-seconds.maximum", getMaxReforgeDelay());
key.setInt("delays-in-seconds.reforge-cooldown", getReforgeCoolDown());
key.setInt("percent-chance-to-fail-reforge", getFailChance());
key.setInt("percent-chance-for-extra-enchantment", getExtraEnchantmentChance());
key.setInt("maximum-enchantments", getMaxEnchantments());
key.setBoolean("drop-item", getDropItem());
key.setBoolean("disable-delay", getDisableDelay());
key.setBoolean("disable-cooldown", getDisableCoolDown());
}
public String getBusyWithPlayerMessage() {
return busyWithPlayerMessage;
}
public String getBusyReforgingMessage() {
return busyReforgingMessage;
}
public String getCostMessage() {
return costMessage;
}
public String getInvalidItemMessage() {
return invalidItemMessage;
}
public String getStartReforgeMessage() {
return startReforgeMessage;
}
public String getSuccessMessage() {
return successMessage;
}
public String getFailMessage() {
return failMessage;
}
public String getInsufficientFundsMessage() {
return insufficientFundsMessage;
}
public String getCoolDownUnexpiredMessage() {
return coolDownUnexpiredMessage;
}
public String getItemChangedMessage() {
return itemChangedMessage;
}
public int getMinReforgeDelay() {
return minReforgeDelay;
}
public int getMaxReforgeDelay() {
return maxReforgeDelay;
}
public int getReforgeCoolDown() {
return reforgeCoolDown;
}
public int getFailChance() {
return failChance;
}
public int getExtraEnchantmentChance() {
return extraEnchantmentChance;
}
public int getMaxEnchantments() {
return maxEnchantments;
}
public boolean getDropItem() {
return dropItem;
}
public boolean getDisableCoolDown() {
return disableCoolDown;
}
public boolean getDisableDelay() {
return disableDelay;
}
}