Skip to content

Commit

Permalink
Add lifeSpan to history data (#90).
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
BONNe committed Feb 19, 2019
1 parent 8649409 commit f38aee6
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 22 deletions.
40 changes: 32 additions & 8 deletions src/main/java/world/bentobox/challenges/ChallengesManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@

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;
Expand Down Expand Up @@ -431,7 +425,24 @@ private void savePlayerData(@NonNull UUID uniqueID)
{
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);
}
}

Expand All @@ -441,6 +452,19 @@ private void savePlayerData(@NonNull UUID uniqueID)
// ---------------------------------------------------------------------


/**
* 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.
*
Expand Down
47 changes: 37 additions & 10 deletions src/main/java/world/bentobox/challenges/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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.")
Expand Down Expand Up @@ -146,7 +152,7 @@ public class Settings implements DataObject
* Configuration version
*/
@ConfigComment("")
private String configVersion = "v1.4";
private String configVersion = "v1.5";

// ---------------------------------------------------------------------
// Section: Methods
Expand Down Expand Up @@ -304,6 +310,26 @@ public boolean isUseCommonGUI()
}


/**
* 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.
Expand Down Expand Up @@ -466,12 +492,13 @@ public void setUseCommonGUI(boolean useCommonGUI)


/**
* 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;
}


Expand Down
13 changes: 9 additions & 4 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -95,4 +100,4 @@ disabled-gamemodes: []
#
uniqueId: config
#
configVersion: v1.4
configVersion: v1.5

0 comments on commit f38aee6

Please sign in to comment.