Skip to content

Commit a9db08d

Browse files
committed
Add ConsumeEffects
1 parent 32a354b commit a9db08d

File tree

5 files changed

+331
-0
lines changed

5 files changed

+331
-0
lines changed

src/main/java/org/spongepowered/api/data/Keys.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import org.spongepowered.api.data.type.CatType;
6262
import org.spongepowered.api.data.type.ChestAttachmentType;
6363
import org.spongepowered.api.data.type.ComparatorMode;
64+
import org.spongepowered.api.data.type.ConsumeEffect;
6465
import org.spongepowered.api.data.type.DoorHinge;
6566
import org.spongepowered.api.data.type.DripstoneSegment;
6667
import org.spongepowered.api.data.type.DyeColor;
@@ -772,6 +773,11 @@ public final class Keys {
772773
*/
773774
public static final Key<SetValue<Direction>> CONNECTED_DIRECTIONS = Keys.setKey(ResourceKey.sponge("connected_directions"), Direction.class);
774775

776+
/**
777+
* The {@link ConsumeEffect}s an {@link ItemStack} will apply when consumed.
778+
*/
779+
public static final Key<ListValue<ConsumeEffect>> CONSUME_EFFECTS = Keys.listKey(ResourceKey.sponge("consume_effects"), ConsumeEffect.class);
780+
775781
/**
776782
* The container {@link ItemType} of an {@link ItemStack}.
777783
* e.g. {@link ItemTypes#BUCKET} for a {@link ItemTypes#WATER_BUCKET} stack.
Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
/*
2+
* This file is part of SpongeAPI, 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.api.data.type;
26+
27+
import org.spongepowered.api.Sponge;
28+
import org.spongepowered.api.effect.potion.PotionEffect;
29+
import org.spongepowered.api.effect.potion.PotionEffectType;
30+
import org.spongepowered.api.effect.sound.SoundType;
31+
import org.spongepowered.api.entity.living.Living;
32+
import org.spongepowered.api.item.inventory.ItemStack;
33+
import org.spongepowered.api.item.inventory.ItemStackLike;
34+
import org.spongepowered.api.tag.Tag;
35+
import org.spongepowered.api.world.World;
36+
37+
import java.util.Arrays;
38+
import java.util.Collection;
39+
import java.util.List;
40+
import java.util.Set;
41+
import java.util.function.Supplier;
42+
import java.util.stream.Collectors;
43+
44+
/**
45+
* Represents an effect that can be applied on {@link ItemStack} consumption.
46+
*/
47+
public interface ConsumeEffect {
48+
49+
static ApplyEffects applyEffects(final Collection<PotionEffect> effects) {
50+
return ConsumeEffect.applyEffects(1.0D, effects);
51+
}
52+
53+
static ApplyEffects applyEffects(final PotionEffect... effects) {
54+
return ConsumeEffect.applyEffects(1.0D, effects);
55+
}
56+
57+
static ApplyEffects applyEffects(final double chance, final Collection<PotionEffect> effects) {
58+
return ConsumeEffect.factory().applyEffects(chance, List.copyOf(effects));
59+
}
60+
61+
static ApplyEffects applyEffects(final double chance, final PotionEffect... effects) {
62+
return ConsumeEffect.factory().applyEffects(chance, List.of(effects));
63+
}
64+
65+
static RemoveEffects removeEffects(final Collection<PotionEffectType> effectTypes) {
66+
return ConsumeEffect.factory().removeEffects(Set.copyOf(effectTypes));
67+
}
68+
69+
static RemoveEffects removeEffects(final PotionEffectType... effectTypes) {
70+
return ConsumeEffect.factory().removeEffects(Set.of(effectTypes));
71+
}
72+
73+
@SafeVarargs
74+
static RemoveEffects removeEffects(final Supplier<PotionEffectType>... effectTypes) {
75+
return ConsumeEffect.factory().removeEffects(Arrays.stream(effectTypes).map(Supplier::get).collect(Collectors.toSet()));
76+
}
77+
78+
static RemoveEffects removeEffects(final Tag<PotionEffectType> effectTypeTag) {
79+
return ConsumeEffect.factory().removeEffects(effectTypeTag);
80+
}
81+
82+
static ClearEffects clearEffects() {
83+
return ConsumeEffect.factory().clearEffects();
84+
}
85+
86+
static PlaySound playSound(final SoundType soundType) {
87+
return ConsumeEffect.factory().playSound(soundType);
88+
}
89+
90+
static PlaySound playSound(final Supplier<SoundType> soundType) {
91+
return ConsumeEffect.factory().playSound(soundType.get());
92+
}
93+
94+
static TeleportRandomly teleportRandomly(final double distance) {
95+
return ConsumeEffect.factory().teleportRandomly(distance);
96+
}
97+
98+
private static Factory factory() {
99+
return Sponge.game().factoryProvider().provide(Factory.class);
100+
}
101+
102+
/**
103+
* Returns the type of this effect.
104+
* @return The type of this effect
105+
*/
106+
ConsumeEffectType type();
107+
108+
/**
109+
* Tries to apply this effect and returns whether it was successfully applied.
110+
* The definition of success is purely left up to the implementation.
111+
*
112+
* @param world The world to apply effect in
113+
* @param entity The entity to apply effect to
114+
* @param stack The item to apply effect with
115+
* @return true if effect was successfully applied
116+
*/
117+
boolean apply(World<?, ?> world, Living entity, ItemStackLike stack);
118+
119+
/**
120+
* Applies this effect with {@link ItemStack#empty()}.
121+
*
122+
* @param world The world to apply effect in
123+
* @param entity The entity to apply effect to
124+
* @return true if effect was successfully applied
125+
* @see #apply(World, Living, ItemStackLike)
126+
*/
127+
default boolean apply(final World<?, ?> world, final Living entity) {
128+
return this.apply(world, entity, ItemStack.empty());
129+
}
130+
131+
/**
132+
* Applies this effect with {@link Living#world()}.
133+
*
134+
* @param entity The entity to apply effect to
135+
* @param stack The item to apply effect with
136+
* @return true if effect was successfully applied
137+
* @see #apply(World, Living, ItemStackLike)
138+
*/
139+
default boolean apply(final Living entity, final ItemStackLike stack) {
140+
return this.apply(entity.world(), entity, stack);
141+
}
142+
143+
/**
144+
* Applies this effect with {@link Living#world()} and {@link ItemStack#empty()}.
145+
*
146+
* @param entity The entity to apply effect to
147+
* @return true if effect was successfully applied
148+
* @see #apply(World, Living, ItemStackLike)
149+
*/
150+
default boolean apply(final Living entity) {
151+
return this.apply(entity.world(), entity, ItemStack.empty());
152+
}
153+
154+
/**
155+
* Applies {@link PotionEffect}s with chance.
156+
*/
157+
interface ApplyEffects extends ConsumeEffect {
158+
/**
159+
* Returns the probability for effects to be applied.
160+
* @return The probability for effects to be applied
161+
*/
162+
double chance();
163+
164+
/**
165+
* Returns {@link PotionEffect}s that will be applied.
166+
* @return {@link PotionEffect}s that will be applied
167+
*/
168+
List<PotionEffect> effects();
169+
}
170+
171+
/**
172+
* Removes {@link PotionEffect}s with matching {@link PotionEffectType}s.
173+
*/
174+
interface RemoveEffects extends ConsumeEffect {
175+
/**
176+
* Returns {@link PotionEffectType}s that will be removed.
177+
* @return {@link PotionEffectType}s that will be removed
178+
*/
179+
Set<PotionEffectType> effectTypes();
180+
}
181+
182+
/**
183+
* Clears all {@link PotionEffect}s.
184+
*/
185+
interface ClearEffects extends ConsumeEffect {
186+
}
187+
188+
/**
189+
* Plays {@link SoundType}.
190+
*/
191+
interface PlaySound extends ConsumeEffect {
192+
/**
193+
* Returns the consumption {@link SoundType}.
194+
* @return The consumption {@link SoundType}
195+
*/
196+
SoundType soundType();
197+
}
198+
199+
/**
200+
* Teleports randomly within maximum distance.
201+
*/
202+
interface TeleportRandomly extends ConsumeEffect {
203+
/**
204+
* Returns the maximum distance entity can be teleported.
205+
* @return The maximum distance entity can be teleported
206+
*/
207+
double distance();
208+
}
209+
210+
interface Factory {
211+
212+
ApplyEffects applyEffects(double chance, List<PotionEffect> effects);
213+
214+
RemoveEffects removeEffects(Set<PotionEffectType> effectTypes);
215+
216+
RemoveEffects removeEffects(Tag<PotionEffectType> effectTypeTag);
217+
218+
ClearEffects clearEffects();
219+
220+
PlaySound playSound(SoundType soundType);
221+
222+
TeleportRandomly teleportRandomly(double distance);
223+
}
224+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* This file is part of SpongeAPI, 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.api.data.type;
26+
27+
import org.spongepowered.api.registry.DefaultedRegistryValue;
28+
import org.spongepowered.api.util.annotation.CatalogedBy;
29+
30+
/**
31+
* Represents a possible type of {@link ConsumeEffect}.
32+
*/
33+
@CatalogedBy(ConsumeEffectTypes.class)
34+
public interface ConsumeEffectType extends DefaultedRegistryValue {
35+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* This file is part of SpongeAPI, 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.api.data.type;
26+
27+
import org.spongepowered.api.ResourceKey;
28+
import org.spongepowered.api.Sponge;
29+
import org.spongepowered.api.registry.DefaultedRegistryReference;
30+
import org.spongepowered.api.registry.Registry;
31+
import org.spongepowered.api.registry.RegistryKey;
32+
import org.spongepowered.api.registry.RegistryScope;
33+
import org.spongepowered.api.registry.RegistryScopes;
34+
import org.spongepowered.api.registry.RegistryTypes;
35+
36+
/**
37+
* <!-- This file is automatically generated. Any manual changes will be overwritten. -->
38+
*/
39+
@SuppressWarnings("unused")
40+
@RegistryScopes(scopes = RegistryScope.GAME)
41+
public final class ConsumeEffectTypes {
42+
43+
public static final DefaultedRegistryReference<ConsumeEffectType> APPLY_EFFECTS = ConsumeEffectTypes.key(ResourceKey.minecraft("apply_effects"));
44+
45+
public static final DefaultedRegistryReference<ConsumeEffectType> CLEAR_ALL_EFFECTS = ConsumeEffectTypes.key(ResourceKey.minecraft("clear_all_effects"));
46+
47+
public static final DefaultedRegistryReference<ConsumeEffectType> PLAY_SOUND = ConsumeEffectTypes.key(ResourceKey.minecraft("play_sound"));
48+
49+
public static final DefaultedRegistryReference<ConsumeEffectType> REMOVE_EFFECTS = ConsumeEffectTypes.key(ResourceKey.minecraft("remove_effects"));
50+
51+
public static final DefaultedRegistryReference<ConsumeEffectType> TELEPORT_RANDOMLY = ConsumeEffectTypes.key(ResourceKey.minecraft("teleport_randomly"));
52+
53+
private ConsumeEffectTypes() {
54+
}
55+
56+
public static Registry<ConsumeEffectType> registry() {
57+
return Sponge.game().registry(RegistryTypes.CONSUME_EFFECT_TYPE);
58+
}
59+
60+
private static DefaultedRegistryReference<ConsumeEffectType> key(final ResourceKey location) {
61+
return RegistryKey.of(RegistryTypes.CONSUME_EFFECT_TYPE, location).asDefaultedReference(Sponge::game);
62+
}
63+
}

src/main/java/org/spongepowered/api/registry/RegistryTypes.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import org.spongepowered.api.data.type.CatType;
5757
import org.spongepowered.api.data.type.ChestAttachmentType;
5858
import org.spongepowered.api.data.type.ComparatorMode;
59+
import org.spongepowered.api.data.type.ConsumeEffectType;
5960
import org.spongepowered.api.data.type.DoorHinge;
6061
import org.spongepowered.api.data.type.DripstoneSegment;
6162
import org.spongepowered.api.data.type.DyeColor;
@@ -226,6 +227,8 @@ public final class RegistryTypes {
226227

227228
public static final DefaultedRegistryType<ChunkState> CHUNK_STATE = RegistryTypes.minecraftKeyInGame("chunk_status");
228229

230+
public static final DefaultedRegistryType<ConsumeEffectType> CONSUME_EFFECT_TYPE = RegistryTypes.minecraftKeyInGame("consume_effect_type");
231+
229232
public static final DefaultedRegistryType<ContainerType> CONTAINER_TYPE = RegistryTypes.minecraftKeyInGame("menu");
230233

231234
public static final DefaultedRegistryType<DensityFunction> DENSITY_FUNCTION = RegistryTypes.minecraftKeyInServer("worldgen/density_function");

0 commit comments

Comments
 (0)