Skip to content

Commit

Permalink
Add extremely simple item NBT tags
Browse files Browse the repository at this point in the history
<i@item.has_nbt[<key>]>, <i@item.nbt_keys[<filter>]>,
<i@item.nbt[<key>]>
  • Loading branch information
Morphan1 committed Mar 17, 2016
1 parent 24ddb70 commit a6ae074
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/main/java/net/aufdemrand/denizen/Denizen.java
Expand Up @@ -813,6 +813,7 @@ public void onEnable() {
propertyParser.registerProperty(ItemFlags.class, dItem.class);
propertyParser.registerProperty(ItemLore.class, dItem.class);
propertyParser.registerProperty(ItemMap.class, dItem.class);
propertyParser.registerProperty(ItemNBT.class, dItem.class);
propertyParser.registerProperty(ItemPatterns.class, dItem.class);
propertyParser.registerProperty(ItemPlantgrowth.class, dItem.class);
propertyParser.registerProperty(ItemPotion.class, dItem.class);
Expand Down
@@ -0,0 +1,97 @@
package net.aufdemrand.denizen.objects.properties.item;

import net.aufdemrand.denizen.objects.dItem;
import net.aufdemrand.denizen.utilities.nbt.CustomNBT;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.Mechanism;
import net.aufdemrand.denizencore.objects.dList;
import net.aufdemrand.denizencore.objects.dObject;
import net.aufdemrand.denizencore.objects.properties.Property;
import net.aufdemrand.denizencore.tags.Attribute;

public class ItemNBT implements Property {

public static boolean describes(dObject item) {
return item instanceof dItem;
}

public static ItemNBT getFrom(dObject item) {
if (!describes(item)) {
return null;
}
else {
return new ItemNBT((dItem) item);
}
}

private ItemNBT(dItem item) {
this.item = item;
}

dItem item;

@Override
public String getAttribute(Attribute attribute) {

if (attribute == null) {
return null;
}

// <--[tag]
// @attribute <i@item.has_nbt[<key>]>
// @returns Element(Boolean)
// @group properties
// @description
// Returns whether this item has the specified NBT key.
// -->
if (attribute.startsWith("has_nbt")) {
return new Element(CustomNBT.hasCustomNBT(item.getItemStack(), attribute.getContext(1)))
.getAttribute(attribute.fulfill(1));
}

// <--[tag]
// @attribute <i@item.nbt_keys[<filter>]>
// @returns dList
// @group properties
// @description
// Returns all of this item's NBT keys as a dList. Optionally, specify
// a filter for the start of the keys.
// -->
if (attribute.startsWith("nbt_keys")) {
String filter = attribute.hasContext(1) ? attribute.getContext(1) : "";
return new dList(CustomNBT.listNBT(item.getItemStack(), filter))
.getAttribute(attribute.fulfill(1));
}

// <--[tag]
// @attribute <i@item.nbt[<key>]>
// @returns Element
// @group properties
// @description
// Returns the value of this item's NBT key as a string Element as best it can.
// -->
if (attribute.startsWith("nbt")) {
return new Element(CustomNBT.getCustomNBT(item.getItemStack(), attribute.getContext(1)))
.getAttribute(attribute.fulfill(1));
}

return null;
}


@Override
public String getPropertyString() {
// TODO: list Denizen command-created NBT if/when that is added?
return null;
}

@Override
public String getPropertyId() {
return "nbt";
}

@Override
public void adjust(Mechanism mechanism) {
// Do nothing
}
}
117 changes: 107 additions & 10 deletions src/main/java/net/aufdemrand/denizen/utilities/nbt/CustomNBT.java
@@ -1,13 +1,22 @@
package net.aufdemrand.denizen.utilities.nbt;

import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.utilities.CoreUtilities;
import net.minecraft.server.v1_9_R1.EntityLiving;
import net.minecraft.server.v1_9_R1.NBTBase;
import net.minecraft.server.v1_9_R1.NBTTagCompound;
import net.minecraft.server.v1_9_R1.NBTTagString;
import org.bukkit.craftbukkit.v1_9_R1.entity.CraftEntity;
import org.bukkit.craftbukkit.v1_9_R1.inventory.CraftItemStack;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.inventory.ItemStack;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class CustomNBT {

public static MapOfEnchantments getEnchantments(ItemStack item) {
Expand All @@ -23,15 +32,18 @@ public static boolean hasCustomNBT(ItemStack item, String key) {
if (item == null) {
return false;
}
NBTTagCompound tag;
net.minecraft.server.v1_9_R1.ItemStack cis = CraftItemStack.asNMSCopy(item);
if (!cis.hasTag()) {
return false;
}
tag = cis.getTag();
// dB.echoDebug(tag.toString());
// if this item has the NBTData for 'owner', there is an engraving.
return tag.hasKey(key);
key = CoreUtilities.toLowerCase(key);
List<String> keys = listNBT(item, "");
for (String string : keys) {
if (CoreUtilities.toLowerCase(string).equals(key)) {
return true;
}
}
return false;
}

public static String getCustomNBT(ItemStack item, String key) {
Expand All @@ -44,9 +56,41 @@ public static String getCustomNBT(ItemStack item, String key) {
cis.setTag(new NBTTagCompound());
}
tag = cis.getTag();
// if this item has the NBTData for 'owner', return the value, which is the playername of the 'owner'.
if (tag.hasKey(key)) {
return tag.getString(key);
key = CoreUtilities.toLowerCase(key);
String finalKey = null;
List<String> keys = listNBT(item, "");
for (String string : keys) {
if (CoreUtilities.toLowerCase(string).equals(key)) {
finalKey = string;
break;
}
}
if (finalKey == null) {
return null;
}
Iterator<String> subkeys = CoreUtilities.split(finalKey, '.').iterator();
if (subkeys.hasNext()) {
while (true) {
String subkey = subkeys.next();
NBTBase base = tag.get(subkey);
if (!subkeys.hasNext()) {
if (base instanceof NBTTagString) {
return ((NBTTagString) base).a_();
}
else if (base instanceof NBTBase.NBTNumber) {
return new Element(((NBTBase.NBTNumber) base).h()).asString();
}
else {
return base.toString();
}
}
else if (base instanceof NBTTagCompound) {
tag = (NBTTagCompound) base;
}
else {
return null;
}
}
}
return null;

Expand All @@ -62,11 +106,64 @@ public static ItemStack removeCustomNBT(ItemStack item, String key) {
cis.setTag(new NBTTagCompound());
}
tag = cis.getTag();
// remove 'owner' NBTData
tag.remove(key);
key = CoreUtilities.toLowerCase(key);
String finalKey = null;
List<String> keys = listNBT(item, "");
for (String string : keys) {
if (CoreUtilities.toLowerCase(string).equals(key)) {
finalKey = string;
break;
}
}
if (finalKey == null) {
return null;
}
Iterator<String> subkeys = CoreUtilities.split(finalKey, '.').iterator();
if (subkeys.hasNext()) {
while (true) {
String subkey = subkeys.next();
if (!subkeys.hasNext()) {
tag.remove(subkey);
}
else if (tag.get(subkey).getTypeId() == tag.getTypeId()) {
tag = tag.getCompound(subkey);
continue;
}
break;
}
}
return CraftItemStack.asCraftMirror(cis);
}

public static List<String> listNBT(ItemStack item, String filter) {
if (item == null) {
return null;
}
net.minecraft.server.v1_9_R1.ItemStack cis = CraftItemStack.asNMSCopy(item);
NBTTagCompound tag;
if (!cis.hasTag()) {
cis.setTag(new NBTTagCompound());
}
tag = cis.getTag();
return recursiveSearch(tag, "", filter);
}

private static List<String> recursiveSearch(NBTTagCompound compound, String base, String filter) {
Set<String> keys = compound.c();
List<String> finalKeys = new ArrayList<String>();
filter = CoreUtilities.toLowerCase(filter);
for (String key : keys) {
String full = base + key;
if (CoreUtilities.toLowerCase(full).startsWith(filter)) {
finalKeys.add(full);
}
if (compound.get(key).getTypeId() == compound.getTypeId()) {
finalKeys.addAll(recursiveSearch(compound.getCompound(key), full + ".", filter));
}
}
return finalKeys;
}

public static ItemStack addCustomNBT(ItemStack item, String key, String value) {
if (item == null) {
return null;
Expand Down

0 comments on commit a6ae074

Please sign in to comment.