Skip to content

Commit

Permalink
Added the ability to use /is accept without any parameters (#1366)
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerBenGera committed Sep 24, 2022
1 parent 6a3326b commit 91f2cca
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 18 deletions.
Expand Up @@ -16,6 +16,7 @@
import org.bukkit.scheduler.BukkitTask;

import javax.annotation.Nullable;
import java.util.List;
import java.util.Locale;
import java.util.UUID;
import java.util.function.Consumer;
Expand Down Expand Up @@ -268,6 +269,31 @@ public interface SuperiorPlayer extends IMissionsHolder, IPersistentDataHolder,
*/
boolean hasIsland();

/**
* Add an invitation to an island for the player.
* Do not call use this method directly unless you know what you're doing.
* Instead, use {@link Island#inviteMember(SuperiorPlayer)}
*
* @param island The island that invited the player.
*/
void addInvite(Island island);

/**
* Remove an invitation from an island for the player.
* Do not call use this method directly unless you know what you're doing.
* Instead, use {@link Island#revokeInvite(SuperiorPlayer)} (SuperiorPlayer)}
*
* @param island The island to remove the invitation from.
*/
void removeInvite(Island island);

/**
* Get all pending invites of the player.
*
* @return Pending invites, in the same order they were sent.
*/
List<Island> getInvites();

/**
* Get the role of the player.
*/
Expand Down
Expand Up @@ -3,14 +3,13 @@
import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin;
import com.bgsoftware.superiorskyblock.api.island.Island;
import com.bgsoftware.superiorskyblock.api.wrappers.SuperiorPlayer;
import com.bgsoftware.superiorskyblock.core.messages.Message;
import com.bgsoftware.superiorskyblock.commands.CommandTabCompletes;
import com.bgsoftware.superiorskyblock.commands.ISuperiorCommand;
import com.bgsoftware.superiorskyblock.island.role.SPlayerRole;
import com.bgsoftware.superiorskyblock.core.messages.Message;
import com.bgsoftware.superiorskyblock.island.IslandUtils;
import com.bgsoftware.superiorskyblock.island.role.SPlayerRole;
import org.bukkit.command.CommandSender;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Expand All @@ -29,9 +28,9 @@ public String getPermission() {

@Override
public String getUsage(java.util.Locale locale) {
return "accept <" +
return "accept [" +
Message.COMMAND_ARGUMENT_PLAYER_NAME.getMessage(locale) + "/" +
Message.COMMAND_ARGUMENT_ISLAND_NAME.getMessage(locale) + ">";
Message.COMMAND_ARGUMENT_ISLAND_NAME.getMessage(locale) + "]";
}

@Override
Expand All @@ -41,7 +40,7 @@ public String getDescription(java.util.Locale locale) {

@Override
public int getMinArgs() {
return 2;
return 1;
}

@Override
Expand All @@ -57,19 +56,22 @@ public boolean canBeExecutedByConsole() {
@Override
public void execute(SuperiorSkyblockPlugin plugin, CommandSender sender, String[] args) {
SuperiorPlayer superiorPlayer = plugin.getPlayers().getSuperiorPlayer(sender);
SuperiorPlayer targetPlayer = plugin.getPlayers().getSuperiorPlayer(args[1]);

SuperiorPlayer targetPlayer;
Island island;

if (targetPlayer == null) {
if ((island = plugin.getGrid().getIsland(args[1])) == null || !island.isInvited(superiorPlayer)) {
Message.NO_ISLAND_INVITE.send(superiorPlayer);
return;
}
if (args.length == 1) {
List<Island> playerPendingInvites = superiorPlayer.getInvites();
island = playerPendingInvites.isEmpty() ? null : playerPendingInvites.get(0);
targetPlayer = null;
} else {
if ((island = targetPlayer.getIsland()) == null || !island.isInvited(superiorPlayer)) {
Message.NO_ISLAND_INVITE.send(superiorPlayer);
return;
}
targetPlayer = plugin.getPlayers().getSuperiorPlayer(args[1]);
island = targetPlayer == null ? plugin.getGrid().getIsland(args[1]) : targetPlayer.getIsland();
}

if (island == null || !island.isInvited(superiorPlayer)) {
Message.NO_ISLAND_INVITE.send(superiorPlayer);
return;
}

if (superiorPlayer.getIsland() != null) {
Expand Down
Expand Up @@ -429,8 +429,8 @@ public List<Pair<SuperiorPlayer, Long>> getUniqueVisitorsWithTimes() {
public void inviteMember(SuperiorPlayer superiorPlayer) {
Preconditions.checkNotNull(superiorPlayer, "superiorPlayer parameter cannot be null.");
PluginDebugger.debug("Action: Invite, Island: " + owner.getName() + ", Target: " + superiorPlayer.getName());

invitedPlayers.add(superiorPlayer);
superiorPlayer.addInvite(this);
//Revoke the invite after 5 minutes
BukkitExecutor.sync(() -> revokeInvite(superiorPlayer), 6000L);
}
Expand All @@ -439,8 +439,8 @@ public void inviteMember(SuperiorPlayer superiorPlayer) {
public void revokeInvite(SuperiorPlayer superiorPlayer) {
Preconditions.checkNotNull(superiorPlayer, "superiorPlayer parameter cannot be null.");
PluginDebugger.debug("Action: Invite Revoke, Island: " + owner.getName() + ", Target: " + superiorPlayer.getName());

invitedPlayers.remove(superiorPlayer);
superiorPlayer.removeInvite(this);
}

@Override
Expand Down Expand Up @@ -1271,6 +1271,8 @@ public void disbandIsland() {
}).forEach(superiorPlayer::resetMission);
});

invitedPlayers.forEach(invitedPlayer -> invitedPlayer.removeInvite(this));

if (BuiltinModules.BANK.disbandRefund > 0)
plugin.getProviders().depositMoney(getOwner(), islandBank.getBalance()
.multiply(BigDecimal.valueOf(BuiltinModules.BANK.disbandRefund)));
Expand Down
Expand Up @@ -38,6 +38,7 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand All @@ -56,6 +57,8 @@ public class SSuperiorPlayer implements SuperiorPlayer {
private PersistentDataContainer persistentDataContainer; // Lazy loading

private final Map<Mission<?>, Integer> completedMissions = new ConcurrentHashMap<>();
private final List<UUID> pendingInvites = new LinkedList<>();

private final UUID uuid;

private Island playerIsland = null;
Expand Down Expand Up @@ -433,6 +436,22 @@ public boolean hasIsland() {
return getIsland() != null;
}

@Override
public void addInvite(Island island) {
this.pendingInvites.add(island.getUniqueId());
}

@Override
public void removeInvite(Island island) {
this.pendingInvites.remove(island.getUniqueId());
}

@Override
public List<Island> getInvites() {
return new SequentialListBuilder<UUID>()
.map(this.pendingInvites, uuid -> plugin.getGrid().getIslandByUUID(uuid));
}

@Override
public PlayerRole getPlayerRole() {
if (playerRole == null)
Expand Down
Expand Up @@ -216,6 +216,21 @@ public boolean hasIsland() {
return false;
}

@Override
public void addInvite(Island island) {
// Do nothing.
}

@Override
public void removeInvite(Island island) {
// Do nothing.
}

@Override
public List<Island> getInvites() {
return Collections.emptyList();
}

@Override
public PlayerRole getPlayerRole() {
return SPlayerRole.guestRole();
Expand Down

0 comments on commit 91f2cca

Please sign in to comment.