2424 */
2525package org .spongepowered .common .data .provider .item .stack ;
2626
27+ import net .minecraft .core .Holder ;
2728import net .minecraft .core .component .DataComponents ;
28- import net .minecraft .world . item . BannerItem ;
29+ import net .minecraft .sounds . SoundEvent ;
2930import net .minecraft .world .item .ItemStack ;
30- import net .minecraft .world .item .ShieldItem ;
31+ import net .minecraft .world .item .component . BlocksAttacks ;
3132import net .minecraft .world .level .block .entity .BannerPatternLayers ;
33+ import org .checkerframework .checker .nullness .qual .Nullable ;
3234import org .spongepowered .api .data .Keys ;
3335import org .spongepowered .api .data .meta .BannerPatternLayer ;
3436import org .spongepowered .api .data .type .DyeColor ;
37+ import org .spongepowered .api .data .type .ShieldDamageReduction ;
38+ import org .spongepowered .api .data .type .ShieldItemDamageFunction ;
39+ import org .spongepowered .api .effect .sound .SoundType ;
40+ import org .spongepowered .api .event .cause .entity .damage .DamageType ;
41+ import org .spongepowered .api .tag .Tag ;
42+ import org .spongepowered .api .util .Ticks ;
43+ import org .spongepowered .common .bridge .tags .TagBridge ;
3544import org .spongepowered .common .data .provider .DataProviderRegistrator ;
45+ import org .spongepowered .common .util .Constants ;
46+
47+ import java .util .List ;
48+ import java .util .Optional ;
3649
3750public final class ShieldItemStackData {
3851
@@ -46,16 +59,201 @@ public static void register(final DataProviderRegistrator registrator) {
4659 .create (Keys .DYE_COLOR )
4760 .get (h -> (DyeColor ) (Object ) h .getOrDefault (DataComponents .BASE_COLOR , net .minecraft .world .item .DyeColor .WHITE ))
4861 .set ((h , v ) -> h .set (DataComponents .BASE_COLOR , (net .minecraft .world .item .DyeColor ) (Object ) v ))
49- .supports (h -> h .getItem () instanceof ShieldItem )
5062 .create (Keys .BANNER_PATTERN_LAYERS )
5163 .get (h -> h .getOrDefault (DataComponents .BANNER_PATTERNS , BannerPatternLayers .EMPTY ).layers ()
5264 .stream ().map (BannerPatternLayer .class ::cast ).toList ())
5365 .set ((h , v ) -> {
5466 h .set (DataComponents .BANNER_PATTERNS , new BannerPatternLayers (v .stream ().map (BannerPatternLayers .Layer .class ::cast ).toList ()));
5567 // TODO check setting banner base? Constants.TileEntity.Banner.BANNER_BASE / BannerPatternShapes.BASE
5668 })
57- .supports (h -> h .getItem () instanceof ShieldItem || h .getItem () instanceof BannerItem );
69+ .create (Keys .SHIELD_DEPLOY_TICKS )
70+ .get (h -> {
71+ final @ Nullable BlocksAttacks blocksAttacks = h .get (DataComponents .BLOCKS_ATTACKS );
72+ if (blocksAttacks == null ) {
73+ return null ;
74+ }
75+ return Ticks .of ((long ) (Constants .TickConversions .TICKS_PER_SECOND * blocksAttacks .blockDelaySeconds ()));
76+ })
77+ .set ((h , v ) -> {
78+ final @ Nullable BlocksAttacks blocksAttacks = h .getOrDefault (DataComponents .BLOCKS_ATTACKS , BLOCKS_ATTACKS_DEFAULTS );
79+ h .set (DataComponents .BLOCKS_ATTACKS , new BlocksAttacks (
80+ v .ticks () / (float ) Constants .TickConversions .TICKS_PER_SECOND ,
81+ blocksAttacks .disableCooldownScale (),
82+ blocksAttacks .damageReductions (),
83+ blocksAttacks .itemDamage (),
84+ blocksAttacks .bypassedBy (),
85+ blocksAttacks .blockSound (),
86+ blocksAttacks .disableSound ()
87+ ));
88+ })
89+ .create (Keys .DISABLE_SHIELD_TICKS_SCALE )
90+ .get (h -> {
91+ final @ Nullable BlocksAttacks blocksAttacks = h .get (DataComponents .BLOCKS_ATTACKS );
92+ if (blocksAttacks == null ) {
93+ return null ;
94+ }
95+ return (double ) blocksAttacks .disableCooldownScale ();
96+ })
97+ .set ((h , v ) -> {
98+ final @ Nullable BlocksAttacks blocksAttacks = h .getOrDefault (DataComponents .BLOCKS_ATTACKS , BLOCKS_ATTACKS_DEFAULTS );
99+ h .set (DataComponents .BLOCKS_ATTACKS , new BlocksAttacks (
100+ blocksAttacks .blockDelaySeconds (),
101+ v .floatValue (),
102+ blocksAttacks .damageReductions (),
103+ blocksAttacks .itemDamage (),
104+ blocksAttacks .bypassedBy (),
105+ blocksAttacks .blockSound (),
106+ blocksAttacks .disableSound ()
107+ ));
108+ })
109+ .create (Keys .SHIELD_DAMAGE_REDUCTIONS )
110+ .get (h -> {
111+ final @ Nullable BlocksAttacks blocksAttacks = h .get (DataComponents .BLOCKS_ATTACKS );
112+ if (blocksAttacks == null ) {
113+ return null ;
114+ }
115+ return (List <ShieldDamageReduction <?>>) (Object ) List .copyOf (blocksAttacks .damageReductions ());
116+ })
117+ .set ((h , v ) -> {
118+ final @ Nullable BlocksAttacks blocksAttacks = h .getOrDefault (DataComponents .BLOCKS_ATTACKS , BLOCKS_ATTACKS_DEFAULTS );
119+ h .set (DataComponents .BLOCKS_ATTACKS , new BlocksAttacks (
120+ blocksAttacks .blockDelaySeconds (),
121+ blocksAttacks .disableCooldownScale (),
122+ (List <BlocksAttacks .DamageReduction >) (Object ) List .copyOf (v ),
123+ blocksAttacks .itemDamage (),
124+ blocksAttacks .bypassedBy (),
125+ blocksAttacks .blockSound (),
126+ blocksAttacks .disableSound ()
127+ ));
128+ })
129+ .create (Keys .SHIELD_ITEM_DAMAGE_FUNCTION )
130+ .get (h -> {
131+ final @ Nullable BlocksAttacks blocksAttacks = h .get (DataComponents .BLOCKS_ATTACKS );
132+ if (blocksAttacks == null ) {
133+ return null ;
134+ }
135+ return (ShieldItemDamageFunction <?>) (Object ) blocksAttacks .itemDamage ();
136+ })
137+ .set ((h , v ) -> {
138+ final @ Nullable BlocksAttacks blocksAttacks = h .getOrDefault (DataComponents .BLOCKS_ATTACKS , BLOCKS_ATTACKS_DEFAULTS );
139+ h .set (DataComponents .BLOCKS_ATTACKS , new BlocksAttacks (
140+ blocksAttacks .blockDelaySeconds (),
141+ blocksAttacks .disableCooldownScale (),
142+ blocksAttacks .damageReductions (),
143+ (BlocksAttacks .ItemDamageFunction ) (Object ) v ,
144+ blocksAttacks .bypassedBy (),
145+ blocksAttacks .blockSound (),
146+ blocksAttacks .disableSound ()
147+ ));
148+ })
149+ .create (Keys .BYPASS_DAMAGE_TAG )
150+ .get (h -> {
151+ final @ Nullable BlocksAttacks blocksAttacks = h .get (DataComponents .BLOCKS_ATTACKS );
152+ if (blocksAttacks == null || blocksAttacks .bypassedBy ().isEmpty ()) {
153+ return null ;
154+ }
155+ return (Tag <DamageType >) (Object ) blocksAttacks .bypassedBy ().get ();
156+ })
157+ .set ((h , v ) -> {
158+ final @ Nullable BlocksAttacks blocksAttacks = h .getOrDefault (DataComponents .BLOCKS_ATTACKS , BLOCKS_ATTACKS_DEFAULTS );
159+ h .set (DataComponents .BLOCKS_ATTACKS , new BlocksAttacks (
160+ blocksAttacks .blockDelaySeconds (),
161+ blocksAttacks .disableCooldownScale (),
162+ blocksAttacks .damageReductions (),
163+ blocksAttacks .itemDamage (),
164+ Optional .of (((TagBridge <net .minecraft .world .damagesource .DamageType >) v ).bridge$asVanillaTag ()),
165+ blocksAttacks .blockSound (),
166+ blocksAttacks .disableSound ()
167+ ));
168+ })
169+ .delete (h -> {
170+ final @ Nullable BlocksAttacks blocksAttacks = h .getOrDefault (DataComponents .BLOCKS_ATTACKS , BLOCKS_ATTACKS_DEFAULTS );
171+ h .set (DataComponents .BLOCKS_ATTACKS , new BlocksAttacks (
172+ blocksAttacks .blockDelaySeconds (),
173+ blocksAttacks .disableCooldownScale (),
174+ blocksAttacks .damageReductions (),
175+ blocksAttacks .itemDamage (),
176+ Optional .empty (),
177+ blocksAttacks .blockSound (),
178+ blocksAttacks .disableSound ()
179+ ));
180+ })
181+ .create (Keys .SHIELD_BLOCK_SOUND )
182+ .get (h -> {
183+ final @ Nullable BlocksAttacks blocksAttacks = h .get (DataComponents .BLOCKS_ATTACKS );
184+ if (blocksAttacks == null || blocksAttacks .blockSound ().isEmpty ()) {
185+ return null ;
186+ }
187+ return (SoundType ) (Object ) blocksAttacks .blockSound ().get ().value ();
188+ })
189+ .set ((h , v ) -> {
190+ final @ Nullable BlocksAttacks blocksAttacks = h .getOrDefault (DataComponents .BLOCKS_ATTACKS , BLOCKS_ATTACKS_DEFAULTS );
191+ h .set (DataComponents .BLOCKS_ATTACKS , new BlocksAttacks (
192+ blocksAttacks .blockDelaySeconds (),
193+ blocksAttacks .disableCooldownScale (),
194+ blocksAttacks .damageReductions (),
195+ blocksAttacks .itemDamage (),
196+ blocksAttacks .bypassedBy (),
197+ Optional .of (Holder .direct ((SoundEvent ) (Object ) v )),
198+ blocksAttacks .disableSound ()
199+ ));
200+ })
201+ .delete (h -> {
202+ final @ Nullable BlocksAttacks blocksAttacks = h .getOrDefault (DataComponents .BLOCKS_ATTACKS , BLOCKS_ATTACKS_DEFAULTS );
203+ h .set (DataComponents .BLOCKS_ATTACKS , new BlocksAttacks (
204+ blocksAttacks .blockDelaySeconds (),
205+ blocksAttacks .disableCooldownScale (),
206+ blocksAttacks .damageReductions (),
207+ blocksAttacks .itemDamage (),
208+ blocksAttacks .bypassedBy (),
209+ Optional .empty (),
210+ blocksAttacks .disableSound ()
211+ ));
212+ })
213+ .create (Keys .SHIELD_DISABLE_SOUND )
214+ .get (h -> {
215+ final @ Nullable BlocksAttacks blocksAttacks = h .get (DataComponents .BLOCKS_ATTACKS );
216+ if (blocksAttacks == null || blocksAttacks .disableSound ().isEmpty ()) {
217+ return null ;
218+ }
219+ return (SoundType ) (Object ) blocksAttacks .disableSound ().get ().value ();
220+ })
221+ .set ((h , v ) -> {
222+ final @ Nullable BlocksAttacks blocksAttacks = h .getOrDefault (DataComponents .BLOCKS_ATTACKS , BLOCKS_ATTACKS_DEFAULTS );
223+ h .set (DataComponents .BLOCKS_ATTACKS , new BlocksAttacks (
224+ blocksAttacks .blockDelaySeconds (),
225+ blocksAttacks .disableCooldownScale (),
226+ blocksAttacks .damageReductions (),
227+ blocksAttacks .itemDamage (),
228+ blocksAttacks .bypassedBy (),
229+ blocksAttacks .blockSound (),
230+ Optional .of (Holder .direct ((SoundEvent ) (Object ) v ))
231+ ));
232+ })
233+ .delete (h -> {
234+ final @ Nullable BlocksAttacks blocksAttacks = h .getOrDefault (DataComponents .BLOCKS_ATTACKS , BLOCKS_ATTACKS_DEFAULTS );
235+ h .set (DataComponents .BLOCKS_ATTACKS , new BlocksAttacks (
236+ blocksAttacks .blockDelaySeconds (),
237+ blocksAttacks .disableCooldownScale (),
238+ blocksAttacks .damageReductions (),
239+ blocksAttacks .itemDamage (),
240+ blocksAttacks .bypassedBy (),
241+ blocksAttacks .blockSound (),
242+ Optional .empty ()
243+ ));
244+ })
245+ ;
58246 }
59247 // @formatter:on
60248
249+ private static final BlocksAttacks BLOCKS_ATTACKS_DEFAULTS = new BlocksAttacks (
250+ 0 ,
251+ 1 ,
252+ List .of (),
253+ BlocksAttacks .ItemDamageFunction .DEFAULT ,
254+ Optional .empty (),
255+ Optional .empty (),
256+ Optional .empty ()
257+ );
258+
61259}
0 commit comments