|
| 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 | +} |
0 commit comments