|
4 | 4 | import com.hypixel.hytale.server.core.Message; |
5 | 5 | import com.hypixel.hytale.server.core.asset.type.item.config.Item; |
6 | 6 | import com.hypixel.hytale.server.core.asset.type.item.config.ItemTranslationProperties; |
| 7 | +import com.hypixel.hytale.server.core.asset.type.item.config.metadata.ItemDisplayMetadata; |
7 | 8 | import com.hypixel.hytale.server.core.inventory.ItemStack; |
8 | 9 | import com.hypixel.hytale.server.core.util.MessageUtil; |
9 | 10 | import io.github.syst3ms.skriptparser.lang.Expression; |
| 11 | +import io.github.syst3ms.skriptparser.lang.TriggerContext; |
10 | 12 | import io.github.syst3ms.skriptparser.lang.properties.PropertyExpression; |
11 | 13 | import io.github.syst3ms.skriptparser.parsing.ParseContext; |
| 14 | +import io.github.syst3ms.skriptparser.types.changers.ChangeMode; |
12 | 15 | import org.jetbrains.annotations.NotNull; |
13 | 16 | import org.jetbrains.annotations.Nullable; |
14 | 17 |
|
| 18 | +import java.util.Optional; |
| 19 | + |
15 | 20 | public class ExprItemStackName extends PropertyExpression<Object, String> { |
16 | 21 |
|
17 | 22 | public static void register(SkriptRegistration reg) { |
18 | 23 | reg.newPropertyExpression(ExprItemStackName.class, String.class, |
19 | | - "item (:name|description)", "itemstacks/items") |
| 24 | + "item (:name|description)", "itemstack/item") |
20 | 25 | .name("Item Name/Description") |
21 | 26 | .description("Get the name/description of an Item/ItemStack.", |
22 | | - "These currently cannot be set.") |
| 27 | + "Name/description of an ItemStack can be set, but cannot be set for an Item.") |
| 28 | + .examples("set {_i} to itemstack of ingredient_stick", |
| 29 | + "set item name of {_i} to formatted \"<blue>My Cool Item\"", |
| 30 | + "set item description of {_i} to \"This item is special\", formatted \"<gradient:#31F527:#27EBF5>REALLY SPECIAL\" and \"I hope you enjoy!\"", |
| 31 | + "add {_i} to inventory of player") |
23 | 32 | .since("1.1.0") |
24 | 33 | .register(); |
25 | 34 | } |
@@ -49,4 +58,57 @@ public boolean init(Expression<?> @NotNull [] expressions, int matchedPattern, P |
49 | 58 | return MessageUtil.toAnsiString(Message.translation(prop)).toAnsi(); |
50 | 59 | } |
51 | 60 |
|
| 61 | + @Override |
| 62 | + public Optional<Class<?>[]> acceptsChange(ChangeMode mode) { |
| 63 | + if (mode == ChangeMode.SET || mode == ChangeMode.DELETE || mode == ChangeMode.RESET) { |
| 64 | + if (this.name) { |
| 65 | + return Optional.of(new Class<?>[]{String.class, Message.class}); |
| 66 | + } else { |
| 67 | + return Optional.of(new Class<?>[]{String[].class, Message[].class}); |
| 68 | + } |
| 69 | + } |
| 70 | + return Optional.empty(); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void change(TriggerContext ctx, ChangeMode changeMode, Object[] changeWith) { |
| 75 | + Message message = null; |
| 76 | + if (changeMode == ChangeMode.SET) { |
| 77 | + if (this.name) { |
| 78 | + if (changeWith != null && changeWith.length > 0) { |
| 79 | + if (changeWith[0] instanceof Message m) { |
| 80 | + message = m; |
| 81 | + } else if (changeWith[0] instanceof String s) { |
| 82 | + message = Message.raw(s); |
| 83 | + } |
| 84 | + } |
| 85 | + } else if (changeWith != null) { |
| 86 | + message = Message.empty(); |
| 87 | + for (int i = 0; i < changeWith.length; i++) { |
| 88 | + if (changeWith[i] instanceof Message m) { |
| 89 | + message.insert(m); |
| 90 | + } else if (changeWith[i] instanceof String s) { |
| 91 | + message.insert(s); |
| 92 | + } |
| 93 | + if (i < changeWith.length - 1) { |
| 94 | + message.insert("\n"); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + Optional<?> single = getOwner().getSingle(ctx); |
| 100 | + if (single.isEmpty()) return; |
| 101 | + |
| 102 | + if (single.get() instanceof ItemStack itemStack) { |
| 103 | + ItemDisplayMetadata meta = itemStack.getFromMetadataOrDefault(ItemDisplayMetadata.KEY, ItemDisplayMetadata.CODEC); |
| 104 | + if (this.name) { |
| 105 | + meta.setName(message); |
| 106 | + } else { |
| 107 | + meta.setDescription(message); |
| 108 | + } |
| 109 | + ItemStack itemStack1 = itemStack.withMetadata(ItemDisplayMetadata.KEYED_CODEC, meta); |
| 110 | + getOwner().change(ctx, ChangeMode.SET, new ItemStack[]{itemStack1}); |
| 111 | + } |
| 112 | + } |
| 113 | + |
52 | 114 | } |
0 commit comments