Implement history data storing in ChallengesPlayerData object (#90).

- Add new variable "history" in ChallengesPlayerData.
- Add new methods in ChallengesManager that populates LogEntry and adds it to history variable.
- Add ability to enable/disable history storing in config (default: disabled).

- Fix issue when resetAllChallenges resets only caller challenges.
This commit is contained in:
BONNe 2019-02-18 11:28:07 +02:00
parent ca2b7e2ec6
commit ffaffde734
7 changed files with 114 additions and 30 deletions

View File

@ -15,6 +15,7 @@ import java.util.stream.Collectors;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.World; import org.bukkit.World;
import world.bentobox.bentobox.api.logs.LogEntry;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.Database; import world.bentobox.bentobox.database.Database;
import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.database.objects.Island;
@ -689,6 +690,26 @@ public class ChallengesManager
} }
/**
* This methods adds given log entry to database.
*
* @param storageDataID of type UUID
* @param entry of type LogEntry
*/
private void addLogEntry(@NonNull UUID storageDataID, @NonNull LogEntry entry)
{
// Store data only if it is enabled.
if (this.settings.isStoreHistory())
{
this.addPlayerData(storageDataID);
this.playerCacheData.get(storageDataID).addHistoryRecord(entry);
// Save
this.savePlayerData(storageDataID);
}
}
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
// Section: Public methods for processing player/island data. // Section: Public methods for processing player/island data.
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
@ -754,8 +775,32 @@ public class ChallengesManager
*/ */
public void setChallengeComplete(UUID userID, World world, Challenge challenge) public void setChallengeComplete(UUID userID, World world, Challenge challenge)
{ {
world = Util.getWorld(world); UUID storageID = this.getDataUniqueID(userID, Util.getWorld(world));
this.setChallengeComplete(this.getDataUniqueID(userID, world), challenge.getUniqueId()); this.setChallengeComplete(storageID, challenge.getUniqueId());
this.addLogEntry(storageID, new LogEntry.Builder("COMPLETE").
data("user-id", userID.toString()).
data("challenge-id", challenge.getUniqueId()).
build());
}
/**
* This method sets given challenge as completed.
* @param userID - Targeted user.
* @param world - World where completion must be called.
* @param challenge - That must be completed.
* @param adminID - admin who sets challenge as completed.
*/
public void setChallengeComplete(UUID userID, World world, Challenge challenge, UUID adminID)
{
UUID storageID = this.getDataUniqueID(userID, Util.getWorld(world));
this.setChallengeComplete(storageID, challenge.getUniqueId());
this.addLogEntry(storageID, new LogEntry.Builder("COMPLETE").
data("user-id", userID.toString()).
data("challenge-id", challenge.getUniqueId()).
data("admin-id", adminID == null ? "OP" : adminID.toString()).
build());
} }
@ -765,10 +810,16 @@ public class ChallengesManager
* @param world - World where reset must be called. * @param world - World where reset must be called.
* @param challenge - That must be reset. * @param challenge - That must be reset.
*/ */
public void resetChallenge(UUID userID, World world, Challenge challenge) public void resetChallenge(UUID userID, World world, Challenge challenge, UUID adminID)
{ {
world = Util.getWorld(world); UUID storageID = this.getDataUniqueID(userID, Util.getWorld(world));
this.resetChallenge(this.getDataUniqueID(userID, world), challenge.getUniqueId());
this.resetChallenge(storageID, challenge.getUniqueId());
this.addLogEntry(storageID, new LogEntry.Builder("RESET").
data("user-id", userID.toString()).
data("challenge-id", challenge.getUniqueId()).
data("admin-id", adminID == null ? "RESET" : adminID.toString()).
build());
} }
@ -779,8 +830,7 @@ public class ChallengesManager
*/ */
public void resetAllChallenges(User user, World world) public void resetAllChallenges(User user, World world)
{ {
world = Util.getWorld(world); this.resetAllChallenges(user.getUniqueId(), world, null);
this.resetAllChallenges(this.getDataUniqueID(user, world), world.getName());
} }
@ -788,11 +838,18 @@ public class ChallengesManager
* This method resets all challenges in given world. * This method resets all challenges in given world.
* @param userID - Targeted user. * @param userID - Targeted user.
* @param world - World where challenges must be reset. * @param world - World where challenges must be reset.
* @param adminID - admin iD
*/ */
public void resetAllChallenges(UUID userID, World world) public void resetAllChallenges(UUID userID, World world, UUID adminID)
{ {
world = Util.getWorld(world); world = Util.getWorld(world);
this.resetChallenge(this.getDataUniqueID(userID, world), world.getName()); UUID storageID = this.getDataUniqueID(userID, world);
this.resetAllChallenges(storageID, world.getName());
this.addLogEntry(storageID, new LogEntry.Builder("RESET_ALL").
data("user-id", userID.toString()).
data("admin-id", adminID == null ? "ISLAND_RESET" : adminID.toString()).
build());
} }
@ -820,8 +877,7 @@ public class ChallengesManager
*/ */
public boolean isLevelCompleted(User user, World world, ChallengeLevel level) public boolean isLevelCompleted(User user, World world, ChallengeLevel level)
{ {
world = Util.getWorld(world); return this.isLevelCompleted(this.getDataUniqueID(user, Util.getWorld(world)), level.getUniqueId());
return this.isLevelCompleted(this.getDataUniqueID(user, world), level.getUniqueId());
} }
@ -834,8 +890,7 @@ public class ChallengesManager
*/ */
public boolean isLevelUnlocked(User user, World world, ChallengeLevel level) public boolean isLevelUnlocked(User user, World world, ChallengeLevel level)
{ {
world = Util.getWorld(world); return this.isLevelUnlocked(this.getDataUniqueID(user, Util.getWorld(world)), level);
return this.isLevelUnlocked(this.getDataUniqueID(user, world), level);
} }
@ -847,8 +902,12 @@ public class ChallengesManager
*/ */
public void setLevelComplete(User user, World world, ChallengeLevel level) public void setLevelComplete(User user, World world, ChallengeLevel level)
{ {
world = Util.getWorld(world); UUID storageID = this.getDataUniqueID(user, Util.getWorld(world));
this.setLevelComplete(this.getDataUniqueID(user, world), level.getUniqueId());
this.setLevelComplete(storageID, level.getUniqueId());
this.addLogEntry(storageID, new LogEntry.Builder("COMPLETE_LEVEL").
data("user-id", user.getUniqueId().toString()).
data("level", level.getUniqueId()).build());
} }
@ -861,8 +920,7 @@ public class ChallengesManager
*/ */
public boolean validateLevelCompletion(User user, World world, ChallengeLevel level) public boolean validateLevelCompletion(User user, World world, ChallengeLevel level)
{ {
world = Util.getWorld(world); return this.validateLevelCompletion(this.getDataUniqueID(user, Util.getWorld(world)), level);
return this.validateLevelCompletion(this.getDataUniqueID(user, world), level);
} }
@ -875,8 +933,7 @@ public class ChallengesManager
*/ */
public LevelStatus getChallengeLevelStatus(User user, World world, ChallengeLevel level) public LevelStatus getChallengeLevelStatus(User user, World world, ChallengeLevel level)
{ {
world = Util.getWorld(world); return this.getChallengeLevelStatus(this.getDataUniqueID(user, Util.getWorld(world)), level);
return this.getChallengeLevelStatus(this.getDataUniqueID(user, world), level);
} }
@ -889,8 +946,7 @@ public class ChallengesManager
*/ */
public LevelStatus getChallengeLevelStatus(UUID user, World world, ChallengeLevel level) public LevelStatus getChallengeLevelStatus(UUID user, World world, ChallengeLevel level)
{ {
world = Util.getWorld(world); return this.getChallengeLevelStatus(this.getDataUniqueID(user, Util.getWorld(world)), level);
return this.getChallengeLevelStatus(this.getDataUniqueID(user, world), level);
} }

