2022-07-19 02:41:01 +02:00
|
|
|
package net.knarcraft.blacksmith;
|
2017-07-31 11:53:59 +02:00
|
|
|
|
2022-02-02 00:36:15 +01:00
|
|
|
import net.citizensnpcs.api.CitizensAPI;
|
2022-02-01 19:52:41 +01:00
|
|
|
import net.citizensnpcs.api.npc.NPC;
|
|
|
|
import net.citizensnpcs.api.trait.Trait;
|
|
|
|
import net.citizensnpcs.api.util.DataKey;
|
2022-08-07 01:21:47 +02:00
|
|
|
import net.knarcraft.blacksmith.config.NPCSettings;
|
2017-07-31 11:53:59 +02:00
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.Material;
|
2022-08-07 01:21:47 +02:00
|
|
|
import org.bukkit.enchantments.EnchantmentTarget;
|
2022-02-02 00:36:15 +01:00
|
|
|
import org.bukkit.entity.Entity;
|
2017-07-31 11:53:59 +02:00
|
|
|
import org.bukkit.entity.LivingEntity;
|
|
|
|
import org.bukkit.entity.Player;
|
2022-02-02 00:36:15 +01:00
|
|
|
import org.bukkit.event.Event;
|
2017-07-31 11:53:59 +02:00
|
|
|
import org.bukkit.event.EventHandler;
|
2022-02-02 00:36:15 +01:00
|
|
|
import org.bukkit.event.EventPriority;
|
|
|
|
import org.bukkit.event.block.Action;
|
|
|
|
import org.bukkit.event.player.PlayerInteractEvent;
|
2017-07-31 11:53:59 +02:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
2022-08-07 01:21:47 +02:00
|
|
|
import org.bukkit.inventory.meta.Damageable;
|
2022-02-02 00:36:15 +01:00
|
|
|
import org.bukkit.util.Vector;
|
2017-07-31 11:53:59 +02:00
|
|
|
|
2022-02-01 19:52:41 +01:00
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Objects;
|
2022-02-01 22:24:39 +01:00
|
|
|
import java.util.UUID;
|
2017-07-31 11:53:59 +02:00
|
|
|
|
2022-07-16 14:56:29 +02:00
|
|
|
/**
|
|
|
|
* The class representing the Blacksmith NPC trait
|
|
|
|
*/
|
2017-07-31 11:53:59 +02:00
|
|
|
public class BlacksmithTrait extends Trait {
|
2022-02-01 19:52:41 +01:00
|
|
|
|
2022-02-01 22:24:39 +01:00
|
|
|
private final Map<UUID, Calendar> coolDowns = new HashMap<>();
|
2022-02-01 19:52:41 +01:00
|
|
|
private ReforgeSession session;
|
2022-08-07 01:21:47 +02:00
|
|
|
private final NPCSettings config;
|
2022-07-16 14:56:29 +02:00
|
|
|
private long _sessionStart = System.currentTimeMillis();
|
2022-02-01 19:52:41 +01:00
|
|
|
|
2022-07-16 14:56:29 +02:00
|
|
|
/**
|
|
|
|
* Instantiates a new blacksmith trait
|
|
|
|
*/
|
2022-02-01 19:52:41 +01:00
|
|
|
public BlacksmithTrait() {
|
|
|
|
super("blacksmith");
|
2022-08-07 01:21:47 +02:00
|
|
|
//This should crash if the blacksmith plugin hasn't been properly registered
|
|
|
|
Bukkit.getServer().getPluginManager().getPlugin("Blacksmith");
|
|
|
|
this.config = new NPCSettings(BlacksmithPlugin.getInstance().getSettings());
|
2022-02-01 22:24:39 +01:00
|
|
|
}
|
|
|
|
|
2022-07-16 14:56:29 +02:00
|
|
|
/**
|
|
|
|
* Adds a cool-down for the given player's next blacksmith reforge
|
|
|
|
*
|
|
|
|
* @param playerUUID <p>The ID of the player to add the cool-down for</p>
|
|
|
|
* @param waitUntil <p>The time when the player can interact again</p>
|
|
|
|
*/
|
2022-02-01 22:24:39 +01:00
|
|
|
public void addCoolDown(UUID playerUUID, Calendar waitUntil) {
|
|
|
|
coolDowns.put(playerUUID, waitUntil);
|
|
|
|
}
|
|
|
|
|
2022-07-16 14:56:29 +02:00
|
|
|
/**
|
|
|
|
* Unsets the session of this blacksmith, making it ready for another round
|
|
|
|
*/
|
2022-02-01 22:24:39 +01:00
|
|
|
public void unsetSession() {
|
|
|
|
this.session = null;
|
2022-02-01 19:52:41 +01:00
|
|
|
}
|
|
|
|
|
2022-08-07 01:21:47 +02:00
|
|
|
/**
|
|
|
|
* Loads all config values stored in citizens' config file for this NPC
|
|
|
|
*
|
|
|
|
* @param key <p>The data key used for the config root</p>
|
|
|
|
*/
|
2022-02-01 19:52:41 +01:00
|
|
|
@Override
|
|
|
|
public void load(DataKey key) {
|
2022-02-01 22:24:39 +01:00
|
|
|
config.loadVariables(key);
|
2022-02-01 19:52:41 +01:00
|
|
|
}
|
|
|
|
|
2022-08-07 01:21:47 +02:00
|
|
|
/**
|
|
|
|
* Saves all config values for this NPC
|
|
|
|
*
|
|
|
|
* @param key <p>The data key used for the config root</p>
|
|
|
|
*/
|
2022-07-16 14:56:29 +02:00
|
|
|
@Override
|
|
|
|
public void save(DataKey key) {
|
|
|
|
config.saveVariables(key);
|
|
|
|
}
|
|
|
|
|
2022-02-02 00:36:15 +01:00
|
|
|
@EventHandler(priority = EventPriority.HIGHEST)
|
|
|
|
public void onClick(PlayerInteractEvent event) {
|
|
|
|
if (event.getHand() == null || (event.getAction() != Action.RIGHT_CLICK_AIR && event.getAction() !=
|
|
|
|
Action.RIGHT_CLICK_BLOCK)) {
|
|
|
|
return;
|
|
|
|
}
|
2022-07-16 14:56:29 +02:00
|
|
|
//If the player is not looking at a blacksmith, there is no need to do anything
|
2022-08-05 17:07:16 +02:00
|
|
|
Entity target = getTarget(event.getPlayer(), event.getPlayer().getNearbyEntities(15, 10, 15));
|
2022-02-02 00:36:15 +01:00
|
|
|
if (!CitizensAPI.getNPCRegistry().isNPC(target) ||
|
|
|
|
!CitizensAPI.getNPCRegistry().getNPC(target).hasTrait(BlacksmithTrait.class)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-16 14:56:29 +02:00
|
|
|
//Block armor equip if interacting with a blacksmith
|
2022-02-02 00:36:15 +01:00
|
|
|
ItemStack usedItem = event.getPlayer().getInventory().getItem(event.getHand());
|
2022-08-07 01:21:47 +02:00
|
|
|
if (usedItem != null && isArmor(usedItem)) {
|
2022-02-02 00:36:15 +01:00
|
|
|
event.setUseItemInHand(Event.Result.DENY);
|
|
|
|
event.getPlayer().updateInventory();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-01 19:52:41 +01:00
|
|
|
@EventHandler
|
|
|
|
public void onRightClick(net.citizensnpcs.api.event.NPCRightClickEvent event) {
|
|
|
|
if (this.npc != event.getNPC()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-16 14:56:29 +02:00
|
|
|
//Perform any necessary pre-session work
|
2022-02-01 19:52:41 +01:00
|
|
|
Player player = event.getClicker();
|
2022-07-16 14:56:29 +02:00
|
|
|
if (!prepareForSession(player)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (session != null) {
|
|
|
|
//Continue the existing session
|
|
|
|
continueSession(player);
|
|
|
|
} else {
|
|
|
|
//Start a new session
|
|
|
|
startSession(player);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs necessary work before the session is started or continued
|
|
|
|
*
|
|
|
|
* @param player <p>The player to prepare the session for</p>
|
|
|
|
* @return <p>True if preparations were successful. False if a session shouldn't be started</p>
|
|
|
|
*/
|
|
|
|
private boolean prepareForSession(Player player) {
|
|
|
|
//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) {
|
2022-02-01 22:24:39 +01:00
|
|
|
coolDowns.remove(player.getUniqueId());
|
2022-02-01 19:52:41 +01:00
|
|
|
}
|
2022-07-16 14:56:29 +02:00
|
|
|
//Deny if permission is missing
|
2022-02-01 19:52:41 +01:00
|
|
|
if (!player.hasPermission("blacksmith.reforge")) {
|
2022-07-16 14:56:29 +02:00
|
|
|
return false;
|
2022-02-01 19:52:41 +01:00
|
|
|
}
|
2022-07-16 14:56:29 +02:00
|
|
|
//Deny if on cool-down, or remove cool-down if expired
|
2022-02-01 22:24:39 +01:00
|
|
|
if (coolDowns.get(player.getUniqueId()) != null) {
|
|
|
|
if (!Calendar.getInstance().after(coolDowns.get(player.getUniqueId()))) {
|
|
|
|
player.sendMessage(config.getCoolDownUnexpiredMessage());
|
2022-07-16 14:56:29 +02:00
|
|
|
return false;
|
2022-02-01 19:52:41 +01:00
|
|
|
}
|
2022-02-01 22:24:39 +01:00
|
|
|
coolDowns.remove(player.getUniqueId());
|
2022-02-01 19:52:41 +01:00
|
|
|
}
|
|
|
|
|
2022-07-16 14:56:29 +02:00
|
|
|
//If already in a session, but the player has failed to interact, or left the blacksmith, allow a new session
|
2022-02-01 19:52:41 +01:00
|
|
|
if (session != null) {
|
2022-02-02 00:36:15 +01:00
|
|
|
if (System.currentTimeMillis() > _sessionStart + 10 * 1000 ||
|
|
|
|
this.npc.getEntity().getLocation().distance(session.getPlayer().getLocation()) > 20) {
|
2022-02-01 19:52:41 +01:00
|
|
|
session = null;
|
|
|
|
}
|
|
|
|
}
|
2022-07-16 14:56:29 +02:00
|
|
|
return true;
|
|
|
|
}
|
2022-02-01 19:52:41 +01:00
|
|
|
|
2022-07-16 14:56:29 +02:00
|
|
|
/**
|
|
|
|
* Tries to continue the session for the given player
|
|
|
|
*
|
|
|
|
* @param player <p>The player to continue the session for</p>
|
|
|
|
*/
|
|
|
|
private void continueSession(Player player) {
|
|
|
|
//Another player is using the blacksmith
|
|
|
|
if (!session.isInSession(player)) {
|
|
|
|
player.sendMessage(config.getBusyWithPlayerMessage());
|
|
|
|
return;
|
|
|
|
}
|
2022-02-01 19:52:41 +01:00
|
|
|
|
2022-07-16 14:56:29 +02:00
|
|
|
//The blacksmith is already reforging for the player
|
|
|
|
if (session.isRunning()) {
|
|
|
|
player.sendMessage(config.getBusyReforgingMessage());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (session.endSession()) {
|
|
|
|
//Quit if the player cannot afford, or has changed their item
|
|
|
|
session = null;
|
2022-02-01 19:52:41 +01:00
|
|
|
} else {
|
2022-07-16 14:56:29 +02:00
|
|
|
//Start reforging for the player
|
|
|
|
reforge(npc, player);
|
2022-02-01 19:52:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-16 14:56:29 +02:00
|
|
|
/**
|
|
|
|
* Starts a new session, and prepares to repair the player's item
|
|
|
|
*
|
|
|
|
* @param player <p>The player to start the session for</p>
|
|
|
|
*/
|
|
|
|
private void startSession(Player player) {
|
|
|
|
ItemStack hand = player.getInventory().getItemInMainHand();
|
2022-08-07 01:21:47 +02:00
|
|
|
//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()))) {
|
2022-07-16 14:56:29 +02:00
|
|
|
player.sendMessage(config.getInvalidItemMessage());
|
|
|
|
return;
|
2022-02-01 19:52:41 +01:00
|
|
|
}
|
|
|
|
|
2022-07-16 14:56:29 +02:00
|
|
|
//Start a new reforge session for the player
|
|
|
|
_sessionStart = System.currentTimeMillis();
|
|
|
|
session = new ReforgeSession(this, player, npc, config);
|
|
|
|
|
|
|
|
//Tell the player the cost of repairing the item
|
2022-08-07 01:21:47 +02:00
|
|
|
String cost = EconomyManager.formatCost(player);
|
2022-07-16 14:56:29 +02:00
|
|
|
String itemName = hand.getType().name().toLowerCase().replace('_', ' ');
|
|
|
|
player.sendMessage(config.getCostMessage().replace("<price>", cost).replace("<item>", itemName));
|
2022-02-01 19:52:41 +01:00
|
|
|
}
|
|
|
|
|
2022-07-16 14:56:29 +02:00
|
|
|
/**
|
|
|
|
* Starts reforging the player's item
|
|
|
|
*
|
|
|
|
* @param npc <p>The NPC performing the reforge</p>
|
|
|
|
* @param player <p>The player that initiated the reforge</p>
|
|
|
|
*/
|
2022-02-01 19:52:41 +01:00
|
|
|
private void reforge(NPC npc, Player player) {
|
2022-02-01 22:24:39 +01:00
|
|
|
player.sendMessage(config.getStartReforgeMessage());
|
2022-08-07 01:21:47 +02:00
|
|
|
EconomyManager.withdraw(player);
|
2022-02-01 19:52:41 +01:00
|
|
|
session.beginReforge();
|
2022-07-16 14:56:29 +02:00
|
|
|
ItemStack heldItem = player.getInventory().getItemInMainHand();
|
|
|
|
|
|
|
|
//Display the item in the NPC's hand
|
2022-02-01 19:52:41 +01:00
|
|
|
if (npc.getEntity() instanceof Player) {
|
2022-07-16 14:56:29 +02:00
|
|
|
((Player) npc.getEntity()).getInventory().setItemInMainHand(heldItem);
|
2022-02-01 19:52:41 +01:00
|
|
|
} else {
|
2022-07-16 14:56:29 +02:00
|
|
|
Objects.requireNonNull(((LivingEntity) npc.getEntity()).getEquipment()).setItemInMainHand(heldItem);
|
2022-02-01 19:52:41 +01:00
|
|
|
}
|
2022-07-16 14:56:29 +02:00
|
|
|
//Remove the item from the player's inventory
|
2022-02-01 19:52:41 +01:00
|
|
|
player.getInventory().setItemInMainHand(null);
|
|
|
|
}
|
|
|
|
|
2022-07-16 14:56:29 +02:00
|
|
|
/**
|
|
|
|
* Gets the target-entity the entity is looking at
|
|
|
|
*
|
|
|
|
* @param entity <p>The entity looking at something</p>
|
|
|
|
* @param entities <p>Entities near the player</p>
|
|
|
|
* @param <T> <p>The type of entity the player is looking at</p>
|
|
|
|
* @return <p>The entity the player is looking at, or null if no such entity exists</p>
|
|
|
|
*/
|
|
|
|
private static <T extends Entity> T getTarget(final Entity entity, final Iterable<T> entities) {
|
|
|
|
if (entity == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
T target = null;
|
|
|
|
final double threshold = 1;
|
|
|
|
for (final T other : entities) {
|
|
|
|
final Vector n = other.getLocation().toVector().subtract(entity.getLocation().toVector());
|
|
|
|
if (entity.getLocation().getDirection().normalize().crossProduct(n).lengthSquared() < threshold &&
|
|
|
|
n.normalize().dot(entity.getLocation().getDirection().normalize()) >= 0) {
|
|
|
|
if (target == null ||
|
|
|
|
target.getLocation().distanceSquared(entity.getLocation()) >
|
|
|
|
other.getLocation().distanceSquared(entity.getLocation())) {
|
|
|
|
target = other;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
2022-08-07 01:21:47 +02:00
|
|
|
/**
|
|
|
|
* Gets whether the given item is repairable
|
|
|
|
*
|
|
|
|
* @param item <p>The item to check</p>
|
|
|
|
* @return <p>True if the item is repairable</p>
|
|
|
|
*/
|
|
|
|
private static boolean isRepairable(ItemStack item) {
|
|
|
|
return item.getItemMeta() instanceof Damageable;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets whether the given item is a type of armor
|
|
|
|
*
|
|
|
|
* @param item <p>The item to check if is armor or not</p>
|
|
|
|
* @return <p>True if the given item is a type of armor</p>
|
|
|
|
*/
|
|
|
|
public static boolean isArmor(ItemStack item) {
|
|
|
|
return EnchantmentTarget.WEARABLE.includes(item);
|
|
|
|
//TODO: Remove this commented-out code if the above line works
|
|
|
|
/*return switch (item.getType()) {
|
|
|
|
case LEATHER_HELMET, LEATHER_CHESTPLATE, LEATHER_LEGGINGS, LEATHER_BOOTS, CHAINMAIL_HELMET,
|
|
|
|
CHAINMAIL_CHESTPLATE, CHAINMAIL_LEGGINGS, CHAINMAIL_BOOTS, GOLDEN_HELMET, GOLDEN_CHESTPLATE,
|
|
|
|
GOLDEN_LEGGINGS, GOLDEN_BOOTS, IRON_HELMET, IRON_CHESTPLATE, IRON_LEGGINGS, IRON_BOOTS,
|
|
|
|
DIAMOND_HELMET, DIAMOND_CHESTPLATE, DIAMOND_LEGGINGS, DIAMOND_BOOTS, TURTLE_HELMET, ELYTRA,
|
|
|
|
NETHERITE_HELMET, NETHERITE_CHESTPLATE, NETHERITE_LEGGINGS, NETHERITE_BOOTS -> true;
|
|
|
|
default -> false;
|
|
|
|
};*/
|
|
|
|
}
|
|
|
|
|
2017-07-31 11:53:59 +02:00
|
|
|
}
|