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()