Fix bugs, refactor a bit

This commit is contained in:
aPunch 2012-04-11 14:54:11 -05:00
parent 0f60be96ad
commit a4c342c545
3 changed files with 53 additions and 65 deletions

View File

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

View File

@ -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, "<c>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;
}
}

View File

@ -37,14 +37,23 @@ public class Settings {
BASE_PRICE("base-prices.default", 10),
BUSY_WITH_PLAYER_MESSAGE("defaults.messages.busy-with-player", "<c>I'm busy at the moment. Come back later!"),
BUSY_WITH_REFORGE_MESSAGE("defaults.messages.busy-with-reforge", "<c>I'm working on it. Be patient!"),
COOLDOWN_UNEXPIRED_MESSAGE("defaults.messages.cooldown-not-expired", "<c>You've already had your chance! Give me a break!"),
COST_MESSAGE("defaults.messages.cost", "<e>It will cost <a><price> <e>to reforge that <a><item><e>! Click again to reforge!"),
COOLDOWN_UNEXPIRED_MESSAGE(
"defaults.messages.cooldown-not-expired",
"<c>You've already had your chance! Give me a break!"),
COST_MESSAGE(
"defaults.messages.cost",
"<e>It will cost <a><price> <e>to reforge that <a><item><e>! 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", "<c>Whoops! Didn't mean to do that! Maybe next time?"),
INSUFFICIENT_FUNDS_MESSAGE("defaults.messages.insufficient-funds", "<c>You don't have enough money to reforge that item!"),
INSUFFICIENT_FUNDS_MESSAGE(
"defaults.messages.insufficient-funds",
"<c>You don't have enough money to reforge that item!"),
INVALID_ITEM_MESSAGE("defaults.messages.invalid-item", "<c>I'm sorry, but I don't know how to reforge that!"),
ITEM_UNEXPECTEDLY_CHANGED_MESSAGE(
"defaults.messages.item-changed-during-reforge",
"<c>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),