170 lines
3.5 KiB
Java
170 lines
3.5 KiB
Java
|
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.entity.EntityDamageByEntityEvent;
|
||
|
import org.bukkit.event.player.PlayerInteractEvent;
|
||
|
import org.bukkit.inventory.ItemStack;
|
||
|
|
||
|
/**
|
||
|
* 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;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @return the {@link Player} that owns this gear set.
|
||
|
*/
|
||
|
public Player getPlayer()
|
||
|
{
|
||
|
return Bukkit.getPlayer(_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(EntityDamageByEntityEvent 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(EntityDamageByEntityEvent event)
|
||
|
{
|
||
|
for (CustomItem item : getGear())
|
||
|
{
|
||
|
item.onAttacked(event);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
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<CustomItem> getGear()
|
||
|
{
|
||
|
Set<CustomItem> items = new HashSet<CustomItem>();
|
||
|
items.add(getWeapon());
|
||
|
items.add(getHelmet());
|
||
|
items.add(getChestplate());
|
||
|
items.add(getLeggings());
|
||
|
items.add(getBoots());
|
||
|
return items;
|
||
|
}
|
||
|
|
||
|
private boolean itemsMatch(CustomItem customItem, ItemStack item)
|
||
|
{
|
||
|
if (customItem == null || item == null) return false;
|
||
|
|
||
|
// TODO: Implement more sophisticated match-checking that checks hidden UUID
|
||
|
return customItem.getDisplayName().equals(item.getItemMeta().getDisplayName());
|
||
|
}
|
||
|
|
||
|
private CustomItem parseItem(ItemStack item)
|
||
|
{
|
||
|
return GearManager.parseItem(item);
|
||
|
}
|
||
|
}
|