|
| 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.event.cause.entity.damage.DamageType; |
| 29 | +import org.spongepowered.api.event.cause.entity.damage.source.DamageSource; |
| 30 | +import org.spongepowered.api.tag.Tag; |
| 31 | +import org.spongepowered.api.util.ResettableBuilder; |
| 32 | + |
| 33 | +import java.util.Optional; |
| 34 | +import java.util.Set; |
| 35 | + |
| 36 | + |
| 37 | +/** |
| 38 | + * Defines the amount of damage reduced when blocking with a shield-like {@link org.spongepowered.api.item.inventory.ItemStack}. |
| 39 | + */ |
| 40 | +public interface ShieldDamageReduction<T> { |
| 41 | + |
| 42 | + static ShieldDamageReduction<MultiplyAdd> of(MultiplyAdd config) { |
| 43 | + return Sponge.game().factoryProvider().provide(Factory.class).create(config); |
| 44 | + } |
| 45 | + |
| 46 | + T configuration(); |
| 47 | + |
| 48 | + double resolve(DamageSource source, double damage, double angle); |
| 49 | + |
| 50 | + interface Factory { |
| 51 | + |
| 52 | + ShieldDamageReduction<MultiplyAdd> create(MultiplyAdd config); |
| 53 | + |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * The final amount of blocked damage will be {@code constantReduction + fractionalReduction * damage} |
| 58 | + */ |
| 59 | + interface MultiplyAdd { |
| 60 | + |
| 61 | + static Builder builder() { |
| 62 | + return Sponge.game().builderProvider().provide(Builder.class); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Returns the {@link DamageType damage types} this reduction applies to. |
| 67 | + * {@link Optional#empty()} means this reduction is not restricted to any given damage type. |
| 68 | + * |
| 69 | + * @return the affected damage types |
| 70 | + */ |
| 71 | + Optional<Set<DamageType>> damageTypes(); |
| 72 | + |
| 73 | + /** |
| 74 | + * Returns the maximum angle between the users facing direction and the direction of the incoming attack. |
| 75 | + * |
| 76 | + * @return the maximum angle |
| 77 | + */ |
| 78 | + double horizontalBlockingAngle(); |
| 79 | + |
| 80 | + /** |
| 81 | + * Returns the constant amount of damage to be blocked. |
| 82 | + * |
| 83 | + * @return a constant amount of damage to block |
| 84 | + */ |
| 85 | + double constantReduction(); |
| 86 | + |
| 87 | + /** |
| 88 | + * Returns fractional amount of damage to block, where a factor of 1 means that all damage is blocked, |
| 89 | + * and a factor of 0 that no damage is blocked. |
| 90 | + * |
| 91 | + * @return fractional amount of damage to block |
| 92 | + */ |
| 93 | + double fractionalReduction(); |
| 94 | + |
| 95 | + interface Builder extends ResettableBuilder<MultiplyAdd, Builder> { |
| 96 | + |
| 97 | + /** |
| 98 | + * Limits the {@link DamageType damage types} this reduction applies to. |
| 99 | + * |
| 100 | + * @param damageTypes the affected damage types |
| 101 | + * @return This builder, for chaining |
| 102 | + */ |
| 103 | + Builder damageTypes(Set<DamageType> damageTypes); |
| 104 | + |
| 105 | + /** |
| 106 | + * Limits the {@link DamageType damage types} this reduction applies to. |
| 107 | + * |
| 108 | + * @param tag the tag defining affected damage types |
| 109 | + * @return This builder, for chaining |
| 110 | + */ |
| 111 | + Builder damageTypes(Tag<DamageType> tag); |
| 112 | + |
| 113 | + /** |
| 114 | + * Sets the maximum angle between the users facing direction and the direction of the incoming attack. |
| 115 | + * |
| 116 | + * @param angle the maximum angle |
| 117 | + * @return This builder, for chaining |
| 118 | + */ |
| 119 | + Builder horizontalBlockingAngle(double angle); |
| 120 | + |
| 121 | + /** |
| 122 | + * Sets the constant amount of damage to be blocked. |
| 123 | + * |
| 124 | + * @param constant a constant amount of damage to block |
| 125 | + * @return This builder, for chaining |
| 126 | + */ |
| 127 | + Builder constantReduction(double constant); |
| 128 | + |
| 129 | + /** |
| 130 | + * Sets fractional amount of damage to block, where a factor of 1 means that all damage is blocked, |
| 131 | + * and a factor of 0 that no damage is blocked. |
| 132 | + * |
| 133 | + * @param fraction fractional amount of damage to block |
| 134 | + * @return This builder, for chaining |
| 135 | + */ |
| 136 | + Builder fractionalReduction(double fraction); |
| 137 | + |
| 138 | + MultiplyAdd build(); |
| 139 | + |
| 140 | + } |
| 141 | + |
| 142 | + } |
| 143 | + |
| 144 | +} |
0 commit comments