Skip to content

Commit

Permalink
fix outdated dItem valueOf calls
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jan 29, 2019
1 parent 1cbecba commit 5f952b3
Show file tree
Hide file tree
Showing 22 changed files with 57 additions and 49 deletions.
Expand Up @@ -105,7 +105,7 @@ else if (dLocation.matches(determination)) {
}
}
else if (dItem.matches(determination)) {
dItem it = dItem.valueOf(determination);
dItem it = dItem.valueOf(determination, container);
if (it == null) {
dB.echoError("[" + getName() + "] Invalid item!");
}
Expand Down
Expand Up @@ -164,7 +164,7 @@ else if (aH.Argument.valueOf(lower).matchesArgumentList(dItem.class)) {
dList drops_list = dList.valueOf(determination);
drops_list.filter(dItem.class);
for (String drop : drops_list) {
dItem item = dItem.valueOf(drop);
dItem item = dItem.valueOf(drop, container);
if (item != null) {
dropItems.add(item);
drops.add(item.identify());
Expand Down
Expand Up @@ -109,13 +109,13 @@ public boolean applyDetermination(ScriptContainer container, String determinatio
}
else if (CoreUtilities.toLowerCase(determination).startsWith("result:")) {
String ditem = determination.substring("result:".length());
item = dItem.valueOf(ditem);
item = dItem.valueOf(ditem, container);
itemEdited = true;
return true;
}
else if (CoreUtilities.toLowerCase(determination).startsWith("enchants:")) {
String ditem = determination.substring("enchants:".length());
enchantsRes = dItem.valueOf(ditem);
enchantsRes = dItem.valueOf(ditem, container);
return true;
}
else {
Expand Down
Expand Up @@ -95,7 +95,7 @@ public void destroy() {
@Override
public boolean applyDetermination(ScriptContainer container, String determination) {
if (dItem.matches(determination)) {
result = dItem.valueOf(determination);
result = dItem.valueOf(determination, container);
resultChanged = true;
return true;
}
Expand Down
Expand Up @@ -118,7 +118,7 @@ public boolean nonSwitchWithCheck(ScriptPath path, dItem held) {
if (with.equals("item")) {
return true;
}
dItem it = dItem.valueOf(with);
dItem it = dItem.valueOf(with, false);
if (it == null) {
dB.echoError("Invalid WITH item in " + getName() + " for '" + path.event + "' in " + path.container.getName());
return false;
Expand Down
Expand Up @@ -92,7 +92,7 @@ public void destroy() {
@Override
public boolean applyDetermination(ScriptContainer container, String determination) {
if (dItem.matches(determination)) {
result = dItem.valueOf(determination);
result = dItem.valueOf(determination, container);
resultChanged = true;
return true;
}
Expand Down
Expand Up @@ -99,7 +99,7 @@ public void destroy() {
public boolean applyDetermination(ScriptContainer container, String determination) {
String lower = CoreUtilities.toLowerCase(determination);
if (lower.startsWith("item:")) {
item = dItem.valueOf(determination.substring("item:".length()));
item = dItem.valueOf(determination.substring("item:".length()), container);
itemChanged = true;
return true;
}
Expand Down
Expand Up @@ -98,7 +98,7 @@ public boolean applyDetermination(ScriptContainer container, String determinatio
return true;
}
else if (dItem.matches(determination)) {
result = dItem.valueOf(determination);
result = dItem.valueOf(determination, container);
resultChanged = true;
return true;
}
Expand Down
Expand Up @@ -75,11 +75,11 @@ public void destroy() {
public boolean applyDetermination(ScriptContainer container, String determination) {
String lower = CoreUtilities.toLowerCase(determination);
if (lower.startsWith("main:")) {
event.setMainHandItem(dItem.valueOf(determination.substring("main:".length())).getItemStack());
event.setMainHandItem(dItem.valueOf(determination.substring("main:".length()), container).getItemStack());
return true;
}
else if (lower.startsWith("offhand:")) {
event.setOffHandItem(dItem.valueOf(determination.substring("offhand:".length())).getItemStack());
event.setOffHandItem(dItem.valueOf(determination.substring("offhand:".length()), container).getItemStack());
return true;
}
return super.applyDetermination(container, determination);
Expand Down
Expand Up @@ -86,7 +86,7 @@ public void destroy() {
@Override
public boolean applyDetermination(ScriptContainer container, String determination) {
if (dItem.matches(determination)) {
result_item = dItem.valueOf(determination);
result_item = dItem.valueOf(determination, container);
return true;
}
return super.applyDetermination(container, determination);
Expand Down
Expand Up @@ -100,7 +100,7 @@ public void destroy() {
@Override
public boolean applyDetermination(ScriptContainer container, String determination) {
if (dItem.matches(determination)) {
item = dItem.valueOf(determination);
item = dItem.valueOf(determination, container);
itemSet = true;
return true;
}
Expand Down
Expand Up @@ -503,7 +503,7 @@ public boolean removeItem(dItem item, int amount) {
int count = is.getAmount();
is.setAmount(1);
// Note: this double-parsing is intentional, as part of a hotfix for a larger issue
String newItem = CoreUtilities.toLowerCase(dItem.valueOf(new dItem(is).getFullString()).getFullString());
String newItem = CoreUtilities.toLowerCase(dItem.valueOf(new dItem(is).getFullString(), false).getFullString());
if (myItem.equals(newItem)) {
if (count <= amount) {
inventory.setItem(i, null);
Expand Down Expand Up @@ -1404,7 +1404,7 @@ public String getAttribute(Attribute attribute) {
// -->
if (attribute.startsWith("include") && attribute.hasContext(1)
&& dItem.matches(attribute.getContext(1))) {
dItem item = dItem.valueOf(attribute.getContext(1));
dItem item = dItem.valueOf(attribute.getContext(1), attribute.context);
if (item == null) {
return null;
}
Expand Down Expand Up @@ -1880,7 +1880,7 @@ else if (item != null && item.hasItemMeta() && item.getItemMeta().hasDisplayName
if (attribute.startsWith("find.scriptname")
&& attribute.hasContext(2)
&& dItem.matches(attribute.getContext(2))) {
String scrname = dItem.valueOf(attribute.getContext(2)).getScriptName();
String scrname = dItem.valueOf(attribute.getContext(2), attribute.context).getScriptName();
if (scrname == null) {
return null;
}
Expand All @@ -1906,9 +1906,7 @@ else if (item != null && item.hasItemMeta() && item.getItemMeta().hasDisplayName
if (attribute.startsWith("find_imperfect")
&& attribute.hasContext(1)
&& dItem.matches(attribute.getContext(1))) {
dItem item = dItem.valueOf(attribute.getContext(1),
attribute.getScriptEntry() != null ? ((BukkitScriptEntryData) attribute.getScriptEntry().entryData).getPlayer() : null,
attribute.getScriptEntry() != null ? ((BukkitScriptEntryData) attribute.getScriptEntry().entryData).getNPC() : null);
dItem item = dItem.valueOf(attribute.getContext(1), attribute.context);
item.setAmount(1);
int slot = -1;
for (int i = 0; i < inventory.getSize(); i++) {
Expand All @@ -1935,9 +1933,7 @@ else if (item != null && item.hasItemMeta() && item.getItemMeta().hasDisplayName
if (attribute.startsWith("find")
&& attribute.hasContext(1)
&& dItem.matches(attribute.getContext(1))) {
dItem item = dItem.valueOf(attribute.getContext(1),
attribute.getScriptEntry() != null ? ((BukkitScriptEntryData) attribute.getScriptEntry().entryData).getPlayer() : null,
attribute.getScriptEntry() != null ? ((BukkitScriptEntryData) attribute.getScriptEntry().entryData).getNPC() : null);
dItem item = dItem.valueOf(attribute.getContext(1), attribute.context);
item.setAmount(1);
int slot = -1;
for (int i = 0; i < inventory.getSize(); i++) {
Expand Down Expand Up @@ -2003,9 +1999,7 @@ else if (item != null && item.hasItemMeta() && item.getItemMeta().hasDisplayName
if (attribute.startsWith("quantity") || attribute.startsWith("qty")) {
if (attribute.hasContext(1) && dItem.matches(attribute.getContext(1))) {
return new Element(count // TODO: Handle no-script-entry cases
(dItem.valueOf(attribute.getContext(1),
((BukkitScriptEntryData) attribute.getScriptEntry().entryData).getPlayer(),
((BukkitScriptEntryData) attribute.getScriptEntry().entryData).getNPC()).getItemStack(), false))
(dItem.valueOf(attribute.getContext(1), attribute.context).getItemStack(), false))
.getAttribute(attribute.fulfill(1));
}
else {
Expand All @@ -2024,9 +2018,7 @@ else if (item != null && item.hasItemMeta() && item.getItemMeta().hasDisplayName
if (attribute.startsWith("stacks")) {
if (attribute.hasContext(1) && dItem.matches(attribute.getContext(1))) {
return new Element(count // TODO: Handle no-script-entry cases
(dItem.valueOf(attribute.getContext(1),
((BukkitScriptEntryData) attribute.getScriptEntry().entryData).getPlayer(),
((BukkitScriptEntryData) attribute.getScriptEntry().entryData).getNPC()).getItemStack(), true))
(dItem.valueOf(attribute.getContext(1), attribute.context).getItemStack(), true))
.getAttribute(attribute.fulfill(1));
}
else {
Expand Down
20 changes: 18 additions & 2 deletions plugin/src/main/java/net/aufdemrand/denizen/objects/dItem.java
Expand Up @@ -16,10 +16,12 @@
import net.aufdemrand.denizencore.objects.notable.Notable;
import net.aufdemrand.denizencore.objects.notable.Note;
import net.aufdemrand.denizencore.objects.properties.PropertyParser;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
import net.aufdemrand.denizencore.scripts.ScriptRegistry;
import net.aufdemrand.denizencore.tags.Attribute;
import net.aufdemrand.denizencore.tags.TagContext;
import net.aufdemrand.denizencore.utilities.CoreUtilities;
import net.aufdemrand.denizencore.utilities.debugging.Debuggable;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
Expand Down Expand Up @@ -52,7 +54,7 @@ public class dItem implements dObject, Notable, Adjustable {


public static dItem valueOf(String string) {
return valueOf(string, null);
return valueOf(string, null, null);
}

@Fetchable("i")
Expand All @@ -68,6 +70,20 @@ public static dItem valueOf(String string, TagContext context) {
}
}

public static dItem valueOf(String string, Debuggable debugMe) {
nope = debugMe != null && !debugMe.shouldDebug();
dItem tmp = valueOf(string, null, null);
nope = false;
return tmp;
}

public static dItem valueOf(String string, boolean debugMe) {
nope = !debugMe;
dItem tmp = valueOf(string, null, null);
nope = false;
return tmp;
}

/**
* Gets a Item Object from a string form.
*
Expand Down Expand Up @@ -215,7 +231,7 @@ else if (ScriptRegistry.containsScript(arg, BookScriptContainer.class)) {
// TODO: Make this better. Probably creating some unnecessary
// objects by doing this :(
nope = true;
if (valueOf(arg) != null) {
if (valueOf(arg, null, null) != null) {
nope = false;
return true;
}
Expand Down
Expand Up @@ -766,7 +766,7 @@ public String getAttribute(Attribute attribute) {
if (attribute.startsWith("drops")) {
Collection<ItemStack> its;
if (attribute.hasContext(1)) {
dItem item = dItem.valueOf(attribute.getContext(1));
dItem item = dItem.valueOf(attribute.getContext(1), attribute.context);
its = getBlock().getDrops(item.getItemStack());
}
else {
Expand Down
Expand Up @@ -179,7 +179,7 @@ public void adjust(Mechanism mechanism) {
dList list = dList.valueOf(mechanism.getValue().asString());
ItemStack[] stacks = new ItemStack[list.size()];
for (int i = 0; i < list.size(); i++) {
stacks[i] = dItem.valueOf(list.get(i)).getItemStack();
stacks[i] = dItem.valueOf(list.get(i), mechanism.context).getItemStack();
}
entity.getLivingEntity().getEquipment().setArmorContents(stacks);
}
Expand Down
Expand Up @@ -112,7 +112,7 @@ public void adjust(Mechanism mechanism) {
inv.clear();
int i = 0;
for (String str : list) {
inv.setSlots(i, dItem.valueOf(str).getItemStack());
inv.setSlots(i, dItem.valueOf(str, mechanism.context).getItemStack());
i++;
}
}
Expand Down
Expand Up @@ -41,36 +41,36 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException
}
else if (arg.matchesArgumentType(dItem.class)
&& arg.matchesPrefix("head", "helmet")) {
equipment.put("head", dItem.valueOf(arg.getValue()));
equipment.put("head", dItem.valueOf(arg.getValue(), scriptEntry.entryData.getTagContext()));
}
else if (arg.matchesArgumentType(dItem.class)
&& arg.matchesPrefix("chest", "chestplate")) {
equipment.put("chest", dItem.valueOf(arg.getValue()));
equipment.put("chest", dItem.valueOf(arg.getValue(), scriptEntry.entryData.getTagContext()));
}
else if (arg.matchesArgumentType(dItem.class)
&& arg.matchesPrefix("legs", "leggings")) {
equipment.put("legs", dItem.valueOf(arg.getValue()));
equipment.put("legs", dItem.valueOf(arg.getValue(), scriptEntry.entryData.getTagContext()));
}
else if (arg.matchesArgumentType(dItem.class)
&& arg.matchesPrefix("boots", "feet")) {
equipment.put("boots", dItem.valueOf(arg.getValue()));
equipment.put("boots", dItem.valueOf(arg.getValue(), scriptEntry.entryData.getTagContext()));
}
else if (arg.matchesArgumentType(dItem.class)
&& arg.matchesPrefix("saddle")) {
equipment.put("saddle", dItem.valueOf(arg.getValue()));
equipment.put("saddle", dItem.valueOf(arg.getValue(), scriptEntry.entryData.getTagContext()));
}
else if (arg.matchesArgumentType(dItem.class)
&& arg.matchesPrefix("horse_armor", "horse_armour")) {
equipment.put("horse_armor", dItem.valueOf(arg.getValue()));
equipment.put("horse_armor", dItem.valueOf(arg.getValue(), scriptEntry.entryData.getTagContext()));
}
else if (arg.matchesArgumentType(dItem.class)
&& arg.matchesPrefix("offhand")) {
equipment.put("offhand", dItem.valueOf(arg.getValue()));
equipment.put("offhand", dItem.valueOf(arg.getValue(), scriptEntry.entryData.getTagContext()));
}

// Default to item in hand if no prefix is used
else if (arg.matchesArgumentType(dItem.class)) {
equipment.put("hand", dItem.valueOf(arg.getValue()));
equipment.put("hand", dItem.valueOf(arg.getValue(), scriptEntry.entryData.getTagContext()));
}
else if (arg.matches("player") && ((BukkitScriptEntryData) scriptEntry.entryData).hasPlayer()) {
// Player arg for compatibility with old scripts
Expand Down
Expand Up @@ -99,7 +99,7 @@ else if (arg.matches("random")) {
else if (arg.startsWith("iconcrack_")) {
// Allow iconcrack_[item] for item break effects (ex: iconcrack_stone)
String shrunk = arg.getValue().substring("iconcrack_".length());
dItem item = dItem.valueOf(shrunk);
dItem item = dItem.valueOf(shrunk, scriptEntry.entryData.getTagContext());
if (item != null) {
scriptEntry.addObject("iconcrack", item);
}
Expand Down
Expand Up @@ -167,7 +167,7 @@ public dItem getItemFrom(dPlayer player, dNPC npc) {
if (material.startsWith("m@")) {
material = material.substring(2);
}
stack = dItem.valueOf(material);
stack = dItem.valueOf(material, this);
}

// Make sure we're working with a valid base ItemStack
Expand Down
Expand Up @@ -74,7 +74,7 @@ public void scriptReload(ScriptReloadEvent event) {
String[] elements = recipeRow.split("\\|", 3);

for (String element : elements) {
dItem ingredient = dItem.valueOf(element.replaceAll("[iImM]@", ""));
dItem ingredient = dItem.valueOf(element.replaceAll("[iImM]@", ""), container);
if (ingredient == null) {
dB.echoError("Invalid dItem ingredient, recipe will not be registered for item script '"
+ container.getName() + "': " + element);
Expand Down Expand Up @@ -104,7 +104,7 @@ public void scriptReload(ScriptReloadEvent event) {

boolean shouldRegister = true;
for (String element : dList.valueOf(list)) {
dItem ingredient = dItem.valueOf(element.replaceAll("[iImM]@", ""));
dItem ingredient = dItem.valueOf(element.replaceAll("[iImM]@", ""), container);
if (ingredient == null) {
dB.echoError("Invalid dItem ingredient, shapeless recipe will not be registered for item script '"
+ container.getName() + "': " + element);
Expand All @@ -120,7 +120,7 @@ public void scriptReload(ScriptReloadEvent event) {

for (Map.Entry<ItemScriptContainer, String> entry : furnace_to_register.entrySet()) {

dItem furnace_item = dItem.valueOf(entry.getValue());
dItem furnace_item = dItem.valueOf(entry.getValue(), entry.getKey());
if (furnace_item == null) {
dB.echoError("Invalid item '" + entry.getValue() + "'");
continue;
Expand All @@ -143,7 +143,7 @@ public void furnaceSmeltHandler(FurnaceSmeltEvent event) {
ItemScriptContainer isc = getItemScriptContainer(event.getResult());
String inp = currentFurnaceRecipes.get(isc);
if (inp != null) {
dItem itm = dItem.valueOf(inp);
dItem itm = dItem.valueOf(inp, isc);
if (itm != null) {
itm.setAmount(1);
dItem src = new dItem(event.getSource().clone());
Expand Down
Expand Up @@ -120,7 +120,7 @@ public void clickTrigger(NPCRightClickEvent event) {
(player, npc, false, null, false, null));
// Check if the item specified in the specified id's 'trigger:' key
// matches the item that the player is holding.
dItem item = dItem.valueOf(entry_value);
dItem item = dItem.valueOf(entry_value, script);
if (item == null) {
dB.echoError("Invalid click trigger in script '" + script.getName() + "' (null trigger item)!");
}
Expand Down

0 comments on commit 5f952b3

Please sign in to comment.