Adds most of the code necessary for #11
This commit is contained in:
@ -5,7 +5,7 @@ import net.citizensnpcs.api.trait.Trait;
|
||||
import net.citizensnpcs.api.util.DataKey;
|
||||
import net.knarcraft.blacksmith.BlacksmithPlugin;
|
||||
import net.knarcraft.blacksmith.config.NPCSettings;
|
||||
import net.knarcraft.blacksmith.formatting.StringFormatter;
|
||||
import net.knarcraft.blacksmith.formatting.TimeFormatter;
|
||||
import net.knarcraft.blacksmith.manager.EconomyManager;
|
||||
import net.knarcraft.blacksmith.util.ItemHelper;
|
||||
import org.bukkit.Bukkit;
|
||||
@ -23,6 +23,7 @@ import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
|
||||
import static net.knarcraft.blacksmith.formatting.StringFormatter.replacePlaceholder;
|
||||
import static net.knarcraft.blacksmith.formatting.StringFormatter.sendNPCMessage;
|
||||
|
||||
/**
|
||||
@ -107,9 +108,10 @@ public class BlacksmithTrait extends Trait {
|
||||
* @return <p>True if preparations were successful. False if a session shouldn't be started</p>
|
||||
*/
|
||||
public boolean prepareForSession(Player player) {
|
||||
UUID playerId = player.getUniqueId();
|
||||
//If cool-down has been disabled after it was set for this player, remove the cool-down
|
||||
if (config.getDisableCoolDown() && coolDowns.get(player.getUniqueId()) != null) {
|
||||
coolDowns.remove(player.getUniqueId());
|
||||
if (config.getDisableCoolDown() && coolDowns.get(playerId) != null) {
|
||||
coolDowns.remove(playerId);
|
||||
}
|
||||
//Deny if permission is missing
|
||||
if (!player.hasPermission("blacksmith.reforge")) {
|
||||
@ -117,17 +119,21 @@ public class BlacksmithTrait extends Trait {
|
||||
}
|
||||
|
||||
//Deny if on cool-down, or remove cool-down if expired
|
||||
if (coolDowns.get(player.getUniqueId()) != null) {
|
||||
if (!Calendar.getInstance().after(coolDowns.get(player.getUniqueId()))) {
|
||||
sendNPCMessage(this.npc, player, config.getCoolDownUnexpiredMessage());
|
||||
if (coolDowns.get(playerId) != null) {
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
if (!calendar.after(coolDowns.get(playerId))) {
|
||||
int secondDifference = (int) ((calendar.getTimeInMillis() - coolDowns.get(playerId).getTimeInMillis()) * 1000);
|
||||
boolean exactTime = BlacksmithPlugin.getInstance().getSettings().getShowExactTime();
|
||||
sendNPCMessage(this.npc, player, replacePlaceholder(config.getCoolDownUnexpiredMessage(),
|
||||
"{time}", TimeFormatter.formatTime(exactTime, secondDifference)));
|
||||
return false;
|
||||
}
|
||||
coolDowns.remove(player.getUniqueId());
|
||||
coolDowns.remove(playerId);
|
||||
}
|
||||
|
||||
//If already in a session, but the player has failed to interact, or left the blacksmith, allow a new session
|
||||
//If already in a session, but the player has failed to interact, and left the blacksmith, allow a new session
|
||||
if (session != null) {
|
||||
if (System.currentTimeMillis() > _sessionStart + 10 * 1000 ||
|
||||
if (System.currentTimeMillis() > _sessionStart + 10 * 1000 &&
|
||||
this.npc.getEntity().getLocation().distance(session.getPlayer().getLocation()) > 20) {
|
||||
session = null;
|
||||
}
|
||||
@ -149,7 +155,10 @@ public class BlacksmithTrait extends Trait {
|
||||
|
||||
//The blacksmith is already reforging for the player
|
||||
if (session.isRunning()) {
|
||||
sendNPCMessage(this.npc, player, config.getBusyReforgingMessage());
|
||||
int timeRemaining = (int) ((session.getFinishTime() - System.currentTimeMillis()) / 1000);
|
||||
boolean showExactTime = BlacksmithPlugin.getInstance().getSettings().getShowExactTime();
|
||||
sendNPCMessage(this.npc, player, replacePlaceholder(config.getBusyReforgingMessage(), "{time}",
|
||||
TimeFormatter.formatTime(showExactTime, timeRemaining)));
|
||||
return;
|
||||
}
|
||||
if (session.endSession()) {
|
||||
@ -171,7 +180,7 @@ public class BlacksmithTrait extends Trait {
|
||||
//Refuse if not repairable, or if reforge-able items is set, but doesn't include the held item
|
||||
List<Material> reforgeAbleItems = config.getReforgeAbleItems();
|
||||
if (!isRepairable(hand) || (!reforgeAbleItems.isEmpty() && !reforgeAbleItems.contains(hand.getType()))) {
|
||||
String invalidMessage = StringFormatter.replacePlaceholder(config.getInvalidItemMessage(),
|
||||
String invalidMessage = replacePlaceholder(config.getInvalidItemMessage(),
|
||||
"{title}", config.getBlacksmithTitle());
|
||||
sendNPCMessage(this.npc, player, invalidMessage);
|
||||
return;
|
||||
@ -189,7 +198,8 @@ public class BlacksmithTrait extends Trait {
|
||||
//Tell the player the cost of repairing the item
|
||||
String cost = EconomyManager.formatCost(player);
|
||||
String itemName = hand.getType().name().toLowerCase().replace('_', ' ');
|
||||
sendNPCMessage(this.npc, player, config.getCostMessage().replace("{cost}", cost).replace("{item}", itemName));
|
||||
sendNPCMessage(this.npc, player, config.getCostMessage().replace("{cost}", cost).replace("{item}",
|
||||
itemName));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -12,6 +12,7 @@ import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.Damageable;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.scheduler.BukkitScheduler;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
@ -32,6 +33,7 @@ public class ReforgeSession implements Runnable {
|
||||
private final NPC npc;
|
||||
private final ItemStack itemToReforge;
|
||||
private int taskId;
|
||||
private long finishTime = 0;
|
||||
private final NPCSettings config;
|
||||
private static final String[] enchantments = new String[Enchantment.values().length];
|
||||
private static final Random random = new Random();
|
||||
@ -48,15 +50,27 @@ public class ReforgeSession implements Runnable {
|
||||
this.blacksmithTrait = blacksmithTrait;
|
||||
this.player = player;
|
||||
this.npc = npc;
|
||||
itemToReforge = player.getInventory().getItemInMainHand();
|
||||
this.itemToReforge = player.getInventory().getItemInMainHand();
|
||||
this.config = config;
|
||||
|
||||
int i = 0;
|
||||
for (Enchantment enchantment : Enchantment.values()) {
|
||||
enchantments[i++] = enchantment.getKey().toString();
|
||||
//Populate enchantments the first time this is run
|
||||
if (enchantments[0] == null) {
|
||||
int i = 0;
|
||||
for (Enchantment enchantment : Enchantment.values()) {
|
||||
enchantments[i++] = enchantment.getKey().toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the time in milliseconds when this reforging session will finish
|
||||
*
|
||||
* @return <p>The time the reforging will finish</p>
|
||||
*/
|
||||
public long getFinishTime() {
|
||||
return this.finishTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the actual reforge which fixes the item and gives it back to the player
|
||||
*/
|
||||
@ -237,16 +251,17 @@ public class ReforgeSession implements Runnable {
|
||||
* Begins the actual item reforging
|
||||
*/
|
||||
public void beginReforge() {
|
||||
BukkitScheduler scheduler = BlacksmithPlugin.getInstance().getServer().getScheduler();
|
||||
int reforgeDelay;
|
||||
if (!config.getDisableCoolDown()) {
|
||||
//Finish the reforging 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);
|
||||
reforgeDelay = new Random().nextInt(config.getMaxReforgeDelay()) + config.getMinReforgeDelay();
|
||||
} else {
|
||||
//Finish the reforging as soon as possible
|
||||
taskId = BlacksmithPlugin.getInstance().getServer().getScheduler().scheduleSyncDelayedTask(
|
||||
BlacksmithPlugin.getInstance(), this, 0);
|
||||
reforgeDelay = 0;
|
||||
}
|
||||
this.finishTime = System.currentTimeMillis() + (reforgeDelay * 1000L);
|
||||
taskId = scheduler.scheduleSyncDelayedTask(BlacksmithPlugin.getInstance(), this, reforgeDelay * 20L);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user