Skip to content

Commit 63ab92c

Browse files
committed
Expose minecraft:blocks_attacks related Keys
� Conflicts: � SpongeAPI
1 parent cd8fb8e commit 63ab92c

7 files changed

Lines changed: 423 additions & 2 deletions

File tree

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

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

27+
import net.minecraft.core.Holder;
2728
import net.minecraft.core.component.DataComponents;
29+
import net.minecraft.sounds.SoundEvent;
2830
import net.minecraft.world.item.BannerItem;
2931
import net.minecraft.world.item.ItemStack;
3032
import net.minecraft.world.item.ShieldItem;
33+
import net.minecraft.world.item.component.BlocksAttacks;
3134
import net.minecraft.world.level.block.entity.BannerPatternLayers;
35+
import org.checkerframework.checker.nullness.qual.Nullable;
3236
import org.spongepowered.api.data.Keys;
3337
import org.spongepowered.api.data.meta.BannerPatternLayer;
3438
import 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;
3546
import 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

3752
public 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
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package org.spongepowered.common.item;
2+
3+
import net.minecraft.core.HolderSet;
4+
import net.minecraft.core.Registry;
5+
import net.minecraft.world.item.component.BlocksAttacks;
6+
import org.spongepowered.api.Sponge;
7+
import org.spongepowered.api.data.type.ShieldDamageReduction;
8+
import org.spongepowered.api.event.cause.entity.damage.DamageType;
9+
import org.spongepowered.api.registry.RegistryTypes;
10+
import org.spongepowered.api.tag.Tag;
11+
import org.spongepowered.common.bridge.tags.TagBridge;
12+
import org.spongepowered.common.util.Preconditions;
13+
14+
import java.util.Optional;
15+
import java.util.Set;
16+
17+
public class SpongeShieldDamageReductionBuilder implements ShieldDamageReduction.Builder {
18+
private HolderSet<net.minecraft.world.damagesource.DamageType> damageTypes;
19+
private Double horizontalBlockingAngle;
20+
private double base = 0;
21+
private double factor = 0;
22+
23+
@Override
24+
public ShieldDamageReduction.Builder damageTypes(Set<DamageType> damageTypes) {
25+
final Registry<net.minecraft.world.damagesource.DamageType> registry = (Registry<net.minecraft.world.damagesource.DamageType>) Sponge.server().registry(RegistryTypes.DAMAGE_TYPE);
26+
27+
this.damageTypes = HolderSet.direct(damageTypes.stream()
28+
.map(dt -> registry.wrapAsHolder((net.minecraft.world.damagesource.DamageType) (Object) dt))
29+
.toList());
30+
31+
return this;
32+
}
33+
34+
@Override
35+
public ShieldDamageReduction.Builder damageTypes(Tag<DamageType> tag) {
36+
final Registry<net.minecraft.world.damagesource.DamageType> registry = (Registry<net.minecraft.world.damagesource.DamageType>) Sponge.server().registry(RegistryTypes.DAMAGE_TYPE);
37+
final var vanillaTag = ((TagBridge<net.minecraft.world.damagesource.DamageType>) tag).bridge$asVanillaTag();
38+
this.damageTypes = registry.getOrThrow(vanillaTag);
39+
40+
return this;
41+
}
42+
43+
@Override
44+
public ShieldDamageReduction.Builder horizontalBlockingAngle(double angle) {
45+
Preconditions.checkArgument(angle > 0, "angle must be positive");
46+
this.horizontalBlockingAngle = angle;
47+
48+
return this;
49+
}
50+
51+
@Override
52+
public ShieldDamageReduction.Builder constantReduction(double constant) {
53+
this.base = constant;
54+
55+
return this;
56+
}
57+
58+
@Override
59+
public ShieldDamageReduction.Builder fractionalReduction(double fraction) {
60+
this.factor = fraction;
61+
62+
return this;
63+
}
64+
65+
@Override
66+
public ShieldDamageReduction build() {
67+
return (ShieldDamageReduction) (Object) new BlocksAttacks.DamageReduction(
68+
horizontalBlockingAngle != null ? horizontalBlockingAngle.floatValue() : 90,
69+
Optional.ofNullable(damageTypes),
70+
(float) base,
71+
(float) factor
72+
);
73+
}
74+
75+
@Override
76+
public ShieldDamageReduction.Builder reset() {
77+
this.damageTypes = null;
78+
this.horizontalBlockingAngle = null;
79+
this.base = 0;
80+
this.factor = 0;
81+
82+
return this;
83+
}
84+
85+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.spongepowered.common.item;
2+
3+
import net.minecraft.world.item.component.BlocksAttacks;
4+
import org.spongepowered.api.data.type.ShieldItemDamageFunction;
5+
import org.spongepowered.common.util.Preconditions;
6+
7+
public class SpongeShieldItemDamageFunctionBuilder implements ShieldItemDamageFunction.Builder {
8+
private double minAttackDamage = 0;
9+
private double constantDamage = 0;
10+
private double fractionalDamage = 0;
11+
12+
@Override
13+
public ShieldItemDamageFunction.Builder minAttackDamage(double minDamage) {
14+
Preconditions.checkArgument(minDamage >= 0, "minAttackDamage must be >= 0");
15+
this.minAttackDamage = minDamage;
16+
17+
return this;
18+
}
19+
20+
@Override
21+
public ShieldItemDamageFunction.Builder constantDamage(double constantDamage) {
22+
this.constantDamage = constantDamage;
23+
24+
return this;
25+
}
26+
27+
@Override
28+
public ShieldItemDamageFunction.Builder fractionalDamage(double fractionalDamage) {
29+
this.fractionalDamage = fractionalDamage;
30+
31+
return this;
32+
}
33+
34+
@Override
35+
public ShieldItemDamageFunction build() {
36+
return (ShieldItemDamageFunction) (Object) new BlocksAttacks.ItemDamageFunction(
37+
(float) minAttackDamage,
38+
(float) constantDamage,
39+
(float) fractionalDamage
40+
);
41+
}
42+
43+
@Override
44+
public ShieldItemDamageFunction.Builder reset() {
45+
this.minAttackDamage = 0;
46+
this.constantDamage = 0;
47+
this.fractionalDamage = 0;
48+
49+
return this;
50+
}
51+
52+
}

0 commit comments

Comments
 (0)