2015-05-05 21:33:42 +02:00
|
|
|
package mineplex.game.clans.items;
|
|
|
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Set;
|
|
|
|
|
2015-06-01 18:25:20 +02:00
|
|
|
import mineplex.game.clans.items.legendaries.LegendaryItem;
|
|
|
|
import mineplex.minecraft.game.core.damage.CustomDamageEvent;
|
|
|
|
|
2015-05-05 21:33:42 +02:00
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
|
|
|
import org.bukkit.event.player.PlayerInteractEvent;
|
2015-05-25 20:22:06 +02:00
|
|
|
import org.bukkit.event.player.PlayerItemHeldEvent;
|
2015-05-05 21:33:42 +02:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
2015-05-25 20:22:06 +02:00
|
|
|
import org.bukkit.inventory.PlayerInventory;
|
2015-05-05 21:33:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2015-07-20 22:57:57 +02:00
|
|
|
public String getPlayerName() { return _playerName; }
|
2015-05-05 21:33:42 +02:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
2015-06-01 18:25:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
}
|
2015-05-05 21:33:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the {@link Player} that owns this gear set.
|
|
|
|
*/
|
|
|
|
public Player getPlayer()
|
|
|
|
{
|
|
|
|
return Bukkit.getPlayer(_playerName);
|
|
|
|
}
|
|
|
|
|
2015-05-25 20:22:06 +02:00
|
|
|
/**
|
|
|
|
* @return the {@link PlayerInventory} associated with the owner of this {@link PlayerGear}.
|
|
|
|
*/
|
|
|
|
public PlayerInventory getInventory()
|
|
|
|
{
|
|
|
|
return getPlayer().getInventory();
|
|
|
|
}
|
|
|
|
|
2015-05-05 21:33:42 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2015-06-01 18:25:20 +02:00
|
|
|
public void onAttack(CustomDamageEvent event)
|
2015-05-05 21:33:42 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
|
*/
|
2015-06-01 18:25:20 +02:00
|
|
|
public void onAttacked(CustomDamageEvent event)
|
2015-05-05 21:33:42 +02:00
|
|
|
{
|
|
|
|
for (CustomItem item : getGear())
|
|
|
|
{
|
|
|
|
item.onAttacked(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-25 20:22:06 +02:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-05 21:33:42 +02:00
|
|
|
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>();
|
2015-05-25 20:22:06 +02:00
|
|
|
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());
|
2015-05-05 21:33:42 +02:00
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean itemsMatch(CustomItem customItem, ItemStack item)
|
|
|
|
{
|
|
|
|
if (customItem == null || item == null) return false;
|
|
|
|
|
2015-05-25 20:22:06 +02:00
|
|
|
if (GearManager.isCustomItem(item))
|
|
|
|
{
|
|
|
|
CustomItem customItem2 = GearManager.parseItem(item);
|
|
|
|
return customItem2.matches(customItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2015-05-05 21:33:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private CustomItem parseItem(ItemStack item)
|
|
|
|
{
|
|
|
|
return GearManager.parseItem(item);
|
|
|
|
}
|
|
|
|
}
|