Correctly assign invites for islands.

This commit is contained in:
tastybento 2023-09-04 15:35:57 -07:00
parent b032965c9b
commit 2012726959
8 changed files with 36 additions and 28 deletions

View File

@ -3,6 +3,8 @@ package world.bentobox.bentobox.api.commands.island.team;
import java.util.Objects;
import java.util.UUID;
import world.bentobox.bentobox.database.objects.Island;
/**
* Represents an invite
* @author tastybento
@ -23,16 +25,19 @@ public class Invite {
private final Type type;
private final UUID inviter;
private final UUID invitee;
private final Island island;
/**
* @param type - invitation type, e.g., coop, team, trust
* @param inviter - UUID of inviter
* @param invitee - UUID of invitee
* @param island - the island this invite is for
*/
public Invite(Type type, UUID inviter, UUID invitee) {
public Invite(Type type, UUID inviter, UUID invitee, Island island) {
this.type = type;
this.inviter = inviter;
this.invitee = invitee;
this.island = island;
}
/**
@ -56,6 +61,13 @@ public class Invite {
return invitee;
}
/**
* @return the island
*/
public Island getIsland() {
return island;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/

View File

@ -58,24 +58,20 @@ public class IslandTeamCommand extends CompositeCommand {
@Override
public boolean execute(User user, String label, List<String> args) {
// Player issuing the command must have an island
UUID ownerUUID = getIslands().getOwner(getWorld(), user.getUniqueId());
if (ownerUUID == null) {
Island island = getIslands().getPrimaryIsland(getWorld(), user.getUniqueId());
if (island == null) {
user.sendMessage("general.errors.no-island");
return false;
}
UUID playerUUID = user.getUniqueId();
// Fire event so add-ons can run commands, etc.
if (fireEvent(user)) {
if (fireEvent(user, island)) {
// Cancelled
return false;
}
Island island = getIslands().getIsland(getWorld(), playerUUID);
if (island == null) {
return false;
}
Set<UUID> teamMembers = getMembers(getWorld(), user);
if (ownerUUID.equals(playerUUID)) {
if (playerUUID.equals(island.getOwner())) {
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()));
@ -169,10 +165,9 @@ public class IslandTeamCommand extends CompositeCommand {
}
private boolean fireEvent(User user) {
private boolean fireEvent(User user, Island island) {
IslandBaseEvent e = TeamEvent.builder()
.island(getIslands()
.getIsland(getWorld(), user.getUniqueId()))
.island(island)
.reason(TeamEvent.Reason.INFO)
.involvedPlayer(user.getUniqueId())
.build();
@ -187,8 +182,8 @@ public class IslandTeamCommand extends CompositeCommand {
* @param invitee - uuid of invitee
* @since 1.8.0
*/
public void addInvite(Invite.Type type, @NonNull UUID inviter, @NonNull UUID invitee) {
inviteMap.put(invitee, new Invite(type, inviter, invitee));
public void addInvite(Invite.Type type, @NonNull UUID inviter, @NonNull UUID invitee, @NonNull Island island) {
inviteMap.put(invitee, new Invite(type, inviter, invitee, island));
}
/**

View File

@ -93,7 +93,7 @@ public class IslandTeamCoopCommand extends CompositeCommand {
if (getPlugin().getSettings().isInviteConfirmation()) {
// 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.COOP, user.getUniqueId(), target.getUniqueId());
itc.addInvite(Invite.Type.COOP, user.getUniqueId(), target.getUniqueId(), island);
user.sendMessage("commands.island.team.invite.invitation-sent", TextVariables.NAME, target.getName());
// Send message to online player
target.sendMessage("commands.island.team.coop.name-has-invited-you", TextVariables.NAME, user.getName());

View File

@ -97,7 +97,7 @@ public class IslandTeamInviteAcceptCommand extends ConfirmableCommand {
// Remove the invite
itc.removeInvite(playerUUID);
User inviter = User.getInstance(invite.getInviter());
Island island = getIslands().getIsland(getWorld(), inviter);
Island island = invite.getIsland();
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");
@ -124,7 +124,7 @@ public class IslandTeamInviteAcceptCommand extends ConfirmableCommand {
// Remove the invite
itc.removeInvite(playerUUID);
User inviter = User.getInstance(invite.getInviter());
Island island = getIslands().getIsland(getWorld(), inviter);
Island island = invite.getIsland();
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");
@ -153,7 +153,7 @@ public class IslandTeamInviteAcceptCommand extends ConfirmableCommand {
// Get the player's island - may be null if the player has no island
Set<Island> islands = getIslands().getIslands(getWorld(), playerUUID);
// Get the team's island
Island teamIsland = getIslands().getIsland(getWorld(), prospectiveOwnerUUID);
Island teamIsland = invite.getIsland();
if (teamIsland == null) {
user.sendMessage(INVALID_INVITE);
return;
@ -193,7 +193,7 @@ public class IslandTeamInviteAcceptCommand extends ConfirmableCommand {
getIslands().save(teamIsland);
// Fire event
TeamEvent.builder()
.island(getIslands().getIsland(getWorld(), prospectiveOwnerUUID))
.island(teamIsland)
.reason(TeamEvent.Reason.JOINED)
.involvedPlayer(playerUUID)
.build();

View File

@ -166,9 +166,10 @@ public class IslandTeamInviteCommand extends CompositeCommand {
itc.removeInvite(invitedPlayer.getUniqueId());
user.sendMessage("commands.island.team.invite.removing-invite");
}
Island island = getIslands().getIsland(getWorld(), user.getUniqueId());
// Fire event so add-ons can run commands, etc.
IslandBaseEvent e = TeamEvent.builder()
.island(getIslands().getIsland(getWorld(), user.getUniqueId()))
.island(island)
.reason(TeamEvent.Reason.INVITE)
.involvedPlayer(invitedPlayer.getUniqueId())
.build();
@ -177,7 +178,7 @@ public class IslandTeamInviteCommand extends CompositeCommand {
}
// 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());
itc.addInvite(Invite.Type.TEAM, user.getUniqueId(), invitedPlayer.getUniqueId(), island);
user.sendMessage("commands.island.team.invite.invitation-sent", TextVariables.NAME, invitedPlayer.getName(), TextVariables.DISPLAY_NAME, invitedPlayer.getDisplayName());
// Send message to online player
invitedPlayer.sendMessage("commands.island.team.invite.name-has-invited-you", TextVariables.NAME, user.getName(), TextVariables.DISPLAY_NAME, user.getDisplayName());

View File

@ -95,7 +95,7 @@ public class IslandTeamTrustCommand extends CompositeCommand {
if (getPlugin().getSettings().isInviteConfirmation()) {
// 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(Type.TRUST, user.getUniqueId(), target.getUniqueId());
itc.addInvite(Type.TRUST, user.getUniqueId(), target.getUniqueId(), island);
user.sendMessage("commands.island.team.invite.invitation-sent", TextVariables.NAME, target.getName(), TextVariables.DISPLAY_NAME, target.getDisplayName());
// Send message to online player
target.sendMessage("commands.island.team.trust.name-has-invited-you", TextVariables.NAME, user.getName(), TextVariables.DISPLAY_NAME, user.getDisplayName());

View File

@ -190,7 +190,7 @@ public class IslandTeamCommandTest {
*/
@Test
public void testAddInvite() {
tc.addInvite(Invite.Type.TEAM, uuid, invitee);
tc.addInvite(Invite.Type.TEAM, uuid, invitee, island);
assertTrue(tc.isInvited(invitee));
}
@ -207,7 +207,7 @@ public class IslandTeamCommandTest {
*/
@Test
public void testGetInviter() {
tc.addInvite(Invite.Type.TEAM, uuid, invitee);
tc.addInvite(Invite.Type.TEAM, uuid, invitee, island);
assertEquals(uuid, tc.getInviter(invitee));
}
@ -225,7 +225,7 @@ public class IslandTeamCommandTest {
@Test
public void testGetInvite() {
assertNull(tc.getInvite(invitee));
tc.addInvite(Invite.Type.TEAM, uuid, invitee);
tc.addInvite(Invite.Type.TEAM, uuid, invitee, island);
@Nullable
Invite invite = tc.getInvite(invitee);
assertEquals(invitee, invite.getInvitee());
@ -239,7 +239,7 @@ public class IslandTeamCommandTest {
@Test
public void testRemoveInvite() {
assertNull(tc.getInvite(invitee));
tc.addInvite(Invite.Type.TEAM, uuid, invitee);
tc.addInvite(Invite.Type.TEAM, uuid, invitee, island);
tc.removeInvite(invitee);
assertNull(tc.getInvite(invitee));
}

View File

@ -299,7 +299,7 @@ public class IslandTeamInviteCommandTest {
assertTrue(itl.execute(user, itl.getLabel(), List.of("target")));
verify(pim).callEvent(any(IslandBaseEvent.class));
verify(user, never()).sendMessage(eq("commands.island.team.invite.removing-invite"));
verify(ic).addInvite(eq(Invite.Type.TEAM), eq(uuid), eq(notUUID));
verify(ic).addInvite(Invite.Type.TEAM, uuid, notUUID, null);
verify(user).sendMessage("commands.island.team.invite.invitation-sent", TextVariables.NAME, "target", TextVariables.DISPLAY_NAME, "&Ctarget");
verify(target).sendMessage("commands.island.team.invite.name-has-invited-you", TextVariables.NAME, "tastybento", TextVariables.DISPLAY_NAME, "&Ctastbento");
verify(target).sendMessage("commands.island.team.invite.to-accept-or-reject", TextVariables.LABEL, "island");
@ -316,7 +316,7 @@ public class IslandTeamInviteCommandTest {
assertTrue(itl.execute(user, itl.getLabel(), List.of("target")));
verify(pim).callEvent(any(IslandBaseEvent.class));
verify(user, never()).sendMessage("commands.island.team.invite.removing-invite");
verify(ic).addInvite(Invite.Type.TEAM, uuid, notUUID);
verify(ic).addInvite(Invite.Type.TEAM, uuid, notUUID, null);
verify(user).sendMessage("commands.island.team.invite.invitation-sent", TextVariables.NAME, "target", TextVariables.DISPLAY_NAME, "&Ctarget");
verify(target).sendMessage("commands.island.team.invite.name-has-invited-you", TextVariables.NAME, "tastybento", TextVariables.DISPLAY_NAME, "&Ctastbento");
verify(target).sendMessage("commands.island.team.invite.to-accept-or-reject", TextVariables.LABEL, "island");