Skip to content

Commit

Permalink
Protect invisible player from town add tab completes. (#5873)
Browse files Browse the repository at this point in the history
* Protect invisible player from town add tab completes.

Closes #5872.

* Fix build.

* Actually fix build.

* Catch similar issue with /ta checkperm, fix up method in ResidentUtil
that was doing something...
  • Loading branch information
LlmDl authored Apr 25, 2022
1 parent e702432 commit f74db87
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 20 deletions.
19 changes: 19 additions & 0 deletions src/com/palmergames/bukkit/towny/command/BaseCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;

public class BaseCommand implements TabCompleter{

Expand Down Expand Up @@ -207,6 +208,24 @@ public static List<String> getResidentsWithoutTownStartingWith(String str) {
else
return Collections.emptyList();
}

/**
* Returns a list of residents which are online and have no town, and can be seen by the sender.
*
* @param arg the string to check if the resident's name starts with.
* @return the residents names or an empty list.
*/
public List<String> getVisibleResidentsForPlayerWithoutTownsStartingWith(String arg, CommandSender sender) {
if (!(sender instanceof Player player))
return getResidentsWithoutTownStartingWith(arg);
List<String> residents = getOnlinePlayersWithoutTown().stream()
.filter(res -> player.canSee(res.getPlayer()))
.map(res -> res.getName())
.collect(Collectors.toCollection(ArrayList::new));
return !residents.isEmpty()
? NameUtil.filterByStart(residents, arg)
: Collections.emptyList();
}

/**
* Parses the given string into a boolean choice.
Expand Down
4 changes: 2 additions & 2 deletions src/com/palmergames/bukkit/towny/command/TownCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ public List<String> onTabComplete(CommandSender sender, Command command, String
break;
case "add":
if (args.length == 2)
return getResidentsWithoutTownStartingWith(args[1]);
return getVisibleResidentsForPlayerWithoutTownsStartingWith(args[1], sender);
break;
case "kick":
if (args.length == 2)
Expand Down Expand Up @@ -419,7 +419,7 @@ public List<String> onTabComplete(CommandSender sender, Command command, String
.collect(Collectors.toList());
} catch (TownyException ignore) {}
} else {
return getResidentsWithoutTownStartingWith(args[1]);
return getVisibleResidentsForPlayerWithoutTownsStartingWith(args[1], sender);
}
}
case 3:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ else if (args.length > 2 && TownyCommandAddonAPI.hasCommand(CommandType.TOWNYADM
break;
case "checkperm":
if (args.length == 2)
return NameUtil.filterByStart(BukkitTools.getOnlinePlayers()
return NameUtil.filterByStart(BukkitTools.getVisibleOnlinePlayers(sender)
.stream()
.map(Player::getName)
.collect(Collectors.toList()), args[1]);
Expand Down
20 changes: 3 additions & 17 deletions src/com/palmergames/bukkit/towny/utils/ResidentUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,9 @@ public class ResidentUtil {
* @return - List of residents that can actually be seen.
*/
public static List<Resident> getOnlineResidentsViewable(Player viewer, ResidentList residentList) {

List<Resident> onlineResidents = new ArrayList<>();
for (Player player : BukkitTools.getOnlinePlayers()) {
if (player != null) {
/*
* Loop town/nation resident list
*/
for (Resident resident : residentList.getResidents()) {
if (resident.getName().equalsIgnoreCase(player.getName()))
if ((viewer == null) || (viewer.canSee(BukkitTools.getPlayerExact(resident.getName())))) {
onlineResidents.add(resident);
}
}
}
}

return onlineResidents;
return residentList.getResidents().stream()
.filter(res -> viewer != null ? res.isOnline() && viewer.canSee(res.getPlayer()) : res.isOnline())
.collect(Collectors.toList());
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/com/palmergames/bukkit/util/BukkitTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.bukkit.OfflinePlayer;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager;
Expand Down Expand Up @@ -95,6 +96,15 @@ public static Player getPlayer(UUID playerUUID) {
return server.getPlayer(playerUUID);
}

public static Collection<? extends Player> getVisibleOnlinePlayers(CommandSender sender) {
if (!(sender instanceof Player player))
return Bukkit.getOnlinePlayers();

return Bukkit.getOnlinePlayers().stream()
.filter(p -> player.canSee(p))
.collect(Collectors.toCollection(ArrayList::new));
}

/**
* Tests if this player is online.
*
Expand Down

0 comments on commit f74db87

Please sign in to comment.