13
0
geforkt von Mirrors/Paper

#1124: Adjust CreatureSpawner to allow for spawners with no mob type

By: Doc <nachito94@msn.com>
Dieser Commit ist enthalten in:
CraftBukkit/Spigot 2023-06-11 11:05:16 +10:00
Ursprung 0c94630926
Commit c49031bc5d

Datei anzeigen

@ -3,6 +3,7 @@ package org.bukkit.craftbukkit.block;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import java.util.Optional; import java.util.Optional;
import net.minecraft.util.RandomSource; import net.minecraft.util.RandomSource;
import net.minecraft.util.random.SimpleWeightedRandomList;
import net.minecraft.world.entity.EntityTypes; import net.minecraft.world.entity.EntityTypes;
import net.minecraft.world.level.MobSpawnerData; import net.minecraft.world.level.MobSpawnerData;
import net.minecraft.world.level.block.entity.TileEntityMobSpawner; import net.minecraft.world.level.block.entity.TileEntityMobSpawner;
@ -20,18 +21,21 @@ public class CraftCreatureSpawner extends CraftBlockEntityState<TileEntityMobSpa
public EntityType getSpawnedType() { public EntityType getSpawnedType() {
MobSpawnerData spawnData = this.getSnapshot().getSpawner().nextSpawnData; MobSpawnerData spawnData = this.getSnapshot().getSpawner().nextSpawnData;
if (spawnData == null) { if (spawnData == null) {
return EntityType.PIG; // TODO: Change API contract to nullable? return null;
} }
Optional<EntityTypes<?>> type = EntityTypes.by(spawnData.getEntityToSpawn()); Optional<EntityTypes<?>> 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 @Override
public void setSpawnedType(EntityType entityType) { public void setSpawnedType(EntityType entityType) {
if (entityType == null || entityType.getName() == null) { if (entityType == null) {
throw new IllegalArgumentException("Can't spawn EntityType " + entityType + " from mobspawners!"); 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(); RandomSource rand = (this.isPlaced()) ? this.getWorldHandle().getRandom() : RandomSource.create();
this.getSnapshot().setEntityId(EntityTypes.byString(entityType.getName()).get(), rand); this.getSnapshot().setEntityId(EntityTypes.byString(entityType.getName()).get(), rand);
@ -41,11 +45,11 @@ public class CraftCreatureSpawner extends CraftBlockEntityState<TileEntityMobSpa
public String getCreatureTypeName() { public String getCreatureTypeName() {
MobSpawnerData spawnData = this.getSnapshot().getSpawner().nextSpawnData; MobSpawnerData spawnData = this.getSnapshot().getSpawner().nextSpawnData;
if (spawnData == null) { if (spawnData == null) {
return ""; // TODO: Change API contract to nullable? return null;
} }
Optional<EntityTypes<?>> type = EntityTypes.by(spawnData.getEntityToSpawn()); Optional<EntityTypes<?>> type = EntityTypes.by(spawnData.getEntityToSpawn());
return (type.isEmpty()) ? "" : EntityTypes.getKey(type.get()).getPath(); return type.map(entityTypes -> EntityTypes.getKey(entityTypes).getPath()).orElse(null);
} }
@Override @Override
@ -53,6 +57,7 @@ public class CraftCreatureSpawner extends CraftBlockEntityState<TileEntityMobSpa
// Verify input // Verify input
EntityType type = EntityType.fromName(creatureType); EntityType type = EntityType.fromName(creatureType);
if (type == null) { if (type == null) {
setSpawnedType(null);
return; return;
} }
setSpawnedType(type); setSpawnedType(type);