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