22
33import io .papermc .paper .adventure .PaperAdventure ;
44import kr .toxicity .libraries .datacomponent .api .Converter ;
5+ import kr .toxicity .libraries .datacomponent .api .TrimPattern ;
56import kr .toxicity .libraries .datacomponent .api .wrapper .*;
67import net .kyori .adventure .text .Component ;
78import net .minecraft .core .Holder ;
89import net .minecraft .core .HolderSet ;
910import net .minecraft .core .registries .BuiltInRegistries ;
1011import net .minecraft .resources .ResourceLocation ;
1112import net .minecraft .world .effect .MobEffect ;
13+ import net .minecraft .world .item .ArmorItem ;
14+ import net .minecraft .world .item .Item ;
1215import net .minecraft .world .level .block .Block ;
1316import org .bukkit .craftbukkit .inventory .CraftItemStack ;
1417import org .bukkit .inventory .ItemStack ;
1518import org .jetbrains .annotations .NotNull ;
1619
17- import java .util .ArrayList ;
18- import java .util .HashMap ;
19- import java .util .Objects ;
20- import java .util .Optional ;
20+ import java .util .*;
21+ import java .util .stream .Collectors ;
2122
2223final class Converters {
24+ static final Converter <Integer , Integer > INTEGER = Converter .of (i -> i , i -> i );
25+ static final Converter <Boolean , Boolean > BOOL = Converter .of (b -> b , b -> b );
26+ static final Converter <Component , net .minecraft .network .chat .Component > COMPONENT = Converter .of (
27+ PaperAdventure ::asVanilla ,
28+ PaperAdventure ::asAdventure
29+ );
30+
2331 private static final Converter <String , ResourceLocation > RESOURCE_LOCATION = Converter .of (
2432 s -> new ResourceLocation ("minecraft" , s ),
2533 ResourceLocation ::getPath
@@ -28,6 +36,10 @@ final class Converters {
2836 s -> Objects .requireNonNull (BuiltInRegistries .MOB_EFFECT .get (RESOURCE_LOCATION .asVanilla (s )), s ),
2937 e -> RESOURCE_LOCATION .asWrapper (Objects .requireNonNull (BuiltInRegistries .MOB_EFFECT .getKey (e )))
3038 );
39+ private static final Converter <String , Item > ITEM = Converter .of (
40+ s -> Objects .requireNonNull (BuiltInRegistries .ITEM .get (RESOURCE_LOCATION .asVanilla (s )), s ),
41+ e -> RESOURCE_LOCATION .asWrapper (Objects .requireNonNull (BuiltInRegistries .ITEM .getKey (e )))
42+ );
3143 private static final Converter <MobEffectInstance , net .minecraft .world .effect .MobEffectInstance > MOB_EFFECT_INSTANCE = MobEffectConverter .INSTANCE ;
3244
3345 private static class MobEffectConverter implements Converter <MobEffectInstance , net .minecraft .world .effect .MobEffectInstance > {
@@ -72,13 +84,105 @@ public net.minecraft.world.effect.MobEffectInstance asVanilla(@NotNull MobEffect
7284 s -> new net .minecraft .advancements .critereon .BlockPredicate (s .blocks ().map (l -> HolderSet .direct (l .stream ().map (n -> Holder .direct (BLOCK .asVanilla (n ))).toList ())), Optional .empty (), Optional .empty ()),
7385 p -> new BlockPredicate (p .blocks ().map (h -> h .stream ().map (b -> BLOCK .asWrapper (b .value ())).toList ()))
7486 );
75-
76- static final Converter <Integer , Integer > INTEGER = Converter .of (i -> i , i -> i );
77- static final Converter <Boolean , Boolean > BOOL = Converter .of (b -> b , b -> b );
78- static final Converter <Component , net .minecraft .network .chat .Component > COMPONENT = Converter .of (
79- PaperAdventure ::asVanilla ,
80- PaperAdventure ::asAdventure
87+ private static final Converter <Ingredient , net .minecraft .world .item .crafting .Ingredient > INGREDIENT = Converter .of (
88+ i -> net .minecraft .world .item .crafting .Ingredient .of (i .entries ().map (CraftItemStack ::asNMSCopy )),
89+ i -> new Ingredient (Arrays .stream (i .getItems ()).map (net .minecraft .world .item .ItemStack ::asBukkitCopy ))
8190 );
91+ private static final Converter <SoundEvent , net .minecraft .sounds .SoundEvent > SOUND_EVENT = Converter .of (
92+ s -> net .minecraft .sounds .SoundEvent .createFixedRangeEvent (RESOURCE_LOCATION .asVanilla (s .id ()), s .distanceToTravel ()),
93+ s -> new SoundEvent (RESOURCE_LOCATION .asWrapper (s .getLocation ()), s .getRange (1F ))
94+ );
95+ private static final Converter <ArmorMaterial , net .minecraft .world .item .ArmorMaterial > ARMOR_MATERIAL = Converter .of (
96+ a -> {
97+ var get = INGREDIENT .asVanilla (a .repairIngredient ().get ());
98+ return new net .minecraft .world .item .ArmorMaterial (
99+ a .defense ().entrySet ().stream ().collect (Collectors .toMap (e -> switch (e .getKey ()) {
100+ case HELMET -> ArmorItem .Type .HELMET ;
101+ case CHESTPLATE -> ArmorItem .Type .CHESTPLATE ;
102+ case LEGGINGS -> ArmorItem .Type .LEGGINGS ;
103+ case BOOTS -> ArmorItem .Type .BOOTS ;
104+ case BODY -> ArmorItem .Type .BODY ;
105+ }, Map .Entry ::getValue )),
106+ a .enchantmentValue (),
107+ Holder .direct (SOUND_EVENT .asVanilla (a .equipSound ())),
108+ () -> get ,
109+ a .layers ().stream ().map (l -> new net .minecraft .world .item .ArmorMaterial .Layer (RESOURCE_LOCATION .asVanilla (l .id ()), l .suffix (), l .dyeable ())).toList (),
110+ a .toughness (),
111+ a .knockbackResistance ()
112+ );
113+ },
114+ a -> {
115+ var assetsName = Arrays .stream (net .minecraft .world .item .ArmorMaterial .Layer .class .getDeclaredFields ()).filter (f -> ResourceLocation .class .isAssignableFrom (f .getType ())).findFirst ().map (f -> {
116+ try {
117+ f .setAccessible (true );
118+ } catch (Exception e ) {
119+ throw new RuntimeException (e );
120+ }
121+ return f ;
122+ }).orElseThrow ();
123+ var suffix = Arrays .stream (net .minecraft .world .item .ArmorMaterial .Layer .class .getDeclaredFields ()).filter (f -> String .class .isAssignableFrom (f .getType ())).findFirst ().map (f -> {
124+ try {
125+ f .setAccessible (true );
126+ } catch (Exception e ) {
127+ throw new RuntimeException (e );
128+ }
129+ return f ;
130+ }).orElseThrow ();
131+ var get = INGREDIENT .asWrapper (a .repairIngredient ().get ());
132+ return new ArmorMaterial (
133+ a .defense ().entrySet ().stream ().collect (Collectors .toMap (e -> switch (e .getKey ()) {
134+ case HELMET -> ArmorMaterial .Type .HELMET ;
135+ case CHESTPLATE -> ArmorMaterial .Type .CHESTPLATE ;
136+ case LEGGINGS -> ArmorMaterial .Type .LEGGINGS ;
137+ case BOOTS -> ArmorMaterial .Type .BOOTS ;
138+ case BODY -> ArmorMaterial .Type .BODY ;
139+ }, Map .Entry ::getValue )),
140+ a .enchantmentValue (),
141+ SOUND_EVENT .asWrapper (a .equipSound ().value ()),
142+ () -> get ,
143+ a .layers ().stream ().map (l -> {
144+ try {
145+ return new ArmorMaterial .Layer (RESOURCE_LOCATION .asWrapper ((ResourceLocation ) assetsName .get (l )), (String ) suffix .get (l ), l .dyeable ());
146+ } catch (Exception e ) {
147+ throw new RuntimeException (e );
148+ }
149+ }).toList (),
150+ a .toughness (),
151+ a .knockbackResistance ()
152+ );
153+ }
154+ );
155+ private static final Converter <TrimMaterial , net .minecraft .world .item .armortrim .TrimMaterial > TRIM_MATERIAL = Converter .of (
156+ t -> new net .minecraft .world .item .armortrim .TrimMaterial (
157+ t .assetName (),
158+ Holder .direct (ITEM .asVanilla (t .ingredient ())),
159+ t .itemModelIndex (),
160+ t .overrideArmorMaterials ().entrySet ().stream ().collect (Collectors .toMap (e -> Holder .direct (ARMOR_MATERIAL .asVanilla (e .getKey ())), Map .Entry ::getValue )),
161+ COMPONENT .asVanilla (t .description ())
162+ ),
163+ t -> new TrimMaterial (
164+ t .assetName (),
165+ ITEM .asWrapper (t .ingredient ().value ()),
166+ t .itemModelIndex (),
167+ t .overrideArmorMaterials ().entrySet ().stream ().collect (Collectors .toMap (e -> ARMOR_MATERIAL .asWrapper (e .getKey ().value ()), Map .Entry ::getValue )),
168+ COMPONENT .asWrapper (t .description ())
169+ )
170+ );
171+ private static final Converter <TrimPattern , net .minecraft .world .item .armortrim .TrimPattern > TRIM_PATTERN = Converter .of (
172+ t -> new net .minecraft .world .item .armortrim .TrimPattern (
173+ RESOURCE_LOCATION .asVanilla (t .assetId ()),
174+ Holder .direct (ITEM .asVanilla (t .templateItem ())),
175+ COMPONENT .asVanilla (t .description ()),
176+ t .decal ()
177+ ),
178+ t -> new TrimPattern (
179+ RESOURCE_LOCATION .asWrapper (t .assetId ()),
180+ ITEM .asWrapper (t .templateItem ().value ()),
181+ COMPONENT .asWrapper (t .description ()),
182+ t .decal ()
183+ )
184+ );
185+
82186 static final Converter <ItemLore , net .minecraft .world .item .component .ItemLore > ITEM_LORE = Converter .of (
83187 l -> new net .minecraft .world .item .component .ItemLore (
84188 l .lines ().stream ().map (COMPONENT ::asVanilla ).toList (),
@@ -192,4 +296,32 @@ public net.minecraft.world.effect.MobEffectInstance asVanilla(@NotNull MobEffect
192296 s -> new net .minecraft .world .item .component .BlockItemStateProperties (new HashMap <>(s .properties ())),
193297 s -> new BlockItemStateProperties (new HashMap <>(s .properties ()))
194298 );
299+ static final Converter <FoodProperties , net .minecraft .world .food .FoodProperties > FOOD = Converter .of (
300+ f -> new net .minecraft .world .food .FoodProperties (
301+ f .nutrition (),
302+ f .saturation (),
303+ f .canAlwaysEat (),
304+ f .eatSeconds (),
305+ f .effects ().stream ().map (e -> new net .minecraft .world .food .FoodProperties .PossibleEffect (MOB_EFFECT_INSTANCE .asVanilla (e .effect ()), e .probability ())).toList ()
306+ ),
307+ f -> new FoodProperties (
308+ f .nutrition (),
309+ f .saturation (),
310+ f .canAlwaysEat (),
311+ f .eatSeconds (),
312+ f .effects ().stream ().map (e -> new FoodProperties .PossibleEffect (MOB_EFFECT_INSTANCE .asWrapper (e .effect ()), e .probability ())).toList ()
313+ )
314+ );
315+ static final Converter <ArmorTrim , net .minecraft .world .item .armortrim .ArmorTrim > TRIM = Converter .of (
316+ t -> new net .minecraft .world .item .armortrim .ArmorTrim (
317+ Holder .direct (TRIM_MATERIAL .asVanilla (t .material ())),
318+ Holder .direct (TRIM_PATTERN .asVanilla (t .pattern ())),
319+ t .showInTooltip ()
320+ ),
321+ t -> new ArmorTrim (
322+ TRIM_MATERIAL .asWrapper (t .material ().value ()),
323+ TRIM_PATTERN .asWrapper (t .pattern ().value ()),
324+ t .showInTooltip
325+ )
326+ );
195327}
0 commit comments