read & write spawn data properly for placing spawners option

This commit is contained in:
granny
2025-04-12 21:46:18 -07:00
parent c5bb590f88
commit ba521eb66d
2 changed files with 7 additions and 3 deletions

View File

@@ -1,6 +1,6 @@
--- a/net/minecraft/world/level/block/SpawnerBlock.java
+++ b/net/minecraft/world/level/block/SpawnerBlock.java
@@ -38,6 +_,57 @@
@@ -38,6 +_,59 @@
);
}
@@ -14,7 +14,9 @@
+ java.util.Optional<net.minecraft.world.entity.EntityType<?>> type = java.util.Optional.empty();
+ if (nextSpawnData != null) {
+ type = net.minecraft.world.entity.EntityType.by(nextSpawnData.getEntityToSpawn());
+ net.minecraft.world.level.SpawnData.CODEC.encodeStart(net.minecraft.nbt.NbtOps.INSTANCE, nextSpawnData).result().ifPresent(tag -> item.set(net.minecraft.core.component.DataComponents.CUSTOM_DATA, net.minecraft.world.item.component.CustomData.EMPTY.update(compoundTag -> compoundTag.put("Purpur.SpawnData", tag))));
+ net.minecraft.nbt.CompoundTag spawnDataTag = new net.minecraft.nbt.CompoundTag();
+ spawnDataTag.storeNullable("SpawnData", net.minecraft.world.level.SpawnData.CODEC, nextSpawnData);
+ item.set(net.minecraft.core.component.DataComponents.CUSTOM_DATA, net.minecraft.world.item.component.CustomData.EMPTY.update(compoundTag -> compoundTag.put("Purpur.SpawnData", spawnDataTag)));
+ }
+
+ if (type.isPresent()) {

View File

@@ -32,7 +32,9 @@ public class SpawnerItem extends BlockItem {
if (mobTypeStringOptional.isPresent()) {
EntityType.byString(mobTypeStringOptional.get()).ifPresent(type -> spawner.getSpawner().setEntityId(type, level, level.random, pos));
} else if (customData.contains("Purpur.SpawnData")) {
customData.read("SpawnData", net.minecraft.world.level.SpawnData.CODEC).ifPresent(spawnData -> spawner.getSpawner().nextSpawnData = spawnData);
customData.getCompound("Purpur.SpawnData")
.flatMap(spawnerData -> spawnerData.read("SpawnData", net.minecraft.world.level.SpawnData.CODEC))
.ifPresent(spawnData -> spawner.getSpawner().nextSpawnData = spawnData);
}
}
}