diff --git a/paper-api/src/main/java/io/papermc/paper/InternalAPIBridge.java b/paper-api/src/main/java/io/papermc/paper/InternalAPIBridge.java index 5a039f815a29..1590367ec2ff 100644 --- a/paper-api/src/main/java/io/papermc/paper/InternalAPIBridge.java +++ b/paper-api/src/main/java/io/papermc/paper/InternalAPIBridge.java @@ -29,6 +29,7 @@ import org.bukkit.entity.EntityType; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Pose; +import org.bukkit.entity.SpawnCategory; import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.java.JavaPlugin; @@ -97,6 +98,8 @@ class Holder { String getTranslationKey(EntityType entityType); + SpawnCategory getSpawnCategory(EntityType entityType); + /* * Called once by the version command on first use, then cached. */ diff --git a/paper-api/src/main/java/org/bukkit/entity/EntityType.java b/paper-api/src/main/java/org/bukkit/entity/EntityType.java index 1583067fe6fa..543bd4f08285 100644 --- a/paper-api/src/main/java/org/bukkit/entity/EntityType.java +++ b/paper-api/src/main/java/org/bukkit/entity/EntityType.java @@ -335,7 +335,7 @@ public String getTranslationKey() { // Paper start /** - * @throws IllegalArgumentException if the entity does not have a translation key (is probably a custom entity) + * @throws IllegalArgumentException if the entity does not have a translation key */ @Override public @NotNull String translationKey() { @@ -344,7 +344,18 @@ public String getTranslationKey() { } /** - * Checks if the entity has default attributes. + * Gets the spawn category of this entity type. + * + * @return the spawn category + * @throws IllegalArgumentException if the entity does not have a spawn category + */ + public @NotNull SpawnCategory getSpawnCategory() { + Preconditions.checkArgument(this != UNKNOWN, "UNKNOWN entities do not have a spawn category"); + return InternalAPIBridge.get().getSpawnCategory(this); + } + + /** + * Checks if the entity type has default attributes. * * @return true if it has default attributes */ @@ -356,10 +367,10 @@ public boolean hasDefaultAttributes() { } /** - * Gets the default attributes for the entity. + * Gets the default attributes for the entity type. * * @return an unmodifiable instance of Attributable for reading default attributes. - * @throws IllegalArgumentException if the entity does not exist of have default attributes (use {@link #hasDefaultAttributes()} first) + * @throws IllegalArgumentException if it doesn't have default attributes (use {@link #hasDefaultAttributes()} first) */ public @NotNull org.bukkit.attribute.Attributable getDefaultAttributes() { Preconditions.checkArgument(this.hasDefaultAttributes(), this.key + " doesn't have default attributes"); diff --git a/paper-server/src/main/java/io/papermc/paper/PaperServerInternalAPIBridge.java b/paper-server/src/main/java/io/papermc/paper/PaperServerInternalAPIBridge.java index f4d5123d9488..06c0dbd40996 100644 --- a/paper-server/src/main/java/io/papermc/paper/PaperServerInternalAPIBridge.java +++ b/paper-server/src/main/java/io/papermc/paper/PaperServerInternalAPIBridge.java @@ -59,6 +59,7 @@ import org.bukkit.craftbukkit.inventory.CraftItemStack; import org.bukkit.craftbukkit.util.CraftMagicNumbers; import org.bukkit.craftbukkit.util.CraftNamespacedKey; +import org.bukkit.craftbukkit.util.CraftSpawnCategory; import org.bukkit.damage.DamageEffect; import org.bukkit.damage.DamageSource; import org.bukkit.damage.DamageType; @@ -66,6 +67,7 @@ import org.bukkit.entity.EntityType; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Pose; +import org.bukkit.entity.SpawnCategory; import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.java.JavaPlugin; @@ -179,6 +181,11 @@ public String getTranslationKey(final EntityType entityType) { return CraftEntityType.bukkitToMinecraft(entityType).getDescriptionId(); } + @Override + public SpawnCategory getSpawnCategory(final EntityType entityType) { + return CraftSpawnCategory.toBukkit(CraftEntityType.bukkitToMinecraft(entityType).getCategory()); + } + @Override public VersionFetcher getVersionFetcher() { return new PaperVersionFetcher(); diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/util/CraftSpawnCategory.java b/paper-server/src/main/java/org/bukkit/craftbukkit/util/CraftSpawnCategory.java index cba660a05069..16a2493cb9be 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/util/CraftSpawnCategory.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/util/CraftSpawnCategory.java @@ -43,8 +43,8 @@ public static long getDefaultTicksPerSpawn(SpawnCategory spawnCategory) { }; } - public static SpawnCategory toBukkit(MobCategory enumCreatureType) { - return switch (enumCreatureType) { + public static SpawnCategory toBukkit(MobCategory category) { + return switch (category) { case MONSTER -> SpawnCategory.MONSTER; case CREATURE -> SpawnCategory.ANIMAL; case AMBIENT -> SpawnCategory.AMBIENT; @@ -53,7 +53,6 @@ public static SpawnCategory toBukkit(MobCategory enumCreatureType) { case WATER_AMBIENT -> SpawnCategory.WATER_AMBIENT; case UNDERGROUND_WATER_CREATURE -> SpawnCategory.WATER_UNDERGROUND_CREATURE; case MISC -> SpawnCategory.MISC; - default -> throw new UnsupportedOperationException("Unknown EnumCreatureType " + enumCreatureType + " for SpawnCategory"); }; } @@ -67,7 +66,6 @@ public static MobCategory toNMS(SpawnCategory spawnCategory) { case WATER_AMBIENT -> MobCategory.WATER_AMBIENT; case WATER_UNDERGROUND_CREATURE -> MobCategory.UNDERGROUND_WATER_CREATURE; case MISC -> MobCategory.MISC; - default -> throw new UnsupportedOperationException("Unknown SpawnCategory " + spawnCategory + " for EnumCreatureType"); }; } diff --git a/paper-server/src/test/java/org/bukkit/entity/EntityTypesTest.java b/paper-server/src/test/java/org/bukkit/entity/EntityTypesTest.java deleted file mode 100644 index a4e2c42e9a5d..000000000000 --- a/paper-server/src/test/java/org/bukkit/entity/EntityTypesTest.java +++ /dev/null @@ -1,20 +0,0 @@ -package org.bukkit.entity; - -import static org.junit.jupiter.api.Assertions.*; - -import org.bukkit.support.environment.AllFeatures; -import org.junit.jupiter.api.Test; - -@AllFeatures -public class EntityTypesTest { - - @Test - public void testTranslationKey() { - for (org.bukkit.entity.EntityType entityType : org.bukkit.entity.EntityType.values()) { - // Currently EntityType#getTranslationKey has a validation for null name then for test skip this and check correct names. - if (entityType.getName() != null) { - assertNotNull(entityType.getTranslationKey(), "Nulllable translation key for " + entityType); - } - } - } -} diff --git a/paper-server/src/test/java/org/bukkit/entity/SpawnCategoryTest.java b/paper-server/src/test/java/org/bukkit/entity/SpawnCategoryTest.java index 62816aadf262..1ae831c1d68c 100644 --- a/paper-server/src/test/java/org/bukkit/entity/SpawnCategoryTest.java +++ b/paper-server/src/test/java/org/bukkit/entity/SpawnCategoryTest.java @@ -1,6 +1,5 @@ package org.bukkit.entity; -import net.minecraft.world.entity.MobCategory; import org.bukkit.craftbukkit.util.CraftSpawnCategory; import org.bukkit.support.environment.Normal; import org.junit.jupiter.api.Test; @@ -10,20 +9,12 @@ public class SpawnCategoryTest { @Test public void testMatch() { - for (MobCategory enumCreatureType : MobCategory.values()) { - // If it is missing a convert to Bukkit then throw a UnsupportedOperationException - SpawnCategory spawnCategory = CraftSpawnCategory.toBukkit(enumCreatureType); - - if (CraftSpawnCategory.isValidForLimits(spawnCategory)) { - long defaultTicks = CraftSpawnCategory.getDefaultTicksPerSpawn(spawnCategory); - String nameConfigSpawnLimit = CraftSpawnCategory.getConfigNameSpawnLimit(spawnCategory); - String nameConfigTicksPerSpawn = CraftSpawnCategory.getConfigNameTicksPerSpawn(spawnCategory); + for (SpawnCategory category : SpawnCategory.values()) { + if (CraftSpawnCategory.isValidForLimits(category)) { + long defaultTicks = CraftSpawnCategory.getDefaultTicksPerSpawn(category); + String nameConfigSpawnLimit = CraftSpawnCategory.getConfigNameSpawnLimit(category); + String nameConfigTicksPerSpawn = CraftSpawnCategory.getConfigNameTicksPerSpawn(category); } } - - for (SpawnCategory spawnCategory : SpawnCategory.values()) { - // If it is missing a convert to NMS then throw a UnsupportedOperationException - MobCategory enumCreatureType = CraftSpawnCategory.toNMS(spawnCategory); - } } }