Skip to content

Commit 3462203

Browse files
MrHell228Yeregorix
authored andcommitted
Add ItemActions (#2585)
1 parent 5dd330e commit 3462203

File tree

6 files changed

+316
-2
lines changed

6 files changed

+316
-2
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
import org.spongepowered.api.data.type.HorseColor;
7171
import org.spongepowered.api.data.type.HorseStyle;
7272
import org.spongepowered.api.data.type.InstrumentType;
73+
import org.spongepowered.api.data.type.ItemAction;
7374
import org.spongepowered.api.data.type.ItemTier;
7475
import org.spongepowered.api.data.type.LlamaType;
7576
import org.spongepowered.api.data.type.MatterType;
@@ -779,6 +780,11 @@ public final class Keys {
779780
*/
780781
public static final Key<SetValue<Direction>> CONNECTED_DIRECTIONS = Keys.setKey(ResourceKey.sponge("connected_directions"), Direction.class);
781782

783+
/**
784+
* The {@link ItemAction}s an {@link ItemStack} will apply when consumed.
785+
*/
786+
public static final Key<ListValue<ItemAction>> CONSUME_ACTIONS = Keys.listKey(ResourceKey.sponge("consume_effects"), ItemAction.class);
787+
782788
/**
783789
* The container {@link ItemType} of an {@link ItemStack}.
784790
* e.g. {@link ItemTypes#BUCKET} for a {@link ItemTypes#WATER_BUCKET} stack.
@@ -929,6 +935,11 @@ public final class Keys {
929935
*/
930936
public static final Key<Value<Double>> DAMAGE_PER_BLOCK = Keys.key(ResourceKey.sponge("damage_per_block"), Double.class);
931937

938+
/**
939+
* The {@link ItemAction}s an {@link ItemStack} will apply on death.
940+
*/
941+
public static final Key<ListValue<ItemAction>> DEATH_PROTECTION_ACTIONS = Keys.listKey(ResourceKey.sponge("death_protection_effects"), ItemAction.class);
942+
932943
/**
933944
* The distance at which a {@link BlockState} will decay.
934945
* This usually applies to leaves, for example {@link BlockTypes#OAK_LEAVES}.
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
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.data.Keys;
29+
import org.spongepowered.api.effect.potion.PotionEffect;
30+
import org.spongepowered.api.effect.potion.PotionEffectType;
31+
import org.spongepowered.api.effect.sound.SoundType;
32+
import org.spongepowered.api.entity.living.Living;
33+
import org.spongepowered.api.item.inventory.ItemStack;
34+
import org.spongepowered.api.item.inventory.ItemStackLike;
35+
import org.spongepowered.api.tag.Tag;
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 action an {@link ItemStack} can apply to {@link Living} in different circumstances.
46+
*
47+
* @see Keys#CONSUME_ACTIONS
48+
* @see Keys#DEATH_PROTECTION_ACTIONS
49+
*/
50+
public interface ItemAction {
51+
52+
static ApplyEffects applyEffects(final Collection<PotionEffect> effects) {
53+
return ItemAction.applyEffects(1.0D, effects);
54+
}
55+
56+
static ApplyEffects applyEffects(final PotionEffect... effects) {
57+
return ItemAction.applyEffects(1.0D, effects);
58+
}
59+
60+
static ApplyEffects applyEffects(final double chance, final Collection<PotionEffect> effects) {
61+
return ItemAction.factory().applyEffects(chance, List.copyOf(effects));
62+
}
63+
64+
static ApplyEffects applyEffects(final double chance, final PotionEffect... effects) {
65+
return ItemAction.factory().applyEffects(chance, List.of(effects));
66+
}
67+
68+
static RemoveEffects removeEffects(final Collection<PotionEffectType> effectTypes) {
69+
return ItemAction.factory().removeEffects(Set.copyOf(effectTypes));
70+
}
71+
72+
static RemoveEffects removeEffects(final PotionEffectType... effectTypes) {
73+
return ItemAction.factory().removeEffects(Set.of(effectTypes));
74+
}
75+
76+
@SafeVarargs
77+
static RemoveEffects removeEffects(final Supplier<PotionEffectType>... effectTypes) {
78+
return ItemAction.factory().removeEffects(Arrays.stream(effectTypes).map(Supplier::get).collect(Collectors.toSet()));
79+
}
80+
81+
static RemoveEffects removeEffects(final Tag<PotionEffectType> effectTypeTag) {
82+
return ItemAction.factory().removeEffects(effectTypeTag);
83+
}
84+
85+
static ClearEffects clearEffects() {
86+
return ItemAction.factory().clearEffects();
87+
}
88+
89+
static PlaySound playSound(final SoundType soundType) {
90+
return ItemAction.factory().playSound(soundType);
91+
}
92+
93+
static PlaySound playSound(final Supplier<SoundType> soundType) {
94+
return ItemAction.factory().playSound(soundType.get());
95+
}
96+
97+
static TeleportRandomly teleportRandomly(final double distance) {
98+
return ItemAction.factory().teleportRandomly(distance);
99+
}
100+
101+
private static Factory factory() {
102+
return Sponge.game().factoryProvider().provide(Factory.class);
103+
}
104+
105+
/**
106+
* Returns the type of this effect.
107+
* @return The type of this effect
108+
*/
109+
ItemActionType type();
110+
111+
/**
112+
* Tries to apply this effect and returns whether it was successfully applied.
113+
* The definition of success is purely left up to the implementation.
114+
*
115+
* @param entity The entity to apply effect to
116+
* @param stack The item to apply effect with
117+
* @return true if effect was successfully applied
118+
*/
119+
boolean apply(Living entity, ItemStackLike stack);
120+
121+
/**
122+
* Applies this effect with {@link ItemStack#empty()}.
123+
*
124+
* @param entity The entity to apply effect to
125+
* @return true if effect was successfully applied
126+
* @see #apply(Living, ItemStackLike)
127+
*/
128+
default boolean apply(final Living entity) {
129+
return this.apply(entity, ItemStack.empty());
130+
}
131+
132+
/**
133+
* Applies {@link PotionEffect}s with chance.
134+
*/
135+
interface ApplyEffects extends ItemAction {
136+
/**
137+
* Returns the probability for effects to be applied.
138+
* @return The probability for effects to be applied
139+
*/
140+
double chance();
141+
142+
/**
143+
* Returns {@link PotionEffect}s that will be applied.
144+
* @return {@link PotionEffect}s that will be applied
145+
*/
146+
List<PotionEffect> effects();
147+
}
148+
149+
/**
150+
* Removes {@link PotionEffect}s with matching {@link PotionEffectType}s.
151+
*/
152+
interface RemoveEffects extends ItemAction {
153+
/**
154+
* Returns {@link PotionEffectType}s that will be removed.
155+
* @return {@link PotionEffectType}s that will be removed
156+
*/
157+
Set<PotionEffectType> effectTypes();
158+
}
159+
160+
/**
161+
* Clears all {@link PotionEffect}s.
162+
*/
163+
interface ClearEffects extends ItemAction {
164+
}
165+
166+
/**
167+
* Plays {@link SoundType}.
168+
*/
169+
interface PlaySound extends ItemAction {
170+
/**
171+
* Returns the {@link SoundType} to be played.
172+
* @return The {@link SoundType}
173+
*/
174+
SoundType soundType();
175+
}
176+
177+
/**
178+
* Teleports randomly within maximum distance.
179+
*/
180+
interface TeleportRandomly extends ItemAction {
181+
/**
182+
* Returns the maximum distance entity can be teleported.
183+
* @return The maximum distance entity can be teleported
184+
*/
185+
double distance();
186+
}
187+
188+
interface Factory {
189+
190+
ApplyEffects applyEffects(double chance, List<PotionEffect> effects);
191+
192+
RemoveEffects removeEffects(Set<PotionEffectType> effectTypes);
193+
194+
RemoveEffects removeEffects(Tag<PotionEffectType> effectTypeTag);
195+
196+
ClearEffects clearEffects();
197+
198+
PlaySound playSound(SoundType soundType);
199+
200+
TeleportRandomly teleportRandomly(double distance);
201+
}
202+
}
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 ItemAction}.
32+
*/
33+
@CatalogedBy(ItemActionTypes.class)
34+
public interface ItemActionType 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 ItemActionTypes {
42+
43+
public static final DefaultedRegistryReference<ItemActionType> APPLY_EFFECTS = ItemActionTypes.key(ResourceKey.minecraft("apply_effects"));
44+
45+
public static final DefaultedRegistryReference<ItemActionType> CLEAR_ALL_EFFECTS = ItemActionTypes.key(ResourceKey.minecraft("clear_all_effects"));
46+
47+
public static final DefaultedRegistryReference<ItemActionType> PLAY_SOUND = ItemActionTypes.key(ResourceKey.minecraft("play_sound"));
48+
49+
public static final DefaultedRegistryReference<ItemActionType> REMOVE_EFFECTS = ItemActionTypes.key(ResourceKey.minecraft("remove_effects"));
50+
51+
public static final DefaultedRegistryReference<ItemActionType> TELEPORT_RANDOMLY = ItemActionTypes.key(ResourceKey.minecraft("teleport_randomly"));
52+
53+
private ItemActionTypes() {
54+
}
55+
56+
public static Registry<ItemActionType> registry() {
57+
return Sponge.game().registry(RegistryTypes.ITEM_ACTION_TYPE);
58+
}
59+
60+
private static DefaultedRegistryReference<ItemActionType> key(final ResourceKey location) {
61+
return RegistryKey.of(RegistryTypes.ITEM_ACTION_TYPE, location).asDefaultedReference(Sponge::game);
62+
}
63+
}

src/main/java/org/spongepowered/api/effect/potion/PotionEffectType.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525
package org.spongepowered.api.effect.potion;
2626

2727
import net.kyori.adventure.text.ComponentLike;
28-
import org.spongepowered.api.registry.DefaultedRegistryValue;
28+
import org.spongepowered.api.tag.Taggable;
2929
import org.spongepowered.api.util.annotation.CatalogedBy;
3030

3131
/**
3232
* Represents a possible type of {@link PotionEffect}.
3333
*/
3434
@CatalogedBy(PotionEffectTypes.class)
35-
public interface PotionEffectType extends DefaultedRegistryValue<PotionEffectType>, ComponentLike {
35+
public interface PotionEffectType extends Taggable<PotionEffectType>, ComponentLike {
3636

3737
/**
3838
* Gets whether this potion effect is applied instantly or over time.

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import org.spongepowered.api.data.type.HorseColor;
6666
import org.spongepowered.api.data.type.HorseStyle;
6767
import org.spongepowered.api.data.type.InstrumentType;
68+
import org.spongepowered.api.data.type.ItemActionType;
6869
import org.spongepowered.api.data.type.ItemTier;
6970
import org.spongepowered.api.data.type.JigsawBlockOrientation;
7071
import org.spongepowered.api.data.type.LlamaType;
@@ -224,6 +225,8 @@ public final class RegistryTypes {
224225

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

228+
public static final DefaultedRegistryType<ItemActionType> ITEM_ACTION_TYPE = RegistryTypes.minecraftKeyInGame("consume_effect_type");
229+
227230
public static final DefaultedRegistryType<ContainerType> CONTAINER_TYPE = RegistryTypes.minecraftKeyInGame("menu");
228231

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

0 commit comments

Comments
 (0)