View File

@ -51,6 +51,11 @@ public class Settings implements DataObject
@ConfigEntry(path = "store-island-data") @ConfigEntry(path = "store-island-data")
private boolean storeAsIslandData = false; 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("")
@ConfigComment("This allows to change lore description line length. By default it is 25, but some server") @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.") @ConfigComment("owners may like it to be larger.")
@ -110,7 +115,7 @@ public class Settings implements DataObject
* Configuration version * Configuration version
*/ */
@ConfigComment("") @ConfigComment("")
private String configVersion = "v1.2"; private String configVersion = "v1.3";
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
// Section: Methods // Section: Methods
@ -228,6 +233,16 @@ public class Settings implements DataObject
} }
/**
* This method returns the storeHistory object.
* @return the storeHistory object.
*/
public boolean isStoreHistory()
{
return storeHistory;
}
/** /**
* This method sets the configVersion object value. * This method sets the configVersion object value.
* @param configVersion the configVersion object new value. * @param configVersion the configVersion object new value.
@ -337,4 +352,14 @@ public class Settings implements DataObject
{ {
this.storeAsIslandData = storeAsIslandData; this.storeAsIslandData = storeAsIslandData;
} }
/**
* This method sets the storeHistory object value.
* @param storeHistory the storeHistory object new value.
*/
public void setStoreHistory(boolean storeHistory)
{
this.storeHistory = storeHistory;
}
} }

