Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds maxMembers to the Island object for persistent storage #1691

Merged
merged 4 commits into from
Feb 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ public class IslandTeamCommand extends CompositeCommand {
*/
private Map<UUID, Invite> inviteMap;

private IslandTeamInviteCommand inviteCommand;

public IslandTeamCommand(CompositeCommand parent) {
super(parent, "team");
inviteMap = new HashMap<>();
Expand All @@ -44,7 +42,7 @@ public void setup() {
setOnlyPlayer(true);
setDescription("commands.island.team.description");
// Register commands
inviteCommand = new IslandTeamInviteCommand(this);
new IslandTeamInviteCommand(this);
new IslandTeamLeaveCommand(this);
new IslandTeamSetownerCommand(this);
new IslandTeamKickCommand(this);
Expand Down Expand Up @@ -73,17 +71,21 @@ public boolean execute(User user, String label, List<String> args) {
// Cancelled
return false;
}
Island island = getIslands().getIsland(getWorld(), playerUUID);
if (island == null) {
return false;
}
Set<UUID> teamMembers = getMembers(getWorld(), user);
if (ownerUUID.equals(playerUUID)) {
int maxSize = inviteCommand.getMaxTeamSize(user);
int maxSize = getIslands().getMaxMembers(island, RanksManager.MEMBER_RANK);
if (teamMembers.size() < maxSize) {
user.sendMessage("commands.island.team.invite.you-can-invite", TextVariables.NUMBER, String.valueOf(maxSize - teamMembers.size()));
} else {
user.sendMessage("commands.island.team.invite.errors.island-is-full");
}
}
// Show members of island
showMembers(getIslands().getIsland(getWorld(), playerUUID), user);
showMembers(island, user);
return true;
}

Expand All @@ -101,7 +103,7 @@ private void showMembers(Island island, User user) {

// Show header:
user.sendMessage("commands.island.team.info.header",
"[max]", String.valueOf(inviteCommand.getMaxTeamSize(user)),
"[max]", String.valueOf(getIslands().getMaxMembers(island, RanksManager.MEMBER_RANK)),
"[total]", String.valueOf(island.getMemberSet().size()),
"[online]", String.valueOf(onlineMembers.size()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public boolean execute(User user, String label, List<String> args) {
target.sendMessage("commands.island.team.coop.name-has-invited-you", TextVariables.NAME, user.getName());
target.sendMessage("commands.island.team.invite.to-accept-or-reject", TextVariables.LABEL, getTopLabel());
} else {
if (island.getMemberSet(RanksManager.COOP_RANK, false).size() > getMaxCoopSize(user)) {
if (island.getMemberSet(RanksManager.COOP_RANK, false).size() >= getIslands().getMaxMembers(island, RanksManager.COOP_RANK)) {
user.sendMessage("commands.island.team.coop.is-full");
return false;
}
Expand All @@ -126,13 +126,4 @@ public Optional<List<String>> tabComplete(User user, String alias, List<String>
return Optional.of(Util.tabLimit(Util.getOnlinePlayerList(user), lastArg));
}

/**
* Gets the maximum coop size for this player in this game based on the permission or the world's setting
* @param user user
* @return max coop size of user
* @since 1.13.0
*/
public int getMaxCoopSize(User user) {
return user.getPermissionValue(getPermissionPrefix() + "coop.maxsize", getIWM().getMaxCoopSize(getWorld()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ private void acceptTrustInvite(User user, Invite invite) {
if (inviter != null) {
Island island = getIslands().getIsland(getWorld(), inviter);
if (island != null) {
if (island.getMemberSet(RanksManager.TRUSTED_RANK, false).size() > getIslands().getMaxMembers(island, RanksManager.TRUSTED_RANK)) {
user.sendMessage("commands.island.team.trust.is-full");
return;
}
island.setRank(user, RanksManager.TRUSTED_RANK);
IslandEvent.builder()
.island(island)
Expand All @@ -122,6 +126,10 @@ private void acceptCoopInvite(User user, Invite invite) {
if (inviter != null) {
Island island = getIslands().getIsland(getWorld(), inviter);
if (island != null) {
if (island.getMemberSet(RanksManager.COOP_RANK, false).size() > getIslands().getMaxMembers(island, RanksManager.COOP_RANK)) {
user.sendMessage("commands.island.team.coop.is-full");
return;
}
island.setRank(user, RanksManager.COOP_RANK);
IslandEvent.builder()
.island(island)
Expand All @@ -143,6 +151,10 @@ private void acceptTeamInvite(User user, Invite invite) {
Island island = getIslands().getIsland(getWorld(), playerUUID);
// Get the team's island
Island teamIsland = getIslands().getIsland(getWorld(), prospectiveOwnerUUID);
if (teamIsland.getMemberSet(RanksManager.MEMBER_RANK, true).size() > getIslands().getMaxMembers(teamIsland, RanksManager.MEMBER_RANK)) {
user.sendMessage("commands.island.team.invite.errors.island-is-full");
return;
}
// Remove player as owner of the old island
getIslands().removePlayer(getWorld(), playerUUID);
// Remove money inventory etc. for leaving
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;

import org.eclipse.jdt.annotation.Nullable;
Expand All @@ -16,6 +15,7 @@
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.bentobox.util.Util;

public class IslandTeamInviteCommand extends CompositeCommand {
Expand Down Expand Up @@ -74,7 +74,11 @@ public boolean canExecute(User user, String label, List<String> args) {
user.sendMessage("general.errors.insufficient-rank", TextVariables.RANK, user.getTranslation(getPlugin().getRanksManager().getRank(rank)));
return false;
}

// Check for space on team
if (island.getMemberSet().size() > getIslands().getMaxMembers(island, RanksManager.MEMBER_RANK)) {
user.sendMessage("commands.island.team.invite.errors.island-is-full");
return false;
}

UUID invitedPlayerUUID = getPlayers().getUUID(args.get(0));
if (invitedPlayerUUID == null) {
Expand Down Expand Up @@ -111,40 +115,32 @@ public boolean canExecute(User user, String label, List<String> args) {

@Override
public boolean execute(User user, String label, List<String> args) {
Set<UUID> teamMembers = getMembers(getWorld(), user);
// Check if player has space on their team
int maxSize = getMaxTeamSize(user);
if (teamMembers.size() < maxSize) {
// If that player already has an invite out then retract it.
// Players can only have one invite one at a time - interesting
if (itc.isInvited(invitedPlayer.getUniqueId())) {
itc.removeInvite(invitedPlayer.getUniqueId());
user.sendMessage("commands.island.team.invite.removing-invite");
}
// Fire event so add-ons can run commands, etc.
IslandBaseEvent e = TeamEvent.builder()
.island(getIslands().getIsland(getWorld(), user.getUniqueId()))
.reason(TeamEvent.Reason.INVITE)
.involvedPlayer(invitedPlayer.getUniqueId())
.build();
if (e.getNewEvent().map(IslandBaseEvent::isCancelled).orElse(e.isCancelled())) {
return false;
}
// Put the invited player (key) onto the list with inviter (value)
// If someone else has invited a player, then this invite will overwrite the previous invite!
itc.addInvite(Invite.Type.TEAM, user.getUniqueId(), invitedPlayer.getUniqueId());
user.sendMessage("commands.island.team.invite.invitation-sent", TextVariables.NAME, invitedPlayer.getName());
// Send message to online player
invitedPlayer.sendMessage("commands.island.team.invite.name-has-invited-you", TextVariables.NAME, user.getName());
invitedPlayer.sendMessage("commands.island.team.invite.to-accept-or-reject", TextVariables.LABEL, getTopLabel());
if (getIslands().hasIsland(getWorld(), invitedPlayer.getUniqueId())) {
invitedPlayer.sendMessage("commands.island.team.invite.you-will-lose-your-island");
}
return true;
} else {
user.sendMessage("commands.island.team.invite.errors.island-is-full");
// If that player already has an invite out then retract it.
// Players can only have one invite one at a time - interesting
if (itc.isInvited(invitedPlayer.getUniqueId())) {
itc.removeInvite(invitedPlayer.getUniqueId());
user.sendMessage("commands.island.team.invite.removing-invite");
}
// Fire event so add-ons can run commands, etc.
IslandBaseEvent e = TeamEvent.builder()
.island(getIslands().getIsland(getWorld(), user.getUniqueId()))
.reason(TeamEvent.Reason.INVITE)
.involvedPlayer(invitedPlayer.getUniqueId())
.build();
if (e.getNewEvent().map(IslandBaseEvent::isCancelled).orElse(e.isCancelled())) {
return false;
}
// Put the invited player (key) onto the list with inviter (value)
// If someone else has invited a player, then this invite will overwrite the previous invite!
itc.addInvite(Invite.Type.TEAM, user.getUniqueId(), invitedPlayer.getUniqueId());
user.sendMessage("commands.island.team.invite.invitation-sent", TextVariables.NAME, invitedPlayer.getName());
// Send message to online player
invitedPlayer.sendMessage("commands.island.team.invite.name-has-invited-you", TextVariables.NAME, user.getName());
invitedPlayer.sendMessage("commands.island.team.invite.to-accept-or-reject", TextVariables.LABEL, getTopLabel());
if (getIslands().hasIsland(getWorld(), invitedPlayer.getUniqueId())) {
invitedPlayer.sendMessage("commands.island.team.invite.you-will-lose-your-island");
}
return true;
}

@Override
Expand All @@ -158,12 +154,4 @@ public Optional<List<String>> tabComplete(User user, String alias, List<String>
return Optional.of(Util.tabLimit(options, lastArg));
}

/**
* Gets the maximum team size for this player in this game based on the permission or the world's setting
* @param user user
* @return max team size of user
*/
public int getMaxTeamSize(User user) {
return user.getPermissionValue(getPermissionPrefix() + "team.maxsize", getIWM().getMaxTeamSize(getWorld()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public boolean execute(User user, String label, List<String> args) {
target.sendMessage("commands.island.team.trust.name-has-invited-you", TextVariables.NAME, user.getName());
target.sendMessage("commands.island.team.invite.to-accept-or-reject", TextVariables.LABEL, getTopLabel());
} else {
if (island.getMemberSet(RanksManager.TRUSTED_RANK, false).size() > getMaxTrustSize(user)) {
if (island.getMemberSet(RanksManager.TRUSTED_RANK, false).size() >= getIslands().getMaxMembers(island, RanksManager.TRUSTED_RANK)) {
user.sendMessage("commands.island.team.trust.is-full");
return false;
}
Expand All @@ -128,13 +128,4 @@ public Optional<List<String>> tabComplete(User user, String alias, List<String>
return Optional.of(Util.tabLimit(Util.getOnlinePlayerList(user), lastArg));
}

/**
* Gets the maximum trust size for this player in this game based on the permission or the world's setting
* @param user user
* @return max trust size of user
* @since 1.13.0
*/
public int getMaxTrustSize(User user) {
return user.getPermissionValue(getPermissionPrefix() + "trust.maxsize", getIWM().getMaxTrustSize(getWorld()));
}
}
52 changes: 49 additions & 3 deletions src/main/java/world/bentobox/bentobox/database/objects/Island.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ public class Island implements DataObject, MetaDataAble {
@Expose
private Map<UUID, Integer> members = new HashMap<>();

/**
* Maximum number of members allowed in this island.
* Key is rank, value is number
* @since 1.16.0
*/
@Expose
private Map<Integer, Integer> maxMembers;

//// State ////
@Expose
private boolean spawn = false;
Expand Down Expand Up @@ -1411,6 +1419,44 @@ public void setProtectionCenter(Location location) throws IOException {
setChanged();
}

/**
* @return the maxMembers
* @since 1.16.0
*/
public Map<Integer, Integer> getMaxMembers() {
return maxMembers == null ? new HashMap<>() : maxMembers;
}

/**
* @param maxMembers the maxMembers to set
* @since 1.16.0
*/
public void setMaxMembers(Map<Integer, Integer> maxMembers) {
this.maxMembers = maxMembers;
}

/**
* Get the maximum number of island members
* @param rank island rank value from {@link RanksManager}
* @return the maxMembers for the rank given - if null then the world default should be used. Negative values = unlimited.
* @since 1.16.0
*/
@Nullable
public Integer getMaxMembers(int rank) {
return getMaxMembers().get(rank);
}


/**
* Set the maximum number of island members
* @param rank island rank value from {@link RanksManager}
* @param maxMembers the maxMembers to set. If null then the world default applies. Negative values = unlimited.
* @since 1.16.0
*/
public void setMaxMembers(int rank, Integer maxMembers) {
getMaxMembers().put(rank, maxMembers);
}

@Override
public String toString() {
return "Island [changed=" + changed + ", deleted=" + deleted + ", "
Expand All @@ -1422,9 +1468,9 @@ public String toString() {
+ (gameMode != null ? "gameMode=" + gameMode + ", " : "") + (name != null ? "name=" + name + ", " : "")
+ "createdDate=" + createdDate + ", updatedDate=" + updatedDate + ", "
+ (owner != null ? "owner=" + owner + ", " : "") + (members != null ? "members=" + members + ", " : "")
+ "spawn=" + spawn + ", purgeProtected=" + purgeProtected + ", "
+ (flags != null ? "flags=" + flags + ", " : "") + (history != null ? "history=" + history + ", " : "")
+ "levelHandicap=" + levelHandicap + ", "
+ (maxMembers != null ? "maxMembers=" + maxMembers + ", " : "") + "spawn=" + spawn + ", purgeProtected="
+ purgeProtected + ", " + (flags != null ? "flags=" + flags + ", " : "")
+ (history != null ? "history=" + history + ", " : "") + "levelHandicap=" + levelHandicap + ", "
+ (spawnPoint != null ? "spawnPoint=" + spawnPoint + ", " : "") + "doNotLoad=" + doNotLoad + ", "
+ (cooldowns != null ? "cooldowns=" + cooldowns + ", " : "")
+ (commandRanks != null ? "commandRanks=" + commandRanks + ", " : "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void onPlayerJoin(final PlayerJoinEvent event) {
if (user == null || user.getUniqueId() == null) {
return;
}
UUID playerUUID = user.getUniqueId();
UUID playerUUID = event.getPlayer().getUniqueId();

// Check if player hasn't joined before
if (!players.isKnown(playerUUID)) {
Expand Down Expand Up @@ -89,6 +89,16 @@ public void onPlayerJoin(final PlayerJoinEvent event) {

// Clear inventory if required
clearPlayersInventory(Util.getWorld(event.getPlayer().getWorld()), user);

// Set island max members based on permissions if this player is the owner of an island
plugin.getIWM().getOverWorlds().stream()
.map(w -> plugin.getIslands().getIsland(w, playerUUID))
.filter(i -> playerUUID.equals(i.getOwner()))
.forEach(i -> {
plugin.getIslands().getMaxMembers(i, RanksManager.MEMBER_RANK);
plugin.getIslands().getMaxMembers(i, RanksManager.COOP_RANK);
plugin.getIslands().getMaxMembers(i, RanksManager.TRUSTED_RANK);
});
}


Expand Down
51 changes: 51 additions & 0 deletions src/main/java/world/bentobox/bentobox/managers/IslandsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,57 @@ public Set<UUID> getMembers(@NonNull World world, @NonNull UUID playerUUID) {
return islandCache.getMembers(world, playerUUID, RanksManager.MEMBER_RANK);
}

/**
* Gets the maximum number of island members allowed on this island.
* Will update the value based on world settings or island owner permissions (if online).
* If the island is unowned, then this value will be 0.
* @param island - island
* @param rank {@link RanksManager.MEMBER_RANK}, {@link RanksManager.COOP_RANK}, or {@link RanksManager.TRUSTED_RANK}
* @return max number of members. If negative, then this means unlimited.
* @since 1.16.0
*/
public int getMaxMembers(@NonNull Island island, int rank) {
if (island.getOwner() == null) {
// No owner, no rank settings
island.setMaxMembers(null);
this.save(island);
return 0;
}
// Island max is either the world default or specified amount for this island
int worldDefault = plugin.getIWM().getMaxTeamSize(island.getWorld());
String perm = "team.maxsize";
if (rank == RanksManager.COOP_RANK) {
worldDefault = plugin.getIWM().getMaxCoopSize(island.getWorld());
perm = "coop.maxsize";
} else if (rank == RanksManager.TRUSTED_RANK) {
worldDefault = plugin.getIWM().getMaxTrustSize(island.getWorld());
perm = "trust.maxsize";
}

int islandMax = island.getMaxMembers() == null ? worldDefault : island.getMaxMembers(rank);
// Update based on owner permissions if online
if (Bukkit.getPlayer(island.getOwner()) != null) {
User owner = User.getInstance(island.getOwner());
islandMax = owner.getPermissionValue(plugin.getIWM().getPermissionPrefix(island.getWorld())
+ perm, islandMax);
}
island.setMaxMembers(rank, islandMax == worldDefault ? null : islandMax);
this.save(island);
return islandMax;
}

/**
* Sets the island max member size.
* @param island - island
* @param rank {@link RanksManager.MEMBER_RANK}, {@link RanksManager.COOP_RANK}, or {@link RanksManager.TRUSTED_RANK}
* @param maxMembers - max number of members. If negative, then this means unlimited. Null means the world
* default will be used.
* @since 1.16.0
*/
public void setMaxMembers(@NonNull Island island, int rank, Integer maxMembers) {
island.setMaxMembers(rank, maxMembers);
}

/**
* Returns the island at the location or Optional empty if there is none.
* This includes only the protected area. Use {@link #getIslandAt(Location)}
Expand Down
Loading