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.
This commit is contained in:
Bjarne Koll 2024-04-25 18:42:50 +02:00
parent 653f0432b5
commit bab31b6f55
No known key found for this signature in database
GPG Key ID: 27F6CCCF55D2EE62
12 changed files with 57 additions and 48 deletions

View File

@ -36,7 +36,7 @@ index 0000000000000000000000000000000000000000..e6a40c1fcea761bd66743b50e3da3d14
+ }
+}
diff --git a/src/main/java/org/bukkit/enchantments/Enchantment.java b/src/main/java/org/bukkit/enchantments/Enchantment.java
index 3375ce0f9ac50cd3f145291ceb587d4e6f72569c..d38a23dcab5312e610d7ea328b167a6105bbc0a7 100644
index 3375ce0f9ac50cd3f145291ceb587d4e6f72569c..a72202b653549b4074feb89748849fcb495ba0d4 100644
--- a/src/main/java/org/bukkit/enchantments/Enchantment.java
+++ b/src/main/java/org/bukkit/enchantments/Enchantment.java
@@ -291,11 +291,7 @@ public abstract class Enchantment implements Keyed, Translatable, net.kyori.adve
@ -51,7 +51,7 @@ index 3375ce0f9ac50cd3f145291ceb587d4e6f72569c..d38a23dcab5312e610d7ea328b167a61
public abstract boolean isCursed();
/**
@@ -329,6 +325,70 @@ public abstract class Enchantment implements Keyed, Translatable, net.kyori.adve
@@ -329,6 +325,84 @@ public abstract class Enchantment implements Keyed, Translatable, net.kyori.adve
* @return the name of the enchantment with {@code level} applied
*/
public abstract net.kyori.adventure.text.@NotNull Component displayName(int level);
@ -109,10 +109,24 @@ index 3375ce0f9ac50cd3f145291ceb587d4e6f72569c..d38a23dcab5312e610d7ea328b167a61
+ * @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

View File

@ -9,7 +9,7 @@ public net.minecraft.world.item.enchantment.Enchantment slots
Co-authored-by: Luis <luisc99@icloud.com>
diff --git a/src/main/java/org/bukkit/craftbukkit/enchantments/CraftEnchantment.java b/src/main/java/org/bukkit/craftbukkit/enchantments/CraftEnchantment.java
index bb8a3e670924ebcce48669c5dce57e2df03fad77..5b6490786a7670a836fbf0a99545f77ae04771a6 100644
index bb8a3e670924ebcce48669c5dce57e2df03fad77..58a24e149ae9500c9db90693ac9eb6446794535c 100644
--- a/src/main/java/org/bukkit/craftbukkit/enchantments/CraftEnchantment.java
+++ b/src/main/java/org/bukkit/craftbukkit/enchantments/CraftEnchantment.java
@@ -71,7 +71,7 @@ public class CraftEnchantment extends Enchantment implements Handleable<net.mine
@ -21,7 +21,7 @@ index bb8a3e670924ebcce48669c5dce57e2df03fad77..5b6490786a7670a836fbf0a99545f77a
}
@Override
@@ -150,6 +150,55 @@ public class CraftEnchantment extends Enchantment implements Handleable<net.mine
@@ -150,6 +150,77 @@ public class CraftEnchantment extends Enchantment implements Handleable<net.mine
public String translationKey() {
return this.handle.getDescriptionId();
}
@ -53,7 +53,29 @@ index bb8a3e670924ebcce48669c5dce57e2df03fad77..5b6490786a7670a836fbf0a99545f77a
+
+ @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<net.minecraft.world.entity.EntityType<?>> 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 bb8a3e670924ebcce48669c5dce57e2df03fad77..5b6490786a7670a836fbf0a99545f77a
// 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 2701e53086f4be07c341cd1e4fcd7a351e77c486..ab4008a94ba622cbe423b0ce1d45333334850494 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
@@ -1010,4 +1010,22 @@ 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..03a53ceb0dc3aaff7b5d10ec57f74d71be90ec3a

View File

