forked from SkriptLang/Skript
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement LitAnyItem, basic tests, fix parser bug that wasn't attempt…
…ing all literals.
- Loading branch information
Showing
5 changed files
with
136 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/main/java/org/skriptlang/skript/bukkit/tags/elements/LitAnyItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package org.skriptlang.skript.bukkit.tags.elements; | ||
|
||
import ch.njol.skript.Skript; | ||
import ch.njol.skript.aliases.ItemData; | ||
import ch.njol.skript.aliases.ItemType; | ||
import ch.njol.skript.lang.Expression; | ||
import ch.njol.skript.lang.ExpressionType; | ||
import ch.njol.skript.lang.Literal; | ||
import ch.njol.skript.lang.SkriptParser.ParseResult; | ||
import ch.njol.skript.lang.util.SimpleLiteral; | ||
import ch.njol.util.Kleenean; | ||
import ch.njol.util.coll.CollectionUtils; | ||
import org.bukkit.Material; | ||
import org.bukkit.Tag; | ||
|
||
import java.util.Objects; | ||
|
||
public class LitAnyItem extends SimpleLiteral<ItemType> { | ||
|
||
static { | ||
Skript.registerExpression(LitAnyItem.class, ItemType.class, ExpressionType.SIMPLE, | ||
"[an[y]] item [tagged as %-minecrafttag%]", | ||
"all items [tagged as %-minecrafttag%]"); | ||
} | ||
|
||
public LitAnyItem() { | ||
super(CollectionUtils.array(new ItemType(ItemData.wildcard())), ItemType.class, true); | ||
} | ||
|
||
@Override | ||
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { | ||
Expression<?> tagExpression = expressions[0]; | ||
if (tagExpression != null) { | ||
if (!(tagExpression.simplify() instanceof Literal)) { | ||
Skript.error("The tag in an any/all item expression must be a literal."); | ||
return false; | ||
} | ||
// filter by tag | ||
Tag<?> tag = (Tag<?>) ((Literal<?>) tagExpression.simplify()).getSingle(); | ||
Material[] values = tag.getValues().stream() | ||
.filter(Objects::nonNull) | ||
.filter(Material.class::isInstance) | ||
.toArray(Material[]::new); | ||
if (values.length == 0) { | ||
Skript.error("The tag in an any/all item expression must be a tag of materials."); | ||
return false; | ||
} | ||
this.data[0] = new ItemType(values); | ||
} | ||
if (matchedPattern == 1) | ||
this.data[0].setAll(true); | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
test "any item literal": | ||
assert a diamond sword is any item with "diamond sword should be in any item" | ||
assert a diamond sword is an item with "diamond sword should be an item" | ||
assert a diamond sword is an item type with "diamond sword should be an item type" | ||
assert a diamond sword is an item tagged as (tag "swords") with "diamond sword should be an item tagged as 'swords'" | ||
|
||
parse: | ||
results: {LitAnyItem::parse-a::*} | ||
code: | ||
on right click with any item tagged as tag "pickaxes": | ||
broadcast "test" | ||
|
||
parse: | ||
results: {LitAnyItem::parse-b::*} | ||
code: | ||
on right click with any item tagged as tag {_tag}: | ||
broadcast "test" | ||
|
||
|
||
test "any item literal parse check": | ||
assert {LitAnyItem::parse-a::*} is not set with "Any item literal failed to parse" | ||
assert {LitAnyItem::parse-b::*} is "The tag in an any/all item expression must be a literal." with "Any item literal didn't fail properly when given a non-literal tag" |