Skip to content

Commit

Permalink
Adds island history for team members adding and removing
Browse files Browse the repository at this point in the history
Adds placeholder for the historical number of players
  • Loading branch information
tastybento committed Nov 11, 2024
1 parent d339edf commit b135484
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import world.bentobox.bentobox.api.events.island.IslandEvent;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.logs.LogEntry;
import world.bentobox.bentobox.api.logs.LogEntry.LogType;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.RanksManager;
Expand Down Expand Up @@ -115,10 +116,10 @@ void unregisterIsland(User user) {
// Remove all island players that reference this island
targetIsland.getMembers().clear();
if (user.isPlayer()) {
targetIsland.log(new LogEntry.Builder("UNREGISTER").data("player", targetUUID.toString())
targetIsland.log(new LogEntry.Builder(LogType.UNREGISTER).data("player", targetUUID.toString())
.data("admin", user.getUniqueId().toString()).build());
} else {
targetIsland.log(new LogEntry.Builder("UNREGISTER").data("player", targetUUID.toString())
targetIsland.log(new LogEntry.Builder(LogType.UNREGISTER).data("player", targetUUID.toString())
.data("admin", "console").build());
}
user.sendMessage("commands.admin.unregister.unregistered-island", TextVariables.XYZ, Util.xyz(targetIsland.getCenter().toVector()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import world.bentobox.bentobox.api.events.island.IslandEvent;
import world.bentobox.bentobox.api.events.team.TeamEvent;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.logs.LogEntry;
import world.bentobox.bentobox.api.logs.LogEntry.LogType;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.database.objects.TeamInvite;
Expand Down Expand Up @@ -120,6 +122,9 @@ void acceptTrustInvite(User user, TeamInvite invite) {
user.sendMessage("commands.island.team.trust.you-are-trusted", TextVariables.NAME, inviter.getName(),
TextVariables.DISPLAY_NAME, inviter.getDisplayName());
}
// Add historu record
island.log(new LogEntry.Builder(LogType.TRUSTED).data(user.getUniqueId().toString(), "trusted")
.data(invite.getInviter().toString(), "trusted by").build());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import world.bentobox.bentobox.api.events.island.IslandEvent;
import world.bentobox.bentobox.api.events.team.TeamEvent;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.logs.LogEntry;
import world.bentobox.bentobox.api.logs.LogEntry.LogType;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.RanksManager;
Expand Down Expand Up @@ -91,6 +93,9 @@ protected boolean setOwner(User user, @NonNull UUID targetUUID2) {
IslandEvent.builder().island(island).involvedPlayer(user.getUniqueId()).admin(false)
.reason(IslandEvent.Reason.RANK_CHANGE).rankChange(RanksManager.OWNER_RANK, RanksManager.SUB_OWNER_RANK)
.build();
// Add historu record
island.log(new LogEntry.Builder(LogType.NEWOWNER).data(targetUUID2.toString(), "new owner")
.data(user.getUniqueId().toString(), "old owner").build());
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.logs.LogEntry;
import world.bentobox.bentobox.api.logs.LogEntry.LogType;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.database.objects.TeamInvite.Type;
Expand Down Expand Up @@ -110,6 +112,10 @@ public boolean execute(User user, String label, List<String> args) {
island.setRank(target, RanksManager.TRUSTED_RANK);
user.sendMessage("commands.island.team.trust.success", TextVariables.NAME, target.getName(), TextVariables.DISPLAY_NAME, target.getDisplayName());
target.sendMessage("commands.island.team.trust.you-are-trusted", TextVariables.NAME, user.getName(), TextVariables.DISPLAY_NAME, user.getDisplayName());
// Add historu record
island.log(new LogEntry.Builder(LogType.TRUSTED).data(targetUUID.toString(), "trusted")
.data(user.getUniqueId().toString(), "trusted by").build());

}
return true;
} else {
Expand Down
17 changes: 11 additions & 6 deletions src/main/java/world/bentobox/bentobox/api/logs/LogEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,21 @@
* An {@link world.bentobox.bentobox.database.objects.adapters.AdapterInterface AdapterInterface} is provided to be able to save/retrieve
* a list of instances of this object to/from the database: {@link world.bentobox.bentobox.database.objects.adapters.LogEntryListAdapter LogEntryListAdapter}.
*
* @author Poslovitch
* @author Poslovitch, tastybento
*
*/
public class LogEntry {
@Expose
private final long timestamp;
@Expose
private final String type;
private final LogType type;
@Expose
private final Map<String, String> data;

public enum LogType {
REMOVE, ADD, UNREGISTER, BAN, UNOWNED, SPAWN, UNBAN, JOINED, NEWOWNER, TRUSTED, UNKNOWN
}

private LogEntry(@NonNull Builder builder) {
this.timestamp = builder.timestamp;
this.type = builder.type;
Expand All @@ -36,7 +41,7 @@ public long getTimestamp() {
}

@NonNull
public String getType() {
public LogType getType() {
return type;
}

Expand All @@ -47,12 +52,12 @@ public Map<String, String> getData() {

public static class Builder {
private long timestamp;
private final String type;
private final LogType type;
private Map<String, String> data;

public Builder(@NonNull String type) {
public Builder(LogType type) {
this.timestamp = System.currentTimeMillis();
this.type = type.toUpperCase(Locale.ENGLISH);
this.type = type;
this.data = new LinkedHashMap<>();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import world.bentobox.bentobox.api.configuration.WorldSettings;
import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.bentobox.api.logs.LogEntry;
import world.bentobox.bentobox.api.logs.LogEntry.LogType;
import world.bentobox.bentobox.api.metadata.MetaDataAble;
import world.bentobox.bentobox.api.metadata.MetaDataValue;
import world.bentobox.bentobox.api.user.User;
Expand Down Expand Up @@ -327,7 +328,7 @@ public void addMember(@NonNull UUID playerUUID) {
public boolean ban(@NonNull UUID issuer, @NonNull UUID target) {
if (getRank(target) != RanksManager.BANNED_RANK) {
setRank(target, RanksManager.BANNED_RANK);
log(new LogEntry.Builder("BAN").data("player", target.toString()).data("issuer", issuer.toString())
log(new LogEntry.Builder(LogType.BAN).data("player", target.toString()).data("issuer", issuer.toString())
.build());
setChanged();
}
Expand Down Expand Up @@ -360,7 +361,7 @@ public Set<UUID> getBanned() {
*/
public boolean unban(@NonNull UUID issuer, @NonNull UUID target) {
if (members.remove(target) != null) {
log(new LogEntry.Builder("UNBAN").data("player", target.toString()).data("issuer", issuer.toString())
log(new LogEntry.Builder(LogType.UNBAN).data("player", target.toString()).data("issuer", issuer.toString())
.build());
return true;
}
Expand Down Expand Up @@ -1132,7 +1133,7 @@ public void setOwner(@Nullable UUID owner) {

this.owner = owner;
if (owner == null) {
log(new LogEntry.Builder("UNOWNED").build());
log(new LogEntry.Builder(LogType.UNOWNED).build());
return;
}
// Defensive code: demote any previous owner
Expand Down Expand Up @@ -1281,7 +1282,7 @@ public void setSpawn(boolean isSpawn) {
setFlagsDefaults();
setFlag(Flags.LOCK, RanksManager.VISITOR_RANK);
}
log(new LogEntry.Builder("SPAWN").data("value", String.valueOf(isSpawn)).build());
log(new LogEntry.Builder(LogType.SPAWN).data("value", String.valueOf(isSpawn)).build());
setChanged();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
import java.util.List;
import java.util.Map;

import com.google.common.base.Enums;

import world.bentobox.bentobox.api.logs.LogEntry;
import world.bentobox.bentobox.api.logs.LogEntry.LogType;

/**
* @author Poslovitch
Expand Down Expand Up @@ -41,7 +44,7 @@ public List<LogEntry> deserialize(Object object) {
List<Map<String, Object>> serialized = (List<Map<String, Object>>) object;
for (Map<String, Object> entry : serialized) {
long timestamp = (long) entry.get(TIMESTAMP);
String type = (String) entry.get(TYPE);
LogType type = Enums.getIfPresent(LogType.class, (String) entry.get(TYPE)).or(LogType.UNKNOWN);
Map<String, String> data = (Map<String, String>) entry.get(DATA);

result.add(new LogEntry.Builder(type).timestamp(timestamp).data(data).build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
import java.text.DateFormat;
import java.time.Instant;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;

import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.logs.LogEntry;
import world.bentobox.bentobox.api.logs.LogEntry.LogType;
import world.bentobox.bentobox.api.placeholders.GameModePlaceholderReplacer;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
Expand Down Expand Up @@ -120,6 +125,14 @@ public enum GameModePlaceholder {
* @since 1.5.0
*/
ISLAND_MEMBERS_COUNT("island_members_count", (addon, user, island) -> island == null ? "" : String.valueOf(island.getMemberSet().size())),

/**
* Returns the number of players that are or have ever been a MEMBER on this island.
* @since 3.0.0
*/
ISLAND_HISTORICAL_MEMBERS_COUNT("island_historical_members_count",
(addon, user, island) -> island == null ? "" : getHistoricalMembers(island)),

/**
* Returns a comma separated list of player names that are at least MEMBER on this island.
* @since 1.13.0
Expand Down Expand Up @@ -395,6 +408,24 @@ public enum GameModePlaceholder {
this.replacer = replacer;
}

/**
* Provides a count of how many players have ever joined the island as a member including the owner
* @param island island
* @return String count of the number of members
*/
private static String getHistoricalMembers(@Nullable Island island) {
Set<String> uniqueMembers = new HashSet<>();
for (LogEntry le : island.getHistory()) {
if (le.getType() == LogType.JOINED) {
Iterator<String> it = le.getData().keySet().iterator();
while (it.hasNext()) {
uniqueMembers.add(it.next());
}
}
}
return String.valueOf(uniqueMembers.size());
}

/**
* Get the visited island
* @param addon - game mode addon
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
import world.bentobox.bentobox.api.events.island.IslandEvent.Reason;
import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.logs.LogEntry;
import world.bentobox.bentobox.api.logs.LogEntry.LogType;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.Database;
import world.bentobox.bentobox.database.json.BentoboxTypeAdapterFactory;
Expand Down Expand Up @@ -1189,6 +1191,7 @@ public void spawnTeleport(@NonNull World world, @NonNull Player player) {
*
* @param player player
*/
@SuppressWarnings("deprecation")
private void readyPlayer(@NonNull Player player) {
// Stop any gliding
player.setGliding(false);
Expand Down Expand Up @@ -1537,6 +1540,8 @@ public void setJoinTeam(Island teamIsland, UUID playerUUID) {
// Add player to new island
teamIsland.addMember(playerUUID);
islandCache.addPlayer(playerUUID, teamIsland);
// Add historu record
teamIsland.log(new LogEntry.Builder(LogType.JOINED).data(playerUUID.toString(), "player").build());
// Save the island
updateIsland(teamIsland);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.bentobox.api.logs.LogEntry;
import world.bentobox.bentobox.api.logs.LogEntry.LogType;
import world.bentobox.bentobox.database.Database;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.RanksManager;
Expand Down Expand Up @@ -431,6 +433,8 @@ public void removePlayer(@NonNull Island island, @NonNull UUID uuid) {
}
island.removeMember(uuid);
island.removePrimary(uuid);
// Add historu record
island.log(new LogEntry.Builder(LogType.REMOVE).data(uuid.toString(), "player").build());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import world.bentobox.bentobox.api.events.island.IslandCreateEvent;
import world.bentobox.bentobox.api.events.island.IslandEvent;
import world.bentobox.bentobox.api.events.island.IslandEvent.Reason;
import world.bentobox.bentobox.api.logs.LogEntry;
import world.bentobox.bentobox.api.logs.LogEntry.LogType;
import world.bentobox.bentobox.api.events.island.IslandResetEvent;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
Expand Down Expand Up @@ -217,6 +219,8 @@ public void newIsland(Island oldIsland) throws IOException {
island.setFlagsDefaults();
// Register metrics
plugin.getMetrics().ifPresent(BStats::increaseIslandsCreatedCount);
// Add historu record
island.log(new LogEntry.Builder(LogType.JOINED).data(user.getUniqueId().toString(), "owner").build());
// Save island
IslandsManager.updateIsland(island);
}
Expand Down

0 comments on commit b135484

Please sign in to comment.