Skip to content

Commit

Permalink
Add IslandVisitSettings object that stores values per each island.
Browse files Browse the repository at this point in the history
Create methods in VisitAddonManager that allows to load, save and access IslandVisitSettings object.
  • Loading branch information
BONNe committed Jun 17, 2020
1 parent d3196c9 commit a947eb0
Show file tree
Hide file tree
Showing 3 changed files with 304 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package world.bentobox.visit.database.object;


import com.google.gson.annotations.Expose;

import world.bentobox.bentobox.database.objects.DataObject;


/**
* This class stores information about island visiting data.
*/
public class IslandVisitSettings implements DataObject
{
// ---------------------------------------------------------------------
// Section: Variables
// ---------------------------------------------------------------------

/**
* Current object uniqueId object.
*/
@Expose
private String uniqueId = "";

/**
* Stores payment value for visitor.
*/
@Expose
private double payment = 0.0;

/**
* Stores if visits are allowed while offline.
*/
@Expose
private boolean offlineVisit = true;


// ---------------------------------------------------------------------
// Section: Constructor
// ---------------------------------------------------------------------


/**
* Empty constructor.
*/
public IslandVisitSettings()
{
}


// ---------------------------------------------------------------------
// Section: Methods
// ---------------------------------------------------------------------


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


/**
* @param uniqueId new uniqueId value.
*/
@Override
public void setUniqueId(String uniqueId)
{
this.uniqueId = uniqueId;
}


/**
* This method returns the payment value.
* @return the value of payment.
*/
public double getPayment()
{
return this.payment;
}


/**
* This method sets the payment value.
* @param payment the payment new value.
*
*/
public void setPayment(double payment)
{
this.payment = payment;
}


/**
* This method returns the offlineVisit value.
* @return the value of offlineVisit.
*/
public boolean isOfflineVisit()
{
return this.offlineVisit;
}


/**
* This method sets the offlineVisit value.
* @param offlineVisit the offlineVisit new value.
*
*/
public void setOfflineVisit(boolean offlineVisit)
{
this.offlineVisit = offlineVisit;
}
}
179 changes: 171 additions & 8 deletions src/main/java/world/bentobox/visit/managers/VisitAddonManager.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package world.bentobox.visit.managers;


import org.eclipse.jdt.annotation.NonNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.Database;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.visit.VisitAddon;
import world.bentobox.visit.database.object.IslandVisitSettings;


/**
Expand All @@ -22,6 +29,16 @@ public class VisitAddonManager
*/
private final List<GameModeAddon> enabledAddonList;

/**
* This config object stores structures for island visit settings objects.
*/
private Database<IslandVisitSettings> visitSettingsDatabase;

/**
* This is local cache that links island unique id with island visit settings object.
*/
private Map<String, IslandVisitSettings> visitSettingsCacheData;

/**
* Reference to main addon class.
*/
Expand All @@ -41,6 +58,160 @@ public VisitAddonManager(VisitAddon addon)
{
this.addon = addon;
this.enabledAddonList = new ArrayList<>(5);

// Init database and cache
this.visitSettingsDatabase = new Database<>(addon, IslandVisitSettings.class);
this.visitSettingsCacheData = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);

this.load();
}


// ---------------------------------------------------------------------
// Section: Database related methods
// ---------------------------------------------------------------------


/**
* Clear and reload all challenges
*/
public void load()
{
this.addon.log("Loading stored island visit settings...");
this.visitSettingsCacheData.clear();
this.visitSettingsDatabase.loadObjects().forEach(this::loadSettings);
}


/**
* This method tries to load island visit settings from database silently with
* overwrite enabled.
* @param islandSettings object that must be loaded in local cache
*/
private void loadSettings(@NonNull IslandVisitSettings islandSettings)
{
this.loadSettings(islandSettings, true, null, true);
}


