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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user