From c49031bc5d746efbe6d84030e5c60cfd230805e3 Mon Sep 17 00:00:00 2001 From: CraftBukkit/Spigot Date: Sun, 11 Jun 2023 11:05:16 +1000 Subject: [PATCH] #1124: Adjust CreatureSpawner to allow for spawners with no mob type By: Doc --- .../craftbukkit/block/CraftCreatureSpawner.java | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/block/CraftCreatureSpawner.java b/paper-server/src/main/java/org/bukkit/craftbukkit/block/CraftCreatureSpawner.java index 4f8197e726..09e31acc1a 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/block/CraftCreatureSpawner.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/block/CraftCreatureSpawner.java @@ -3,6 +3,7 @@ package org.bukkit.craftbukkit.block; import com.google.common.base.Preconditions; import java.util.Optional; import net.minecraft.util.RandomSource; +import net.minecraft.util.random.SimpleWeightedRandomList; import net.minecraft.world.entity.EntityTypes; import net.minecraft.world.level.MobSpawnerData; import net.minecraft.world.level.block.entity.TileEntityMobSpawner; @@ -20,18 +21,21 @@ public class CraftCreatureSpawner extends CraftBlockEntityState> type = EntityTypes.by(spawnData.getEntityToSpawn()); - return (type.isEmpty()) ? EntityType.PIG : EntityType.fromName(EntityTypes.getKey(type.get()).getPath()); + return type.map(entityTypes -> EntityType.fromName(EntityTypes.getKey(entityTypes).getPath())).orElse(null); } @Override public void setSpawnedType(EntityType entityType) { - if (entityType == null || entityType.getName() == null) { - throw new IllegalArgumentException("Can't spawn EntityType " + entityType + " from mobspawners!"); + if (entityType == null) { + this.getSnapshot().getSpawner().spawnPotentials = SimpleWeightedRandomList.empty(); // need clear the spawnPotentials to avoid nextSpawnData being replaced later + this.getSnapshot().getSpawner().nextSpawnData = null; + return; } + Preconditions.checkArgument(entityType != EntityType.UNKNOWN, "Can't spawn EntityType %s from mob spawners!", entityType); RandomSource rand = (this.isPlaced()) ? this.getWorldHandle().getRandom() : RandomSource.create(); this.getSnapshot().setEntityId(EntityTypes.byString(entityType.getName()).get(), rand); @@ -41,11 +45,11 @@ public class CraftCreatureSpawner extends CraftBlockEntityState> type = EntityTypes.by(spawnData.getEntityToSpawn()); - return (type.isEmpty()) ? "" : EntityTypes.getKey(type.get()).getPath(); + return type.map(entityTypes -> EntityTypes.getKey(entityTypes).getPath()).orElse(null); } @Override @@ -53,6 +57,7 @@ public class CraftCreatureSpawner extends CraftBlockEntityState