Skip to content

Commit

Permalink
Save item script IDs in invisible item lore instead of custom NBT tha…
Browse files Browse the repository at this point in the history
…t Bukkit keeps erasing.
  • Loading branch information
davidcernat committed Jul 28, 2013
1 parent 06ddbaf commit 1384a28
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -15,7 +15,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<craftbukkit.version>1.6.2-R0.1-SNAPSHOT</craftbukkit.version>
<json.version>20090211</json.version>
<citizens.version>2.0.8-SNAPSHOT</citizens.version>
<citizens.version>2.0.9-SNAPSHOT</citizens.version>
<build.number>Unknown</build.number>
</properties>

Expand Down
43 changes: 41 additions & 2 deletions src/main/java/net/aufdemrand/denizen/objects/dItem.java
Expand Up @@ -347,6 +347,44 @@ public int comparesTo(ItemStack item) {

// Additional helper methods

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

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 String 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 "";
}

public void setAmount(int value) {
if (item != null)
item.setAmount(value);
Expand Down Expand Up @@ -437,8 +475,9 @@ public String identify() {
return "i@" + getSaved(this);

// If not a saved item, but is a custom item, return the script id
else if (CustomNBT.hasCustomNBT(getItemStack(), "denizen-script-id"))
return "i@" + CustomNBT.getCustomNBT(getItemStack(), "denizen-script-id");
else if (containsLore("§0id:")) {
return "i@" + getLore("§0id: ");
}
}

// Else, return the material name and data
Expand Down
Expand Up @@ -388,7 +388,7 @@ public String getAttribute(Attribute attribute) {
return loc.getAttribute(attribute.fulfill(1));
}

if (attribute.startsWith("find")) {
if (attribute.startsWith("find") || attribute.startsWith("nearest")) {
attribute.fulfill(1);
ArrayList<dObject> found = new ArrayList<dObject>();

Expand Down
Expand Up @@ -10,7 +10,6 @@
import net.aufdemrand.denizen.tags.TagManager;
import net.aufdemrand.denizen.objects.dItem;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.nbt.CustomNBT;
import net.aufdemrand.denizen.utilities.nbt.LeatherColorer;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.enchantments.Enchantment;
Expand Down Expand Up @@ -44,7 +43,11 @@ public dItem getItemFrom(dPlayer player, dNPC npc) {
if (stack == null) return null;

ItemMeta meta = stack.getItemStack().getItemMeta();

List<String> lore = new ArrayList<String>();

// Set Id of the first, invisible lore
lore.add("§0id: " + getName());

// Set Display Name
if (contains("DISPLAY NAME")){
String displayName = TagManager.tag(player, npc, getString("DISPLAY NAME"));
Expand All @@ -53,14 +56,14 @@ public dItem getItemFrom(dPlayer player, dNPC npc) {

// Set Lore
if (contains("LORE")) {
List<String> taggedLore = new ArrayList<String>();

for (String l : getStringList("LORE")){
l = TagManager.tag(player, npc, l);
taggedLore.add(l);
lore.add(l);
}
meta.setLore(taggedLore);
}

meta.setLore(lore);
stack.getItemStack().setItemMeta(meta);

// Set Enchantments
Expand Down Expand Up @@ -100,9 +103,6 @@ public dItem getItemFrom(dPlayer player, dNPC npc) {
stack = book.writeBookTo(stack, player, npc);
}

// Set Id of the stack
stack.setItemStack(CustomNBT.addCustomNBT(stack.getItemStack(), "denizen-script-id", getName()));

} catch (Exception e) {
dB.echoError("Woah! An exception has been called with this item script!");
if (!dB.showStackTraces)
Expand Down

3 comments on commit 1384a28

@Morphan1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This forces i@itemname.lore.get[1] to always return id:itemname in black text. That's a bit of an issue

@davidcernat
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in my next commit. Not exactly hard to fix, you know.

@Morphan1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I was going to sleep. I do sleep sometimes, you know

Please sign in to comment.