Skip to content

Commit

Permalink
Create and implement proper Config file reader/writer.
Browse files Browse the repository at this point in the history
  • Loading branch information
BONNe committed Jan 2, 2019
1 parent 3713f83 commit 8e50546
Show file tree
Hide file tree
Showing 3 changed files with 253 additions and 21 deletions.
35 changes: 34 additions & 1 deletion src/main/java/world/bentobox/addons/biomes/BiomesAddon.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import world.bentobox.addons.biomes.commands.admin.AdminCommand;
import world.bentobox.addons.biomes.commands.user.BiomesCommand;
import world.bentobox.addons.biomes.listeners.ChangeOwnerListener;
import world.bentobox.addons.biomes.objects.Settings;
import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.configuration.Config;
import world.bentobox.bentobox.managers.CommandsManager;


Expand All @@ -15,11 +17,19 @@
public class BiomesAddon extends Addon
{
@Override
public void onEnable()
public void onLoad()
{
// Load the plugin's config
this.saveDefaultConfig();

// Load settings from config.yml. This will check if there are any issues with it too.
this.settings = new Config<>(this, Settings.class).loadConfigObject();
}


@Override
public void onEnable()
{
this.addonManager = new BiomesAddonManager(this);

CommandsManager commandsManager = this.getPlugin().getCommandsManager();
Expand All @@ -38,6 +48,8 @@ public void onEnable()
new AdminCommand(this, acidCmd);
}

// Probably better would be casting and getting from settings, but then it should be added as
// dependency.
String currentWorld = a.getConfig().getString("world.world-name");

if (this.addonManager.getBiomes(currentWorld).isEmpty())
Expand All @@ -61,6 +73,8 @@ public void onEnable()
new AdminCommand(this, bsbAdminCmd);
}

// Probably better would be casting and getting from settings, but then it should be added as
// dependency.
String currentWorld = a.getConfig().getString("world.world-name");

if (this.addonManager.getBiomes(currentWorld).isEmpty())
Expand All @@ -77,6 +91,10 @@ public void onEnable()
@Override
public void onDisable()
{
if (this.settings != null)
{
new Config<>(this, Settings.class).saveConfigObject(this.settings);
}
}


Expand All @@ -90,6 +108,16 @@ public BiomesAddonManager getAddonManager()
}


/**
* This method returns addon settings.
* @return Addon settings object.
*/
public Settings getSettings()
{
return this.settings;
}


// ---------------------------------------------------------------------
// Section: Variables
// ---------------------------------------------------------------------
Expand All @@ -98,4 +126,9 @@ public BiomesAddonManager getAddonManager()
* This variable stores biomes manager.
*/
private BiomesAddonManager addonManager;

/**
* This variable stores biomes addon settings.
*/
private Settings settings;
}
187 changes: 187 additions & 0 deletions src/main/java/world/bentobox/addons/biomes/objects/Settings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
package world.bentobox.addons.biomes.objects;


import world.bentobox.addons.biomes.utils.Utils.UpdateMode;
import world.bentobox.bentobox.api.configuration.ConfigComment;
import world.bentobox.bentobox.api.configuration.ConfigEntry;
import world.bentobox.bentobox.api.configuration.StoreAt;
import world.bentobox.bentobox.database.objects.DataObject;