/**
* This method loads given island visit settings into local cache. It provides
* functionality to overwrite local value with new one, and send message to given user.
*
* @param islandSettings object that must be loaded in local cache
* @param overwrite of type boolean that indicate if local element must be overwritten.
* @param user of type User who will receive messages.
* @param silent of type boolean that indicate if message to user must be sent.
* @return boolean that indicate about load status.
*/
public boolean loadSettings(@NonNull IslandVisitSettings islandSettings,
boolean overwrite,
User user,
boolean silent)
{
// This may happen if database somehow failed to load challenge and return
// null as input.
if (islandSettings == null)
{
if (!silent)
{
user.sendMessage("load-error", "[value]", "NULL");
}

return false;
}

if (this.visitSettingsCacheData.containsKey(islandSettings.getUniqueId()))
{
if (!overwrite)
{
if (!silent)
{
user.sendMessage("visit.messages.load-skipping",
"[value]", islandSettings.getUniqueId());
}

return false;
}
else
{
if (!silent)
{
user.sendMessage("visit.messages.load-overwriting",
"[value]", islandSettings.getUniqueId());
}
}
}
else
{
if (!silent)
{
user.sendMessage("visit.messages.load-add",
"[value]", islandSettings.getUniqueId());
}
}

this.visitSettingsCacheData.put(islandSettings.getUniqueId(), islandSettings);
return true;
}


/**
* This method saves given settings object to database.
* @param settings object that must be saved
*/
public void saveSettings(IslandVisitSettings settings)
{
// Save only if default values are not the same as current object.
// Limitation is that if owner changes default, it will affect this
// island too, but that is how it is intended to work.

if (this.addon.getSettings().getDefaultVisitingPayment() != settings.getPayment() ||
this.addon.getSettings().isDefaultVisitingOffline() != settings.isOfflineVisit())
{
this.visitSettingsDatabase.saveObjectAsync(settings);
}
}

// ---------------------------------------------------------------------
// Section: Local cache access data
// ---------------------------------------------------------------------


/**
* This method returns island visit settings object from cache for given island.
* @param island Island.
* @return IslandVisitSettings object.
*/
public IslandVisitSettings getIslandVisitSettings(Island island)
{
return this.getIslandVisitSettings(island.getUniqueId());
}


/**
* This method returns island visit settings object from cache for given island uuid.
* @param uuid Island UUID.
* @return IslandVisitSettings object.
*/
public IslandVisitSettings getIslandVisitSettings(String uuid)
{
return this.visitSettingsCacheData.getOrDefault(uuid, this.defaultIslandVisitSettings());
}


/**
* This method creates default IslandVisitSettings object.
* @return IslandVisitSettings object that has default values.
*/
private IslandVisitSettings defaultIslandVisitSettings()
{
IslandVisitSettings settings = new IslandVisitSettings();

settings.setOfflineVisit(this.addon.getSettings().isDefaultVisitingOffline());
settings.setPayment(this.addon.getSettings().getDefaultVisitingPayment());

return settings;
}


Expand All @@ -67,12 +238,4 @@ public List<GameModeAddon> getEnabledAddonList()
{
return enabledAddonList;
}


// ---------------------------------------------------------------------
// Section: Database methods
// ---------------------------------------------------------------------



}
18 changes: 18 additions & 0 deletions src/main/resources/locales/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@ visit:
main:
description: 'Opens GUI that allows to edit default or targeted island settings.'
parameters: '[player]'
gui:
player:
title:
choose: '&7 Choose Island'
button:
gamemode:
description: 'Choose from [gamemode] islands'
next:
name: 'Next Page'
previous:
name: 'Previous Page'

messages:
visit:
messages:
load-skipping: 'Skipping to load [value] visit settings object'
load-overwriting: 'Overwriting visit settings object with ID [value]'
load-add: 'Adding visit settings object with ID [value]'

protection:
flags:
Expand Down

0 comments on commit a947eb0

Please sign in to comment.