|
| 1 | +/* |
| 2 | + * This file is part of Sponge, 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.common.data.key; |
| 26 | + |
| 27 | +import com.google.common.collect.ImmutableMap; |
| 28 | +import io.leangen.geantyref.GenericTypeReflector; |
| 29 | +import org.junit.jupiter.api.Assertions; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | +import org.spongepowered.api.ResourceKeyed; |
| 32 | +import org.spongepowered.api.data.Key; |
| 33 | +import org.spongepowered.api.data.Keys; |
| 34 | +import org.spongepowered.api.data.type.ShieldDamageReduction; |
| 35 | +import org.spongepowered.api.data.type.ShieldItemDamageFunction; |
| 36 | +import org.spongepowered.api.data.value.Value; |
| 37 | +import org.spongepowered.api.effect.sound.SoundTypes; |
| 38 | +import org.spongepowered.api.event.cause.entity.damage.DamageTypes; |
| 39 | +import org.spongepowered.api.item.ItemTypes; |
| 40 | +import org.spongepowered.api.item.inventory.ItemStack; |
| 41 | +import org.spongepowered.api.tag.DamageTypeTags; |
| 42 | +import org.spongepowered.api.util.Ticks; |
| 43 | + |
| 44 | +import java.util.List; |
| 45 | +import java.util.Map; |
| 46 | +import java.util.Set; |
| 47 | +import java.util.function.Function; |
| 48 | + |
| 49 | +public class KeysTest { |
| 50 | + private final Map<Key<?>, Object> toTest = ImmutableMap.<Key<?>, Object>builder() |
| 51 | + .put(Keys.WEAPON_DAMAGE_PER_ATTACK, 5) |
| 52 | + .put(Keys.DISABLE_BLOCKING_TICKS, Ticks.of(10)) |
| 53 | + .put(Keys.BLOCK_DELAY_TICKS, Ticks.of(15)) |
| 54 | + .put(Keys.DISABLED_BLOCKING_COOLDOWN_SCALE, 2.5f) |
| 55 | + .put(Keys.SHIELD_DAMAGE_REDUCTIONS, List.of(ShieldDamageReduction.builder() |
| 56 | + .horizontalBlockingAngle(45) |
| 57 | + .constantReduction(2) |
| 58 | + .fractionalReduction(0.5) |
| 59 | + .damageTypes(Set.of(DamageTypes.ARROW.get(), DamageTypes.PLAYER_ATTACK.get())) |
| 60 | + .build())) |
| 61 | + .put(Keys.SHIELD_ITEM_DAMAGE_FUNCTION, ShieldItemDamageFunction.builder() |
| 62 | + .constantDamage(5) |
| 63 | + .fractionalDamage(2) |
| 64 | + .minAttackDamage(2.5) |
| 65 | + .build()) |
| 66 | + .put(Keys.SHIELD_BLOCK_SOUND, SoundTypes.ENTITY_SHULKER_HURT.get()) |
| 67 | + .put(Keys.SHIELD_DISABLE_SOUND, SoundTypes.ENTITY_ENDER_DRAGON_DEATH.get()) |
| 68 | + .build(); |
| 69 | + |
| 70 | + @Test |
| 71 | + void testSingleKeys() { |
| 72 | + toTest.forEach(KeysTest::testSingleKeyUnchecked); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void testBypassDamageTag() { |
| 77 | + testSingleKey(Keys.BYPASS_DAMAGE_TAG, DamageTypeTags.BYPASSES_ARMOR, ResourceKeyed::key); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + void testAllWeaponKeys() { |
| 82 | + final var stack = ItemStack.builder() |
| 83 | + .itemType(ItemTypes.DIAMOND_SWORD) |
| 84 | + .add(Keys.WEAPON_DAMAGE_PER_ATTACK, 5) |
| 85 | + .add(Keys.DISABLE_BLOCKING_TICKS, Ticks.of(5)) |
| 86 | + .build(); |
| 87 | + |
| 88 | + Assertions.assertEquals(5, stack.require(Keys.WEAPON_DAMAGE_PER_ATTACK)); |
| 89 | + Assertions.assertEquals(Ticks.of(5), stack.require(Keys.DISABLE_BLOCKING_TICKS)); |
| 90 | + |
| 91 | + stack.remove(Keys.DISABLE_BLOCKING_TICKS); |
| 92 | + Assertions.assertEquals(5, stack.require(Keys.WEAPON_DAMAGE_PER_ATTACK)); |
| 93 | + |
| 94 | + stack.remove(Keys.WEAPON_DAMAGE_PER_ATTACK); |
| 95 | + |
| 96 | + Assertions.assertNull(stack.getOrNull(Keys.WEAPON_DAMAGE_PER_ATTACK)); |
| 97 | + Assertions.assertNull(stack.getOrNull(Keys.DISABLE_BLOCKING_TICKS)); |
| 98 | + } |
| 99 | + |
| 100 | + @SuppressWarnings("unchecked") |
| 101 | + private static <T, V extends Value<T>> void testSingleKeyUnchecked(Key<?> k, Object value) { |
| 102 | + if (!GenericTypeReflector.isSuperType(k.elementType(), value.getClass())) { |
| 103 | + throw new IllegalArgumentException("Invalid value type for key " + k.key() + ": " + value.getClass().getName()); |
| 104 | + } |
| 105 | + |
| 106 | + testSingleKey((Key<V>) k, (T) value, a -> a); |
| 107 | + } |
| 108 | + |
| 109 | + private static <T, V extends Value<T>> void testSingleKey(Key<V> k, T value, Function<T, Object> equalityExtractor) { |
| 110 | + final var stack = ItemStack.builder() |
| 111 | + .itemType(ItemTypes.DIAMOND_SWORD) |
| 112 | + .add(k, value) |
| 113 | + .build(); |
| 114 | + |
| 115 | + Assertions.assertEquals(equalityExtractor.apply(value), equalityExtractor.apply(stack.require(k)), () -> "retrieved value is not equal " + |
| 116 | + "to the original for " + k.key().asString()); |
| 117 | + } |
| 118 | + |
| 119 | +} |
0 commit comments