99import com .hypixel .hytale .server .core .util .MessageUtil ;
1010import io .github .syst3ms .skriptparser .lang .Expression ;
1111import io .github .syst3ms .skriptparser .lang .TriggerContext ;
12- import io .github .syst3ms .skriptparser .lang .properties .PropertyExpression ;
1312import io .github .syst3ms .skriptparser .parsing .ParseContext ;
1413import io .github .syst3ms .skriptparser .types .changers .ChangeMode ;
1514import org .jetbrains .annotations .NotNull ;
16- import org .jetbrains .annotations .Nullable ;
1715
1816import java .util .Optional ;
1917
20- public class ExprItemStackName extends PropertyExpression <Object , String > {
18+ public class ExprItemStackName implements Expression <Object > {
2119
2220 public static void register (SkriptRegistration reg ) {
23- reg .newPropertyExpression (ExprItemStackName .class , String .class ,
24- "item (:name|description)" , " itemstack/item" )
21+ reg .newExpression (ExprItemStackName .class , Object .class , false ,
22+ "item [:message] (:name|description) of % itemstack/item% " )
2523 .name ("Item Name/Description" )
2624 .description ("Get the name/description of an Item/ItemStack." ,
25+ "The optional `message` will return as a Message otherwise as a string." ,
2726 "Name/description of an ItemStack can be set, but cannot be set for an Item." )
28- .examples ("set {_i} to itemstack of ingredient_stick" ,
27+ .examples ("set {_name} to item name of {_item}" ,
28+ "set {_i} to itemstack of ingredient_stick" ,
2929 "set item name of {_i} to formatted \" <blue>My Cool Item\" " ,
3030 "set item description of {_i} to \" This item is special\" , formatted \" <gradient:#31F527:#27EBF5>REALLY SPECIAL\" and \" I hope you enjoy!\" " ,
3131 "add {_i} to inventory of player" )
3232 .since ("1.1.0" )
3333 .register ();
3434 }
3535
36+ private boolean formatted ;
37+ private Expression <?> item ;
3638 private boolean name ;
3739
3840 @ Override
3941 public boolean init (Expression <?> @ NotNull [] expressions , int matchedPattern , ParseContext parseContext ) {
42+ this .item = expressions [0 ];
43+ this .formatted = parseContext .hasMark ("message" );
4044 this .name = parseContext .hasMark ("name" );
41- return super . init ( expressions , matchedPattern , parseContext ) ;
45+ return true ;
4246 }
4347
4448 @ Override
45- public @ Nullable String getProperty (@ NotNull Object owner ) {
46- Item item ;
47- if (owner instanceof Item i ) {
48- item = i ;
49+ public Object [] getValues (TriggerContext triggerContext ) {
50+ Optional <?> single = this .item .getSingle (triggerContext );
51+ if (single .isEmpty ()) return null ;
52+ Object owner = single .get ();
53+
54+ if (owner instanceof Item it ) {
55+ ItemTranslationProperties prop = it .getTranslationProperties ();
56+ if (this .name ) {
57+ String name = prop .getName ();
58+ if (name == null ) return null ;
59+
60+ Message translation = Message .translation (name );
61+ if (this .formatted ) {
62+ return new Object []{translation };
63+ } else {
64+ return new Object []{MessageUtil .toAnsiString (translation ).toAnsi ()};
65+ }
66+ } else {
67+ String description = prop .getDescription ();
68+ if (description == null ) return null ;
69+
70+ Message translation = Message .translation (description );
71+ if (this .formatted ) {
72+ return new Object []{translation };
73+ } else {
74+ return new Object []{MessageUtil .toAnsiString (translation ).toAnsi ()};
75+ }
76+ }
4977 } else if (owner instanceof ItemStack itemStack ) {
50- item = itemStack .getItem ();
78+ if (this .name ) {
79+ Message displayName = itemStack .getDisplayName ();
80+
81+ if (this .formatted ) {
82+ return new Object []{displayName };
83+ } else {
84+ return new Object []{MessageUtil .toAnsiString (displayName ).toAnsi ()};
85+ }
86+ } else {
87+ Message displayDescription = itemStack .getDisplayDescription ();
88+
89+ if (this .formatted ) {
90+ return new Object []{displayDescription };
91+ } else {
92+
93+ return new Object []{MessageUtil .toAnsiString (displayDescription ).toAnsi ()};
94+ }
95+ }
5196 } else {
5297 return null ;
5398 }
54- ItemTranslationProperties translation = item .getTranslationProperties ();
55- String prop = this .name ? translation .getName () : translation .getDescription ();
56- if (prop == null ) return null ;
57-
58- return MessageUtil .toAnsiString (Message .translation (prop )).toAnsi ();
5999 }
60100
61101 @ Override
@@ -96,7 +136,7 @@ public void change(TriggerContext ctx, ChangeMode changeMode, Object[] changeWit
96136 }
97137 }
98138 }
99- Optional <?> single = getOwner () .getSingle (ctx );
139+ Optional <?> single = this . item .getSingle (ctx );
100140 if (single .isEmpty ()) return ;
101141
102142 if (single .get () instanceof ItemStack itemStack ) {
@@ -107,8 +147,24 @@ public void change(TriggerContext ctx, ChangeMode changeMode, Object[] changeWit
107147 meta .setDescription (message );
108148 }
109149 ItemStack itemStack1 = itemStack .withMetadata (ItemDisplayMetadata .KEYED_CODEC , meta );
110- getOwner ().change (ctx , ChangeMode .SET , new ItemStack []{itemStack1 });
150+ this .item .change (ctx , ChangeMode .SET , new ItemStack []{itemStack1 });
151+ }
152+ }
153+
154+ @ Override
155+ public Class <?> getReturnType () {
156+ if (this .formatted ) {
157+ return Message .class ;
158+ } else {
159+ return String .class ;
111160 }
112161 }
113162
163+ @ Override
164+ public String toString (TriggerContext triggerContext , boolean debug ) {
165+ String f = this .formatted ? "message" : "" ;
166+ String type = this .name ? "name" : "description" ;
167+ return String .format ("item %s %s of %s" , f , type , this .item .toString (triggerContext ,debug ));
168+ }
169+
114170}
0 commit comments