Mineplex2018-withcommit/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/items/CustomItem.java

186 lines
4.5 KiB
Java
Raw Normal View History

package mineplex.game.clans.items;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import mineplex.game.clans.items.attributes.ItemAttribute;
import org.bukkit.Material;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
/**
* Represents a customizable wrapper for an {@link ItemStack}, enabling the possession
* of special abilities, attributes, and triggers on item.
* @author MrTwiggy
*
*/
public class CustomItem
{
private ItemAttribute _superPrefix;
public void setSuperPrefix(ItemAttribute attribute) { _superPrefix = attribute; }
private ItemAttribute _prefix;
public void setPrefix(ItemAttribute attribute) { _prefix = attribute; }
private ItemAttribute _suffix;
public void setSuffix(ItemAttribute attribute) { _suffix = attribute; }
private String _displayName;
private String _description;
private Material _material;
private String _uuid;
public String getUuid() { return _uuid; }
public CustomItem(String displayName, String description, Material material)
{
_displayName = displayName;
_description = description;
_material = material;
_uuid = UUID.randomUUID().toString();
}
public CustomItem(Material material)
{
this(material.toString(), null, material); // TODO: Prettify item materal name
}
/**
* @return the name displayed to players for the item.
*/
public String getDisplayName()
{
// Concatenate attribute prefixes/suffixes to display name.
String display = _displayName;
if (_prefix != null)
{
display = _prefix.getDisplayName() + " " + display;
}
if (_superPrefix != null)
{
display = _superPrefix.getDisplayName() + " " + display;
}
if (_suffix != null)
{
display += " of " + _suffix.getDisplayName();
}
return display;
}
public List<String> getLore()
{
String serialization = GearManager.getItemSerialization(this);
List<String> lore = new ArrayList<String>();
if (_description != null)
{
lore.add(_description);
}
// Display attribute descriptions and stats in lore
for (ItemAttribute attribute : getAttributes())
{
String attributeLine = attribute.getDisplayName() + " - " + attribute.getDescription();
lore.add(attributeLine);
}
// Tack on serialized JSON encoded line for utility purposes. (Not seen by user)
List<String> serializedLines = new ArrayList<String>();
String[] seri = serialization.split("\n");
for (String line : seri)
{
serializedLines.add(line);
}
lore.addAll(serializedLines);
return lore;
}
public ItemStack toItemStack(int amount)
{
ItemStack item = new ItemStack(_material, amount);
update(item);
// TODO: Add non-descript enchantment for glowing efect?
return item;
}
public void onInteract(PlayerInteractEvent event)
{
System.out.println("Triggered interact!");
for (ItemAttribute attribute : getAttributes())
{
attribute.onInteract(event);
}
}
public void onAttack(EntityDamageByEntityEvent event)
{
System.out.println("Triggered attack!");
for (ItemAttribute attribute : getAttributes())
{
attribute.onAttack(event);
}
}
public void onAttacked(EntityDamageByEntityEvent event)
{
System.out.println("Triggered damage!");
for (ItemAttribute attribute : getAttributes())
{
attribute.onAttacked(event);
}
}
/**
* @return the (possibly empty) set of {@link ItemAttribute}s attached to this item.
*/
public Set<ItemAttribute> getAttributes()
{
Set<ItemAttribute> attributes = new HashSet<ItemAttribute>();
if (_superPrefix != null) attributes.add(_superPrefix);
if (_prefix != null) attributes.add(_prefix);
if (_suffix != null) attributes.add(_suffix);
return attributes;
}
/**
* @param item - the item to check for a matching link
* @return true, if {@code item} matches this CustomItem via UUID, false otherwise.
*/
public boolean matches(CustomItem item)
{
return item.getUuid().equals(_uuid);
}
/**
* Update {@code item} with the proper meta properties suited for this
* {@link CustomItem}.
* @param item - the item whose meta properties are being updated to become a version of this updated custom item.
*/
public void update(ItemStack item)
{
ItemMeta meta = item.getItemMeta();
String displayName = getDisplayName();
List<String> lore = getLore();
meta.setDisplayName(displayName);
meta.setLore(lore);
item.setItemMeta(meta);
}
}