Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

Commit

Permalink
Add a helpful deftag to item script logic
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed May 22, 2018
1 parent 1513a0d commit 04e62a6
Showing 1 changed file with 12 additions and 8 deletions.
Expand Up @@ -58,6 +58,7 @@ public class ItemScript extends CommandScript {
// as that is likely very localized. The item stack given directly by the item script system will have a quantity of 1.
//
// All options listed below are used to define the item's specific details. They all support tags on input.
// All options other than "material" may use the automatically included definition tag <[material]> to get the base material.
//
// Set key "material" directly to an ItemTag of the basic item type to use. You may list an item type, or an existing item.
// Be careful to not list the item script within itself, even indirectly, as this can cause recursion errors.
Expand Down Expand Up @@ -162,20 +163,23 @@ public void prepValues() {
}
}

public AbstractTagObject parseVal(CommandQueue queue, Argument arg) {
public AbstractTagObject parseVal(CommandQueue queue, Argument arg, HashMap<String, AbstractTagObject> varBack) {
Action<String> error = (es) -> {
throw new ErrorInducedException(es);
};
return arg.parse(queue, new HashMap<>(), getDebugMode(), error);
return arg.parse(queue, varBack, getDebugMode(), error);
}

public ItemStack generateItem(CommandQueue queue) {
Action<String> error = (es) -> {
throw new ErrorInducedException(es);
};
ItemStack.Builder its = ItemStack.builder().from(ItemTag.getFor(error, parseVal(queue, material), queue).getInternal()).quantity(1);
HashMap<String, AbstractTagObject> varBack = new HashMap<>();
ItemTag baseMat = ItemTag.getFor(error, parseVal(queue, material, varBack), queue);
varBack.put("material", baseMat);
ItemStack.Builder its = ItemStack.builder().from(baseMat.getInternal().copy()).quantity(1);
if (displayName != null) {
AbstractTagObject ato = parseVal(queue, displayName);
AbstractTagObject ato = parseVal(queue, displayName, varBack);
if (ato instanceof FormattedTextTag) {
its = its.add(Keys.DISPLAY_NAME, ((FormattedTextTag) ato).getInternal());
}
Expand All @@ -186,7 +190,7 @@ public ItemStack generateItem(CommandQueue queue) {
if (lore != null) {
List<Text> loreVal = new ArrayList<>();
for (Argument arg : lore) {
AbstractTagObject ato = parseVal(queue, arg);
AbstractTagObject ato = parseVal(queue, arg, varBack);
if (ato instanceof FormattedTextTag) {
loreVal.add(((FormattedTextTag) ato).getInternal());
}
Expand All @@ -199,10 +203,10 @@ public ItemStack generateItem(CommandQueue queue) {
MapTag flagsMap = new MapTag();
if (flags != null) {
for (Tuple<String, Argument> flagVal : flags) {
flagsMap.getInternal().put(flagVal.one, parseVal(queue, flagVal.two));
flagsMap.getInternal().put(flagVal.one, parseVal(queue, flagVal.two, varBack));
}
}
if (plain == null || !BooleanTag.getFor(error, parseVal(queue, plain)).getInternal()) {
if (plain == null || !BooleanTag.getFor(error, parseVal(queue, plain, varBack)).getInternal()) {
flagsMap.getInternal().put("_d2_script", new ScriptTag(this));
}
ItemStack toRet = its.build();
Expand All @@ -212,7 +216,7 @@ public ItemStack generateItem(CommandQueue queue) {
if (k == null) {
throw new ErrorInducedException("Key '" + input.one + "' does not seem to exist.");
}
DataKeys.tryApply(toRet, k, parseVal(queue, input.two), error);
DataKeys.tryApply(toRet, k, parseVal(queue, input.two, varBack), error);
}
}
if (!flagsMap.getInternal().isEmpty()) {
Expand Down

0 comments on commit 04e62a6

Please sign in to comment.