-
-
Notifications
You must be signed in to change notification settings - Fork 453
add item model support #8779
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
novystar
wants to merge
11
commits into
SkriptLang:dev/feature
Choose a base branch
from
novystar:feature/item-model
base: dev/feature
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
add item model support #8779
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e29406e
add ExprItemModel
novystar 4ffa700
add ExprItemWithModel
novystar 5f64658
add tests for Item model syntax
novystar 00c44d3
remove redundant conversion to ItemStack
novystar ab9361e
Apply suggestions from code review
novystar 8f646c1
Apply suggestion from @APickledWalrus
novystar 5c16a52
clean up code
novystar 0f61c68
use NamespacedUtils instead of fromString method, for runtime error h…
novystar 067f15e
check for null after validation
novystar aa7831f
minor readability changes
novystar 29cf6ac
minor readability changes
novystar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
83 changes: 83 additions & 0 deletions
83
src/main/java/org/skriptlang/skript/bukkit/item/elements/ExprItemModel.java
This file contains hidden or 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,83 @@ | ||
| package org.skriptlang.skript.bukkit.item.elements; | ||
|
|
||
| import ch.njol.skript.aliases.ItemType; | ||
| import ch.njol.skript.bukkitutil.NamespacedUtils; | ||
| import ch.njol.skript.classes.Changer.ChangeMode; | ||
| import ch.njol.skript.doc.Description; | ||
| import ch.njol.skript.doc.Example; | ||
| import ch.njol.skript.doc.Name; | ||
| import ch.njol.skript.doc.Since; | ||
| import ch.njol.skript.expressions.base.SimplePropertyExpression; | ||
| import ch.njol.util.coll.CollectionUtils; | ||
| import org.bukkit.NamespacedKey; | ||
| import org.bukkit.event.Event; | ||
| import org.bukkit.inventory.meta.ItemMeta; | ||
| import org.jetbrains.annotations.Nullable; | ||
| import org.skriptlang.skript.registration.SyntaxRegistry; | ||
|
|
||
| import static ch.njol.skript.classes.Changer.ChangeMode.SET; | ||
|
|
||
| @Name("Item Model") | ||
| @Description("The item model of an item. Accepts a Namespaced Key (e.g. 'minecraft:emerald').") | ||
| @Example(""" | ||
| set the item model of player's held item to "diamond" | ||
| set the item model of {_item} to "minecraft:dirt" | ||
| """) | ||
| @Since("INSERT VERSION") | ||
| public class ExprItemModel extends SimplePropertyExpression<ItemType, String> { | ||
|
|
||
| public static void register(SyntaxRegistry syntaxRegistry) { | ||
| syntaxRegistry.register( | ||
| SyntaxRegistry.EXPRESSION, | ||
| infoBuilder(ExprItemModel.class, String.class, "item model", "itemtypes", true) | ||
| .supplier(ExprItemModel::new) | ||
| .build() | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public @Nullable String convert(ItemType from) { | ||
| NamespacedKey key = from.getItemMeta().getItemModel(); | ||
| if (key == null) | ||
| return null; | ||
|
|
||
| return key.asString(); | ||
| } | ||
|
|
||
| @Override | ||
| public Class<?> @Nullable [] acceptChange(ChangeMode mode) { | ||
| return switch (mode) { | ||
| case SET, DELETE, RESET -> CollectionUtils.array(String.class); | ||
| default -> null; | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public void change(Event event, Object @Nullable [] delta, ChangeMode mode) { | ||
| NamespacedKey key = null; | ||
| if (mode == SET) { | ||
| //noinspection DataFlowIssue | ||
| key = NamespacedUtils.checkValidationAndSend((String) delta[0], this); | ||
| if (key == null) { | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| for (ItemType item : getExpr().getArray(event)) { | ||
| ItemMeta meta = item.getItemMeta(); | ||
| meta.setItemModel(key); | ||
| item.setItemMeta(meta); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public Class<? extends String> getReturnType() { | ||
| return String.class; | ||
| } | ||
|
|
||
| @Override | ||
| protected String getPropertyName() { | ||
| return "item model"; | ||
| } | ||
|
novystar marked this conversation as resolved.
|
||
|
|
||
| } | ||
76 changes: 76 additions & 0 deletions
76
src/main/java/org/skriptlang/skript/bukkit/item/elements/ExprItemWithModel.java
This file contains hidden or 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,76 @@ | ||
| package org.skriptlang.skript.bukkit.item.elements; | ||
|
|
||
| import ch.njol.skript.aliases.ItemType; | ||
| import ch.njol.skript.bukkitutil.NamespacedUtils; | ||
| import ch.njol.skript.doc.*; | ||
| import ch.njol.skript.expressions.base.PropertyExpression; | ||
| import ch.njol.skript.lang.Expression; | ||
| import ch.njol.skript.lang.SkriptParser.ParseResult; | ||
| import ch.njol.util.Kleenean; | ||
| import org.bukkit.NamespacedKey; | ||
| import org.bukkit.event.Event; | ||
| import org.bukkit.inventory.meta.ItemMeta; | ||
| import org.jetbrains.annotations.Nullable; | ||
| import org.skriptlang.skript.registration.DefaultSyntaxInfos; | ||
| import org.skriptlang.skript.registration.SyntaxRegistry; | ||
|
|
||
| @Name("Item With Model") | ||
| @Description("Returns a copy of an item with a custom item model. Accepts a Namespaced Key (e.g. 'minecraft:emerald').") | ||
| @Example(""" | ||
| set {_item} to emerald with model "minecraft:diamond" named "fake diamond"' | ||
| give {_item} to player | ||
| """) | ||
| @Since("INSERT VERSION") | ||
| @Keywords("item model") | ||
| public class ExprItemWithModel extends PropertyExpression<ItemType, ItemType> { | ||
|
|
||
| public static void register(SyntaxRegistry syntaxRegistry) { | ||
| syntaxRegistry.register( | ||
| SyntaxRegistry.EXPRESSION, | ||
| DefaultSyntaxInfos.Expression.builder(ExprItemWithModel.class, ItemType.class) | ||
| .addPattern("%itemtype% with [the] [item] model %string%") | ||
| .supplier(ExprItemWithModel::new) | ||
| .build() | ||
| ); | ||
| } | ||
|
|
||
| private Expression<String> key; | ||
|
|
||
| @Override | ||
| @SuppressWarnings("unchecked") | ||
| public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { | ||
| setExpr((Expression<ItemType>) exprs[0]); | ||
| key = (Expression<String>) exprs[1]; | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| protected ItemType[] get(Event event, ItemType[] source) { | ||
| String key = this.key.getSingle(event); | ||
| if (key == null) | ||
| return source; | ||
|
|
||
| NamespacedKey namespacedKey = NamespacedUtils.checkValidationAndSend(key, this); | ||
| if (namespacedKey == null) | ||
| return source; | ||
|
|
||
| return get(source, itemType -> { | ||
| itemType = itemType.clone(); | ||
| ItemMeta itemMeta = itemType.getItemMeta(); | ||
| itemMeta.setItemModel(namespacedKey); | ||
| itemType.setItemMeta(itemMeta); | ||
| return itemType; | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public Class<? extends ItemType> getReturnType() { | ||
| return ItemType.class; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString(@Nullable Event event, boolean debug) { | ||
| return getExpr().toString(event, debug) + " with item model " + key.toString(event, debug); | ||
| } | ||
|
novystar marked this conversation as resolved.
|
||
|
|
||
| } | ||
This file contains hidden or 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,46 @@ | ||
| using error catching | ||
|
|
||
| test "ExprItemModel": | ||
| set {_item} to diamond | ||
|
|
||
| set item model of {_item} to "emerald" | ||
| assert item model of {_item} is "minecraft:emerald" with "Set item model of item" | ||
|
|
||
| set {_inv} to chest inventory with 1 row named "test slots" | ||
| set slot 1 of {_inv} to diamond | ||
|
|
||
| set item model of slot 1 of {_inv} to "emerald" | ||
|
|
||
| assert item model of slot 1 of {_inv} is "minecraft:emerald" with "Set item model of slot from container" | ||
|
|
||
| set {_item} to dirt with item model "diamond" | ||
| assert item model of {_item} is "minecraft:diamond" with "ItemType with item model" | ||
|
|
||
| reset item model of {_item} | ||
| assert item model of {_item} is not set with "Reset item model of item" | ||
|
|
||
| set item model of {_item} to "minecraft:redstone" | ||
| delete item model of {_item} | ||
| assert item model of {_item} is not set with "Delete item model of item" | ||
|
|
||
| set {_string} to "test_string" | ||
| set item model of {_item} to {_string} | ||
| assert item model of {_item} is "minecraft:test_string" with "Set item model from variable string" | ||
|
|
||
| set item model of {_item} to {_} | ||
| assert item model of {_item} is not set with "Set item model of item to null" | ||
|
|
||
| set {_item} to diamond with model {_} | ||
| assert item model of {_item} is not set with "Item with null item model" | ||
|
|
||
| set {_item} to emerald with model {_string} | ||
| assert item model of {_item} is "minecraft:test_string" with "Item with item model of variable string" | ||
|
|
||
| set {_item} to diamond | ||
| catch runtime errors: | ||
| set item model of {_item} to "with spaces" | ||
| assert last caught runtime errors are set with "Runtime errors produced for invalid key for item model" | ||
|
|
||
| catch runtime errors: | ||
| set {_item} to diamond with model "with spaces" | ||
| assert last caught runtime errors are set with "Runtime errors produced for invalid key for item with model" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.