Let ShopItem no longer extend CraftItemStack and instead extend ItemStack to remain useful to other plugins and use ItemMeta

This commit is contained in:
libraryaddict 2014-11-29 07:21:37 +13:00
parent d5d9e2eb14
commit e56970a212

View File

@ -1,15 +1,16 @@
package mineplex.core.shop.item; package mineplex.core.shop.item;
import org.bukkit.ChatColor; import java.util.ArrayList;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.craftbukkit.v1_7_R4.inventory.CraftItemStack; import org.bukkit.craftbukkit.v1_7_R4.inventory.CraftItemStack;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import net.minecraft.server.v1_7_R4.NBTTagList; import org.bukkit.inventory.meta.ItemMeta;
import net.minecraft.server.v1_7_R4.NBTTagString;
import mineplex.core.common.util.C;
import mineplex.core.common.util.UtilInv; import mineplex.core.common.util.UtilInv;
public class ShopItem extends CraftItemStack public class ShopItem extends ItemStack
{ {
protected String _name; protected String _name;
private String _deliveryName; private String _deliveryName;
@ -31,11 +32,11 @@ public class ShopItem extends CraftItemStack
else else
_lore = new String[0]; _lore = new String[0];
CraftItemStack craftItem = CraftItemStack.asCraftCopy(itemStack);
getHandle().tag = craftItem.getHandle().tag;
UpdateVisual(true); UpdateVisual(true);
getHandle().tag.set("AttributeModifiers", new NBTTagList()); }
public net.minecraft.server.v1_7_R4.ItemStack getHandle() {
return CraftItemStack.asNMSCopy(this);
} }
public ShopItem(Material type, String name, int deliveryAmount, boolean locked) public ShopItem(Material type, String name, int deliveryAmount, boolean locked)
@ -96,8 +97,7 @@ public class ShopItem extends CraftItemStack
UpdateVisual(false); UpdateVisual(false);
getHandle().tag.setByte("Count", (byte)Math.max(deliveryAmount, 1)); setAmount(Math.max(deliveryAmount, 1));
getHandle().tag.set("AttributeModifiers", new NBTTagList());
} }
public boolean IsLocked() public boolean IsLocked()
@ -111,7 +111,11 @@ public class ShopItem extends CraftItemStack
//Delivery Name //Delivery Name
if (_deliveryName != null) if (_deliveryName != null)
this.getHandle().c(_deliveryName); {
ItemMeta meta = getItemMeta();
meta.setDisplayName(_deliveryName);
setItemMeta(meta);
}
} }
public ShopItem clone() public ShopItem clone()
@ -122,43 +126,30 @@ public class ShopItem extends CraftItemStack
@Override @Override
public boolean equals(Object obj) public boolean equals(Object obj)
{ {
if (!super.equals(obj)) return super.equals(obj);
{
return false;
}
net.minecraft.server.v1_7_R4.ItemStack original = ((CraftItemStack)this).getHandle();
net.minecraft.server.v1_7_R4.ItemStack comparison = ((CraftItemStack)obj).getHandle();
return original.tag == null || original.tag.equals(comparison.tag);
} }
protected void UpdateVisual(boolean clone) protected void UpdateVisual(boolean clone)
{ {
ItemMeta meta = getItemMeta();
if (!clone) if (!clone)
{ {
if (_locked && !_displayItem) meta.setDisplayName((_locked && !_displayItem? C.cRed:C.cGreen) + C.Bold + _name);
{
this.getHandle().c(ChatColor.RED + "§l" + _name);
}
else
{
this.getHandle().c(ChatColor.GREEN + "§l" + _name);
}
} }
NBTTagList lore = new NBTTagList(); ArrayList<String> lore = new ArrayList<String>();
if (_lore != null) if (_lore != null)
{ {
for (String line : _lore) for (String line : _lore)
{ {
if (line != null && !line.isEmpty()) if (line != null && !line.isEmpty())
lore.add(new NBTTagString(line)); lore.add(line);
} }
} }
meta.setLore(lore);;
getHandle().tag.getCompound("display").set("Lore", lore); setItemMeta(meta);
} }
public boolean IsDisplay() public boolean IsDisplay()
@ -191,17 +182,19 @@ public class ShopItem extends CraftItemStack
{ {
_lore = string; _lore = string;
NBTTagList lore = new NBTTagList(); ArrayList<String> lore = new ArrayList<String>();
if (_lore != null) if (_lore != null)
{ {
for (String line : _lore) for (String line : _lore)
{ {
if (line != null && !line.isEmpty()) if (line != null && !line.isEmpty())
lore.add(new NBTTagString(line)); lore.add(line);
} }
} }
getHandle().tag.getCompound("display").set("Lore", lore); ItemMeta meta = getItemMeta();
meta.setLore(lore);
setItemMeta(meta);
} }
} }