From b7e5de9401a991a1bbcbef1ec39f10e4d6673f52 Mon Sep 17 00:00:00 2001 From: Bjarne Koll Date: Thu, 25 Apr 2024 18:42:50 +0200 Subject: [PATCH] Update Enchantment damage increase API The Enchantment damage increase API added previously used the EntityCategory enum as a parameter. These values are now however determined by tags instead of the categories themselves. Deprecated the outdated api method, create a new overload that takes EntityType instead and implement deprecated method by guessing an entity type from the builtin registry based on the category passed. This method allows a) the tags to still be modified and the legacy method still respecting such. b) potential cursed implementations of enchantments of plugins to not break that override the getDamageBonus method on Enchantment. --- patches/api/More-Enchantment-API.patch | 14 +++++ patches/server/Add-entity-knockback-API.patch | 4 +- patches/server/More-Enchantment-API.patch | 51 +++++++++---------- 3 files changed, 39 insertions(+), 30 deletions(-) diff --git a/patches/api/More-Enchantment-API.patch b/patches/api/More-Enchantment-API.patch index c83224b2e4..55863eaed9 100644 --- a/patches/api/More-Enchantment-API.patch +++ b/patches/api/More-Enchantment-API.patch @@ -109,10 +109,24 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + * @param level the level of enchantment + * @param entityCategory the category of entity + * @return the damage increase ++ * @deprecated Use {@link #getDamageIncrease(int, org.bukkit.entity.EntityType)} instead. ++ * Enchantment damage increase is no longer handled via {@link org.bukkit.entity.EntityCategory}s, but ++ * is instead controlled by tags, e.g. {@link org.bukkit.Tag#ENTITY_TYPES_SENSITIVE_TO_BANE_OF_ARTHROPODS}. ++ * As such, a category cannot properly represent all entities defined by the tags. + */ ++ @Deprecated(forRemoval = true, since = "1.20.5") + public abstract float getDamageIncrease(int level, @NotNull org.bukkit.entity.EntityCategory entityCategory); + + /** ++ * Gets the damage increase as a result of the level and entity type specified ++ * ++ * @param level the level of enchantment ++ * @param entityType the type of entity. ++ * @return the damage increase ++ */ ++ public abstract float getDamageIncrease(int level, @NotNull org.bukkit.entity.EntityType entityType); ++ ++ /** + * Gets the equipment slots where this enchantment is considered "active". + * + * @return the equipment slots diff --git a/patches/server/Add-entity-knockback-API.patch b/patches/server/Add-entity-knockback-API.patch index 1d65cf2023..3533ad0b90 100644 --- a/patches/server/Add-entity-knockback-API.patch +++ b/patches/server/Add-entity-knockback-API.patch @@ -9,9 +9,9 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java @@ -0,0 +0,0 @@ public class CraftLivingEntity extends CraftEntity implements LivingEntity { - throw new IllegalArgumentException(entityCategory + " is an unrecognized entity category"); + throw new UnsupportedOperationException("Cannot set the hurt direction on a non player"); } - // Paper end - more enchant API + // Paper end - hurt direction API + + // Paper start - knockback API + @Override diff --git a/patches/server/More-Enchantment-API.patch b/patches/server/More-Enchantment-API.patch index d8fe697e12..eb2be97b33 100644 --- a/patches/server/More-Enchantment-API.patch +++ b/patches/server/More-Enchantment-API.patch @@ -53,7 +53,29 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + + @Override + public float getDamageIncrease(int level, org.bukkit.entity.EntityCategory entityCategory) { -+ return handle.getDamageBonus(level, org.bukkit.craftbukkit.entity.CraftLivingEntity.fromBukkitEntityCategory(entityCategory)); ++ return handle.getDamageBonus(level, guessEntityTypeFromEnchantmentCategory(entityCategory)); ++ } ++ ++ @Override ++ public float getDamageIncrease(int level, org.bukkit.entity.EntityType entityType) { ++ return handle.getDamageBonus(level, org.bukkit.craftbukkit.util.CraftMagicNumbers.getEntityTypes(entityType)); ++ } ++ ++ @Deprecated(forRemoval = true) ++ private net.minecraft.world.entity.EntityType guessEntityTypeFromEnchantmentCategory( ++ final org.bukkit.entity.EntityCategory entityCategory ++ ) { ++ final net.minecraft.tags.TagKey> tag = switch (entityCategory) { ++ case ARTHROPOD -> net.minecraft.tags.EntityTypeTags.SENSITIVE_TO_BANE_OF_ARTHROPODS; ++ case UNDEAD -> net.minecraft.tags.EntityTypeTags.SENSITIVE_TO_SMITE; ++ case WATER -> net.minecraft.tags.EntityTypeTags.SENSITIVE_TO_IMPALING; ++ default -> null; ++ }; ++ if (tag == null) return null; ++ ++ return net.minecraft.core.registries.BuiltInRegistries.ENTITY_TYPE.getTag(tag) ++ .map(e -> e.size() > 0 ? e.get(0).value() : null) ++ .orElse(null); + } + + @Override @@ -77,33 +99,6 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 // Paper end @Override -diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java -index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 ---- a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java -+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java -@@ -0,0 +0,0 @@ public class CraftLivingEntity extends CraftEntity implements LivingEntity { - throw new UnsupportedOperationException("Cannot set the hurt direction on a non player"); - } - // Paper end - hurt direction API -+ -+ // Paper start - more enchant API -+ public static MobType fromBukkitEntityCategory(EntityCategory entityCategory) { -+ switch (entityCategory) { -+ case NONE: -+ return MobType.UNDEFINED; -+ case UNDEAD: -+ return MobType.UNDEAD; -+ case ARTHROPOD: -+ return MobType.ARTHROPOD; -+ case ILLAGER: -+ return MobType.ILLAGER; -+ case WATER: -+ return MobType.WATER; -+ } -+ throw new IllegalArgumentException(entityCategory + " is an unrecognized entity category"); -+ } -+ // Paper end - more enchant API - } diff --git a/src/test/java/io/papermc/paper/enchantments/EnchantmentRarityTest.java b/src/test/java/io/papermc/paper/enchantments/EnchantmentRarityTest.java new file mode 100644 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000