Skip to content

Commit

Permalink
Fixes an issue when biomes prevents server from turning off in accept…
Browse files Browse the repository at this point in the history
…able time (#90).

Rework the way how biome is applied. Use temporary chunk loading before applying biome change to the area. This will change biome for every island chunks, and will not relay on storing data about biome change.
  • Loading branch information
BONNe committed Aug 6, 2021
1 parent cb01687 commit 5b12605
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 317 deletions.
59 changes: 4 additions & 55 deletions src/main/java/world/bentobox/biomes/BiomesAddon.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
package world.bentobox.biomes;


import java.util.Iterator;
import java.util.Optional;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.World;

import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.configuration.Config;
import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.bentobox.hooks.VaultHook;
import world.bentobox.bentobox.util.Util;
import world.bentobox.biomes.commands.admin.AdminCommand;
import world.bentobox.biomes.commands.user.BiomesCommand;
import world.bentobox.biomes.config.Settings;
import world.bentobox.biomes.database.objects.BiomeChunkUpdateObject;
import world.bentobox.biomes.handlers.BiomeDataRequestHandler;
import world.bentobox.biomes.handlers.BiomeListRequestHandler;
import world.bentobox.biomes.handlers.ChangeBiomeRequestHandler;
import world.bentobox.biomes.listeners.ChangeOwnerListener;
import world.bentobox.biomes.listeners.ChunkLoadListener;
import world.bentobox.greenhouses.Greenhouses;
import world.bentobox.level.Level;

Expand Down Expand Up @@ -95,7 +90,6 @@ private void setupAddon() {

// Register the reset listener
this.registerListener(new ChangeOwnerListener(this));
this.registerListener(new ChunkLoadListener(this));

// Register Flags
this.registerFlag(BIOMES_WORLD_PROTECTION);
Expand All @@ -106,55 +100,6 @@ private void setupAddon() {
this.registerRequestHandler(new BiomeListRequestHandler(this));

this.registerRequestHandler(new ChangeBiomeRequestHandler(this));

if (this.settings.getUpdateTickCounter() > 0)
{
// This task will force-load chunk every update tick if its biome is not updated.
this.runChunkUpdatingScheduler();
}
}


/**
* This task will force-load chunk every update tick if its biome is not updated.
*/
private void runChunkUpdatingScheduler()
{
Bukkit.getScheduler().runTaskTimer(this.getPlugin(), () -> {
Iterator<BiomeChunkUpdateObject> iterator =
this.addonManager.getBiomeUpdaterCollection().iterator();

// if there is nothing to load, then skip.
if (!iterator.hasNext())
{
return;
}

BiomeChunkUpdateObject updater = iterator.next();

// if chunk is already force-loaded, then skip.
while (iterator.hasNext() && updater.isForceLoaded())
{
updater = iterator.next();
}

World world = updater.getWorld();

// if chunk is loaded then skip.
if (!world.isChunkLoaded(updater.getChunkX(), updater.getChunkZ()))
{
// Set flag as force-loaded.
updater.setForceLoaded(true);

// force-load chunk asynchronously
Util.getChunkAtAsync(world,
updater.getChunkX(),
updater.getChunkZ());
}
},

this.settings.getUpdateTickCounter(),
this.settings.getUpdateTickCounter());
}


Expand Down Expand Up @@ -272,6 +217,8 @@ public void onDisable()
{
if (this.hooked)
{
this.getLogger().info("Disabling biomes addon.");

if (this.settings != null)
{
new Config<>(this, Settings.class).saveConfigObject(this.settings);
Expand All @@ -281,6 +228,8 @@ public void onDisable()
{
this.addonManager.save();
}

this.getLogger().info("Biomes addon disabled.");
}
}

Expand Down
96 changes: 4 additions & 92 deletions src/main/java/world/bentobox/biomes/BiomesAddonManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
Expand All @@ -25,7 +24,6 @@
import world.bentobox.bentobox.util.ItemParser;
import world.bentobox.bentobox.util.Util;
import world.bentobox.biomes.config.Settings.VisibilityMode;
import world.bentobox.biomes.database.objects.BiomeChunkUpdateObject;
import world.bentobox.biomes.database.objects.BiomesObject;
import world.bentobox.biomes.panels.GuiUtils;
import world.bentobox.biomes.utils.Utils;
Expand Down Expand Up @@ -57,9 +55,6 @@ protected BiomesAddonManager(BiomesAddon addon)
this.addon.saveResource("biomes.yml", false);
}

this.biomePendingChunkUpdateDatabase = new Database<>(addon, BiomeChunkUpdateObject.class);
this.biomePendingChunkUpdateMap = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);

this.load();
}

Expand All @@ -75,12 +70,8 @@ protected BiomesAddonManager(BiomesAddon addon)
private void load()
{
this.biomesCacheData.clear();
this.biomePendingChunkUpdateMap.clear();

this.addon.getLogger().info("Loading biomes...");

this.biomesDatabase.loadObjects().forEach(this::loadBiomes);
this.biomePendingChunkUpdateDatabase.loadObjects().forEach(this::addChunkUpdateObject);
}


Expand All @@ -93,9 +84,6 @@ public void reload()

this.biomesDatabase = new Database<>(this.addon, BiomesObject.class);
this.biomesDatabase.loadObjects().forEach(this::loadBiomes);

