Skip to content

Commit

Permalink
Does not unload chunks in the event handler (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Mar 28, 2020
1 parent 2cecc59 commit e7cebf1
Showing 1 changed file with 68 additions and 72 deletions.
140 changes: 68 additions & 72 deletions src/main/java/world/bentobox/biomes/listeners/ChunkLoadListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,76 +17,72 @@
*/
public class ChunkLoadListener implements Listener
{
/**
* Default constructor that inits listener.
* @param addon BiomeAddon object.
*/
public ChunkLoadListener(BiomesAddon addon)
{
this.addon = addon;
}


/**
* Chunk load event that will force biome change and unload chunk if current chunk is
* loaded by manual process.
* @param event ChunkLoadEvent object.
*/
@EventHandler(priority = EventPriority.LOWEST)
public void onChunkLoad(ChunkLoadEvent event)
{
Chunk chunk = event.getChunk();

BiomeChunkUpdateObject updateObject = this.addon.getAddonManager().
getPendingChunkUpdateObject(chunk.getWorld(), chunk.getX(), chunk.getZ());

if (updateObject != null)
{
if (updateObject.getMaxX() - updateObject.getMinX() > 15 ||
(updateObject.getMinX() >> 4) != chunk.getX() ||
(updateObject.getMaxX() >> 4) != chunk.getX())
{
// Something is wrong with X coordinates. Change to whole chunk.
updateObject.setMinX(updateObject.getChunkX() << 4);
updateObject.setMaxX(updateObject.getChunkX() << 4 + 15);
}

if (updateObject.getMaxZ() - updateObject.getMinZ() > 15 ||
(updateObject.getMinZ() >> 4) != chunk.getZ() ||
(updateObject.getMaxZ() >> 4) != chunk.getZ())
{
// Something is wrong with Z coordinates. Change to whole chunk.
updateObject.setMinZ(updateObject.getChunkZ() << 4);
updateObject.setMaxZ(updateObject.getChunkZ() << 4 + 15);
}

// Update biome from minX till maxX and minZ till maxZ.
for (int x = updateObject.getMinX(); x <= updateObject.getMaxX(); x++)
{
for (int z = updateObject.getMinZ(); z <= updateObject.getMaxZ(); z++)
{
// Change Biome
updateObject.getWorld().setBiome(x, z, updateObject.getBiome());
}
}

// If process is completed, then remove force-load status and unload
// chunk.
chunk.unload(true);

// Remove ChunkUpdateObject.
this.addon.getAddonManager().removeUpdateObject(updateObject);
}
}


// ---------------------------------------------------------------------
// Section: Variables
// ---------------------------------------------------------------------


/**
* BiomeAddon variable.
*/
private BiomesAddon addon;
/**
* Default constructor that inits listener.
* @param addon BiomeAddon object.
*/
public ChunkLoadListener(BiomesAddon addon)
{
this.addon = addon;
}


/**
* Chunk load event that will change the biome if required when a chunk is
* loaded.
* @param event ChunkLoadEvent object.
*/
@EventHandler(priority = EventPriority.LOWEST)
public void onChunkLoad(ChunkLoadEvent event)
{
Chunk chunk = event.getChunk();

BiomeChunkUpdateObject updateObject = this.addon.getAddonManager().
getPendingChunkUpdateObject(chunk.getWorld(), chunk.getX(), chunk.getZ());

if (updateObject != null)
{
if (updateObject.getMaxX() - updateObject.getMinX() > 15 ||
(updateObject.getMinX() >> 4) != chunk.getX() ||
(updateObject.getMaxX() >> 4) != chunk.getX())
{
// Something is wrong with X coordinates. Change to whole chunk.
updateObject.setMinX(updateObject.getChunkX() << 4);
updateObject.setMaxX(updateObject.getChunkX() << 4 + 15);
}

if (updateObject.getMaxZ() - updateObject.getMinZ() > 15 ||
(updateObject.getMinZ() >> 4) != chunk.getZ() ||
(updateObject.getMaxZ() >> 4) != chunk.getZ())
{
// Something is wrong with Z coordinates. Change to whole chunk.
updateObject.setMinZ(updateObject.getChunkZ() << 4);
updateObject.setMaxZ(updateObject.getChunkZ() << 4 + 15);
}

// Update biome from minX till maxX and minZ till maxZ.
for (int x = updateObject.getMinX(); x <= updateObject.getMaxX(); x++)
{
for (int z = updateObject.getMinZ(); z <= updateObject.getMaxZ(); z++)
{
// Change Biome
updateObject.getWorld().setBiome(x, z, updateObject.getBiome());
}
}

// Remove ChunkUpdateObject.
this.addon.getAddonManager().removeUpdateObject(updateObject);
}
}


// ---------------------------------------------------------------------
// Section: Variables
// ---------------------------------------------------------------------


/**
* BiomeAddon variable.
*/
private BiomesAddon addon;
}

0 comments on commit e7cebf1

Please sign in to comment.