Skip to content

Commit af1f0b0

Browse files
authored
Expose minecraft:blocks_attacks and minecraft:weapon (#4233)
1 parent 30968e2 commit af1f0b0

14 files changed

Lines changed: 804 additions & 5 deletions

File tree

src/main/java/org/spongepowered/common/data/provider/item/stack/ItemStackData.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import net.minecraft.world.item.component.ItemLore;
4848
import net.minecraft.world.item.component.UseCooldown;
4949
import net.minecraft.world.item.component.UseRemainder;
50+
import net.minecraft.world.item.component.Weapon;
5051
import net.minecraft.world.item.consume_effects.ConsumeEffect;
5152
import org.checkerframework.checker.nullness.qual.Nullable;
5253
import org.spongepowered.api.Platform;
@@ -65,6 +66,7 @@
6566
import org.spongepowered.common.adventure.SpongeAdventure;
6667
import org.spongepowered.common.data.provider.DataProviderRegistrator;
6768
import org.spongepowered.common.item.util.ItemStackUtil;
69+
import org.spongepowered.common.util.Constants;
6870

6971
import java.util.List;
7072
import java.util.Optional;
@@ -370,6 +372,57 @@ public static void register(final DataProviderRegistrator registrator) {
370372
.get(h -> (ResourceKey) (Object) h.get(DataComponents.TOOLTIP_STYLE))
371373
.set((h, v) -> h.set(DataComponents.TOOLTIP_STYLE, (ResourceLocation) (Object) v))
372374
.delete(h -> h.remove(DataComponents.TOOLTIP_STYLE))
375+
.create(Keys.WEAPON_DAMAGE_PER_ATTACK)
376+
.get(h -> {
377+
final @Nullable Weapon weapon = h.get(DataComponents.WEAPON);
378+
if (weapon == null) {
379+
return null;
380+
}
381+
return weapon.itemDamagePerAttack();
382+
})
383+
.set((h, v) -> {
384+
final @Nullable Weapon weapon = h.get(DataComponents.WEAPON);
385+
h.set(DataComponents.WEAPON, new Weapon(v, weapon == null ? 0 : weapon.disableBlockingForSeconds()));
386+
})
387+
.delete(h -> {
388+
final @Nullable Weapon weapon = h.get(DataComponents.WEAPON);
389+
if (weapon == null) {
390+
return;
391+
}
392+
if (weapon.disableBlockingForSeconds() == 0) {
393+
h.remove(DataComponents.WEAPON);
394+
} else {
395+
h.set(DataComponents.WEAPON, new Weapon(0, weapon.disableBlockingForSeconds()));
396+
}
397+
})
398+
.create(Keys.DISABLE_SHIELD_TICKS)
399+
.get(h -> {
400+
final @Nullable Weapon weapon = h.get(DataComponents.WEAPON);
401+
if (weapon == null) {
402+
return null;
403+
}
404+
return Ticks.of(Math.round(
405+
Constants.TickConversions.TICKS_PER_SECOND * weapon.disableBlockingForSeconds()
406+
));
407+
})
408+
.set((h, v) -> {
409+
final @Nullable Weapon weapon = h.get(DataComponents.WEAPON);
410+
h.set(DataComponents.WEAPON, new Weapon(
411+
weapon == null ? 0 : weapon.itemDamagePerAttack(),
412+
v.ticks() / (float) Constants.TickConversions.TICKS_PER_SECOND
413+
));
414+
})
415+
.delete(h -> {
416+
final @Nullable Weapon weapon = h.get(DataComponents.WEAPON);
417+
if (weapon == null) {
418+
return;
419+
}
420+
if (weapon.itemDamagePerAttack() == 0) {
421+
h.remove(DataComponents.WEAPON);
422+
} else {
423+
h.set(DataComponents.WEAPON, new Weapon(weapon.itemDamagePerAttack()));
424+
}
425+
})
373426
;
374427
}
375428
// @formatter:on

src/main/java/org/spongepowered/common/data/provider/item/stack/ShieldItemStackData.java

Lines changed: 202 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,28 @@
2424
*/
2525
package org.spongepowered.common.data.provider.item.stack;
2626

27+
import net.minecraft.core.Holder;
2728
import net.minecraft.core.component.DataComponents;
28-
import net.minecraft.world.item.BannerItem;
29+
import net.minecraft.sounds.SoundEvent;
2930
import net.minecraft.world.item.ItemStack;
30-
import net.minecraft.world.item.ShieldItem;
31+
import net.minecraft.world.item.component.BlocksAttacks;
3132
import net.minecraft.world.level.block.entity.BannerPatternLayers;
33+
import org.checkerframework.checker.nullness.qual.Nullable;
3234
import org.spongepowered.api.data.Keys;
3335
import org.spongepowered.api.data.meta.BannerPatternLayer;
3436
import 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;
3544
import 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

3750
public 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
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* This file is part of Sponge, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) SpongePowered <https://www.spongepowered.org>
5+
* Copyright (c) contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package org.spongepowered.common.item.shield;
26+
27+
import org.spongepowered.api.data.type.ShieldDamageReduction;
28+
29+
public final class SpongeShieldDamageReductionFactory implements ShieldDamageReduction.Factory {
30+
31+
@Override
32+
public ShieldDamageReduction<ShieldDamageReduction.MultiplyAdd> create(final ShieldDamageReduction.MultiplyAdd config) {
33+
return (ShieldDamageReduction<ShieldDamageReduction.MultiplyAdd>) config;
34+
}
35+
36+
}

0 commit comments

Comments
 (0)