Skip to content

Commit

Permalink
change how item scripts identify
Browse files Browse the repository at this point in the history
to prevent double parsing issues
  • Loading branch information
mcmonkey4eva committed Jan 20, 2019
1 parent e3872a4 commit 2fe2094
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 131 deletions.
1 change: 1 addition & 0 deletions plugin/src/main/java/net/aufdemrand/denizen/Denizen.java
Expand Up @@ -903,6 +903,7 @@ public Property get(dObject o) {
PropertyParser.registerProperty(ItemPotion.class, dItem.class);
}
PropertyParser.registerProperty(ItemQuantity.class, dItem.class);
PropertyParser.registerProperty(ItemScript.class, dItem.class);
PropertyParser.registerProperty(ItemSignContents.class, dItem.class);
PropertyParser.registerProperty(ItemSkullskin.class, dItem.class);
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_9_R2)) {
Expand Down
Expand Up @@ -157,16 +157,13 @@ public boolean tryItem(dItem item, String comparedto) {
String regexd = regexHandle(comparedto);
item = new dItem(item.getItemStack().clone());
item.setAmount(1);
if (equalityCheck(item.identifyNoIdentifier(), comparedto, regexd)) {
if (equalityCheck(item.identify().substring("i@".length()), comparedto, regexd)) {
return true;
}
else if (equalityCheck(item.identifyMaterialNoIdentifier(), comparedto, regexd)) {
return true;
}
else if (equalityCheck(item.identifySimpleNoIdentifier(), comparedto, regexd)) {
return true;
}
else if (equalityCheck(item.identifyNoIdentifier(), comparedto, regexd)) {
else if (equalityCheck(item.identifySimple().substring("i@".length()), comparedto, regexd)) {
return true;
}
item.setDurability((short) 0);
Expand Down
127 changes: 22 additions & 105 deletions plugin/src/main/java/net/aufdemrand/denizen/objects/dItem.java
@@ -1,7 +1,9 @@
package net.aufdemrand.denizen.objects;

import net.aufdemrand.denizen.Settings;
import net.aufdemrand.denizen.nms.NMSHandler;
import net.aufdemrand.denizen.nms.NMSVersion;
import net.aufdemrand.denizen.nms.util.jnbt.StringTag;
import net.aufdemrand.denizen.objects.notable.NotableManager;
import net.aufdemrand.denizen.objects.properties.item.*;
import net.aufdemrand.denizen.scripts.containers.core.BookScriptContainer;
Expand All @@ -25,9 +27,12 @@
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Item;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.material.MaterialData;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -463,6 +468,22 @@ public String getScriptName() {
}
}

public void setItemScript(ItemScriptContainer script) {
if (script.contains("NO_ID") && Boolean.valueOf(script.getString("NO_ID"))) {
return;
}
if (Settings.packetInterception()) {
setItemStack(NMSHandler.getInstance().getItemHelper().addNbtData(getItemStack(), "Denizen Item Script", new StringTag(script.getHashID())));
}
else {
ItemMeta meta = item.getItemMeta();
List<String> lore = meta.hasLore() ? meta.getLore() : new ArrayList<>();
lore.add(0, script.getHashID());
meta.setLore(lore);
item.setItemMeta(meta);
}
}

public dMaterial getMaterial() {
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_13_R2)) {
return dMaterial.getMaterialFrom(getItemStack().getType());
Expand Down Expand Up @@ -549,11 +570,6 @@ public String identify() {
return "i@" + NotableManager.getSavedId(this) + PropertyParser.getPropertiesString(this);
}

// If not a saved item, but is a custom item, return the script id
else if (isItemscript()) {
return "i@" + getScriptName() + PropertyParser.getPropertiesString(this);
}

// Else, return the material name
else if (NMSHandler.getVersion().isAtMost(NMSVersion.v1_12_R1) && (item.getDurability() >= 16 || item.getDurability() < 0) && item.getType() != Material.AIR) {
return "i@" + getMaterial().realName() + "," + item.getDurability() + PropertyParser.getPropertiesString(this);
Expand Down Expand Up @@ -593,56 +609,8 @@ public String identifyMaterialNoIdentifier() {
return getMaterial().identifySimpleNoIdentifier();
}

public String identifyNoIdentifier() {

if (item == null) {
return "null";
}

if (item.getType() != Material.AIR) {

// If saved item, return that
if (isUnique()) {
return NotableManager.getSavedId(this) + (item.getAmount() == 1 ? "" : "[quantity=" + item.getAmount() + "]");
}

// If not a saved item, but is a custom item, return the script id
else if (isItemscript()) {
return getScriptName() + (item.getAmount() == 1 ? "" : "[quantity=" + item.getAmount() + "]");
}
}

// Else, return the material name
if (item.getDurability() >= 16 || item.getDurability() < 0) {
return getMaterial().realName() + "," + item.getDurability() + PropertyParser.getPropertiesString(this);
}
return getMaterial().identifyNoIdentifier() + PropertyParser.getPropertiesString(this);
}

public String identifySimpleNoIdentifier() {
if (item == null) {
return "null";
}

if (item.getType() != Material.AIR) {

// If saved item, return that
if (isUnique()) {
return NotableManager.getSavedId(this);
}

// If not a saved item, but is a custom item, return the script id
else if (isItemscript()) {
return getScriptName();
}
}

// Else, return the material name
return identifyMaterialNoIdentifier();
}

public String getFullString() {
return "i@" + (isItemscript() ? getScriptName() : getMaterial().realName()) + "," + item.getDurability() + PropertyParser.getPropertiesString(this);
return "i@" + getMaterial().realName() + "," + item.getDurability() + PropertyParser.getPropertiesString(this);
}


Expand Down Expand Up @@ -903,57 +871,6 @@ public String run(Attribute attribute, dObject object) {
}
});

// <--[tag]
// @attribute <i@item.has_script>
// @returns Element(Boolean)
// @group scripts
// @description
// Returns whether the item was created by an item script.
// -->
registerTag("has_script", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
return new Element(((dItem) object).isItemscript())
.getAttribute(attribute.fulfill(1));
}
});

