diff --git a/src/main/java/world/bentobox/challenges/ChallengesManager.java b/src/main/java/world/bentobox/challenges/ChallengesManager.java index 5bae211..4178141 100644 --- a/src/main/java/world/bentobox/challenges/ChallengesManager.java +++ b/src/main/java/world/bentobox/challenges/ChallengesManager.java @@ -15,6 +15,7 @@ import java.util.stream.Collectors; import org.bukkit.Bukkit; import org.bukkit.World; +import world.bentobox.bentobox.api.logs.LogEntry; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.database.Database; 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. // --------------------------------------------------------------------- @@ -754,8 +775,32 @@ public class ChallengesManager */ public void setChallengeComplete(UUID userID, World world, Challenge challenge) { - world = Util.getWorld(world); - this.setChallengeComplete(this.getDataUniqueID(userID, world), challenge.getUniqueId()); + 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()). + 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 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); - this.resetChallenge(this.getDataUniqueID(userID, world), challenge.getUniqueId()); + UUID storageID = this.getDataUniqueID(userID, Util.getWorld(world)); + + 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) { - world = Util.getWorld(world); - this.resetAllChallenges(this.getDataUniqueID(user, world), world.getName()); + this.resetAllChallenges(user.getUniqueId(), world, null); } @@ -788,11 +838,18 @@ public class ChallengesManager * This method resets all challenges in given world. * @param userID - Targeted user. * @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); - 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) { - world = Util.getWorld(world); - return this.isLevelCompleted(this.getDataUniqueID(user, world), level.getUniqueId()); + return this.isLevelCompleted(this.getDataUniqueID(user, Util.getWorld(world)), level.getUniqueId()); } @@ -834,8 +890,7 @@ public class ChallengesManager */ public boolean isLevelUnlocked(User user, World world, ChallengeLevel level) { - world = Util.getWorld(world); - return this.isLevelUnlocked(this.getDataUniqueID(user, world), level); + return this.isLevelUnlocked(this.getDataUniqueID(user, Util.getWorld(world)), level); } @@ -847,8 +902,12 @@ public class ChallengesManager */ public void setLevelComplete(User user, World world, ChallengeLevel level) { - world = Util.getWorld(world); - this.setLevelComplete(this.getDataUniqueID(user, world), level.getUniqueId()); + UUID storageID = this.getDataUniqueID(user, Util.getWorld(world)); + + 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) { - world = Util.getWorld(world); - return this.validateLevelCompletion(this.getDataUniqueID(user, world), level); + return this.validateLevelCompletion(this.getDataUniqueID(user, Util.getWorld(world)), level); } @@ -875,8 +933,7 @@ public class ChallengesManager */ public LevelStatus getChallengeLevelStatus(User user, World world, ChallengeLevel level) { - world = Util.getWorld(world); - return this.getChallengeLevelStatus(this.getDataUniqueID(user, world), level); + return this.getChallengeLevelStatus(this.getDataUniqueID(user, Util.getWorld(world)), level); } @@ -889,8 +946,7 @@ public class ChallengesManager */ public LevelStatus getChallengeLevelStatus(UUID user, World world, ChallengeLevel level) { - world = Util.getWorld(world); - return this.getChallengeLevelStatus(this.getDataUniqueID(user, world), level); + return this.getChallengeLevelStatus(this.getDataUniqueID(user, Util.getWorld(world)), level); } diff --git a/src/main/java/world/bentobox/challenges/Settings.java b/src/main/java/world/bentobox/challenges/Settings.java index f6b1863..af84260 100644 --- a/src/main/java/world/bentobox/challenges/Settings.java +++ b/src/main/java/world/bentobox/challenges/Settings.java @@ -51,6 +51,11 @@ 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.") @@ -110,7 +115,7 @@ public class Settings implements DataObject * Configuration version */ @ConfigComment("") - private String configVersion = "v1.2"; + private String configVersion = "v1.3"; // --------------------------------------------------------------------- // 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. * @param configVersion the configVersion object new value. @@ -337,4 +352,14 @@ public class Settings implements DataObject { 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; + } } diff --git a/src/main/java/world/bentobox/challenges/commands/admin/CompleteChallenge.java b/src/main/java/world/bentobox/challenges/commands/admin/CompleteChallenge.java index 5922d09..bb480f1 100644 --- a/src/main/java/world/bentobox/challenges/commands/admin/CompleteChallenge.java +++ b/src/main/java/world/bentobox/challenges/commands/admin/CompleteChallenge.java @@ -57,7 +57,7 @@ public class CompleteChallenge extends CompositeCommand { return false; } // 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"); return true; } diff --git a/src/main/java/world/bentobox/challenges/commands/admin/ResetChallenge.java b/src/main/java/world/bentobox/challenges/commands/admin/ResetChallenge.java index 53d4c1c..b23038c 100644 --- a/src/main/java/world/bentobox/challenges/commands/admin/ResetChallenge.java +++ b/src/main/java/world/bentobox/challenges/commands/admin/ResetChallenge.java @@ -61,7 +61,7 @@ public class ResetChallenge extends CompositeCommand { return false; } // 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"); return true; } diff --git a/src/main/java/world/bentobox/challenges/listeners/ResetListener.java b/src/main/java/world/bentobox/challenges/listeners/ResetListener.java index 0c90f79..5f5f043 100644 --- a/src/main/java/world/bentobox/challenges/listeners/ResetListener.java +++ b/src/main/java/world/bentobox/challenges/listeners/ResetListener.java @@ -28,7 +28,7 @@ public class ResetListener implements Listener { @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) public void onIslandReset(IslandEvent e) { 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()); } } } diff --git a/src/main/java/world/bentobox/challenges/panel/admin/ListUsersGUI.java b/src/main/java/world/bentobox/challenges/panel/admin/ListUsersGUI.java index 6368bfc..3d3b77b 100644 --- a/src/main/java/world/bentobox/challenges/panel/admin/ListUsersGUI.java +++ b/src/main/java/world/bentobox/challenges/panel/admin/ListUsersGUI.java @@ -198,7 +198,7 @@ public class ListUsersGUI extends CommonGUI new SelectChallengeGUI(this.user, challengeDescriptionMap, lineLength, (status, valueSet) -> { 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(); @@ -218,7 +218,7 @@ public class ListUsersGUI extends CommonGUI new SelectChallengeGUI(this.user, challengeDescriptionMap, lineLength, (status, valueSet) -> { 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(); @@ -228,7 +228,7 @@ public class ListUsersGUI extends CommonGUI new ConfirmationGUI(this.user, status -> { if (status) { - manager.resetAllChallenges(this.user, this.world); + manager.resetAllChallenges(player.getUniqueId(), this.world, this.user.getUniqueId()); } this.build(); diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 006b561..fcc96de 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -23,7 +23,10 @@ add-completed-glow: true free-challenges-first: true # # 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 # owners may like it to be larger. @@ -68,4 +71,4 @@ disabled-gamemodes: [] # uniqueId: config # -configVersion: v1.2 \ No newline at end of file +configVersion: v1.3 \ No newline at end of file