Add entity type to Material registry (for spawn eggs) (#2020)

* Add entity type to Material registry (for spawn eggs)

* Rename to spawn entity type and add javadoc
This commit is contained in:
GreatWyrm 2024-03-13 21:50:54 -07:00 committed by GitHub
parent dcbdad9a1b
commit 7e59603d5f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 0 deletions

View File

@ -9,6 +9,7 @@ import net.minestom.server.collision.BoundingBox;
import net.minestom.server.collision.CollisionUtils;
import net.minestom.server.collision.Shape;
import net.minestom.server.entity.EntitySpawnType;
import net.minestom.server.entity.EntityType;
import net.minestom.server.entity.EquipmentSlot;
import net.minestom.server.instance.block.Block;
import net.minestom.server.item.Material;
@ -461,6 +462,7 @@ public final class Registry {
private final boolean isFood;
private final Supplier<Block> blockSupplier;
private final EquipmentSlot equipmentSlot;
private final EntityType entityType;
private final Properties custom;
private MaterialEntry(String namespace, Properties main, Properties custom) {
@ -489,6 +491,14 @@ public final class Registry {
this.equipmentSlot = null;
}
}
{
final Properties spawnEggProperties = main.section("spawnEggProperties");
if (spawnEggProperties != null) {
this.entityType = EntityType.fromNamespaceId(spawnEggProperties.getString("entityType"));
} else {
this.entityType = null;
}
}
}
public @NotNull NamespaceID namespace() {
@ -527,6 +537,14 @@ public final class Registry {
return equipmentSlot;
}
/**
* Gets the entity type this item can spawn. Only present for spawn eggs (e.g. wolf spawn egg, skeleton spawn egg)
* @return The entity type it can spawn, or null if it is not a spawn egg
*/
public @Nullable EntityType spawnEntityType() {
return entityType;
}
@Override
public Properties custom() {
return custom;

View File

@ -2,6 +2,7 @@ package net.minestom.server.item;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.minestom.server.entity.EntityType;
import org.jglrxavpok.hephaistos.nbt.NBT;
import org.junit.jupiter.api.Test;
@ -108,6 +109,15 @@ public class ItemTest {
assertEquals(6, item1.withAmount(6).amount());
}
@Test
public void testEntityType() {
var item1 = ItemStack.of(Material.DIAMOND, 1);
assertNull(item1.material().registry().spawnEntityType());
var item2 = ItemStack.of(Material.CAMEL_SPAWN_EGG, 1);
assertNotNull(item2.material().registry().spawnEntityType());
assertEquals(EntityType.CAMEL, item2.material().registry().spawnEntityType());
}
static ItemStack createItem() {
return ItemStack.builder(Material.STONE)
.displayName(Component.text("Display name!", NamedTextColor.GREEN))