-
-
Notifications
You must be signed in to change notification settings - Fork 341
Add ItemActions #2585
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Add ItemActions #2585
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5b7dab8
delete Keys.APPLICABLE_POTION_EFFECTS
MrHell228 32a354b
make PotionEffectType Taggable
MrHell228 a9db08d
Add ConsumeEffects
MrHell228 c0b74b5
fix some registries having not defaulted type
MrHell228 a0ef5e6
add Keys.DEATH_PROTECTION_EFFECTS
MrHell228 323011f
Merge branch 'api-14' into api-14-consume-effects
MrHell228 b4ec15e
rename ConsumeEffect -> ItemActionEffect
MrHell228 24162fe
remove world arg for ItemActionEffect#apply
MrHell228 495986e
Merge branch 'api-14' into api-14-consume-effects
MrHell228 e0debb6
update ItemActionEffect docs
MrHell228 0b3188c
rename to ItemAction
MrHell228 60cc927
Revert "delete Keys.APPLICABLE_POTION_EFFECTS"
MrHell228 34dda9c
Merge branch 'api-14' into api-14-consume-effects
MrHell228 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/main/java/org/spongepowered/api/data/type/ConsumeEffectTypes.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* This file is part of SpongeAPI, licensed under the MIT License (MIT). | ||
* | ||
* Copyright (c) SpongePowered <https://www.spongepowered.org> | ||
* Copyright (c) contributors | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package org.spongepowered.api.data.type; | ||
|
||
import org.spongepowered.api.ResourceKey; | ||
import org.spongepowered.api.Sponge; | ||
import org.spongepowered.api.registry.DefaultedRegistryReference; | ||
import org.spongepowered.api.registry.Registry; | ||
import org.spongepowered.api.registry.RegistryKey; | ||
import org.spongepowered.api.registry.RegistryScope; | ||
import org.spongepowered.api.registry.RegistryScopes; | ||
import org.spongepowered.api.registry.RegistryTypes; | ||
|
||
/** | ||
* <!-- This file is automatically generated. Any manual changes will be overwritten. --> | ||
*/ | ||
@SuppressWarnings("unused") | ||
@RegistryScopes(scopes = RegistryScope.GAME) | ||
public final class ConsumeEffectTypes { | ||
|
||
public static final DefaultedRegistryReference<ItemActionEffectType> APPLY_EFFECTS = ConsumeEffectTypes.key(ResourceKey.minecraft("apply_effects")); | ||
|
||
public static final DefaultedRegistryReference<ItemActionEffectType> CLEAR_ALL_EFFECTS = ConsumeEffectTypes.key(ResourceKey.minecraft("clear_all_effects")); | ||
|
||
public static final DefaultedRegistryReference<ItemActionEffectType> PLAY_SOUND = ConsumeEffectTypes.key(ResourceKey.minecraft("play_sound")); | ||
|
||
public static final DefaultedRegistryReference<ItemActionEffectType> REMOVE_EFFECTS = ConsumeEffectTypes.key(ResourceKey.minecraft("remove_effects")); | ||
|
||
public static final DefaultedRegistryReference<ItemActionEffectType> TELEPORT_RANDOMLY = ConsumeEffectTypes.key(ResourceKey.minecraft("teleport_randomly")); | ||
|
||
private ConsumeEffectTypes() { | ||
} | ||
|
||
public static Registry<ItemActionEffectType> registry() { | ||
return Sponge.game().registry(RegistryTypes.ITEM_ACTION_EFFECT_TYPE); | ||
} | ||
|
||
private static DefaultedRegistryReference<ItemActionEffectType> key(final ResourceKey location) { | ||
return RegistryKey.of(RegistryTypes.ITEM_ACTION_EFFECT_TYPE, location).asDefaultedReference(Sponge::game); | ||
} | ||
} |
202 changes: 202 additions & 0 deletions
202
src/main/java/org/spongepowered/api/data/type/ItemActionEffect.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
/* | ||
* This file is part of SpongeAPI, licensed under the MIT License (MIT). | ||
* | ||
* Copyright (c) SpongePowered <https://www.spongepowered.org> | ||
* Copyright (c) contributors | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package org.spongepowered.api.data.type; | ||
|
||
import org.spongepowered.api.Sponge; | ||
import org.spongepowered.api.data.Keys; | ||
import org.spongepowered.api.effect.potion.PotionEffect; | ||
import org.spongepowered.api.effect.potion.PotionEffectType; | ||
import org.spongepowered.api.effect.sound.SoundType; | ||
import org.spongepowered.api.entity.living.Living; | ||
import org.spongepowered.api.item.inventory.ItemStack; | ||
import org.spongepowered.api.item.inventory.ItemStackLike; | ||
import org.spongepowered.api.tag.Tag; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.function.Supplier; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Represents an effect an {@link ItemStack} can apply after some actions. | ||
* | ||
* @see Keys#CONSUME_EFFECTS | ||
* @see Keys#DEATH_PROTECTION_EFFECTS | ||
*/ | ||
public interface ItemActionEffect { | ||
|
||
static ApplyEffects applyEffects(final Collection<PotionEffect> effects) { | ||
return ItemActionEffect.applyEffects(1.0D, effects); | ||
} | ||
|
||
static ApplyEffects applyEffects(final PotionEffect... effects) { | ||
return ItemActionEffect.applyEffects(1.0D, effects); | ||
} | ||
|
||
static ApplyEffects applyEffects(final double chance, final Collection<PotionEffect> effects) { | ||
return ItemActionEffect.factory().applyEffects(chance, List.copyOf(effects)); | ||
} | ||
|
||
static ApplyEffects applyEffects(final double chance, final PotionEffect... effects) { | ||
return ItemActionEffect.factory().applyEffects(chance, List.of(effects)); | ||
} | ||
|
||
static RemoveEffects removeEffects(final Collection<PotionEffectType> effectTypes) { | ||
return ItemActionEffect.factory().removeEffects(Set.copyOf(effectTypes)); | ||
} | ||
|
||
static RemoveEffects removeEffects(final PotionEffectType... effectTypes) { | ||
return ItemActionEffect.factory().removeEffects(Set.of(effectTypes)); | ||
} | ||
|
||
@SafeVarargs | ||
static RemoveEffects removeEffects(final Supplier<PotionEffectType>... effectTypes) { | ||
return ItemActionEffect.factory().removeEffects(Arrays.stream(effectTypes).map(Supplier::get).collect(Collectors.toSet())); | ||
} | ||
|
||
static RemoveEffects removeEffects(final Tag<PotionEffectType> effectTypeTag) { | ||
return ItemActionEffect.factory().removeEffects(effectTypeTag); | ||
} | ||
|
||
static ClearEffects clearEffects() { | ||
return ItemActionEffect.factory().clearEffects(); | ||
} | ||
|
||
static PlaySound playSound(final SoundType soundType) { | ||
return ItemActionEffect.factory().playSound(soundType); | ||
} | ||
|
||
static PlaySound playSound(final Supplier<SoundType> soundType) { | ||
return ItemActionEffect.factory().playSound(soundType.get()); | ||
} | ||
|
||
static TeleportRandomly teleportRandomly(final double distance) { | ||
return ItemActionEffect.factory().teleportRandomly(distance); | ||
} | ||
|
||
private static Factory factory() { | ||
return Sponge.game().factoryProvider().provide(Factory.class); | ||
} | ||
|
||
/** | ||
* Returns the type of this effect. | ||
* @return The type of this effect | ||
*/ | ||
ItemActionEffectType type(); | ||
|
||
/** | ||
* Tries to apply this effect and returns whether it was successfully applied. | ||
* The definition of success is purely left up to the implementation. | ||
* | ||
* @param entity The entity to apply effect to | ||
* @param stack The item to apply effect with | ||
* @return true if effect was successfully applied | ||
*/ | ||
boolean apply(Living entity, ItemStackLike stack); | ||
|
||
/** | ||
* Applies this effect with {@link ItemStack#empty()}. | ||
* | ||
* @param entity The entity to apply effect to | ||
* @return true if effect was successfully applied | ||
* @see #apply(Living, ItemStackLike) | ||
*/ | ||
default boolean apply(final Living entity) { | ||
return this.apply(entity, ItemStack.empty()); | ||
} | ||
|
||
/** | ||
* Applies {@link PotionEffect}s with chance. | ||
*/ | ||
interface ApplyEffects extends ItemActionEffect { | ||
/** | ||
* Returns the probability for effects to be applied. | ||
* @return The probability for effects to be applied | ||
*/ | ||
double chance(); | ||
|
||
/** | ||
* Returns {@link PotionEffect}s that will be applied. | ||
* @return {@link PotionEffect}s that will be applied | ||
*/ | ||
List<PotionEffect> effects(); | ||
} | ||
|
||
/** | ||
* Removes {@link PotionEffect}s with matching {@link PotionEffectType}s. | ||
*/ | ||
interface RemoveEffects extends ItemActionEffect { | ||
/** | ||
* Returns {@link PotionEffectType}s that will be removed. | ||
* @return {@link PotionEffectType}s that will be removed | ||
*/ | ||
Set<PotionEffectType> effectTypes(); | ||
} | ||
|
||
/** | ||
* Clears all {@link PotionEffect}s. | ||
*/ | ||
interface ClearEffects extends ItemActionEffect { | ||
} | ||
|
||
/** | ||
* Plays {@link SoundType}. | ||
*/ | ||
interface PlaySound extends ItemActionEffect { | ||
/** | ||
* Returns the consumption {@link SoundType}. | ||
* @return The consumption {@link SoundType} | ||
*/ | ||
SoundType soundType(); | ||
} | ||
|
||
/** | ||
* Teleports randomly within maximum distance. | ||
*/ | ||
interface TeleportRandomly extends ItemActionEffect { | ||
/** | ||
* Returns the maximum distance entity can be teleported. | ||
* @return The maximum distance entity can be teleported | ||
*/ | ||
double distance(); | ||
} | ||
|
||
interface Factory { | ||
|
||
ApplyEffects applyEffects(double chance, List<PotionEffect> effects); | ||
|
||
RemoveEffects removeEffects(Set<PotionEffectType> effectTypes); | ||
|
||
RemoveEffects removeEffects(Tag<PotionEffectType> effectTypeTag); | ||
|
||
ClearEffects clearEffects(); | ||
|
||
PlaySound playSound(SoundType soundType); | ||
|
||
TeleportRandomly teleportRandomly(double distance); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/org/spongepowered/api/data/type/ItemActionEffectType.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* This file is part of SpongeAPI, licensed under the MIT License (MIT). | ||
* | ||
* Copyright (c) SpongePowered <https://www.spongepowered.org> | ||
* Copyright (c) contributors | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package org.spongepowered.api.data.type; | ||
|
||
import org.spongepowered.api.registry.DefaultedRegistryValue; | ||
import org.spongepowered.api.util.annotation.CatalogedBy; | ||
|
||
/** | ||
* Represents a possible type of {@link ItemActionEffect}. | ||
*/ | ||
@CatalogedBy(ConsumeEffectTypes.class) | ||
public interface ItemActionEffectType extends DefaultedRegistryValue { | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because API-14 have been released, this would be a breaking change. Rebase the PR to API-15 or keep this key for now and remove it in later versions.