diff --git a/src/main/java/net/apunch/blacksmith/Blacksmith.java b/src/main/java/net/apunch/blacksmith/Blacksmith.java index b12300d..c18871b 100644 --- a/src/main/java/net/apunch/blacksmith/Blacksmith.java +++ b/src/main/java/net/apunch/blacksmith/Blacksmith.java @@ -37,6 +37,7 @@ public class Blacksmith extends Character { private String failMsg = Setting.FAIL_MESSAGE.asString(); private String insufficientFundsMsg = Setting.INSUFFICIENT_FUNDS_MESSAGE.asString(); private String cooldownUnexpiredMsg = 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(); @@ -76,6 +77,8 @@ public class Blacksmith extends Character { insufficientFundsMsg = key.getString("messages.insufficient-funds"); if (key.keyExists("messages.cooldown-not-expired")) cooldownUnexpiredMsg = 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")) @@ -106,7 +109,7 @@ public class Blacksmith extends Character { ItemStack hand = player.getItemInHand(); if (session != null) { if (!session.isInSession(player)) { - npc.chat(busyWithPlayerMsg); + npc.chat(player, busyWithPlayerMsg); return; } @@ -147,6 +150,7 @@ public class Blacksmith extends Character { key.setString("messages.fail-reforge", failMsg); key.setString("messages.insufficient-funds", insufficientFundsMsg); key.setString("messages.cooldown-not-expired", cooldownUnexpiredMsg); + 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); @@ -155,31 +159,24 @@ public class Blacksmith extends Character { key.setBoolean("drop-item", dropItem); } - public String getInsufficientFundsMessage() { - return insufficientFundsMsg; - } - private void reforge(NPC npc, Player player) { npc.chat(player, startReforgeMsg); plugin.withdraw(player); - session.setTask(plugin - .getServer() - .getScheduler() - .scheduleAsyncDelayedTask(plugin, new ReforgeTask(npc, player), - (new Random().nextInt(maxReforgeDelay) + minReforgeDelay) * 20)); + session.beginReforge(); if (npc.getBukkitEntity() instanceof Player) ((Player) npc.getBukkitEntity()).setItemInHand(player.getItemInHand()); player.setItemInHand(null); } - private class ReforgeTask implements Runnable { - private final NPC npc; + private class ReforgeSession implements Runnable { private final Player player; + private final NPC npc; private final ItemStack reforge; + private int taskId; - private ReforgeTask(NPC npc, Player player) { - this.npc = npc; + private ReforgeSession(Player player, NPC npc) { this.player = player; + this.npc = npc; reforge = player.getItemInHand(); } @@ -240,5 +237,35 @@ public class Blacksmith extends Character { } return true; } + + // Return if the session should end + private boolean handleClick() { + // Prevent player from switching items during session + if (!reforge.equals(player.getItemInHand())) { + npc.chat(player, itemChangedMsg); + return true; + } + if (!plugin.doesPlayerHaveEnough(player)) { + npc.chat(player, 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() { + taskId = plugin + .getServer() + .getScheduler() + .scheduleAsyncDelayedTask(plugin, this, + (new Random().nextInt(maxReforgeDelay) + minReforgeDelay) * 20); + } } } \ No newline at end of file diff --git a/src/main/java/net/apunch/blacksmith/ReforgeSession.java b/src/main/java/net/apunch/blacksmith/ReforgeSession.java deleted file mode 100644 index d8c5f76..0000000 --- a/src/main/java/net/apunch/blacksmith/ReforgeSession.java +++ /dev/null @@ -1,48 +0,0 @@ -package net.apunch.blacksmith; - -import net.citizensnpcs.api.npc.NPC; - -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; - -public class ReforgeSession { - private final BlacksmithPlugin plugin; - private final Player player; - private final ItemStack reforge; - private final NPC npc; - private int taskId; - - public ReforgeSession(Player player, NPC npc) { - this.player = player; - reforge = player.getItemInHand(); - this.npc = npc; - - plugin = (BlacksmithPlugin) player.getServer().getPluginManager().getPlugin("Blacksmith"); - } - - // Return is the session should end - public boolean handleClick() { - // Prevent player from switching items during session - if (!reforge.equals(player.getItemInHand())) { - npc.chat(player, "That's not the item you wanted to reforge before!"); - return true; - } - if (!plugin.doesPlayerHaveEnough(player)) { - npc.chat(player, ((Blacksmith) npc.getCharacter()).getInsufficientFundsMessage()); - return true; - } - return false; - } - - public boolean isRunning() { - return plugin.getServer().getScheduler().isQueued(taskId); - } - - public boolean isInSession(Player other) { - return player.getName().equals(other.getName()); - } - - public void setTask(int taskId) { - this.taskId = taskId; - } -} \ No newline at end of file diff --git a/src/main/java/net/apunch/blacksmith/util/Settings.java b/src/main/java/net/apunch/blacksmith/util/Settings.java index b8fade0..7bfc7ba 100644 --- a/src/main/java/net/apunch/blacksmith/util/Settings.java +++ b/src/main/java/net/apunch/blacksmith/util/Settings.java @@ -37,14 +37,23 @@ public class Settings { BASE_PRICE("base-prices.default", 10), BUSY_WITH_PLAYER_MESSAGE("defaults.messages.busy-with-player", "I'm busy at the moment. Come back later!"), BUSY_WITH_REFORGE_MESSAGE("defaults.messages.busy-with-reforge", "I'm working on it. Be patient!"), - COOLDOWN_UNEXPIRED_MESSAGE("defaults.messages.cooldown-not-expired", "You've already had your chance! Give me a break!"), - COST_MESSAGE("defaults.messages.cost", "It will cost to reforge that ! Click again to reforge!"), + COOLDOWN_UNEXPIRED_MESSAGE( + "defaults.messages.cooldown-not-expired", + "You've already had your chance! Give me a break!"), + COST_MESSAGE( + "defaults.messages.cost", + "It will cost to reforge that ! Click again to reforge!"), DROP_ITEM("defaults.drop-item", true), ENCHANTMENT_MODIFIER("enchantment-modifiers.default", 5), FAIL_CHANCE("defaults.percent-chance-to-fail-reforge", 10), FAIL_MESSAGE("defaults.messages.fail-reforge", "Whoops! Didn't mean to do that! Maybe next time?"), - INSUFFICIENT_FUNDS_MESSAGE("defaults.messages.insufficient-funds", "You don't have enough money to reforge that item!"), + INSUFFICIENT_FUNDS_MESSAGE( + "defaults.messages.insufficient-funds", + "You don't have enough money to reforge that item!"), INVALID_ITEM_MESSAGE("defaults.messages.invalid-item", "I'm sorry, but I don't know how to reforge that!"), + ITEM_UNEXPECTEDLY_CHANGED_MESSAGE( + "defaults.messages.item-changed-during-reforge", + "That's not the item you wanted to reforge before!"), MAX_ENCHANTMENTS("defaults.maximum-enchantments", 3), MAX_REFORGE_DELAY("defaults.delays-in-seconds.maximum", 30), MIN_REFORGE_DELAY("defaults.delays-in-seconds.minimum", 5),