Skip to content

Commit

Permalink
Implement history data storing in ChallengesPlayerData object (#90).
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
BONNe committed Feb 18, 2019
1 parent ca2b7e2 commit ffaffde
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 30 deletions.
98 changes: 77 additions & 21 deletions src/main/java/world/bentobox/challenges/ChallengesManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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;
Expand Down Expand Up @@ -689,6 +690,26 @@ private void setLevelComplete(@NonNull UUID storageDataID, @NonNull String level
}


/**
* 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.
// ---------------------------------------------------------------------
Expand Down Expand Up @@ -754,8 +775,32 @@ public void setChallengeComplete(User user, World world, Challenge challenge)
*/
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());
}


Expand All @@ -765,10 +810,16 @@ public void setChallengeComplete(UUID userID, World world, Challenge challenge)
* @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());
}


Expand All @@ -779,20 +830,26 @@ public void resetChallenge(UUID userID, World world, Challenge challenge)
*/
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);
}


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


Expand Down Expand Up @@ -820,8 +877,7 @@ public long getChallengeTimes(User user, World world, Challenge challenge)
*/
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());
}


Expand All @@ -834,8 +890,7 @@ public boolean isLevelCompleted(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, world), level);
return this.isLevelUnlocked(this.getDataUniqueID(user, Util.getWorld(world)), level);
}


Expand All @@ -847,8 +902,12 @@ public boolean isLevelUnlocked(User user, World world, ChallengeLevel level)
*/
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());
}


Expand All @@ -861,8 +920,7 @@ public void setLevelComplete(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, world), level);
return this.validateLevelCompletion(this.getDataUniqueID(user, Util.getWorld(world)), level);
}


Expand All @@ -875,8 +933,7 @@ public boolean validateLevelCompletion(User user, World world, ChallengeLevel le
*/
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);
}


Expand All @@ -889,8 +946,7 @@ public LevelStatus getChallengeLevelStatus(User user, World world, ChallengeLeve
*/
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);
}


Expand Down
27 changes: 26 additions & 1 deletion src/main/java/world/bentobox/challenges/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down Expand Up @@ -110,7 +115,7 @@ public class Settings implements DataObject
* Configuration version
*/
@ConfigComment("")
private String configVersion = "v1.2";
private String configVersion = "v1.3";

// ---------------------------------------------------------------------
// Section: Methods
Expand Down Expand Up @@ -228,6 +233,16 @@ public boolean isStoreAsIslandData()
}


/**
* 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.
Expand Down Expand Up @@ -337,4 +352,14 @@ public void setStoreAsIslandData(boolean 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public boolean execute(User user, String label, List<String> args) {
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public boolean execute(User user, String label, List<String> args) {
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public ResetListener(ChallengesAddon addon) {
@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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ private PanelItem createPlayerIcon(Player player)
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();
Expand All @@ -218,7 +218,7 @@ private PanelItem createPlayerIcon(Player player)
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();
Expand All @@ -228,7 +228,7 @@ private PanelItem createPlayerIcon(Player player)
new ConfirmationGUI(this.user, status -> {
if (status)
{
manager.resetAllChallenges(this.user, this.world);
manager.resetAllChallenges(player.getUniqueId(), this.world, this.user.getUniqueId());
}

this.build();
Expand Down
7 changes: 5 additions & 2 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -68,4 +71,4 @@ disabled-gamemodes: []
#
uniqueId: config
#
configVersion: v1.2
configVersion: v1.3

0 comments on commit ffaffde

Please sign in to comment.