@ -45,7 +45,7 @@ index 1e720b96f0367652db6924b8654deaa9467e3d2c..4932ba59a6b70b405f7dd05358f6bb00
// Paper end
}
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
index ab4008a94ba622cbe423b0ce1d45333334850494..6e3bddcbf13eae3707f600fed35ca23873f515ea 100644
index 2701e53086f4be07c341cd1e4fcd7a351e77c486..1cfc3d18fb785410f5acfcf3c338776858efe25a 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
@@ -627,6 +627,23 @@ public class CraftLivingEntity extends CraftEntity implements LivingEntity {

View File

@ -796,7 +796,7 @@ index 2cec61a1bb050c1ef81c5fc3d0afafe9ff29d459..97fa4e1e70203194bd939618b2fad926
+ // Paper end
}
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
index 6e3bddcbf13eae3707f600fed35ca23873f515ea..f95e991f1cc7187fc1795af5a4d85a91dd6b66f8 100644
index 1cfc3d18fb785410f5acfcf3c338776858efe25a..ea929ca51bd35447da4bdb42379dfc12ea2aae67 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
@@ -906,14 +906,29 @@ public class CraftLivingEntity extends CraftEntity implements LivingEntity {

View File

@ -5,7 +5,7 @@ Subject: [PATCH] Stinger API
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
index f95e991f1cc7187fc1795af5a4d85a91dd6b66f8..0c718dc32beb7c4afec64c19edcef41bdf1b8e39 100644
index ea929ca51bd35447da4bdb42379dfc12ea2aae67..f0e9d68381fb75900bcd6976a83fd94028fd2712 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
@@ -353,6 +353,11 @@ public class CraftLivingEntity extends CraftEntity implements LivingEntity {

View File

@ -202,7 +202,7 @@ index 321da9572b896b54ad961e1c7179929b145ae0ac..cbad6add9186a151e1994bb3ed0b11d6
ClientboundSoundEntityPacket packet = new ClientboundSoundEntityPacket(Holder.direct(SoundEvent.createVariableRangeEvent(new ResourceLocation(sound))), net.minecraft.sounds.SoundSource.valueOf(category.name()), craftEntity.getHandle(), volume, pitch, seed);
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
index 0c718dc32beb7c4afec64c19edcef41bdf1b8e39..6344ab27c113dd42c729e0b05b957670c4930722 100644
index f0e9d68381fb75900bcd6976a83fd94028fd2712..a4c6fd2f6066ecc4a36a423cb2980ec60d9c7ec1 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
@@ -516,6 +516,7 @@ public class CraftLivingEntity extends CraftEntity implements LivingEntity {

View File

@ -5,7 +5,7 @@ Subject: [PATCH] Add a consumer parameter to ProjectileSource#launchProjectile
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
index 6344ab27c113dd42c729e0b05b957670c4930722..bf58fb8acd09723cbc55f6ac41c24cccda97bac8 100644
index a4c6fd2f6066ecc4a36a423cb2980ec60d9c7ec1..f3ec9ae17355abf430ebb781b9acd9f529d88a11 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
@@ -561,8 +561,15 @@ public class CraftLivingEntity extends CraftEntity implements LivingEntity {

View File

@ -5,13 +5,13 @@ Subject: [PATCH] Add entity knockback API
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
index bf58fb8acd09723cbc55f6ac41c24cccda97bac8..a188aefec543352b4e0d9ce03103ac842db61fc8 100644
index f3ec9ae17355abf430ebb781b9acd9f529d88a11..3e8740f39a0ecd853d7aa9dcee6f8a3d09b14460 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
@@ -1106,4 +1106,12 @@ public class CraftLivingEntity extends CraftEntity implements LivingEntity {
throw new IllegalArgumentException(entityCategory + " is an unrecognized entity category");
@@ -1088,4 +1088,12 @@ public class CraftLivingEntity extends CraftEntity implements LivingEntity {
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

View File

@ -11,10 +11,10 @@ the logic associated with damaging them
public net.minecraft.world.entity.LivingEntity entityEventForEquipmentBreak(Lnet/minecraft/world/entity/EquipmentSlot;)B
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
index a188aefec543352b4e0d9ce03103ac842db61fc8..e03cd68d6636dbd35249c0535072f22e3bd0d1ab 100644
index 3e8740f39a0ecd853d7aa9dcee6f8a3d09b14460..5fe3e5b9f52dc1d2b9b4adb7ccaaa2bbf591af9c 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
@@ -1114,4 +1114,48 @@ public class CraftLivingEntity extends CraftEntity implements LivingEntity {
@@ -1096,4 +1096,48 @@ public class CraftLivingEntity extends CraftEntity implements LivingEntity {
this.getHandle().knockback(strength, directionX, directionZ);
};
// Paper end - knockback API

View File

@ -133,10 +133,10 @@ index 1a291dd8a287db30e71dcb315599fc4b038764c4..30d62ee4d5cd2ddacb8783b5bbbf475d
public int getHealth() {
return this.getHandle().health;
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
index e03cd68d6636dbd35249c0535072f22e3bd0d1ab..e9172aa5e3ff3ae0a507dcc059468416b9c475f6 100644
index 5fe3e5b9f52dc1d2b9b4adb7ccaaa2bbf591af9c..e87a52f5dbb8cd984fd2203d912ac3f1ff9d68aa 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
@@ -1158,4 +1158,18 @@ public class CraftLivingEntity extends CraftEntity implements LivingEntity {
@@ -1140,4 +1140,18 @@ public class CraftLivingEntity extends CraftEntity implements LivingEntity {
nmsStack.hurtAndBreak(amount, this.getHandle(), slot);
}
// Paper end - ItemStack damage API

View File

@ -43,10 +43,10 @@ index 365281dfc0308c5db0c72b56208d9b87be0e955e..991b94ff1186b1071a94b2662873dc07
@Override
public boolean isInvisible() { // Paper - moved up from LivingEntity
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
index e9172aa5e3ff3ae0a507dcc059468416b9c475f6..9440a10f060fce2f7dd78b9d99605022c7b7f000 100644
index e87a52f5dbb8cd984fd2203d912ac3f1ff9d68aa..2e614eb8cdc249670c3ca2be42704e4de69f3175 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
@@ -1172,4 +1172,16 @@ public class CraftLivingEntity extends CraftEntity implements LivingEntity {
@@ -1154,4 +1154,16 @@ public class CraftLivingEntity extends CraftEntity implements LivingEntity {
this.getHandle().frictionState = state;
}
// Paper end - friction API

View File

@ -5,7 +5,7 @@ Subject: [PATCH] Add method to remove all active potion effects
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
index 9440a10f060fce2f7dd78b9d99605022c7b7f000..22dabf38945c5ee2b5b66cf81e62a83fbca20b9e 100644
index 2e614eb8cdc249670c3ca2be42704e4de69f3175..aa351df679f300018367244c7ccb3e5a59e9276f 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
@@ -555,6 +555,13 @@ public class CraftLivingEntity extends CraftEntity implements LivingEntity {