Skip to content

Commit

Permalink
fix: check availability before sending dropdown form (#133)
Browse files Browse the repository at this point in the history
* Fix potential NPE when no other players online

* Check availability before sending dropdown form

* Revert & Cleanup import

* Use noPlayerInput & Readability & Pattern matching

* Tpa & Tpo

* Move TextComponent back
  • Loading branch information
HaHaWTH authored Jul 7, 2024
1 parent 57b5059 commit cdff5d9
Show file tree
Hide file tree
Showing 15 changed files with 139 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private void sendDropdownForm(@NotNull String command, @NotNull Collection<Strin
}

@Contract(pure = true)
private @NotNull List<String> getVisiblePlayerNames() {
public @NotNull List<String> getVisiblePlayerNames() {
return Bukkit.getOnlinePlayers().stream()
.filter(player -> player != getPlayer())
.filter(player -> getPlayer().canSee(player))
Expand Down Expand Up @@ -259,14 +259,7 @@ public void sendUnblockForm() {
}

/** Sends the form for /tpcancel. */
public void sendCancelForm() {

// Builds the list of teleport requests that can be cancelled
final var responders =
TeleportRequest.getRequestsByRequester(getPlayer()).stream()
.map(request -> request.requester().getName())
.toList();

public void sendCancelForm(List<String> responders) {
// Sends the dropdown menu form
sendDropdownForm("tpcancel", responders);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ public boolean onCommand(
if (PluginHookManager.get().floodgateEnabled()
&& atPlayer instanceof ATFloodgatePlayer atFloodgatePlayer
&& MainConfig.get().USE_FLOODGATE_FORMS.get()) {
atFloodgatePlayer.sendDeleteHomeForm();
if (!atFloodgatePlayer.getHomes().isEmpty()) {
atFloodgatePlayer.sendDeleteHomeForm();
} else {
CustomMessages.sendMessage(sender, "Error.noHomes");
}
} else CustomMessages.sendMessage(sender, "Error.noHomeInput");

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,72 +100,81 @@ private void getHomes(CommandSender sender, OfflinePlayer target, ImmutableColle
&& atPlayer instanceof ATFloodgatePlayer atFloodgatePlayer
&& MainConfig.get().USE_FLOODGATE_FORMS.get()) {
CoreClass.debug("Sender is being sent a homes form.");
atFloodgatePlayer.sendHomeForm();

if (!atFloodgatePlayer.getHomes().isEmpty()) {
atFloodgatePlayer.sendHomeForm();
} else {
CustomMessages.sendMessage(
sender,
CustomMessages.contextualPath(sender, target, "Error.noHomes"),
Placeholder.unparsed("player", target.getName()) // TODO: DisplayName
);
}
return;
}

final TextComponent body = (TextComponent) Component.join(JoinConfiguration.commas(true),
homes.stream().map(home ->
new Object[] {home,
atPlayer.canAccessHome(home)
|| ExPermission
.hasPermissionOrStar(
sender,
"at.admin.homes")
}) // How the fuck do you associate a value
// like a pair in java?
.map(
pair -> {
final var home = (Home) pair[0];
final var canAccess = (boolean) pair[1];
final var baseComponent =
Component.text(home.getName())
.hoverEvent(
CustomMessages
.locationBasedTooltip(
sender,
home,
"homes"));

CoreClass.debug("Processing home: " + home.getName() + ", " + canAccess);

if (!canAccess) {
if (!MainConfig.get()
.HIDE_HOMES_IF_DENIED
.get())
return Component
.empty(); // TODO: Make sure
// this doesn't cause
// an extra comma.
return baseComponent
.color(NamedTextColor.GRAY)
.decorate(TextDecoration.ITALIC);
}

return baseComponent.clickEvent(
ClickEvent.runCommand(
"/advancedteleport:home "
+ (sender == target
? ""
: target
.getName()
+ " ")
+ home.getName()));
})
.toList());

homes.stream().map(home ->
new Object[] {home,
atPlayer.canAccessHome(home)
|| ExPermission
.hasPermissionOrStar(
sender,
"at.admin.homes")
}) // How the fuck do you associate a value
// like a pair in java?
.map(
pair -> {
final var home = (Home) pair[0];
final var canAccess = (boolean) pair[1];
final var baseComponent =
Component.text(home.getName())
.hoverEvent(
CustomMessages
.locationBasedTooltip(
sender,
home,
"homes"));

CoreClass.debug("Processing home: " + home.getName() + ", " + canAccess);

if (!canAccess) {
if (!MainConfig.get()
.HIDE_HOMES_IF_DENIED
.get())
return Component
.empty(); // TODO: Make sure
// this doesn't cause
// an extra comma.
return baseComponent
.color(NamedTextColor.GRAY)
.decorate(TextDecoration.ITALIC);
}

return baseComponent.clickEvent(
ClickEvent.runCommand(
"/advancedteleport:home "
+ (sender == target
? ""
: target
.getName()
+ " ")
+ home.getName()));
})
.toList());
CoreClass.debug("Message text built.");

if (!body.content().isEmpty() || !body.children().isEmpty()) {
String text = CustomMessages.config.getString("Info.homes") + "<homes>";
final var component = CustomMessages.translate(text, Placeholder.component("homes", body));

CustomMessages.sendMessage(sender, component);
} else
} else {
CustomMessages.sendMessage(
sender,
CustomMessages.contextualPath(sender, target, "Error.noHomes"),
Placeholder.unparsed("player", target.getName()) // TODO: DisplayName
);
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ public boolean onCommand(
ATPlayer atPlayer = ATPlayer.getPlayer(player);

if (args.length == 0) {
if (atPlayer instanceof ATFloodgatePlayer
if (atPlayer instanceof ATFloodgatePlayer atFloodgatePlayer
&& MainConfig.get().USE_FLOODGATE_FORMS.get()) {
((ATFloodgatePlayer) atPlayer).sendMoveHomeForm();
if (!atFloodgatePlayer.getHomes().isEmpty()) {
atFloodgatePlayer.sendMoveHomeForm();
} else {
CustomMessages.sendMessage(sender, "Error.noHomes");
}
} else {
CustomMessages.sendMessage(sender, "Error.noHomeInput");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ public boolean onCommand(
ATPlayer atPlayer = ATPlayer.getPlayer(player);

if (args.length == 0) {
if (atPlayer instanceof ATFloodgatePlayer
if (atPlayer instanceof ATFloodgatePlayer atFloodgatePlayer
&& MainConfig.get().USE_FLOODGATE_FORMS.get()) {
((ATFloodgatePlayer) atPlayer).sendBlockForm();
if (!atFloodgatePlayer.getVisiblePlayerNames().isEmpty()) {
atFloodgatePlayer.sendBlockForm();
} else {
CustomMessages.sendMessage(sender, "Error.noPlayerInput");
}
} else {
CustomMessages.sendMessage(sender, "Error.noPlayerInput");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,17 @@ public boolean onCommand(
}

ATPlayer atPlayer = ATPlayer.getPlayer(player);
if (atPlayer instanceof ATFloodgatePlayer
if (atPlayer instanceof ATFloodgatePlayer atFloodgatePlayer
&& MainConfig.get().USE_FLOODGATE_FORMS.get()) {
((ATFloodgatePlayer) atPlayer).sendCancelForm();
final var responders =
TeleportRequest.getRequestsByRequester(atFloodgatePlayer.getPlayer()).stream()
.map(request -> request.requester().getName())
.toList();
if (!responders.isEmpty()) {
((ATFloodgatePlayer) atPlayer).sendCancelForm(responders);
} else {
CustomMessages.sendMessage(sender, "Error.noRequests");
}
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ public boolean onCommand(
Player player = (Player) sender;
ATPlayer atPlayer = ATPlayer.getPlayer(player);
if (args.length == 0) {
if (atPlayer instanceof ATFloodgatePlayer
if (atPlayer instanceof ATFloodgatePlayer atFloodgatePlayer
&& MainConfig.get().USE_FLOODGATE_FORMS.get()) {
((ATFloodgatePlayer) atPlayer).sendUnblockForm();
if (!atFloodgatePlayer.getVisiblePlayerNames().isEmpty()) {
atFloodgatePlayer.sendUnblockForm();
} else {
CustomMessages.sendMessage(sender, "Error.noPlayerInput");
}
} else {
CustomMessages.sendMessage(sender, "Error.noPlayerInput");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ public boolean onCommand(

if (args.length == 0) {
ATPlayer atPlayer = ATPlayer.getPlayer(player);
if (atPlayer instanceof ATFloodgatePlayer
if (atPlayer instanceof ATFloodgatePlayer atFloodgatePlayer
&& MainConfig.get().USE_FLOODGATE_FORMS.get()) {
((ATFloodgatePlayer) atPlayer).sendTPAForm(false);
if (!atFloodgatePlayer.getVisiblePlayerNames().isEmpty()) {
atFloodgatePlayer.sendTPAForm(false);
} else {
CustomMessages.sendMessage(sender, "Error.noOthersToTP");
}
} else {
CustomMessages.sendMessage(sender, "Error.noPlayerInput");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@ public boolean onCommand(

if (args.length == 0) {
ATPlayer atPlayer = ATPlayer.getPlayer(player);
if (atPlayer instanceof ATFloodgatePlayer
if (atPlayer instanceof ATFloodgatePlayer atFloodgatePlayer
&& MainConfig.get().USE_FLOODGATE_FORMS.get()) {
((ATFloodgatePlayer) atPlayer).sendTPAForm(true);
if (!atFloodgatePlayer.getVisiblePlayerNames().isEmpty()) {
atFloodgatePlayer.sendTPAForm(true);
} else {
CustomMessages.sendMessage(sender, "Error.noOthersToTP");
}
} else {
CustomMessages.sendMessage(sender, "Error.noPlayerInput");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ public boolean onCommand(

if (args.length == 0) {
ATPlayer atPlayer = ATPlayer.getPlayer(player);
if (atPlayer instanceof ATFloodgatePlayer
if (atPlayer instanceof ATFloodgatePlayer atFloodgatePlayer
&& MainConfig.get().USE_FLOODGATE_FORMS.get()) {
((ATFloodgatePlayer) atPlayer).sendTpoForm();
if (!atFloodgatePlayer.getVisiblePlayerNames().isEmpty()) {
atFloodgatePlayer.sendTpoForm();
} else {
CustomMessages.sendMessage(sender, "Error.noOthersToTP");
}
} else {
CustomMessages.sendMessage(sender, "Error.noPlayerInput");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ public boolean onCommand(
Player player = (Player) sender;
if (args.length == 0) {
ATPlayer atPlayer = ATPlayer.getPlayer(player);
if (atPlayer instanceof ATFloodgatePlayer
if (atPlayer instanceof ATFloodgatePlayer atFloodgatePlayer
&& MainConfig.get().USE_FLOODGATE_FORMS.get()) {
((ATFloodgatePlayer) atPlayer).sendTpoHereForm();
if (!atFloodgatePlayer.getVisiblePlayerNames().isEmpty()) {
atFloodgatePlayer.sendTpoHereForm();
} else {
CustomMessages.sendMessage(sender, "Error.noOthersToTP");
}
} else {
CustomMessages.sendMessage(sender, "Error.noPlayerInput");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ public boolean onCommand(
if (args.length == 0) {
if (sender instanceof Player player) {
ATPlayer atPlayer = ATPlayer.getPlayer(player);
if (atPlayer instanceof ATFloodgatePlayer
if (atPlayer instanceof ATFloodgatePlayer atFloodgatePlayer
&& MainConfig.get().USE_FLOODGATE_FORMS.get()) {
((ATFloodgatePlayer) atPlayer).sendDeleteWarpForm();
if (!AdvancedTeleportAPI.getWarps().isEmpty()) {
atFloodgatePlayer.sendDeleteWarpForm();
} else {
CustomMessages.sendMessage(sender, "Error.noWarps");
}
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ public boolean onCommand(
// continue
if (args.length == 0) {
ATPlayer atPlayer = ATPlayer.getPlayer(player);
if (atPlayer instanceof ATFloodgatePlayer
if (atPlayer instanceof ATFloodgatePlayer atFloodgatePlayer
&& MainConfig.get().USE_FLOODGATE_FORMS.get()) {
((ATFloodgatePlayer) atPlayer).sendMoveWarpForm();
if (!AdvancedTeleportAPI.getWarps().isEmpty()) {
atFloodgatePlayer.sendMoveWarpForm();
} else {
CustomMessages.sendMessage(sender, "Error.noWarps");
}
} else {
CustomMessages.sendMessage(sender, "Error.noWarpInput");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@ public boolean onCommand(
// If there's no arguments specified, see if the player is a Bedrock player and use a form
if (args.length == 0) {
ATPlayer atPlayer = ATPlayer.getPlayer(player);
if (atPlayer instanceof ATFloodgatePlayer
if (atPlayer instanceof ATFloodgatePlayer atFloodgatePlayer
&& MainConfig.get().USE_FLOODGATE_FORMS.get()) {
((ATFloodgatePlayer) atPlayer).sendWarpForm();
if (!AdvancedTeleportAPI.getWarps().isEmpty()) {
atFloodgatePlayer.sendWarpForm();
} else {
CustomMessages.sendMessage(sender, "Error.noWarps");
}
} else {
CustomMessages.sendMessage(sender, "Error.noWarpInput");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ If you prefer to use the Legacy Code format (i.e. &a, &b, etc.) then you can sti
"<prefix> <gray>You can't teleport to <aqua><world></aqua>!");
addDefault("Error.tooFewArguments", "<prefix> <gray>Too few arguments!");
addDefault("Error.invalidArgs", "<prefix> <gray>Invalid arguments!");
addDefault("Error.noOthersToTP", "<prefix> <gray>There are no players for you to teleport!");
addDefault(
"Error.cantTPToPlayer",
"<prefix> <gray>You can't request a teleportation to <aqua><player></aqua>!");
Expand Down

0 comments on commit cdff5d9

Please sign in to comment.