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
|
|
|
|
|
|
|
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-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-05-25 20:22:06 +02:00
|
|
|
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; }
|
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;
|
|
|
|
_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()
|
|
|
|
{
|
|
|
|
// Concatenate attribute prefixes/suffixes to display name.
|
|
|
|
String display = _displayName;
|
|
|
|
|
2015-05-25 20:22:06 +02:00
|
|
|
if (_prefix != null)
|
2015-05-05 21:33:42 +02:00
|
|
|
{
|
2015-05-25 20:22:06 +02:00
|
|
|
display = _prefix.getDisplayName() + " " + display;
|
2015-05-05 21:33:42 +02:00
|
|
|
}
|
|
|
|
|
2015-05-25 20:22:06 +02:00
|
|
|
if (_superPrefix != null)
|
2015-05-05 21:33:42 +02:00
|
|
|
{
|
2015-05-25 20:22:06 +02:00
|
|
|
display = _superPrefix.getDisplayName() + " " + display;
|
2015-05-05 21:33:42 +02:00
|
|
|
}
|
|
|
|
|
2015-05-25 20:22:06 +02:00
|
|
|
if (_suffix != null)
|
2015-05-05 21:33:42 +02:00
|
|
|
{
|
2015-05-25 20:22:06 +02:00
|
|
|
display += " of " + _suffix.getDisplayName();
|
2015-05-05 21:33:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return display;
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
{
|
2015-05-25 20:22:06 +02:00
|
|
|
String serialization = GearManager.getItemSerialization(this);
|
|
|
|
|
2015-05-05 21:33:42 +02:00
|
|
|
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
|
|
|
|
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);
|
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-05-25 20:22:06 +02:00
|
|
|
System.out.println("Triggered interact!");
|
2015-05-05 21:33:42 +02:00
|
|
|
for (ItemAttribute attribute : getAttributes())
|
|
|
|
{
|
|
|
|
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-05-25 20:22:06 +02:00
|
|
|
System.out.println("Triggered attack!");
|
2015-05-05 21:33:42 +02:00
|
|
|
for (ItemAttribute attribute : getAttributes())
|
|
|
|
{
|
|
|
|
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-05-25 20:22:06 +02:00
|
|
|
System.out.println("Triggered damage!");
|
2015-05-05 21:33:42 +02:00
|
|
|
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>();
|
2015-05-25 20:22:06 +02:00
|
|
|
if (_superPrefix != null) attributes.add(_superPrefix);
|
|
|
|
if (_prefix != null) attributes.add(_prefix);
|
|
|
|
if (_suffix != null) attributes.add(_suffix);
|
2015-05-05 21:33:42 +02:00
|
|
|
return attributes;
|
|
|
|
}
|
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-01 18:25:20 +02:00
|
|
|
|
|
|
|
public void addAttribute(ItemAttribute attribute)
|
|
|
|
{
|
|
|
|
if (_superPrefix == null)
|
|
|
|
{
|
|
|
|
_superPrefix = attribute;
|
|
|
|
}
|
|
|
|
else if (_prefix == null)
|
|
|
|
{
|
|
|
|
_prefix = attribute;
|
|
|
|
}
|
|
|
|
else if (_suffix == null)
|
|
|
|
{
|
|
|
|
_suffix = attribute;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|