2015-05-05 21:33:42 +02:00
|
|
|
package mineplex.game.clans.items;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
2015-05-25 20:22:06 +02:00
|
|
|
import java.util.UUID;
|
2015-05-05 21:33:42 +02:00
|
|
|
|
2015-06-22 22:20:20 +02:00
|
|
|
import mineplex.game.clans.items.attributes.AttributeContainer;
|
2015-05-05 21:33:42 +02:00
|
|
|
import mineplex.game.clans.items.attributes.ItemAttribute;
|
2015-06-01 18:25:20 +02:00
|
|
|
import mineplex.minecraft.game.core.damage.CustomDamageEvent;
|
2015-05-05 21:33:42 +02:00
|
|
|
|
2015-07-20 22:57:57 +02:00
|
|
|
import org.bukkit.ChatColor;
|
2015-05-25 20:22:06 +02:00
|
|
|
import org.bukkit.Material;
|
2015-05-05 21:33:42 +02:00
|
|
|
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
|
|
|
import org.bukkit.event.player.PlayerInteractEvent;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
2015-05-25 20:22:06 +02:00
|
|
|
import org.bukkit.inventory.meta.ItemMeta;
|
2015-05-05 21:33:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a customizable wrapper for an {@link ItemStack}, enabling the possession
|
|
|
|
* of special abilities, attributes, and triggers on item.
|
|
|
|
* @author MrTwiggy
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public class CustomItem
|
|
|
|
{
|
|
|
|
|
2015-07-20 22:57:57 +02:00
|
|
|
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
|
|
|
|
|
2015-06-29 22:38:51 +02:00
|
|
|
private AttributeContainer _attributes;
|
|
|
|
public AttributeContainer getAttributes() { return _attributes; }
|
2015-05-05 21:33:42 +02:00
|
|
|
|
2015-06-01 18:25:20 +02:00
|
|
|
protected String _displayName;
|
2015-05-25 20:22:06 +02:00
|
|
|
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;
|
2015-08-27 23:42:07 +02:00
|
|
|
_attributes = new AttributeContainer();
|
2015-05-25 20:22:06 +02:00
|
|
|
_uuid = UUID.randomUUID().toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
public CustomItem(Material material)
|
|
|
|
{
|
2015-06-01 21:51:22 +02:00
|
|
|
this(prettifyName(material), null, material);
|
2015-05-25 20:22:06 +02:00
|
|
|
}
|
2015-05-05 21:33:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the name displayed to players for the item.
|
|
|
|
*/
|
|
|
|
public String getDisplayName()
|
|
|
|
{
|
2015-07-20 22:57:57 +02:00
|
|
|
return ChatColor.RESET.toString() + TITLE_COLOR + _attributes.formatItemName(_displayName);
|
2015-05-05 21:33:42 +02:00
|
|
|
}
|
|
|
|
|
2015-06-01 21:51:22 +02:00
|
|
|
public String getDescription()
|
|
|
|
{
|
|
|
|
return _description;
|
|
|
|
}
|
|
|
|
|
2015-05-05 21:33:42 +02:00
|
|
|
public List<String> getLore()
|
|
|
|
{
|
|
|
|
List<String> lore = new ArrayList<String>();
|
|
|
|
|
2015-06-01 21:51:22 +02:00
|
|
|
if (getDescription() != null)
|
2015-05-25 20:22:06 +02:00
|
|
|
{
|
2015-06-01 21:51:22 +02:00
|
|
|
lore.add(getDescription());
|
2015-05-25 20:22:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Display attribute descriptions and stats in lore
|
2015-06-29 22:38:51 +02:00
|
|
|
for (ItemAttribute attribute : _attributes.getAttributes())
|
2015-05-25 20:22:06 +02:00
|
|
|
{
|
2015-09-29 23:36:42 +02:00
|
|
|
String attributeLine = ATTRIBUTE_COLOR + "• " + attribute.getDescription();
|
2015-05-25 20:22:06 +02:00
|
|
|
lore.add(attributeLine);
|
|
|
|
}
|
|
|
|
|
2015-07-20 22:57:57 +02:00
|
|
|
// Add encoded JSON form of this item, not seen by user.
|
|
|
|
String serialization = GearManager.getItemSerialization(this);
|
|
|
|
lore.add(serialization);
|
2015-05-05 21:33:42 +02:00
|
|
|
|
|
|
|
return lore;
|
|
|
|
}
|
|
|
|
|
2015-05-25 20:22:06 +02:00
|
|
|
public ItemStack toItemStack(int amount)
|
2015-05-05 21:33:42 +02:00
|
|
|
{
|
2015-05-25 20:22:06 +02:00
|
|
|
ItemStack item = new ItemStack(_material, amount);
|
|
|
|
update(item);
|
|
|
|
|
|
|
|
// TODO: Add non-descript enchantment for glowing efect?
|
|
|
|
return item;
|
2015-05-05 21:33:42 +02:00
|
|
|
}
|
|
|
|
|
2015-06-01 18:25:20 +02:00
|
|
|
public ItemStack toItemStack()
|
|
|
|
{
|
|
|
|
return toItemStack(1);
|
|
|
|
}
|
|
|
|
|
2015-05-05 21:33:42 +02:00
|
|
|
public void onInteract(PlayerInteractEvent event)
|
|
|
|
{
|
2015-06-29 22:38:51 +02:00
|
|
|
for (ItemAttribute attribute : _attributes.getAttributes())
|
2015-05-05 21:33:42 +02:00
|
|
|
{
|
|
|
|
attribute.onInteract(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-01 18:25:20 +02:00
|
|
|
public void onAttack(CustomDamageEvent event)
|
2015-05-05 21:33:42 +02:00
|
|
|
{
|
2015-06-29 22:38:51 +02:00
|
|
|
for (ItemAttribute attribute : _attributes.getAttributes())
|
2015-05-05 21:33:42 +02:00
|
|
|
{
|
|
|
|
attribute.onAttack(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-01 18:25:20 +02:00
|
|
|
public void onAttacked(CustomDamageEvent event)
|
2015-05-05 21:33:42 +02:00
|
|
|
{
|
2015-06-29 22:38:51 +02:00
|
|
|
for (ItemAttribute attribute : _attributes.getAttributes())
|
2015-05-05 21:33:42 +02:00
|
|
|
{
|
|
|
|
attribute.onAttacked(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-25 20:22:06 +02:00
|
|
|
/**
|
|
|
|
* @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);
|
|
|
|
}
|
2015-06-22 22:20:20 +02:00
|
|
|
|
2015-06-01 21:51:22 +02:00
|
|
|
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;
|
|
|
|
}
|
2015-05-05 21:33:42 +02:00
|
|
|
}
|