this.biomePendingChunkUpdateDatabase = new Database<>(this.addon, BiomeChunkUpdateObject.class);
this.biomePendingChunkUpdateDatabase.loadObjects().forEach(this::addChunkUpdateObject);
}


Expand All @@ -119,20 +107,9 @@ public void saveBiome(BiomesObject biome)
*/
public void save()
{
this.biomesCacheData.values().forEach(this.biomesDatabase::saveObjectAsync);

// Clear Database.
List<BiomeChunkUpdateObject> objectList =
this.biomePendingChunkUpdateDatabase.loadObjects();
objectList.forEach(object -> this.biomePendingChunkUpdateDatabase.
deleteID(object.getUniqueId()));
this.addon.getLogger().info("Saving biomes...");

// Save cache into database.
if (!this.biomePendingChunkUpdateMap.isEmpty())
{
this.biomePendingChunkUpdateMap.values().forEach(
this.biomePendingChunkUpdateDatabase::saveObjectAsync);
}
this.biomesCacheData.values().forEach(this.biomesDatabase::saveObjectAsync);
}

/**
Expand Down Expand Up @@ -636,73 +613,19 @@ public boolean hasGreenhouseInLocation(World world, int x, int y, int z)
}


// ---------------------------------------------------------------------
// Section: Later Biome Updater
// ---------------------------------------------------------------------


/**
* This method finds and returns BiomeChunkUpdaterObject in given world with given
* chunk X and Z coordinates.
* @param world World where process will happen.
* @param x Chunk X coordinate.
* @param z Chunk Z coordinate.
* @return BiomeChunkUpdateObject where update is pending or null.
*/
public BiomeChunkUpdateObject getPendingChunkUpdateObject(World world, int x, int z)
{
return this.biomePendingChunkUpdateMap.get(Utils.getGameMode(world) + "_" + x + "-" + z);
}


/**
* This method returns collection with all objects that contains information about
* chunks where biome update is still not completed.
* @return Collection of BiomeCHunkUpdateObjects.
*/
public Collection<BiomeChunkUpdateObject> getBiomeUpdaterCollection()
{
return this.biomePendingChunkUpdateMap.values();
}


/**
* This method adds BiomeChunkUpdateObject to cache.
* @param updateObject Object that must be added to cache.
*/
public void addChunkUpdateObject(BiomeChunkUpdateObject updateObject)
{
this.biomePendingChunkUpdateMap.put(updateObject.getUniqueId(), updateObject);
}


/**
* This method removes given element form cache and database.
* @param element Element that should be removed.
*/
public void removeUpdateObject(BiomeChunkUpdateObject element)
{
if (this.biomePendingChunkUpdateMap.containsKey(element.getUniqueId()))
{
this.biomePendingChunkUpdateMap.remove(element.getUniqueId());
this.biomePendingChunkUpdateDatabase.deleteObject(element);
}
}


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

/**
* Variable current addon.
*/
private BiomesAddon addon;
private final BiomesAddon addon;

/**
* Variable stores map that links String to loaded biomes object.
*/
private Map<String, BiomesObject> biomesCacheData;
private final Map<String, BiomesObject> biomesCacheData;

/**
* Variable stores database of biomes objects.
Expand All @@ -713,15 +636,4 @@ public void removeUpdateObject(BiomeChunkUpdateObject element)
* Variable stores biomes.yml location
*/
private File biomesFile;

/**
* Variable stores BiomeChunkUpdateObject objects that contains information about
* chunk that is not updated yet.
*/
private Map<String, BiomeChunkUpdateObject> biomePendingChunkUpdateMap;

/**
* Variable stores database of BiomeChunkUpdateObject.
*/
private Database<BiomeChunkUpdateObject> biomePendingChunkUpdateDatabase;
}
28 changes: 0 additions & 28 deletions src/main/java/world/bentobox/biomes/config/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,6 @@ public boolean isUseProtectionRange()
}


/**
* This method returns the updateTickCounter value.
* @return the value of updateTickCounter.
*/
public int getUpdateTickCounter()
{
return updateTickCounter;
}


// ---------------------------------------------------------------------
// Section: Setters
// ---------------------------------------------------------------------
Expand Down Expand Up @@ -249,17 +239,6 @@ public void setUseProtectionRange(boolean useProtectionRange)
}


/**
* This method sets the updateTickCounter value.
* @param updateTickCounter the updateTickCounter new value.
*
*/
public void setUpdateTickCounter(int updateTickCounter)
{
this.updateTickCounter = updateTickCounter;
}


// ---------------------------------------------------------------------
// Section: Enums used for Settings.
// ---------------------------------------------------------------------
Expand Down Expand Up @@ -453,13 +432,6 @@ public static Lore getLore(String parameter)
@ConfigEntry(path = "cooldown")
private int coolDown = 60;

@ConfigComment("")
@ConfigComment("This indicates tick counter between each background update task.")
@ConfigComment("This process load chunks that require biome update and change biome in it.")
@ConfigComment("Setting 0 will stop background task and biome will be updated only when loaded.")
@ConfigEntry(path = "update-tick-counter", needsRestart = true, since = "1.13.0")
private int updateTickCounter = 5;

@ConfigComment("")
@ConfigComment("This variable allows to choose which biomes users can see in Biomes GUI.")
@ConfigComment("Valid values are:")
Expand Down

0 comments on commit 5b12605

Please sign in to comment.