diff --git a/src/com/wasteofplastic/askyblock/listeners/EntityLimits.java b/src/com/wasteofplastic/askyblock/listeners/EntityLimits.java index d41e002ad..5b2fada6c 100644 --- a/src/com/wasteofplastic/askyblock/listeners/EntityLimits.java +++ b/src/com/wasteofplastic/askyblock/listeners/EntityLimits.java @@ -25,10 +25,10 @@ import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.entity.CreatureSpawnEvent; import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason; -import org.bukkit.event.entity.EntityDeathEvent; import org.bukkit.event.hanging.HangingPlaceEvent; import org.bukkit.event.vehicle.VehicleCreateEvent; import org.bukkit.event.world.ChunkLoadEvent; +import org.bukkit.event.world.ChunkUnloadEvent; import org.bukkit.event.world.StructureGrowEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.metadata.FixedMetadataValue; @@ -54,36 +54,44 @@ public class EntityLimits implements Listener { */ public EntityLimits(ASkyBlock plugin) { this.plugin = plugin; - entities = Util.loadYamlFile("entitylimits.yml"); - if (!Settings.saveEntities) { - // Clear the file completely - entities.set("entities", null); - Util.saveYamlFile(entities, "entitylimits.yml"); + if (Settings.saveEntities) { + entities = Util.loadYamlFile("entitylimits.yml"); } } @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) public void onChunkLoad(ChunkLoadEvent event) { - if (!Settings.saveEntities) { + if (!Settings.saveEntities || !IslandGuard.inWorld(event.getWorld())) { return; } Arrays.asList(event.getChunk().getEntities()).forEach(entity -> { - String loc = entities.getString("entities." + entity.getUniqueId().toString(), ""); + String loc = entities.getString(event.getWorld().getName() + "." + event.getChunk().getX() + "." + event.getChunk().getZ() + "." + + entity.getUniqueId().toString(), ""); if (!loc.isEmpty()) { - entity.setMetadata("spawnLoc", new FixedMetadataValue(plugin, loc )); - entities.set("entity." + entity.getUniqueId(), null); + entity.setMetadata("spawnLoc", new FixedMetadataValue(plugin, loc )); } }); + // Delete the chunk data + entities.set(event.getWorld().getName() + "." + event.getChunk().getX() + "." + event.getChunk().getZ() , null); } @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) - public void onEntityDeath(EntityDeathEvent event) { - if (!Settings.saveEntities) { + public void onChunkUnload(ChunkUnloadEvent event) { + if (!Settings.saveEntities || !IslandGuard.inWorld(event.getWorld())) { return; } - if (event.getEntity().getUniqueId() != null) { - entities.set("entities." + event.getEntity().getUniqueId().toString(), null); - } + // Delete the chunk data + entities.set(event.getWorld().getName() + "." + event.getChunk().getX() + "." + event.getChunk().getZ() , null); + // Create new entry + Arrays.asList(event.getChunk().getEntities()).stream().filter(x -> x.hasMetadata("spawnLoc")).forEach(entity -> { + // Get the meta data + entity.getMetadata("spawnLoc").stream().filter(y -> y.getOwningPlugin().equals(plugin)).forEach(v -> { + entities.set(event.getWorld().getName() + "." + + event.getChunk().getX() + "." + event.getChunk().getZ() + "." + + entity.getUniqueId().toString(), v.asString()); + }); + }); + Util.saveYamlFile(entities, "entitylimits.yml"); } public void disable() { @@ -93,14 +101,16 @@ public void disable() { ASkyBlock.getIslandWorld().getEntities().stream().filter(x -> x.hasMetadata("spawnLoc")).forEach(entity -> { // Get the meta data entity.getMetadata("spawnLoc").stream().filter(y -> y.getOwningPlugin().equals(plugin)).forEach(v -> { - entities.set("entities." + entity.getUniqueId().toString(), v.asString()); + entities.set(entity.getWorld().getName() + "." + entity.getLocation().getChunk().getX() + "." + entity.getLocation().getChunk().getZ() + "." + + entity.getUniqueId().toString(), v.asString()); }); }); if (Settings.createNether && Settings.newNether && ASkyBlock.getNetherWorld() != null) { ASkyBlock.getNetherWorld().getEntities().stream().filter(x -> x.hasMetadata("spawnLoc")).forEach(entity -> { // Get the meta data entity.getMetadata("spawnLoc").stream().filter(y -> y.getOwningPlugin().equals(plugin)).forEach(v -> { - entities.set("entities." + entity.getUniqueId().toString(), v.asString()); + entities.set(entity.getWorld().getName() + "." + entity.getLocation().getChunk().getX() + "." + entity.getLocation().getChunk().getZ() + "." + + entity.getUniqueId().toString(), v.asString()); }); }); } diff --git a/src/com/wasteofplastic/askyblock/listeners/IslandGuard.java b/src/com/wasteofplastic/askyblock/listeners/IslandGuard.java index eabd61ae3..29cff8978 100644 --- a/src/com/wasteofplastic/askyblock/listeners/IslandGuard.java +++ b/src/com/wasteofplastic/askyblock/listeners/IslandGuard.java @@ -27,6 +27,7 @@ import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.Sound; +import org.bukkit.World; import org.bukkit.World.Environment; import org.bukkit.block.Biome; import org.bukkit.block.Block; @@ -153,6 +154,15 @@ protected static boolean inWorld(Location loc) { return false; } + protected static boolean inWorld(World world) { + if (world.equals(ASkyBlock.getIslandWorld())) { + return true; + } + if (Settings.createNether && Settings.newNether && ASkyBlock.getNetherWorld() != null && world.equals(ASkyBlock.getNetherWorld())) { + return true; + } + return false; + } /** * Prevents visitors picking items from riding horses or other inventories * @param event