Add lifeSpan to history data (#90).

In configuration add ability to add lifespan to history data. Data that will be older then configured values, will be removed from database.
This operation will happen only on player data save operation.
This commit is contained in:
BONNe1704 2019-02-19 18:30:12 +02:00
parent 8649409cbb
commit f38aee6ed8
3 changed files with 78 additions and 22 deletions

View File

@ -3,13 +3,7 @@ package world.bentobox.challenges;
import org.eclipse.jdt.annotation.NonNull;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.*;
import java.util.stream.Collectors;
import org.bukkit.Bukkit;
@ -431,7 +425,24 @@ public class ChallengesManager
{
if (this.playerCacheData.containsKey(uniqueID))
{
this.playersDatabase.saveObject(this.playerCacheData.get(uniqueID));
// Clean History Data
ChallengesPlayerData cachedData = this.playerCacheData.get(uniqueID);
if (this.settings.getLifeSpan() > 0)
{
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_YEAR, -this.settings.getLifeSpan());
long survivalTime = calendar.getTimeInMillis();
Iterator<LogEntry> entryIterator = cachedData.getHistory().iterator();
while (this.shouldBeRemoved(entryIterator.next(), survivalTime))
{
entryIterator.remove();
}
}
this.playersDatabase.saveObject(cachedData);
}
}
@ -441,6 +452,19 @@ public class ChallengesManager
// ---------------------------------------------------------------------
/**
* This method returns if given log entry stored time stamp is older then survivalTime.
* @param entry Entry that must be checed.
* @param survivalTime TimeStamp value.
* @return true, if log entry is too old for database.
*/
private boolean shouldBeRemoved(LogEntry entry, long survivalTime)
{
return entry.getTimestamp() < survivalTime;
}
/**
* This method returns UUID that corresponds to player or player's island in given world.
*

View File

@ -49,6 +49,17 @@ public class Settings implements DataObject
@ConfigEntry(path = "commands.single-gamemode")
private GuiMode userGuiMode = GuiMode.CURRENT_WORLD;
@ConfigComment("")
@ConfigComment("This indicate if player challenges data history will be stored or not.")
@ConfigEntry(path = "history.store-history-data")
private boolean storeHistory = false;
@ConfigComment("")
@ConfigComment("This allows to specify an amount of time in days when history data will")
@ConfigComment("be removed. 0 means that data will not be removed.")
@ConfigEntry(path = "history.lifespan")
private int lifeSpan = 14;
@ConfigComment("")
@ConfigComment("Reset Challenges - if this is true, player's challenges will reset when they")
@ConfigComment("reset an island or if they are kicked or leave a team. Prevents exploiting the")
@ -82,11 +93,6 @@ public class Settings implements DataObject
@ConfigEntry(path = "store-island-data")
private boolean storeAsIslandData = false;
@ConfigComment("")
@ConfigComment("This indicate if player challenges data history will be stored or not.")
@ConfigEntry(path = "store-history-data")
private boolean storeHistory = false;
@ConfigComment("")
@ConfigComment("This allows to change lore description line length. By default it is 25, but some server")
@ConfigComment("owners may like it to be larger.")
@ -146,7 +152,7 @@ public class Settings implements DataObject
* Configuration version
*/
@ConfigComment("")
private String configVersion = "v1.4";
private String configVersion = "v1.5";
// ---------------------------------------------------------------------
// Section: Methods
@ -304,6 +310,26 @@ public class Settings implements DataObject
}
/**
* This method returns the userGuiMode value.
* @return the value of userGuiMode.
*/
public GuiMode getUserGuiMode()
{
return userGuiMode;
}
/**
* This method returns the lifeSpan value.
* @return the value of lifeSpan.
*/
public int getLifeSpan()
{
return lifeSpan;
}
/**
* This method sets the userGuiMode value.
* @param userGuiMode the userGuiMode new value.
@ -466,12 +492,13 @@ public class Settings implements DataObject
/**
* This method returns the userGuiMode value.
* @return the value of userGuiMode.
* This method sets the lifeSpan value.
* @param lifeSpan the lifeSpan new value.
*
*/
public GuiMode getUserGuiMode()
public void setLifeSpan(int lifeSpan)
{
return userGuiMode;
this.lifeSpan = lifeSpan;
}

View File

@ -27,6 +27,14 @@ commands:
# - CURRENT_WORLD - will open GUI that corresponds to user location.
# - GAMEMODE_LIST - will open GUI with all installed game modes.
single-gamemode: CURRENT_WORLD
history:
#
# This indicate if player challenges data history will be stored or not.
store-history-data: false
#
# This allows to specify an amount of time in days when history data will
# be removed. 0 means that data will not be removed.
lifespan: false
#
# Reset Challenges - if this is true, player's challenges will reset when they
# reset an island or if they are kicked or leave a team. Prevents exploiting the
@ -49,9 +57,6 @@ free-challenges-first: true
# This indicate if challenges data will be stored per island (true) or per player (false).
store-island-data: false
#
# This indicate if player challenges data history will be stored or not.
store-history-data: false
#
# This allows to change lore description line length. By default it is 25, but some server
# owners may like it to be larger.
lore-length: 25
@ -95,4 +100,4 @@ disabled-gamemodes: []
#
uniqueId: config
#
configVersion: v1.4
configVersion: v1.5