/**
* All the plugin settings are here
* @author BONNe
*/
@StoreAt(filename="config.yml", path="addons/Biomes")
@ConfigComment("Biomes Configuration [version]")
@ConfigComment("This config file is dynamic and saved when the server is shutdown.")
@ConfigComment("You cannot edit it while the server is running because changes will")
@ConfigComment("be lost! Use in-game settings GUI or edit when server is offline.")
@ConfigComment("")
public class Settings implements DataObject
{
// ---------------------------------------------------------------------
// Section: Getters
// ---------------------------------------------------------------------


@Override
public String getUniqueId()
{
return this.uniqueId;
}


/**
* This method returns if advanced menu is enabled.
* @return true - if enabled, otherwise false.
*/
public boolean isAdvancedMenu()
{
return this.advancedMenu;
}


/**
* This method returns default update mode.
* @return Default update mode.
*/
public UpdateMode getDefaultMode()
{
return this.defaultMode;
}


/**
* This method returns default update range.
* @return default update range
*/
public int getDefaultSize()
{
return this.defaultSize;
}


/**
* This method returns if biome reset is necessary on owner change.
* @return true - if biome reset is required.
*/
public boolean isResetBiomes()
{
return this.resetBiomes;
}


/**
* This method returns cooldown between biome changes.
* @return integer that represents seconds between biome changes.
*/
public int getCoolDown()
{
return this.coolDown;
}


@Override
public void setUniqueId(String uniqueId)
{
this.uniqueId = uniqueId;
}


/**
* This method sets value of advancedMenu variable.
* @param advancedMenu new value.
*/
public void setAdvancedMenu(boolean advancedMenu)
{
this.advancedMenu = advancedMenu;
}


/**
* This method sets value of defaultMode variable.
* @param defaultMode new value.
*/
public void setDefaultMode(UpdateMode defaultMode)
{
this.defaultMode = defaultMode;
}


/**
* This method sets value of defaultSize variable.
* @param defaultSize new value.
*/
public void setDefaultSize(int defaultSize)
{
this.defaultSize = defaultSize;
}


/**
* This method sets value of resetBiomes variable.
* @param resetBiomes new value.
*/
public void setResetBiomes(boolean resetBiomes)
{
this.resetBiomes = resetBiomes;
}


/**
* This method sets value of coolDown variable.
* @param coolDown new value.
*/
public void setCoolDown(int coolDown)
{
this.coolDown = coolDown;
}


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


@ConfigComment("This indicates if advanced menu will be enabled in Biomes GUI when users choose")
@ConfigComment("biome or not. Advanced menu contains extra buttons that allows to choose biome")
@ConfigComment("change mode and range.")
@ConfigEntry(path = "advanced-menu")
private boolean advancedMenu = true;

@ConfigComment("")
@ConfigComment("For advanced menu this indicate which mode will be active.")
@ConfigComment("If advanced menu is disabled or in biome set command update mode is not passed")
@ConfigComment("then update algorithm will use this value as update mode.")
@ConfigComment("Valid values are:")
@ConfigComment(" 'ISLAND' - updates biome on whole island")
@ConfigComment(" 'CHUNK' - updates biome on whole chunks around player")
@ConfigComment(" 'SQUARE' - updates biome by block in given range")
@ConfigEntry(path = "default-mode")
private UpdateMode defaultMode = UpdateMode.ISLAND;

@ConfigComment("")
@ConfigComment("For advanced menu this indicate how large range will be set on GUI opening.")
@ConfigComment("If advanced menu is disabled or in biome set command update range is not set")
@ConfigComment("then update algorithm will use this value as update range.")
@ConfigEntry(path = "default-size")
private int defaultSize = 3;

@ConfigComment("")
@ConfigComment("This indicates if biome on island must be restored to default biome, if")
@ConfigComment("island changes owner and it does not have biome set ability.")
@ConfigComment("'true' means that biome will be reset.")
@ConfigEntry(path = "reset-biomes")
private boolean resetBiomes = false;

@ConfigComment("")
@ConfigComment("This indicates cool down in seconds between users can change biomes on their island.")
@ConfigEntry(path = "cooldown")
private int coolDown = 60;

/**
* Default variable.
*/
private String uniqueId = "config";
}
52 changes: 32 additions & 20 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
# Biomes Config

# This indicates if advanced menu should be showed. If advanced menu is disabled, then
# default type and default size will be active.
advancedmenu: true

# Default type shows which update mode will be active when Biomes Panel is opened.
# Valid values 'ISLAND', 'CHUNK' or 'SQUARE'
defaulttype: 'ISLAND'

# Default size which will be enabled when Biomes panel is opened.
# Indicate how much chunks or how large square will be updated with ne biome.
defaultsize: 3

# This indicate if biome on island must be restored on owner change, if new owner does not have permissions to
# change biomes.
resetbiomes: false

# Timeout in minuted between changing biomes.
timeout: 1
# Biomes Configuration [version]
# This config file is dynamic and saved when the server is shutdown.
# You cannot edit it while the server is running because changes will
# be lost! Use in-game settings GUI or edit when server is offline.
#
# This indicates if advanced menu will be enabled in Biomes GUI when users choose
# biome or not. Advanced menu contains extra buttons that allows to choose biome
# change mode and range.
advanced-menu: true
#
# For advanced menu this indicate which mode will be active.
# If advanced menu is disabled or in biome set command update mode is not passed
# then update algorithm will use this value as update mode.
# Valid values are:
# 'ISLAND' - updates biome on whole island
# 'CHUNK' - updates biome on whole chunks around player
# 'SQUARE' - updates biome by block in given range
default-mode: ISLAND
#
# For advanced menu this indicate how large range will be set on GUI opening.
# If advanced menu is disabled or in biome set command update range is not set
# then update algorithm will use this value as update range.
default-size: 3
#
# This indicates if biome on island must be restored to default biome, if
# island changes owner and it does not have biome set ability.
# 'true' means that biome will be reset.
reset-biomes: false
#
# This indicates cool down in seconds between users can change biomes on their island.
cooldown: 60
uniqueId: config

0 comments on commit 8e50546

Please sign in to comment.