View File

@ -57,7 +57,7 @@ public class CompleteChallenge extends CompositeCommand {
return false; return false;
} }
// Complete challenge // Complete challenge
manager.setChallengeComplete(targetUUID, this.getWorld(), this.manager.getChallenge(args.get(1))); manager.setChallengeComplete(targetUUID, this.getWorld(), this.manager.getChallenge(args.get(1)), user.getUniqueId());
user.sendMessage("general.success"); user.sendMessage("general.success");
return true; return true;
} }

View File

@ -61,7 +61,7 @@ public class ResetChallenge extends CompositeCommand {
return false; return false;
} }
// Complete challenge // Complete challenge
manager.resetChallenge(targetUUID, this.getWorld(), manager.getChallenge(args.get(1))); manager.resetChallenge(targetUUID, this.getWorld(), manager.getChallenge(args.get(1)), user.getUniqueId());
user.sendMessage("general.success"); user.sendMessage("general.success");
return true; return true;
} }

View File

@ -28,7 +28,7 @@ public class ResetListener implements Listener {
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onIslandReset(IslandEvent e) { public void onIslandReset(IslandEvent e) {
if (e.getReason().equals(Reason.CREATED) || (addon.getChallengesSettings().isResetChallenges() && e.getReason().equals(Reason.RESETTED))) { if (e.getReason().equals(Reason.CREATED) || (addon.getChallengesSettings().isResetChallenges() && e.getReason().equals(Reason.RESETTED))) {
addon.getChallengesManager().resetAllChallenges(e.getOwner(), e.getLocation().getWorld()); addon.getChallengesManager().resetAllChallenges(e.getOwner(), e.getLocation().getWorld(), e.getOwner());
} }
} }
} }

View File

@ -198,7 +198,7 @@ public class ListUsersGUI extends CommonGUI
new SelectChallengeGUI(this.user, challengeDescriptionMap, lineLength, (status, valueSet) -> { new SelectChallengeGUI(this.user, challengeDescriptionMap, lineLength, (status, valueSet) -> {
if (status) if (status)
{ {
valueSet.forEach(challenge -> manager.setChallengeComplete(player.getUniqueId(), this.world, challenge)); valueSet.forEach(challenge -> manager.setChallengeComplete(player.getUniqueId(), this.world, challenge, this.user.getUniqueId()));
} }
this.build(); this.build();
@ -218,7 +218,7 @@ public class ListUsersGUI extends CommonGUI
new SelectChallengeGUI(this.user, challengeDescriptionMap, lineLength, (status, valueSet) -> { new SelectChallengeGUI(this.user, challengeDescriptionMap, lineLength, (status, valueSet) -> {
if (status) if (status)
{ {
valueSet.forEach(challenge -> manager.resetChallenge(player.getUniqueId(), this.world, challenge)); valueSet.forEach(challenge -> manager.resetChallenge(player.getUniqueId(), this.world, challenge, this.user.getUniqueId()));
} }
this.build(); this.build();
@ -228,7 +228,7 @@ public class ListUsersGUI extends CommonGUI
new ConfirmationGUI(this.user, status -> { new ConfirmationGUI(this.user, status -> {
if (status) if (status)
{ {
manager.resetAllChallenges(this.user, this.world); manager.resetAllChallenges(player.getUniqueId(), this.world, this.user.getUniqueId());
} }
this.build(); this.build();

View File

@ -23,7 +23,10 @@ add-completed-glow: true
free-challenges-first: true free-challenges-first: true
# #
# This indicate if challenges data will be stored per island (true) or per player (false). # This indicate if challenges data will be stored per island (true) or per player (false).
store-island-data: true 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 # This allows to change lore description line length. By default it is 25, but some server
# owners may like it to be larger. # owners may like it to be larger.
@ -68,4 +71,4 @@ disabled-gamemodes: []
# #
uniqueId: config uniqueId: config
# #
configVersion: v1.2 configVersion: v1.3