package mineplex.game.clans.items; import java.util.HashSet; import java.util.Set; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.HandlerList; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerItemHeldEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; import mineplex.core.common.util.C; import mineplex.core.common.util.F; import mineplex.core.common.util.UtilServer; import mineplex.core.common.util.UtilTextMiddle; import mineplex.game.clans.items.legendaries.LegendaryItem; import mineplex.minecraft.game.core.damage.CustomDamageEvent; /** * 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.preUpdate(getPlayer()); legendary.update(getPlayer()); if (legendary.OriginalOwner == null) { legendary.OriginalOwner = getPlayer().getUniqueId().toString(); getPlayer().setItemInHand(legendary.toItemStack()); } } } } 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); } }