Skip to content

Commit

Permalink
item meta handling improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Aug 1, 2020
1 parent 2d1a590 commit b59600a
Show file tree
Hide file tree
Showing 36 changed files with 156 additions and 207 deletions.
Expand Up @@ -105,14 +105,14 @@ public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
if (lower.startsWith("result:")) {
String itemText = determination.substring("result:".length());
item = ItemTag.valueOf(itemText, path.container);
event.getItem().setItemMeta(item.getItemStack().getItemMeta());
event.getItem().setItemMeta(item.getItemMeta());
return true;
}
else if (lower.startsWith("enchants:")) {
String itemText = determination.substring("enchants:".length());
ItemTag enchantsRes = ItemTag.valueOf(itemText, path.container);
event.getEnchantsToAdd().clear();
event.getEnchantsToAdd().putAll(enchantsRes.getItemStack().getItemMeta().getEnchants());
event.getEnchantsToAdd().putAll(enchantsRes.getItemMeta().getEnchants());
return true;
}
}
Expand Down
Expand Up @@ -77,7 +77,7 @@ else if (ScriptTag.matches(determination)) {
ScriptTag script = ScriptTag.valueOf(determination, getTagContext(path));
if (script.getContainer() instanceof BookScriptContainer) {
ItemTag dBook = ((BookScriptContainer) script.getContainer()).getBookFrom(getScriptEntryData().getTagContext());
BookMeta bookMeta = (BookMeta) dBook.getItemStack().getItemMeta();
BookMeta bookMeta = (BookMeta) dBook.getItemMeta();
if (dBook.getMaterial().getMaterial() == Material.WRITABLE_BOOK) {
event.setSigning(false);
}
Expand Down
71 changes: 18 additions & 53 deletions plugin/src/main/java/com/denizenscript/denizen/objects/ItemTag.java
Expand Up @@ -208,11 +208,6 @@ public ItemTag(Material material) {
this(new ItemStack(material));
}

private static ItemStack fixQty(ItemStack item, int qty) {
item.setAmount(qty);
return item;
}

public ItemTag(Material material, int qty) {
this(new ItemStack(material, qty));
}
Expand All @@ -238,16 +233,29 @@ public ItemTag(Item item) {
// INSTANCE FIELDS/METHODS
/////////////////

// Bukkit itemstack associated

private ItemStack item;

private ItemMeta meta;

public ItemStack getItemStack() {
return item;
}

public ItemMeta getItemMeta() {
if (meta == null) {
meta = item.getItemMeta();
}
return meta;
}

public void setItemMeta(ItemMeta meta) {
this.meta = meta;
item.setItemMeta(meta);
}

public void setItemStack(ItemStack item) {
this.item = item;
meta = null;
}

// Compare item to item.
Expand Down Expand Up @@ -357,49 +365,6 @@ public int comparesTo(ItemStack compared_to) {
return determination;
}

// Additional helper methods

public void setStackSize(int size) {
getItemStack().setAmount(size);
}

/**
* Check whether this item contains a lore that starts
* with a certain prefix.
*
* @param prefix The prefix
* @return True if it does, otherwise false
*/
public boolean containsLore(String prefix) {

if (getItemStack().hasItemMeta() && getItemStack().getItemMeta().hasLore()) {
for (String itemLore : getItemStack().getItemMeta().getLore()) {
if (itemLore.startsWith(prefix)) {
return true;
}
}
}

return false;
}

/**
* Get the lore from this item that starts with a
* certain prefix.
*
* @param prefix The prefix
* @return String The lore
*/
public String getLore(String prefix) {
for (String itemLore : getItemStack().getItemMeta().getLore()) {
if (itemLore.startsWith(prefix)) {
return itemLore.substring(prefix.length());
}
}

return "";
}

/**
* Check whether this item contains the lore specific
* to item scripts.
Expand Down Expand Up @@ -694,13 +659,13 @@ public static void registerTags() {
if (attribute.getAttribute(2).equals("formatted")) {
return object;
}
if (object.getItemStack().hasItemMeta() && object.getItemStack().getItemMeta() instanceof BlockStateMeta) {
if (object.getItemMeta() instanceof BlockStateMeta) {
if (object.getItemStack().getType() == Material.SHIELD) {
MaterialTag material = new MaterialTag(Material.SHIELD);
material.setModernData(new ModernBlockData(((BlockStateMeta) object.getItemStack().getItemMeta()).getBlockState()));
material.setModernData(new ModernBlockData(((BlockStateMeta) object.getItemMeta()).getBlockState()));
return material;
}
return new MaterialTag(new ModernBlockData(((BlockStateMeta) object.getItemStack().getItemMeta()).getBlockState()));
return new MaterialTag(new ModernBlockData(((BlockStateMeta) object.getItemMeta()).getBlockState()));
}
return object.getMaterial();
});
Expand Down
Expand Up @@ -699,8 +699,8 @@ public static void registerTags() {
// -->
registerTag("item", (attribute, object) -> {
ItemTag item = new ItemTag(object, 1);
if (item.getItemStack().hasItemMeta() && item.getItemStack().getItemMeta() instanceof BlockStateMeta) {
((BlockStateMeta) item.getItemStack().getItemMeta()).setBlockState(object.modernData.getBlockState());
if (item.getItemMeta() instanceof BlockStateMeta) {
((BlockStateMeta) item.getItemMeta()).setBlockState(object.modernData.getBlockState());
}
return item;
});
Expand Down
Expand Up @@ -3283,7 +3283,7 @@ else if (split.length > 1) {
if (mechanism.matches("show_book")
&& mechanism.requireObject(ItemTag.class)) {
ItemTag book = mechanism.valueAsType(ItemTag.class);
if (!book.getItemStack().hasItemMeta() || !(book.getItemStack().getItemMeta() instanceof BookMeta)) {
if (!(book.getItemMeta() instanceof BookMeta)) {
Debug.echoError("show_book mechanism must have a book as input.");
return;
}
Expand Down
Expand Up @@ -92,8 +92,8 @@ public void adjust(Mechanism mechanism) {
// -->
if (mechanism.matches("firework_item") && mechanism.requireObject(ItemTag.class)) {
ItemTag item = mechanism.valueAsType(ItemTag.class);
if (item != null && item.getItemStack().getItemMeta() instanceof FireworkMeta) {
((Firework) firework.getBukkitEntity()).setFireworkMeta((FireworkMeta) item.getItemStack().getItemMeta());
if (item != null && item.getItemMeta() instanceof FireworkMeta) {
((Firework) firework.getBukkitEntity()).setFireworkMeta((FireworkMeta) item.getItemMeta());
}
else {
Debug.echoError("'" + mechanism.getValue().asString() + "' is not a valid firework item.");
Expand Down
Expand Up @@ -47,7 +47,7 @@ private ItemBaseColor(ItemTag item) {
ItemTag item;

private DyeColor getBaseColor() {
ItemMeta itemMeta = item.getItemStack().getItemMeta();
ItemMeta itemMeta = item.getItemMeta();
if (itemMeta instanceof BlockStateMeta) {
if (item.getItemStack().getType() == Material.SHIELD) { // Hack to avoid blank shields misdisplaying as white
if (!itemMeta.serialize().containsKey("internal")) {
Expand Down
Expand Up @@ -13,7 +13,7 @@ public class ItemBlockMaterial implements Property {

public static boolean describes(ObjectTag item) {
return item instanceof ItemTag
&& ((ItemTag) item).getItemStack().getItemMeta() instanceof BlockDataMeta;
&& ((ItemTag) item).getItemMeta() instanceof BlockDataMeta;
}

public static ItemBlockMaterial getFrom(ObjectTag _item) {
Expand Down Expand Up @@ -55,7 +55,7 @@ public ObjectTag getObjectAttribute(Attribute attribute) {
// Returns the material for an item with complex-block-data attached.
// -->
if (attribute.startsWith("block_material")) {
BlockDataMeta meta = (BlockDataMeta) item.getItemStack().getItemMeta();
BlockDataMeta meta = (BlockDataMeta) item.getItemMeta();
if (meta.hasBlockData()) {
return new MaterialTag(new ModernBlockData(meta.getBlockData(item.getItemStack().getType())))
.getObjectAttribute(attribute.fulfill(1));
Expand All @@ -67,7 +67,7 @@ public ObjectTag getObjectAttribute(Attribute attribute) {

@Override
public String getPropertyString() {
BlockDataMeta meta = (BlockDataMeta) item.getItemStack().getItemMeta();
BlockDataMeta meta = (BlockDataMeta) item.getItemMeta();
if (meta.hasBlockData()) {
return new MaterialTag(new ModernBlockData(meta.getBlockData(item.getItemStack().getType()))).identify();
}
Expand All @@ -92,7 +92,7 @@ public void adjust(Mechanism mechanism) {
// <ItemTag.block_material>
// -->
if (mechanism.matches("block_material") && mechanism.requireObject(MaterialTag.class)) {
BlockDataMeta meta = (BlockDataMeta) item.getItemStack().getItemMeta();
BlockDataMeta meta = (BlockDataMeta) item.getItemMeta();
MaterialTag mat = mechanism.valueAsType(MaterialTag.class);
meta.setBlockData(mat.getModernData().data);
item.getItemStack().setItemMeta(meta);
Expand Down
Expand Up @@ -49,7 +49,7 @@ private ItemBook(ItemTag _item) {
}

public BookMeta getBookInfo() {
return (BookMeta) item.getItemStack().getItemMeta();
return (BookMeta) item.getItemMeta();
}

ItemTag item;
Expand Down Expand Up @@ -117,7 +117,7 @@ public ObjectTag getObjectAttribute(Attribute attribute) {

if (attribute.startsWith("book")) {
Deprecations.itemBookTags.warn(attribute.context);
BookMeta bookInfo = (BookMeta) item.getItemStack().getItemMeta();
BookMeta bookInfo = (BookMeta) item.getItemMeta();
attribute = attribute.fulfill(1);

if (item.getItemStack().getType() == Material.WRITTEN_BOOK) {
Expand Down Expand Up @@ -177,7 +177,7 @@ public String getPropertyString() {

public MapTag getBookMap() {
MapTag outMap = new MapTag();
BookMeta bookInfo = (BookMeta) item.getItemStack().getItemMeta();
BookMeta bookInfo = (BookMeta) item.getItemMeta();
if (item.getItemStack().getType().equals(Material.WRITTEN_BOOK) && bookInfo.hasAuthor() && bookInfo.hasTitle()) {
outMap.putObject("author", new ElementTag(bookInfo.getAuthor()));
outMap.putObject("author", new ElementTag(bookInfo.getTitle()));
Expand All @@ -196,7 +196,7 @@ public MapTag getBookMap() {
@Deprecated
public String getOutputString() {
StringBuilder output = new StringBuilder(128);
BookMeta bookInfo = (BookMeta) item.getItemStack().getItemMeta();
BookMeta bookInfo = (BookMeta) item.getItemMeta();
if (item.getItemStack().getType().equals(Material.WRITTEN_BOOK) && bookInfo.hasAuthor() && bookInfo.hasTitle()) {
output.append("author|").append(EscapeTagBase.escape(bookInfo.getAuthor()))
.append("|title|").append(EscapeTagBase.escape(bookInfo.getTitle())).append("|");
Expand All @@ -220,7 +220,7 @@ public void adjust(Mechanism mechanism) {

if (mechanism.matches("book_raw_pages")) {
Deprecations.bookItemRawTags.warn(mechanism.context);
BookMeta meta = (BookMeta) item.getItemStack().getItemMeta();
BookMeta meta = (BookMeta) item.getItemMeta();
ListTag data = mechanism.valueAsType(ListTag.class);
ArrayList<BaseComponent[]> newPages = new ArrayList<>();
for (String str : data) {
Expand All @@ -241,7 +241,7 @@ public void adjust(Mechanism mechanism) {
// <ItemTag.book_pages>
// -->
if (mechanism.matches("book_pages")) {
BookMeta meta = (BookMeta) item.getItemStack().getItemMeta();
BookMeta meta = (BookMeta) item.getItemMeta();
ListTag data = mechanism.valueAsType(ListTag.class);
ArrayList<BaseComponent[]> newPages = new ArrayList<>();
for (String str : data) {
Expand All @@ -265,7 +265,7 @@ public void adjust(Mechanism mechanism) {
Debug.echoError("Only WRITTEN_BOOK (not WRITABLE_BOOK) can have a title or author!");
}
else {
BookMeta meta = (BookMeta) item.getItemStack().getItemMeta();
BookMeta meta = (BookMeta) item.getItemMeta();
meta.setAuthor(mechanism.getValue().asString());
item.getItemStack().setItemMeta(meta);
}
Expand All @@ -285,7 +285,7 @@ public void adjust(Mechanism mechanism) {
Debug.echoError("Only WRITTEN_BOOK (not WRITABLE_BOOK) can have a title or author!");
}
else {
BookMeta meta = (BookMeta) item.getItemStack().getItemMeta();
BookMeta meta = (BookMeta) item.getItemMeta();
meta.setTitle(mechanism.getValue().asString());
item.getItemStack().setItemMeta(meta);
}
Expand All @@ -305,7 +305,7 @@ public void adjust(Mechanism mechanism) {
// <ItemTag.book_pages>
// -->
if (mechanism.matches("book")) {
BookMeta meta = (BookMeta) item.getItemStack().getItemMeta();
BookMeta meta = (BookMeta) item.getItemMeta();
if (mechanism.getValue().asString().startsWith("map@")) {
MapTag mapData = mechanism.valueAsType(MapTag.class);
ObjectTag author = mapData.getObject("author");
Expand Down
Expand Up @@ -70,15 +70,15 @@ public ObjectTag getObjectAttribute(Attribute attribute) {
// Returns whether this crossbow is charged.
// -->
if (attribute.startsWith("is_charged")) {
return new ElementTag(((CrossbowMeta) item.getItemStack().getItemMeta()).hasChargedProjectiles())
return new ElementTag(((CrossbowMeta) item.getItemMeta()).hasChargedProjectiles())
.getObjectAttribute(attribute.fulfill(1));
}

return null;
}

public ListTag getChargedProjectiles() {
CrossbowMeta meta = (CrossbowMeta) item.getItemStack().getItemMeta();
CrossbowMeta meta = (CrossbowMeta) item.getItemMeta();
ListTag list = new ListTag();
if (!meta.hasChargedProjectiles()) {
return list;
Expand Down Expand Up @@ -115,7 +115,7 @@ public void adjust(Mechanism mechanism) {
// <ItemTag.is_charged>
// -->
if (mechanism.matches("charged_projectiles")) {
CrossbowMeta meta = (CrossbowMeta) item.getItemStack().getItemMeta();
CrossbowMeta meta = (CrossbowMeta) item.getItemMeta();
meta.setChargedProjectiles(null);
for (ItemTag projectile : mechanism.valueAsType(ListTag.class).filter(ItemTag.class, mechanism.context)) {
try {
Expand All @@ -139,7 +139,7 @@ public void adjust(Mechanism mechanism) {
// <ItemTag.is_charged>
// -->
if (mechanism.matches("add_charged_projectile") && mechanism.requireObject(ItemTag.class)) {
CrossbowMeta meta = (CrossbowMeta) item.getItemStack().getItemMeta();
CrossbowMeta meta = (CrossbowMeta) item.getItemMeta();
try {
meta.addChargedProjectile(mechanism.valueAsType(ItemTag.class).getItemStack());
}
Expand All @@ -160,7 +160,7 @@ public void adjust(Mechanism mechanism) {
// <ItemTag.is_charged>
// -->
if (mechanism.matches("remove_charged_projectiles")) {
CrossbowMeta meta = (CrossbowMeta) item.getItemStack().getItemMeta();
CrossbowMeta meta = (CrossbowMeta) item.getItemMeta();
meta.setChargedProjectiles(null);
item.getItemStack().setItemMeta(meta);
}
Expand Down
Expand Up @@ -68,13 +68,13 @@ public ObjectTag getObjectAttribute(Attribute attribute) {
if (mat == Material.POTION
|| mat == Material.LINGERING_POTION
|| mat == Material.SPLASH_POTION) {
PotionMeta pm = (PotionMeta) item.getItemStack().getItemMeta();
PotionMeta pm = (PotionMeta) item.getItemMeta();
if (!pm.hasColor()) {
return new ColorTag(Color.WHITE).getObjectAttribute(attribute.fulfill((1)));
}
return new ColorTag(pm.getColor()).getObjectAttribute(attribute.fulfill((1)));
}
return new ColorTag(((LeatherArmorMeta) item.getItemStack().getItemMeta()).getColor()).getObjectAttribute(attribute.fulfill(1));
return new ColorTag(((LeatherArmorMeta) item.getItemMeta()).getColor()).getObjectAttribute(attribute.fulfill(1));
}

return null;
Expand All @@ -86,13 +86,13 @@ public String getPropertyString() {
if (mat == Material.POTION
|| mat == Material.LINGERING_POTION
|| mat == Material.SPLASH_POTION) {
PotionMeta pm = (PotionMeta) item.getItemStack().getItemMeta();
PotionMeta pm = (PotionMeta) item.getItemMeta();
if (!pm.hasColor()) {
return null;
}
return new ColorTag(pm.getColor()).identify();
}
return new ColorTag(((LeatherArmorMeta) item.getItemStack().getItemMeta()).getColor()).identify();
return new ColorTag(((LeatherArmorMeta) item.getItemMeta()).getColor()).identify();
}

@Override
Expand All @@ -117,12 +117,12 @@ public void adjust(Mechanism mechanism) {
ColorTag color = mechanism.valueAsType(ColorTag.class);
Material mat = item.getItemStack().getType();
if (mat == Material.POTION || mat == Material.LINGERING_POTION || mat == Material.SPLASH_POTION) {
PotionMeta meta = (PotionMeta) item.getItemStack().getItemMeta();
PotionMeta meta = (PotionMeta) item.getItemMeta();
meta.setColor(color.getColor());
item.getItemStack().setItemMeta(meta);
return;
}
LeatherArmorMeta meta = (LeatherArmorMeta) item.getItemStack().getItemMeta();
LeatherArmorMeta meta = (LeatherArmorMeta) item.getItemMeta();
meta.setColor(color.getColor());
item.getItemStack().setItemMeta(meta);
}
Expand Down

0 comments on commit b59600a

Please sign in to comment.