Skip to content

Commit 4beaf1d

Browse files
committed
Implement ConsumeEffects
1 parent 07a4dc7 commit 4beaf1d

15 files changed

Lines changed: 560 additions & 16 deletions

File tree

generator/src/main/java/org/spongepowered/vanilla/generator/item/ItemRegistries.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ public static List<Generator> itemRegistries(final Context context) {
111111
$ -> true,
112112
RegistryScope.SERVER
113113
),
114+
new RegistryEntriesGenerator<>(
115+
"data.type",
116+
"ConsumeEffectTypes",
117+
"CONSUME_EFFECT_TYPE",
118+
context.relativeClass("data.type", "ConsumeEffectType"),
119+
Registries.CONSUME_EFFECT_TYPE
120+
),
114121
new EnumEntriesValidator<>(
115122
"entity.display",
116123
"ItemDisplayTypes",

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

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,11 @@
2525
package org.spongepowered.common.data.provider.item.stack;
2626

2727
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
28-
import net.minecraft.core.Holder;
29-
import net.minecraft.core.HolderSet;
3028
import net.minecraft.core.component.DataComponentPatch;
3129
import net.minecraft.core.component.DataComponents;
3230
import net.minecraft.network.chat.Component;
3331
import net.minecraft.resources.ResourceLocation;
32+
import net.minecraft.sounds.SoundEvents;
3433
import net.minecraft.tags.DamageTypeTags;
3534
import net.minecraft.util.StringUtil;
3635
import net.minecraft.util.Unit;
@@ -49,12 +48,7 @@
4948
import net.minecraft.world.item.component.Unbreakable;
5049
import net.minecraft.world.item.component.UseCooldown;
5150
import net.minecraft.world.item.component.UseRemainder;
52-
import net.minecraft.world.item.consume_effects.ApplyStatusEffectsConsumeEffect;
53-
import net.minecraft.world.item.consume_effects.ClearAllStatusEffectsConsumeEffect;
5451
import net.minecraft.world.item.consume_effects.ConsumeEffect;
55-
import net.minecraft.world.item.consume_effects.PlaySoundConsumeEffect;
56-
import net.minecraft.world.item.consume_effects.RemoveStatusEffectsConsumeEffect;
57-
import net.minecraft.world.item.consume_effects.TeleportRandomlyConsumeEffect;
5852
import org.checkerframework.checker.nullness.qual.Nullable;
5953
import org.spongepowered.api.Platform;
6054
import org.spongepowered.api.ResourceKey;
@@ -79,7 +73,7 @@
7973
public final class ItemStackData {
8074

8175
public static final FoodProperties DEFAULT_FOOD_PROPERTIES = new FoodProperties(0, 0, false);
82-
public static final Consumable DEFAULT_CONSUMABLE_PROPERTIES = new Consumable(1.6F, ItemUseAnimation.EAT, null, true, List.of());
76+
public static final Consumable DEFAULT_CONSUMABLE_PROPERTIES = new Consumable(1.6F, ItemUseAnimation.EAT, SoundEvents.GENERIC_EAT, true, List.of());
8377

8478
private ItemStackData() {
8579
}
@@ -101,13 +95,6 @@ public static void register(final DataProviderRegistrator registrator) {
10195
// TODO DataComponents.RECIPES - for Items.KNOWLEDGE_BOOK
10296
// TODO DataComponents.OMINOUS_BOTTLE_AMPLIFIER 1.21
10397

104-
// TODO rework applicable potion effects to consume effects
105-
ConsumeEffect newPotionEffects = new ApplyStatusEffectsConsumeEffect(List.of());
106-
ConsumeEffect teleportRand = new TeleportRandomlyConsumeEffect(5);
107-
ConsumeEffect removeStatusEffects = new RemoveStatusEffectsConsumeEffect(HolderSet.empty());
108-
ConsumeEffect clearStatusEffects = new ClearAllStatusEffectsConsumeEffect();
109-
ConsumeEffect playSoundEffect = new PlaySoundConsumeEffect(Holder.direct(null));
110-
11198
registrator
11299
.asMutable(ItemStack.class)
113100
.create(Keys.BURN_TIME)
@@ -248,6 +235,13 @@ public static void register(final DataProviderRegistrator registrator) {
248235
})
249236
.set((h, v) -> h.update(DataComponents.CONSUMABLE, DEFAULT_CONSUMABLE_PROPERTIES,
250237
c -> new Consumable(v.ticks() / 20f, c.animation(), c.sound(), c.hasConsumeParticles(), c.onConsumeEffects())))
238+
.create(Keys.CONSUME_EFFECTS)
239+
.get(h -> {
240+
final var consumable = h.get(DataComponents.CONSUMABLE);
241+
return consumable == null ? null : (List<org.spongepowered.api.data.type.ConsumeEffect>) (Object) consumable.onConsumeEffects();
242+
})
243+
.set((h, v) -> h.update(DataComponents.CONSUMABLE, DEFAULT_CONSUMABLE_PROPERTIES,
244+
c -> new Consumable(c.consumeSeconds(), c.animation(), c.sound(), c.hasConsumeParticles(), (List<ConsumeEffect>) (Object) v)))
251245
.create(Keys.FOOD_CONVERTS_TO)
252246
.get(h -> {
253247
final var remainder = h.get(DataComponents.USE_REMAINDER);
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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;
26+
27+
import net.minecraft.core.HolderSet;
28+
import net.minecraft.core.registries.BuiltInRegistries;
29+
import net.minecraft.sounds.SoundEvent;
30+
import net.minecraft.tags.TagKey;
31+
import net.minecraft.world.effect.MobEffect;
32+
import net.minecraft.world.item.consume_effects.ApplyStatusEffectsConsumeEffect;
33+
import net.minecraft.world.item.consume_effects.ClearAllStatusEffectsConsumeEffect;
34+
import net.minecraft.world.item.consume_effects.PlaySoundConsumeEffect;
35+
import net.minecraft.world.item.consume_effects.RemoveStatusEffectsConsumeEffect;
36+
import net.minecraft.world.item.consume_effects.TeleportRandomlyConsumeEffect;
37+
import org.spongepowered.api.data.type.ConsumeEffect;
38+
import org.spongepowered.api.effect.potion.PotionEffect;
39+
import org.spongepowered.api.effect.potion.PotionEffectType;
40+
import org.spongepowered.api.effect.sound.SoundType;
41+
import org.spongepowered.api.tag.Tag;
42+
43+
import java.util.List;
44+
import java.util.Objects;
45+
import java.util.Set;
46+
47+
public class SpongeConsumeEffectFactory implements ConsumeEffect.Factory {
48+
49+
@Override
50+
public ConsumeEffect.ApplyEffects applyEffects(final double chance, final List<PotionEffect> effects) {
51+
Objects.requireNonNull(effects, "effects");
52+
if (chance < 0 || chance > 1) {
53+
throw new IllegalArgumentException("chance must be in range [0; 1]: " + chance);
54+
}
55+
return (ConsumeEffect.ApplyEffects) (Object) new ApplyStatusEffectsConsumeEffect((List) List.copyOf(effects), (float) chance);
56+
}
57+
58+
@Override
59+
public ConsumeEffect.RemoveEffects removeEffects(final Set<PotionEffectType> effectTypes) {
60+
Objects.requireNonNull(effectTypes, "effectTypes");
61+
final var holderSet = HolderSet.direct(
62+
effectType -> BuiltInRegistries.MOB_EFFECT.wrapAsHolder((MobEffect) effectType),
63+
effectTypes);
64+
return (ConsumeEffect.RemoveEffects) (Object) new RemoveStatusEffectsConsumeEffect(holderSet);
65+
}
66+
67+
@Override
68+
public ConsumeEffect.RemoveEffects removeEffects(final Tag<PotionEffectType> effectTypeTag) {
69+
Objects.requireNonNull(effectTypeTag, "effectTypeTag");
70+
final var tag = (TagKey<MobEffect>) (Object) effectTypeTag;
71+
final var holderSet = BuiltInRegistries.MOB_EFFECT.get(tag).map(hs -> (HolderSet<MobEffect>) hs).orElse(HolderSet.empty());
72+
return (ConsumeEffect.RemoveEffects) (Object) new RemoveStatusEffectsConsumeEffect(holderSet);
73+
}
74+
75+
@Override
76+
public ConsumeEffect.ClearEffects clearEffects() {
77+
return (ConsumeEffect.ClearEffects) (Object) ClearAllStatusEffectsConsumeEffect.INSTANCE;
78+
}
79+
80+
@Override
81+
public ConsumeEffect.PlaySound playSound(final SoundType soundType) {
82+
Objects.requireNonNull(soundType, "soundType");
83+
return (ConsumeEffect.PlaySound) (Object) new PlaySoundConsumeEffect(BuiltInRegistries.SOUND_EVENT.wrapAsHolder((SoundEvent) (Object) soundType));
84+
}
85+
86+
@Override
87+
public ConsumeEffect.TeleportRandomly teleportRandomly(final double distance) {
88+
if (distance <= 0) {
89+
throw new IllegalArgumentException("distance must be positive: " + distance);
90+
}
91+
return (ConsumeEffect.TeleportRandomly) (Object) new TeleportRandomlyConsumeEffect((float) distance * 2);
92+
}
93+
}

src/main/java/org/spongepowered/common/registry/SpongeFactoryProvider.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.spongepowered.api.command.registrar.tree.CommandTreeNode;
4242
import org.spongepowered.api.command.selector.Selector;
4343
import org.spongepowered.api.data.DataManipulator;
44+
import org.spongepowered.api.data.type.ConsumeEffect;
4445
import org.spongepowered.api.data.type.ToolRule;
4546
import org.spongepowered.api.data.value.Value;
4647
import org.spongepowered.api.effect.ForwardingViewer;
@@ -124,6 +125,7 @@
124125
import org.spongepowered.common.entity.effect.SpongeVanishState;
125126
import org.spongepowered.common.event.SpongeEventListenerRegistration;
126127
import org.spongepowered.common.event.tracking.BlockChangeFlagManager;
128+
import org.spongepowered.common.item.SpongeConsumeEffectFactory;
127129
import org.spongepowered.common.item.SpongeItemStack;
128130
import org.spongepowered.common.item.SpongeItemStackSnapshot;
129131
import org.spongepowered.common.item.SpongeToolRuleFactory;
@@ -282,6 +284,7 @@ public void registerDefaultFactories() {
282284
.registerFactory(NaturalSpawner.Factory.class, new SpongeNaturalSpawnerFactory())
283285
.registerFactory(ScoreFormat.Factory.class, new SpongeScoreFormatFactory())
284286
.registerFactory(ToolRule.Factory.class, new SpongeToolRuleFactory())
287+
.registerFactory(ConsumeEffect.Factory.class, new SpongeConsumeEffectFactory())
285288
.registerFactory(PortalLogic.Factory.class, new SpongePortalLogicFactory())
286289
.registerFactory(RecipeInput.Factory.class, new SpongeRecipeInputFactory())
287290
.registerFactory(ArmorTrim.Factory.class, new SpongeArmorTrimFactory())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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.mixin.api.minecraft.world.item.consume_effects;
26+
27+
import net.minecraft.world.effect.MobEffectInstance;
28+
import net.minecraft.world.item.consume_effects.ApplyStatusEffectsConsumeEffect;
29+
import org.spongepowered.api.data.type.ConsumeEffect;
30+
import org.spongepowered.api.effect.potion.PotionEffect;
31+
import org.spongepowered.asm.mixin.Final;
32+
import org.spongepowered.asm.mixin.Implements;
33+
import org.spongepowered.asm.mixin.Interface;
34+
import org.spongepowered.asm.mixin.Intrinsic;
35+
import org.spongepowered.asm.mixin.Mixin;
36+
import org.spongepowered.asm.mixin.Shadow;
37+
38+
import java.util.List;
39+
40+
@Mixin(ApplyStatusEffectsConsumeEffect.class)
41+
@Implements(@Interface(iface = ConsumeEffect.ApplyEffects.class, prefix = "consumeEffect$"))
42+
public abstract class ApplyStatusEffectsConsumeEffectMixin_API implements ConsumeEffect.ApplyEffects {
43+
44+
@Shadow @Final private float probability;
45+
@Shadow @Final private List<MobEffectInstance> effects;
46+
47+
@Override
48+
public double chance() {
49+
return this.probability;
50+
}
51+
52+
@Intrinsic
53+
public List<PotionEffect> consumeEffect$effects() {
54+
return (List) this.effects;
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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.mixin.api.minecraft.world.item.consume_effects;
26+
27+
import net.minecraft.world.item.consume_effects.ClearAllStatusEffectsConsumeEffect;
28+
import org.spongepowered.api.data.type.ConsumeEffect;
29+
import org.spongepowered.asm.mixin.Mixin;
30+
31+
@Mixin(ClearAllStatusEffectsConsumeEffect.class)
32+
public abstract class ClearAllStatusEffectsConsumeEffectMixin_API implements ConsumeEffect.ClearEffects {
33+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.mixin.api.minecraft.world.item.consume_effects;
26+
27+
import net.minecraft.world.entity.LivingEntity;
28+
import net.minecraft.world.item.ItemStack;
29+
import net.minecraft.world.item.consume_effects.ConsumeEffect;
30+
import net.minecraft.world.level.Level;
31+
import org.spongepowered.api.data.type.ConsumeEffectType;
32+
import org.spongepowered.api.entity.living.Living;
33+
import org.spongepowered.api.item.inventory.ItemStackLike;
34+
import org.spongepowered.api.world.World;
35+
import org.spongepowered.asm.mixin.Mixin;
36+
import org.spongepowered.asm.mixin.Shadow;
37+
import org.spongepowered.common.item.util.ItemStackUtil;
38+
39+
import java.util.Objects;
40+
41+
@Mixin(ConsumeEffect.class)
42+
public interface ConsumeEffectMixin_API extends org.spongepowered.api.data.type.ConsumeEffect {
43+
44+
@Shadow ConsumeEffect.Type<? extends ConsumeEffect> shadow$getType();
45+
@Shadow boolean shadow$apply(Level level, ItemStack itemStack, LivingEntity livingEntity);
46+
47+
@Override
48+
default ConsumeEffectType type() {
49+
return (ConsumeEffectType) (Object) this.shadow$getType();
50+
}
51+
52+
@Override
53+
default boolean apply(final World<?, ?> world, final Living entity, final ItemStackLike stack) {
54+
return this.shadow$apply(
55+
(Level) Objects.requireNonNull(world, "world"),
56+
ItemStackUtil.fromLikeToNative(stack).copy(),
57+
(LivingEntity) Objects.requireNonNull(entity, "entity"));
58+
}
59+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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.mixin.api.minecraft.world.item.consume_effects;
26+
27+
import net.minecraft.world.item.consume_effects.ConsumeEffect;
28+
import org.spongepowered.api.data.type.ConsumeEffectType;
29+
import org.spongepowered.asm.mixin.Mixin;
30+
31+
@Mixin(ConsumeEffect.Type.class)
32+
public abstract class ConsumeEffect_TypeMixin_API implements ConsumeEffectType {
33+
}

0 commit comments

Comments
 (0)