package mineplex.game.clans.items; import java.util.HashSet; import java.util.Set; import mineplex.game.clans.items.legendaries.LegendaryItem; import mineplex.minecraft.game.core.damage.CustomDamageEvent; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerItemHeldEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; /** * PlayerGear caches and manages a players set of {@link CustomItem}s that they * currently wield. * * @author MrTwiggy * */ public class PlayerGear { private String _playerName; // Name of player who owns the gear // Cached custom item information for player's gear private CustomItem weapon; private CustomItem helmet; private CustomItem chestplate; private CustomItem leggings; private CustomItem boots; /** * Class constructor * * @param playerName */ public PlayerGear(String playerName) { _playerName = playerName; } /** * Tick & update internal logic for the PlayerGear and required custom items * that are equipped. */ public void update() { if (isOnline()) { CustomItem item = getWeapon(); if (item != null && item instanceof LegendaryItem) { LegendaryItem legendary = (LegendaryItem) item; legendary.update(getPlayer()); } } } public boolean isOnline() { Player player = getPlayer(); return player != null && player.isOnline(); } /** * @return the {@link Player} that owns this gear set. */ public Player getPlayer() { return Bukkit.getPlayer(_playerName); } /** * @return the {@link PlayerInventory} associated with the owner of this * {@link PlayerGear}. */ public PlayerInventory getInventory() { return getPlayer().getInventory(); } public String getPlayerName() { return _playerName; } /** * Trigger interact events for the set of equipped {@link CustomItem}s in * gear set. * * @param event - the triggering interact event */ public void onInteract(PlayerInteractEvent event) { for (CustomItem item : getGear()) { item.onInteract(event); } } /** * Trigger on-attack events for the set of equipped {@link CustomItem}s in * gear set. * * @param event - the triggering on-attack event */ public void onAttack(CustomDamageEvent event) { for (CustomItem item : getGear()) { item.onAttack(event); } } /** * Trigger attacked events for the set of equipped {@link CustomItem}s in * gear set. * * @param event - the triggering attacked event */ public void onAttacked(CustomDamageEvent event) { for (CustomItem item : getGear()) { item.onAttacked(event); } } /** * Update appropriate gear status and item lores. * * @param event - the triggering item held change event. */ public void onItemHeldChanged(PlayerItemHeldEvent event) { ItemStack item = getPlayer().getItemInHand(); CustomItem weapon = getWeapon(); if (weapon != null) { weapon.update(item); // Update held-item's stats. } } public CustomItem getWeapon() { ItemStack weaponItem = getPlayer().getInventory().getItemInHand(); if (!itemsMatch(weapon, weaponItem)) { weapon = parseItem(weaponItem); } return weapon; } public CustomItem getHelmet() { ItemStack helmetItem = getPlayer().getInventory().getHelmet(); if (!itemsMatch(helmet, helmetItem)) { helmet = parseItem(helmetItem); } return helmet; } public CustomItem getChestplate() { ItemStack chestplateItem = getPlayer().getInventory().getChestplate(); if (!itemsMatch(chestplate, chestplateItem)) { chestplate = parseItem(chestplateItem); } return chestplate; } public CustomItem getLeggings() { ItemStack leggingsItem = getPlayer().getInventory().getLeggings(); if (!itemsMatch(leggings, leggingsItem)) { leggings = parseItem(leggingsItem); } return leggings; } public CustomItem getBoots() { ItemStack bootsItem = getPlayer().getInventory().getBoots(); if (!itemsMatch(boots, bootsItem)) { boots = parseItem(bootsItem); } return boots; } /** * @return set of currently equipped {@link CustomItem}s in the gear set. */ public Set getGear() { Set items = new HashSet(); if (getWeapon() != null) items.add(getWeapon()); if (getHelmet() != null) items.add(getHelmet()); if (getChestplate() != null) items.add(getChestplate()); if (getLeggings() != null) items.add(getLeggings()); if (getBoots() != null) items.add(getBoots()); return items; } private boolean itemsMatch(CustomItem customItem, ItemStack item) { if (customItem == null || item == null) return false; if (GearManager.isCustomItem(item)) { CustomItem customItem2 = GearManager.parseItem(item); return customItem2.matches(customItem); } return false; } private CustomItem parseItem(ItemStack item) { return GearManager.parseItem(item); } }