From 2e226d22c1913489d39c1e37e75cd25fe874088b Mon Sep 17 00:00:00 2001 From: McHorse Date: Sun, 12 Jun 2022 09:03:34 +0100 Subject: [PATCH] Make NPC respawning work only when chunks are loaded --- .../java/mchorse/mappet/entities/EntityNpc.java | 1 + .../mappet/entities/utils/DiedNpcHolder.java | 4 ++-- .../entities/utils/MappetNpcRespawnManager.java | 15 ++++++++++++--- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/main/java/mchorse/mappet/entities/EntityNpc.java b/src/main/java/mchorse/mappet/entities/EntityNpc.java index ec3d2278..6e336c06 100644 --- a/src/main/java/mchorse/mappet/entities/EntityNpc.java +++ b/src/main/java/mchorse/mappet/entities/EntityNpc.java @@ -525,6 +525,7 @@ public void onDeath(DamageSource cause) if (this.state.respawn && !this.dieOnLoad) { MappetNpcRespawnManager respawnManager = MappetNpcRespawnManager.get(this.world); + respawnManager.addDiedNpc(this); this.dieOnLoad = true; } diff --git a/src/main/java/mchorse/mappet/entities/utils/DiedNpcHolder.java b/src/main/java/mchorse/mappet/entities/utils/DiedNpcHolder.java index a4221b7b..4d449ce1 100644 --- a/src/main/java/mchorse/mappet/entities/utils/DiedNpcHolder.java +++ b/src/main/java/mchorse/mappet/entities/utils/DiedNpcHolder.java @@ -43,12 +43,12 @@ public void spawn(World world) EntityNpc npc = new EntityNpc(world); npc.readEntityFromNBT(this.nbt); - npc.setPosition(posX, posY, posZ); + npc.setPosition(this.posX, this.posY, this.posZ); npc.setHealth(npc.getMaxHealth()); if (npc.getState().respawnSaveUUID) { - npc.setUniqueId(UUID.fromString(uuid)); + npc.setUniqueId(UUID.fromString(this.uuid)); } world.spawnEntity(npc); diff --git a/src/main/java/mchorse/mappet/entities/utils/MappetNpcRespawnManager.java b/src/main/java/mchorse/mappet/entities/utils/MappetNpcRespawnManager.java index b4b123a9..47953fdd 100644 --- a/src/main/java/mchorse/mappet/entities/utils/MappetNpcRespawnManager.java +++ b/src/main/java/mchorse/mappet/entities/utils/MappetNpcRespawnManager.java @@ -6,7 +6,9 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraft.server.MinecraftServer; +import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; +import net.minecraft.world.WorldServer; import net.minecraft.world.storage.WorldSavedData; import net.minecraftforge.common.util.Constants; @@ -16,10 +18,13 @@ public class MappetNpcRespawnManager extends WorldSavedData { public static final String DATA_NAME = Mappet.MOD_ID + "_RespawnData"; + public final List diedNpcHolders = new ArrayList<>(); public final List deleteCache = new ArrayList<>(); public MinecraftServer server; + private BlockPos.MutableBlockPos block = new BlockPos.MutableBlockPos(); + public MappetNpcRespawnManager(String mapName) { super(mapName); @@ -29,7 +34,11 @@ public void onTick() { for (DiedNpcHolder diedNpcHolder : this.diedNpcHolders) { - if (diedNpcHolder.respawnTime <= this.server.getWorld(diedNpcHolder.worldID).getTotalWorldTime()) + WorldServer world = this.server.getWorld(diedNpcHolder.worldID); + + block.setPos(diedNpcHolder.posX, diedNpcHolder.posY, diedNpcHolder.posZ); + + if (diedNpcHolder.respawnTime <= world.getTotalWorldTime() && world.isBlockLoaded(block)) { this.respawnNpc(diedNpcHolder); } @@ -52,7 +61,7 @@ public void addDiedNpc(EntityNpc npc) public void respawnNpc(DiedNpcHolder diedNpcHolder) { - diedNpcHolder.spawn(server.getWorld(diedNpcHolder.worldID)); + diedNpcHolder.spawn(this.server.getWorld(diedNpcHolder.worldID)); this.deleteCache.add(diedNpcHolder); } @@ -64,7 +73,7 @@ public void removeSpawnedNpcs() this.diedNpcHolders.remove(diedNpcHolder); } - clearDeleteCache(); + this.clearDeleteCache(); } public void clearDeleteCache()