Prevent coop and trust commands from being registered
if those ranks don't exist.
This commit is contained in:
parent
44be66bdfc
commit
2655a98aad
|
@ -24,206 +24,210 @@ import world.bentobox.bentobox.util.Util;
|
|||
|
||||
public class IslandTeamCommand extends CompositeCommand {
|
||||
|
||||
/**
|
||||
* Invited list. Key is the invited party, value is the invite.
|
||||
* @since 1.8.0
|
||||
*/
|
||||
private final Map<UUID, Invite> inviteMap;
|
||||
/**
|
||||
* Invited list. Key is the invited party, value is the invite.
|
||||
* @since 1.8.0
|
||||
*/
|
||||
private final Map<UUID, Invite> inviteMap;
|
||||
|
||||
public IslandTeamCommand(CompositeCommand parent) {
|
||||
super(parent, "team");
|
||||
inviteMap = new HashMap<>();
|
||||
}
|
||||
public IslandTeamCommand(CompositeCommand parent) {
|
||||
super(parent, "team");
|
||||
inviteMap = new HashMap<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setup() {
|
||||
setPermission("island.team");
|
||||
setOnlyPlayer(true);
|
||||
setDescription("commands.island.team.description");
|
||||
// Register commands
|
||||
new IslandTeamInviteCommand(this);
|
||||
new IslandTeamLeaveCommand(this);
|
||||
new IslandTeamSetownerCommand(this);
|
||||
new IslandTeamKickCommand(this);
|
||||
new IslandTeamInviteAcceptCommand(this);
|
||||
new IslandTeamInviteRejectCommand(this);
|
||||
new IslandTeamCoopCommand(this);
|
||||
new IslandTeamUncoopCommand(this);
|
||||
new IslandTeamTrustCommand(this);
|
||||
new IslandTeamUntrustCommand(this);
|
||||
new IslandTeamPromoteCommand(this, "promote");
|
||||
new IslandTeamPromoteCommand(this, "demote");
|
||||
}
|
||||
@Override
|
||||
public void setup() {
|
||||
setPermission("island.team");
|
||||
setOnlyPlayer(true);
|
||||
setDescription("commands.island.team.description");
|
||||
// Register commands
|
||||
new IslandTeamInviteCommand(this);
|
||||
new IslandTeamLeaveCommand(this);
|
||||
new IslandTeamSetownerCommand(this);
|
||||
new IslandTeamKickCommand(this);
|
||||
new IslandTeamInviteAcceptCommand(this);
|
||||
new IslandTeamInviteRejectCommand(this);
|
||||
if (getPlugin().getRanksManager().rankExists(RanksManager.COOP_RANK_REF)) {
|
||||
new IslandTeamCoopCommand(this);
|
||||
new IslandTeamUncoopCommand(this);
|
||||
}
|
||||
if (getPlugin().getRanksManager().rankExists(RanksManager.TRUSTED_RANK_REF)) {
|
||||
new IslandTeamTrustCommand(this);
|
||||
new IslandTeamUntrustCommand(this);
|
||||
}
|
||||
new IslandTeamPromoteCommand(this, "promote");
|
||||
new IslandTeamPromoteCommand(this, "demote");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute(User user, String label, List<String> args) {
|
||||
// Player issuing the command must have an island
|
||||
Island island = getIslands().getPrimaryIsland(getWorld(), user.getUniqueId());
|
||||
if (island == null) {
|
||||
user.sendMessage("general.errors.no-island");
|
||||
return false;
|
||||
}
|
||||
@Override
|
||||
public boolean execute(User user, String label, List<String> args) {
|
||||
// Player issuing the command must have an island
|
||||
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, island)) {
|
||||
// Cancelled
|
||||
return false;
|
||||
}
|
||||
Set<UUID> teamMembers = getMembers(getWorld(), user);
|
||||
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()));
|
||||
} else {
|
||||
user.sendMessage("commands.island.team.invite.errors.island-is-full");
|
||||
}
|
||||
}
|
||||
// Show members of island
|
||||
showMembers(island, user);
|
||||
return true;
|
||||
}
|
||||
UUID playerUUID = user.getUniqueId();
|
||||
// Fire event so add-ons can run commands, etc.
|
||||
if (fireEvent(user, island)) {
|
||||
// Cancelled
|
||||
return false;
|
||||
}
|
||||
Set<UUID> teamMembers = getMembers(getWorld(), user);
|
||||
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()));
|
||||
} else {
|
||||
user.sendMessage("commands.island.team.invite.errors.island-is-full");
|
||||
}
|
||||
}
|
||||
// Show members of island
|
||||
showMembers(island, user);
|
||||
return true;
|
||||
}
|
||||
|
||||
private void showMembers(Island island, User user) {
|
||||
// Gather online members
|
||||
long count = island
|
||||
.getMemberSet(RanksManager.MEMBER_RANK)
|
||||
.stream()
|
||||
.filter(uuid -> Util.getOnlinePlayerList(user).contains(Bukkit.getOfflinePlayer(uuid).getName()))
|
||||
.count();
|
||||
private void showMembers(Island island, User user) {
|
||||
// Gather online members
|
||||
long count = island
|
||||
.getMemberSet(RanksManager.MEMBER_RANK)
|
||||
.stream()
|
||||
.filter(uuid -> Util.getOnlinePlayerList(user).contains(Bukkit.getOfflinePlayer(uuid).getName()))
|
||||
.count();
|
||||
|
||||
// List of ranks that we will loop through
|
||||
Integer[] ranks = new Integer[]{RanksManager.OWNER_RANK, RanksManager.SUB_OWNER_RANK, RanksManager.MEMBER_RANK, RanksManager.TRUSTED_RANK, RanksManager.COOP_RANK};
|
||||
// List of ranks that we will loop through
|
||||
Integer[] ranks = new Integer[]{RanksManager.OWNER_RANK, RanksManager.SUB_OWNER_RANK, RanksManager.MEMBER_RANK, RanksManager.TRUSTED_RANK, RanksManager.COOP_RANK};
|
||||
|
||||
// Show header:
|
||||
user.sendMessage("commands.island.team.info.header",
|
||||
"[max]", String.valueOf(getIslands().getMaxMembers(island, RanksManager.MEMBER_RANK)),
|
||||
"[total]", String.valueOf(island.getMemberSet().size()),
|
||||
"[online]", String.valueOf(count));
|
||||
// Show header:
|
||||
user.sendMessage("commands.island.team.info.header",
|
||||
"[max]", String.valueOf(getIslands().getMaxMembers(island, RanksManager.MEMBER_RANK)),
|
||||
"[total]", String.valueOf(island.getMemberSet().size()),
|
||||
"[online]", String.valueOf(count));
|
||||
|
||||
// We now need to get all online "members" of the island - incl. Trusted and coop
|
||||
List<UUID> onlineMembers = island.getMemberSet(RanksManager.COOP_RANK).stream()
|
||||
.filter(uuid -> Util.getOnlinePlayerList(user).contains(Bukkit.getOfflinePlayer(uuid).getName())).toList();
|
||||
// We now need to get all online "members" of the island - incl. Trusted and coop
|
||||
List<UUID> onlineMembers = island.getMemberSet(RanksManager.COOP_RANK).stream()
|
||||
.filter(uuid -> Util.getOnlinePlayerList(user).contains(Bukkit.getOfflinePlayer(uuid).getName())).toList();
|
||||
|
||||
for (int rank : ranks) {
|
||||
Set<UUID> players = island.getMemberSet(rank, false);
|
||||
if (!players.isEmpty()) {
|
||||
if (rank == RanksManager.OWNER_RANK) {
|
||||
// Slightly special handling for the owner rank
|
||||
user.sendMessage("commands.island.team.info.rank-layout.owner",
|
||||
TextVariables.RANK, user.getTranslation(RanksManager.OWNER_RANK_REF));
|
||||
} else {
|
||||
user.sendMessage("commands.island.team.info.rank-layout.generic",
|
||||
TextVariables.RANK, user.getTranslation(getPlugin().getRanksManager().getRank(rank)),
|
||||
TextVariables.NUMBER, String.valueOf(island.getMemberSet(rank, false).size()));
|
||||
}
|
||||
displayOnOffline(user, rank, island, onlineMembers);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int rank : ranks) {
|
||||
Set<UUID> players = island.getMemberSet(rank, false);
|
||||
if (!players.isEmpty()) {
|
||||
if (rank == RanksManager.OWNER_RANK) {
|
||||
// Slightly special handling for the owner rank
|
||||
user.sendMessage("commands.island.team.info.rank-layout.owner",
|
||||
TextVariables.RANK, user.getTranslation(RanksManager.OWNER_RANK_REF));
|
||||
} else {
|
||||
user.sendMessage("commands.island.team.info.rank-layout.generic",
|
||||
TextVariables.RANK, user.getTranslation(getPlugin().getRanksManager().getRank(rank)),
|
||||
TextVariables.NUMBER, String.valueOf(island.getMemberSet(rank, false).size()));
|
||||
}
|
||||
displayOnOffline(user, rank, island, onlineMembers);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void displayOnOffline(User user, int rank, Island island, List<UUID> onlineMembers) {
|
||||
for (UUID member : island.getMemberSet(rank, false)) {
|
||||
OfflinePlayer offlineMember = Bukkit.getOfflinePlayer(member);
|
||||
if (onlineMembers.contains(member)) {
|
||||
// the player is online
|
||||
user.sendMessage("commands.island.team.info.member-layout.online",
|
||||
TextVariables.NAME, offlineMember.getName());
|
||||
} else {
|
||||
// A bit of handling for the last joined date
|
||||
Instant lastJoined = Instant.ofEpochMilli(offlineMember.getLastPlayed());
|
||||
Instant now = Instant.now();
|
||||
private void displayOnOffline(User user, int rank, Island island, List<UUID> onlineMembers) {
|
||||
for (UUID member : island.getMemberSet(rank, false)) {
|
||||
OfflinePlayer offlineMember = Bukkit.getOfflinePlayer(member);
|
||||
if (onlineMembers.contains(member)) {
|
||||
// the player is online
|
||||
user.sendMessage("commands.island.team.info.member-layout.online",
|
||||
TextVariables.NAME, offlineMember.getName());
|
||||
} else {
|
||||
// A bit of handling for the last joined date
|
||||
Instant lastJoined = Instant.ofEpochMilli(offlineMember.getLastPlayed());
|
||||
Instant now = Instant.now();
|
||||
|
||||
Duration duration = Duration.between(lastJoined, now);
|
||||
String lastSeen;
|
||||
final String reference = "commands.island.team.info.last-seen.layout";
|
||||
if (duration.toMinutes() < 60L) {
|
||||
lastSeen = user.getTranslation(reference,
|
||||
TextVariables.NUMBER, String.valueOf(duration.toMinutes()),
|
||||
TextVariables.UNIT, user.getTranslation("commands.island.team.info.last-seen.minutes"));
|
||||
} else if (duration.toHours() < 24L) {
|
||||
lastSeen = user.getTranslation(reference,
|
||||
TextVariables.NUMBER, String.valueOf(duration.toHours()),
|
||||
TextVariables.UNIT, user.getTranslation("commands.island.team.info.last-seen.hours"));
|
||||
} else {
|
||||
lastSeen = user.getTranslation(reference,
|
||||
TextVariables.NUMBER, String.valueOf(duration.toDays()),
|
||||
TextVariables.UNIT, user.getTranslation("commands.island.team.info.last-seen.days"));
|
||||
}
|
||||
Duration duration = Duration.between(lastJoined, now);
|
||||
String lastSeen;
|
||||
final String reference = "commands.island.team.info.last-seen.layout";
|
||||
if (duration.toMinutes() < 60L) {
|
||||
lastSeen = user.getTranslation(reference,
|
||||
TextVariables.NUMBER, String.valueOf(duration.toMinutes()),
|
||||
TextVariables.UNIT, user.getTranslation("commands.island.team.info.last-seen.minutes"));
|
||||
} else if (duration.toHours() < 24L) {
|
||||
lastSeen = user.getTranslation(reference,
|
||||
TextVariables.NUMBER, String.valueOf(duration.toHours()),
|
||||
TextVariables.UNIT, user.getTranslation("commands.island.team.info.last-seen.hours"));
|
||||
} else {
|
||||
lastSeen = user.getTranslation(reference,
|
||||
TextVariables.NUMBER, String.valueOf(duration.toDays()),
|
||||
TextVariables.UNIT, user.getTranslation("commands.island.team.info.last-seen.days"));
|
||||
}
|
||||
|
||||
if(island.getMemberSet(RanksManager.MEMBER_RANK, true).contains(member)) {
|
||||
user.sendMessage("commands.island.team.info.member-layout.offline",
|
||||
TextVariables.NAME, offlineMember.getName(),
|
||||
"[last_seen]", lastSeen);
|
||||
} else {
|
||||
// This will prevent anyone that is trusted or below to not have a last-seen status
|
||||
user.sendMessage("commands.island.team.info.member-layout.offline-not-last-seen",
|
||||
TextVariables.NAME, offlineMember.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
if(island.getMemberSet(RanksManager.MEMBER_RANK, true).contains(member)) {
|
||||
user.sendMessage("commands.island.team.info.member-layout.offline",
|
||||
TextVariables.NAME, offlineMember.getName(),
|
||||
"[last_seen]", lastSeen);
|
||||
} else {
|
||||
// This will prevent anyone that is trusted or below to not have a last-seen status
|
||||
user.sendMessage("commands.island.team.info.member-layout.offline-not-last-seen",
|
||||
TextVariables.NAME, offlineMember.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private boolean fireEvent(User user, Island island) {
|
||||
IslandBaseEvent e = TeamEvent.builder()
|
||||
.island(island)
|
||||
.reason(TeamEvent.Reason.INFO)
|
||||
.involvedPlayer(user.getUniqueId())
|
||||
.build();
|
||||
return e.getNewEvent().map(IslandBaseEvent::isCancelled)
|
||||
.orElse(e.isCancelled());
|
||||
}
|
||||
private boolean fireEvent(User user, Island island) {
|
||||
IslandBaseEvent e = TeamEvent.builder()
|
||||
.island(island)
|
||||
.reason(TeamEvent.Reason.INFO)
|
||||
.involvedPlayer(user.getUniqueId())
|
||||
.build();
|
||||
return e.getNewEvent().map(IslandBaseEvent::isCancelled)
|
||||
.orElse(e.isCancelled());
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an invite
|
||||
* @param type - type of invite
|
||||
* @param inviter - uuid of inviter
|
||||
* @param invitee - uuid of invitee
|
||||
* @since 1.8.0
|
||||
*/
|
||||
public void addInvite(Invite.Type type, @NonNull UUID inviter, @NonNull UUID invitee, @NonNull Island island) {
|
||||
inviteMap.put(invitee, new Invite(type, inviter, invitee, island));
|
||||
}
|
||||
/**
|
||||
* Add an invite
|
||||
* @param type - type of invite
|
||||
* @param inviter - uuid of inviter
|
||||
* @param invitee - uuid of invitee
|
||||
* @since 1.8.0
|
||||
*/
|
||||
public void addInvite(Invite.Type type, @NonNull UUID inviter, @NonNull UUID invitee, @NonNull Island island) {
|
||||
inviteMap.put(invitee, new Invite(type, inviter, invitee, island));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a player has been invited
|
||||
* @param invitee - UUID of invitee to check
|
||||
* @return true if invited, false if not
|
||||
* @since 1.8.0
|
||||
*/
|
||||
public boolean isInvited(@NonNull UUID invitee) {
|
||||
return inviteMap.containsKey(invitee);
|
||||
}
|
||||
/**
|
||||
* Check if a player has been invited
|
||||
* @param invitee - UUID of invitee to check
|
||||
* @return true if invited, false if not
|
||||
* @since 1.8.0
|
||||
*/
|
||||
public boolean isInvited(@NonNull UUID invitee) {
|
||||
return inviteMap.containsKey(invitee);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get whoever invited invitee
|
||||
* @param invitee - uuid
|
||||
* @return UUID of inviter, or null if invitee has not been invited
|
||||
* @since 1.8.0
|
||||
*/
|
||||
@Nullable
|
||||
public UUID getInviter(UUID invitee) {
|
||||
return isInvited(invitee) ? inviteMap.get(invitee).getInviter() : null;
|
||||
}
|
||||
/**
|
||||
* Get whoever invited invitee
|
||||
* @param invitee - uuid
|
||||
* @return UUID of inviter, or null if invitee has not been invited
|
||||
* @since 1.8.0
|
||||
*/
|
||||
@Nullable
|
||||
public UUID getInviter(UUID invitee) {
|
||||
return isInvited(invitee) ? inviteMap.get(invitee).getInviter() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the invite for an invitee.
|
||||
* @param invitee - UUID of invitee
|
||||
* @return invite or null if none
|
||||
* @since 1.8.0
|
||||
*/
|
||||
@Nullable
|
||||
public Invite getInvite(UUID invitee) {
|
||||
return inviteMap.get(invitee);
|
||||
}
|
||||
/**
|
||||
* Gets the invite for an invitee.
|
||||
* @param invitee - UUID of invitee
|
||||
* @return invite or null if none
|
||||
* @since 1.8.0
|
||||
*/
|
||||
@Nullable
|
||||
public Invite getInvite(UUID invitee) {
|
||||
return inviteMap.get(invitee);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a pending invite.
|
||||
* @param invitee - UUID of invited user
|
||||
* @since 1.8.0
|
||||
*/
|
||||
public void removeInvite(@NonNull UUID invitee) {
|
||||
inviteMap.remove(invitee);
|
||||
}
|
||||
/**
|
||||
* Removes a pending invite.
|
||||
* @param invitee - UUID of invited user
|
||||
* @since 1.8.0
|
||||
*/
|
||||
public void removeInvite(@NonNull UUID invitee) {
|
||||
inviteMap.remove(invitee);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -65,6 +65,15 @@ public class RanksManager {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a rank exists
|
||||
* @param reference YAML reference to rank, e.g., ranks.trusted
|
||||
* @return true if the rank exists
|
||||
*/
|
||||
public boolean rankExists(String reference) {
|
||||
return ranks.containsKey(reference);
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to add a new rank. Owner, member, visitor and banned ranks cannot be changed.
|
||||
* @param reference - a reference that can be found in a locale file
|
||||
|
|
|
@ -18,7 +18,6 @@ import org.bukkit.Bukkit;
|
|||
import org.bukkit.World;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
@ -26,7 +25,6 @@ import org.mockito.Mock;
|
|||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.powermock.reflect.Whitebox;
|
||||
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
|
||||
|
@ -40,6 +38,7 @@ import world.bentobox.bentobox.managers.CommandsManager;
|
|||
import world.bentobox.bentobox.managers.IslandWorldManager;
|
||||
import world.bentobox.bentobox.managers.IslandsManager;
|
||||
import world.bentobox.bentobox.managers.RanksManager;
|
||||
import world.bentobox.bentobox.managers.RanksManagerBeforeClassTest;
|
||||
|
||||
/**
|
||||
* @author tastybento
|
||||
|
@ -47,7 +46,7 @@ import world.bentobox.bentobox.managers.RanksManager;
|
|||
*/
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest({Bukkit.class, BentoBox.class, User.class })
|
||||
public class IslandTeamCommandTest {
|
||||
public class IslandTeamCommandTest extends RanksManagerBeforeClassTest {
|
||||
|
||||
@Mock
|
||||
private CompositeCommand ic;
|
||||
|
@ -81,9 +80,7 @@ public class IslandTeamCommandTest {
|
|||
*/
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
// Set up plugin
|
||||
BentoBox plugin = mock(BentoBox.class);
|
||||
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
|
||||
super.setUp();
|
||||
|
||||
// Command manager
|
||||
CommandsManager cm = mock(CommandsManager.class);
|
||||
|
@ -125,18 +122,16 @@ public class IslandTeamCommandTest {
|
|||
// IWM
|
||||
when(plugin.getIWM()).thenReturn(iwm);
|
||||
when(iwm.getPermissionPrefix(any())).thenReturn("bskyblock.");
|
||||
|
||||
// RanksManager
|
||||
RanksManager rm = new RanksManager();
|
||||
when(plugin.getRanksManager()).thenReturn(rm);
|
||||
|
||||
|
||||
// Command under test
|
||||
tc = new IslandTeamCommand(ic);
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamCommand#IslandTeamCommand(world.bentobox.bentobox.api.commands.CompositeCommand)}.
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue