33import org .jetbrains .annotations .Nullable ;
44import org .machinemc .scriptive .Contents ;
55import org .machinemc .scriptive .components .Component ;
6+ import org .machinemc .scriptive .components .TextComponent ;
7+ import org .machinemc .scriptive .serialization .ComponentSerializer ;
68
79import java .util .HashMap ;
810import java .util .Map ;
@@ -30,6 +32,13 @@ public Map<String, Object> asMap() {
3032 );
3133 }
3234
35+ @ SuppressWarnings ("unchecked" )
36+ public static <V extends Value > HoverEvent <?> deserialize (ComponentSerializer serializer , Map <String , Object > map ) {
37+ HoverEvent .Action <V > action = (Action <V >) Action .byName (map .get ("action" ) + "" );
38+ if (action == null ) return null ;
39+ return new HoverEvent <>(action , action .parseValue (serializer , map .get ("contents" )));
40+ }
41+
3342 public static final class Action <V extends Value > {
3443
3544 public static final Action <Text > SHOW_TEXT = new Action <>("show_text" , Text .class );
@@ -52,6 +61,41 @@ public Class<V> type() {
5261 return type ;
5362 }
5463
64+ @ SuppressWarnings ("unchecked" )
65+ public V parseValue (ComponentSerializer serializer , Object value ) {
66+ if (value == null || (this != SHOW_TEXT && !(value instanceof Map <?, ?>))) return null ;
67+ if (this == SHOW_TEXT ) {
68+ if (value instanceof String string )
69+ return (V ) new Text (TextComponent .of (string ));
70+ if (!(value instanceof Map <?, ?> map )) return null ;
71+ return serializer .deserialize ((Map <String , Object >) map );
72+ } else if (this == SHOW_ITEM ) {
73+ Map <String , Object > map = (Map <String , Object >) value ;
74+ if (!map .containsKey ("id" ) || !map .containsKey ("count" )) return null ;
75+ String id = map .get ("id" ) + "" ;
76+ int count = Integer .parseInt (map .get ("count" ) + "" );
77+ String tag = map .containsKey ("tag" ) ? map .get ("tag" ) + "" : null ;
78+ return (V ) new Item (id , count , tag );
79+ } else if (this == SHOW_ENTITY ) {
80+ Map <String , Object > map = (Map <String , Object >) value ;
81+ UUID id = map .containsKey ("id" ) ? UUID .fromString (map .get ("id" ) + "" ) : null ;
82+ if (id == null ) return null ;
83+ String type = map .containsKey ("type" ) ? map .get ("type" ) + "" : null ;
84+ String name = map .containsKey ("name" ) ? map .get ("name" ) + "" : null ;
85+ return (V ) new Entity (id , type , name );
86+ }
87+ throw new UnsupportedOperationException ();
88+ }
89+
90+ public static Action <?> byName (String name ) {
91+ return switch (name ) {
92+ case "show_text" -> SHOW_TEXT ;
93+ case "show_item" -> SHOW_ITEM ;
94+ case "show_entity" -> SHOW_ENTITY ;
95+ default -> null ;
96+ };
97+ }
98+
5599 @ SuppressWarnings ("unchecked" )
56100 public static <V extends Value > Action <V > ofValue (V value ) {
57101 return (Action <V >) switch (value ) {
@@ -61,6 +105,10 @@ public static <V extends Value> Action<V> ofValue(V value) {
61105 };
62106 }
63107
108+ public static Action <?>[] values () {
109+ return new Action []{SHOW_TEXT , SHOW_ITEM , SHOW_ENTITY };
110+ }
111+
64112 }
65113
66114 public interface ValueHolder <V extends Value > {
0 commit comments