Skip to content

Commit

Permalink
Better approach to saving entity limits
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Mar 30, 2018
1 parent e1118a3 commit 8c88d01
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
44 changes: 27 additions & 17 deletions src/com/wasteofplastic/askyblock/listeners/EntityLimits.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand All @@ -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());
});
});
}
Expand Down
10 changes: 10 additions & 0 deletions src/com/wasteofplastic/askyblock/listeners/IslandGuard.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8c88d01

Please sign in to comment.