// <--[tag]
// @attribute <i@item.scriptname>
// @returns Element
// @group scripts
// @description
// Returns the script name of the item if it was created by an item script.
// -->
registerTag("scriptname", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
if (((dItem) object).isItemscript()) {
return new Element(((dItem) object).getScriptName())
.getAttribute(attribute.fulfill(1));
}
return null;
}
});

// <--[tag]
// @attribute <i@item.script>
// @returns dScript
// @group scripts
// @description
// Returns the script of the item if it was created by an item script.
// -->
registerTag("script", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
if (((dItem) object).isItemscript()) {
return new dScript(((dItem) object).getScriptName())
.getAttribute(attribute.fulfill(1));
}
return null;
}
});

registerTag("prefix", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
Expand Down
@@ -0,0 +1,112 @@
package net.aufdemrand.denizen.objects.properties.item;

import net.aufdemrand.denizen.objects.dItem;
import net.aufdemrand.denizen.scripts.containers.core.ItemScriptContainer;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.objects.*;
import net.aufdemrand.denizencore.objects.properties.Property;
import net.aufdemrand.denizencore.tags.Attribute;

public class ItemScript implements Property {

public static boolean describes(dObject item) {
// All items can have a script
return item instanceof dItem;
}

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

private ItemScript(dItem _item) {
item = _item;
}

dItem item;

@Override
public String getAttribute(Attribute attribute) {

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

// <--[tag]
// @attribute <i@item.has_script>
// @returns Element(Boolean)
// @group scripts
// @description
// Returns whether the item was created by an item script.
// -->
if (attribute.startsWith("has_script")) {
return new Element(item.isItemscript())
.getAttribute(attribute.fulfill(1));
}

// <--[tag]
// @attribute <i@item.scriptname>
// @returns Element
// @group scripts
// @description
// Returns the script name of the item if it was created by an item script.
// -->
if (attribute.startsWith("scriptname")) {
if (item.isItemscript()) {
return new Element(item.getScriptName())
.getAttribute(attribute.fulfill(1));
}
}

// <--[tag]
// @attribute <i@item.script>
// @returns dScript
// @group scripts
// @description
// Returns the script of the item if it was created by an item script.
// -->
if (attribute.startsWith("script")) {
if (item.isItemscript()) {
return new dScript(item.getScriptName())
.getAttribute(attribute.fulfill(1));
}
}
return null;
}


@Override
public String getPropertyString() {
if (item.isItemscript()) {
return item.getScriptName();
}
else {
return null;
}
}

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

@Override
public void adjust(Mechanism mechanism) {

// Undocumented as meant for internal usage.

if (mechanism.matches("script") && mechanism.requireObject(dScript.class)) {
dScript script = mechanism.getValue().asType(dScript.class);
if (script.getContainer() instanceof ItemScriptContainer) {
item.setItemScript((ItemScriptContainer) script.getContainer());
}
else {
dB.echoError("Script '" + script.getName() + "' is not an item script (but was specified as one).");
}
}
}
}
@@ -1,8 +1,5 @@
package net.aufdemrand.denizen.scripts.containers.core;

import net.aufdemrand.denizen.Settings;
import net.aufdemrand.denizen.nms.NMSHandler;
import net.aufdemrand.denizen.nms.util.jnbt.StringTag;
import net.aufdemrand.denizen.objects.dItem;
import net.aufdemrand.denizen.objects.dNPC;
import net.aufdemrand.denizen.objects.dPlayer;
Expand Down Expand Up @@ -181,21 +178,6 @@ public dItem getItemFrom(dPlayer player, dNPC npc) {
ItemMeta meta = stack.getItemStack().getItemMeta();
List<String> lore = meta.hasLore() ? meta.getLore() : new ArrayList<String>();

// Set Id of the first, invisible lore
boolean hideLore = false;
boolean pureNbtId = false;
if (contains("NO_ID")) {
hideLore = Boolean.valueOf(getString("NO_ID"));
}
if (!hideLore) {
if (!Settings.packetInterception()) {
lore.add(0, hash);
}
else {
pureNbtId = true;
}
}

// Set Display Name
if (contains("DISPLAY NAME")) {
String displayName = TagManager.tag(getString("DISPLAY NAME"), new BukkitTagContext(player, npc, false, null, debug, new dScript(this)));
Expand Down Expand Up @@ -265,9 +247,7 @@ public dItem getItemFrom(dPlayer player, dNPC npc) {
stack = book.writeBookTo(stack, player, npc);
}

if (pureNbtId) {
stack.setItemStack(NMSHandler.getInstance().getItemHelper().addNbtData(stack.getItemStack(), "Denizen Item Script", new StringTag(hash)));
}
stack.setItemScript(this);
}
catch (Exception e) {
dB.echoError("Woah! An exception has been called with this item script!");
Expand Down

0 comments on commit 2fe2094

Please sign in to comment.