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

214 lines
4.6 KiB
Java

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.core.common.util.UtilInv;
import mineplex.game.clans.items.attributes.AttributeContainer;
import mineplex.game.clans.items.attributes.ItemAttribute;
import mineplex.minecraft.game.core.damage.CustomDamageEvent;
import org.bukkit.ChatColor;
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 static final ChatColor TITLE_COLOR = ChatColor.GOLD; // Chat color
// used for
// item
// display
// name
private static final ChatColor ATTRIBUTE_COLOR = ChatColor.WHITE; // Chat
// color
// used
// for
// attribute
// descriptions
private AttributeContainer _attributes;
public AttributeContainer getAttributes()
{
return _attributes;
}
protected String _displayName;
private String _description;
private Material _material;
private String _uuid;
private boolean _dullEnchantment;
public String getUuid()
{
return _uuid;
}
public CustomItem(String displayName, String description, Material material)
{
_displayName = displayName;
_description = description;
_material = material;
_attributes = new AttributeContainer();
_uuid = UUID.randomUUID().toString();
}
public CustomItem(Material material)
{
this(prettifyName(material), null, material);
}
/**
* @return the name displayed to players for the item.
*/
public String getDisplayName()
{
return ChatColor.RESET.toString() + TITLE_COLOR + _attributes.formatItemName(_displayName);
}
public String getDescription()
{
return _description;
}
public List<String> getLore()
{
List<String> lore = new ArrayList<String>();
if (getDescription() != null)
{
lore.add(ATTRIBUTE_COLOR + getDescription());
}
// Display attribute descriptions and stats in lore
for (ItemAttribute attribute : _attributes.getAttributes())
{
String attributeLine = ATTRIBUTE_COLOR + "" + attribute.getDescription();
lore.add(attributeLine);
}
// Add encoded JSON form of this item, not seen by user.
String serialization = GearManager.getItemSerialization(this);
lore.add(serialization);
return lore;
}
public ItemStack toItemStack(int amount)
{
ItemStack item = new ItemStack(_material, amount);
update(item);
if (_dullEnchantment)
{
UtilInv.addDullEnchantment(item);
}
return item;
}
public ItemStack toItemStack()
{
return toItemStack(1);
}
public void addDullEnchantment()
{
_dullEnchantment = true;
}
public void removeDullEnchantment()
{
_dullEnchantment = false;
}
public void onInteract(PlayerInteractEvent event)
{
for (ItemAttribute attribute : _attributes.getAttributes())
{
attribute.onInteract(event);
}
}
public void onAttack(CustomDamageEvent event)
{
for (ItemAttribute attribute : _attributes.getAttributes())
{
attribute.onAttack(event);
}
}
public void onAttacked(CustomDamageEvent event)
{
for (ItemAttribute attribute : _attributes.getAttributes())
{
attribute.onAttacked(event);
}
}
/**
* @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);
}
public static String prettifyName(Material material)
{
String name = "";
String[] words = material.toString().split("_");
for (String word : words)
{
word = word.toLowerCase();
name += word.substring(0, 1).toUpperCase() + word.substring(1) + " ";
}
return name;
}
public Material getMaterial()
{
return _material;
}
}