Add /tpa, /tpahere, /tpaccept, /tpdeny, strict teleporting (Closes #139)

This commit is contained in:
William committed 2022-08-18 23:20:20 +01:00
1 parent 11d05efcfa
commit 4b01e1bdec
13 files changed
+221 -72

No files matched your search

@@ -22,6 +22,10 @@ public enum BukkitCommandType {
EDIT_WARP_COMMAND(new EditWarpCommand(BukkitHuskHomes.getInstance())),
TP_COMMAND(new TpCommand(BukkitHuskHomes.getInstance())),
TP_HERE_COMMAND(new TpHereCommand(BukkitHuskHomes.getInstance())),
TPA_COMMAND(new TpaCommand(BukkitHuskHomes.getInstance())),
TPA_HERE_COMMAND(new TpaHereCommand(BukkitHuskHomes.getInstance())),
TPACCEPT_COMMAND(new TpRespondCommand(BukkitHuskHomes.getInstance(), true)),
TPDECLINE_COMMAND(new TpRespondCommand(BukkitHuskHomes.getInstance(), false)),
BACK_COMMAND(new BackCommand(BukkitHuskHomes.getInstance()));
public final CommandBase commandBase;
+10
View File
@@ -45,5 +45,15 @@ commands:
tphere:
usage: /tphere <player>
aliases: [tpohere]
tpa:
usage: /tpa <player>
tpahere:
usage: /tpahere <player>
tpaccept:
usage: /tpaccept
aliases: [tpyes]
tpdecline:
usage: /tpdecline
aliases: [tpdeny, tpno]
back:
usage: /back
@@ -64,7 +64,7 @@ public class HomeListCommand extends CommandBase implements ConsoleExecutable {
onlineUser.sendMessage(plugin.getCache().getHomeList(onlineUser, userData.user(),
plugin.getLocales(), homes, plugin.getSettings().listItemsPerPage, pageNumber));
});
}, () -> plugin.getLocales().getLocale("error_invalid_player").ifPresent(onlineUser::sendMessage)));
}, () -> plugin.getLocales().getLocale("error_player_not_found", homeOwner).ifPresent(onlineUser::sendMessage)));
}
@@ -0,0 +1,48 @@
package net.william278.huskhomes.command;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.player.OnlineUser;
import net.william278.huskhomes.util.Permission;
import org.jetbrains.annotations.NotNull;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
/**
* A command used for responding to tp requests - can either be a /tpaccept or /tpdecline command, controlled by the
* acceptRequestCommand flag
*/
public class TpRespondCommand extends CommandBase implements TabCompletable {
private final boolean acceptRequestCommand;
protected TpRespondCommand(@NotNull HuskHomes implementor, boolean acceptRequestCommand) {
super(acceptRequestCommand ? "tpaccept" : "tpdecline", acceptRequestCommand ? Permission.COMMAND_TPACCEPT : Permission.COMMAND_TPDECLINE, implementor);
this.acceptRequestCommand = acceptRequestCommand;
}
@Override
public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
if (args.length > 1) {
plugin.getLocales().getLocale("error_invalid_syntax", "/" + command + " [player]")
.ifPresent(onlineUser::sendMessage);
return;
}
if (args.length == 1) {
// Respond to the request from specified sender name
plugin.getRequestManager().respondToTeleportRequestBySenderName(onlineUser, args[0], acceptRequestCommand);
return;
}
plugin.getRequestManager().respondToTeleportRequest(onlineUser, acceptRequestCommand);
}
@Override
public @NotNull List<String> onTabComplete(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
return args.length <= 1 ? plugin.getCache().players.stream()
.filter(s -> s.toLowerCase().startsWith(args.length == 1 ? args[0].toLowerCase() : ""))
.sorted().collect(Collectors.toList()) : Collections.emptyList();
}
}
@@ -0,0 +1,41 @@
package net.william278.huskhomes.command;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.player.OnlineUser;
import net.william278.huskhomes.request.TeleportRequest;
import net.william278.huskhomes.util.Permission;
import org.jetbrains.annotations.NotNull;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public class TpaCommand extends CommandBase implements TabCompletable {
protected TpaCommand(@NotNull HuskHomes implementor) {
super("tpa", Permission.COMMAND_TPA, implementor);
}
@Override
public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
if (args.length == 1) {
plugin.getRequestManager().sendTeleportRequest(onlineUser, args[0], TeleportRequest.RequestType.TPA).thenAccept(sent -> {
if (!sent) {
plugin.getLocales().getLocale("error_player_not_found", args[0]).ifPresent(onlineUser::sendMessage);
return;
}
plugin.getLocales().getLocale("tpa_request_sent", args[0]).ifPresent(onlineUser::sendMessage);
});
} else {
plugin.getLocales().getLocale("error_invalid_syntax", "/tpa <player>")
.ifPresent(onlineUser::sendMessage);
}
}
@Override
public @NotNull List<String> onTabComplete(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
return args.length <= 1 ? plugin.getCache().players.stream()
.filter(s -> s.toLowerCase().startsWith(args.length == 1 ? args[0].toLowerCase() : ""))
.sorted().collect(Collectors.toList()) : Collections.emptyList();
}
}
@@ -0,0 +1,41 @@
package net.william278.huskhomes.command;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.player.OnlineUser;
import net.william278.huskhomes.request.TeleportRequest;
import net.william278.huskhomes.util.Permission;
import org.jetbrains.annotations.NotNull;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public class TpaHereCommand extends CommandBase implements TabCompletable {
protected TpaHereCommand(@NotNull HuskHomes implementor) {
super("tpahere", Permission.COMMAND_TPAHERE, implementor);
}
@Override
public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
if (args.length == 1) {
plugin.getRequestManager().sendTeleportRequest(onlineUser, args[0], TeleportRequest.RequestType.TPA_HERE).thenAccept(sent -> {
if (!sent) {
plugin.getLocales().getLocale("error_player_not_found", args[0]).ifPresent(onlineUser::sendMessage);
return;
}
plugin.getLocales().getLocale("tpahere_request_sent", args[0]).ifPresent(onlineUser::sendMessage);
});
} else {
plugin.getLocales().getLocale("error_invalid_syntax", "/tpahere <player>")
.ifPresent(onlineUser::sendMessage);
}
}
@Override
public @NotNull List<String> onTabComplete(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
return args.length <= 1 ? plugin.getCache().players.stream()
.filter(s -> s.toLowerCase().startsWith(args.length == 1 ? args[0].toLowerCase() : ""))
.sorted().collect(Collectors.toList()) : Collections.emptyList();
}
}
@@ -36,6 +36,18 @@ public class RequestManager {
this.ignoringRequests = new HashSet<>();
}
public void setIgnoringRequests(@NotNull User user) {
this.ignoringRequests.add(user.uuid);
}
public void removeIgnoringRequests(@NotNull User user) {
this.ignoringRequests.remove(user.uuid);
}
public boolean isIgnoringRequests(@NotNull User user) {
return this.ignoringRequests.contains(user.uuid);
}
public void addTeleportRequest(@NotNull TeleportRequest request, @NotNull User recipient) {
this.requests.computeIfAbsent(recipient.uuid, uuid -> new ArrayList<>()).add(request);
}
@@ -53,7 +65,7 @@ public class RequestManager {
public Optional<TeleportRequest> getTeleportRequest(@NotNull String requesterName, @NotNull User recipient) {
return this.requests.getOrDefault(recipient.uuid, Collections.emptyList()).stream().filter(request ->
request.requester.username.equals(requesterName)).findFirst();
request.requesterName.equals(requesterName)).findFirst();
}
/**
@@ -117,17 +129,22 @@ public class RequestManager {
* @return {@code true} if the request was sent, {@code false} if it was not sent because the recipient is ignoring requests
*/
public boolean sendLocalTeleportRequest(@NotNull TeleportRequest request, @NotNull OnlineUser recipient) {
if (ignoringRequests.contains(recipient.uuid)) {
if (isIgnoringRequests(recipient)) {
request.status = TeleportRequest.RequestStatus.IGNORED;
return false;
}
// If the person already has a request from the sender by name, don't bother sending another one
if (getTeleportRequest(request.requesterName, recipient).isPresent()) {
return true;
}
// Add the request and display a message to the recipient
addTeleportRequest(request, recipient);
plugin.getLocales().getLocale((request.type == TeleportRequest.RequestType.TPA ? "tpa" : "tpahere")
+ "_request_received", request.requester.username)
+ "_request_received", request.requesterName)
.ifPresent(recipient::sendMessage);
plugin.getLocales().getLocale("teleport_request_buttons", request.requester.username)
plugin.getLocales().getLocale("teleport_request_buttons", request.requesterName)
.ifPresent(recipient::sendMessage);
return true;
}
@@ -135,7 +152,7 @@ public class RequestManager {
public void respondToTeleportRequestBySenderName(@NotNull OnlineUser recipient, @NotNull String senderName,
boolean accepted) {
// Check the recipient is not ignoring teleport requests
if (ignoringRequests.contains(recipient.uuid)) {
if (isIgnoringRequests(recipient)) {
plugin.getLocales().getLocale("error_ignoring_teleport_requests").ifPresent(recipient::sendMessage);
return;
}
@@ -152,7 +169,7 @@ public class RequestManager {
public void respondToTeleportRequest(@NotNull OnlineUser recipient, boolean accepted) {
// Check the recipient is not ignoring teleport requests
if (ignoringRequests.contains(recipient.uuid)) {
if (isIgnoringRequests(recipient)) {
plugin.getLocales().getLocale("error_ignoring_teleport_requests").ifPresent(recipient::sendMessage);
return;
}
@@ -178,19 +195,19 @@ public class RequestManager {
// Send request response confirmation to the recipient
plugin.getLocales().getLocale("teleport_request_" + (accepted ? "accepted" : "declined") + "_confirmation",
request.requester.username)
request.requesterName)
.ifPresent(recipient::sendMessage);
request.status = accepted ? TeleportRequest.RequestStatus.ACCEPTED : TeleportRequest.RequestStatus.DECLINED;
// Send request response to the sender
CompletableFuture.runAsync(() -> {
final Optional<OnlineUser> localSender = plugin.findPlayer(request.requester.username);
final Optional<OnlineUser> localSender = plugin.findPlayer(request.requesterName);
if (localSender.isPresent()) {
handleLocalRequestResponse(localSender.get(), request);
} else if (plugin.getSettings().crossServer) {
assert plugin.getNetworkMessenger() != null;
// Ensure the sender is still online
if (!plugin.getNetworkMessenger().findPlayer(recipient, request.requester.username).thenApply(networkedTarget -> {
if (!plugin.getNetworkMessenger().findPlayer(recipient, request.requesterName).thenApply(networkedTarget -> {
if (networkedTarget.isEmpty()) {
plugin.getLocales().getLocale("error_teleport_request_sender_not_online")
.ifPresent(recipient::sendMessage);
@@ -222,9 +239,10 @@ public class RequestManager {
if (accepted && request.type == TeleportRequest.RequestType.TPA_HERE) {
// Strict /tpahere requests will teleport to where the sender was when typing the command
if (plugin.getSettings().strictTpaHereRequests) {
plugin.getTeleportManager().teleport(recipient, request.requesterPosition);
plugin.getTeleportManager().timedTeleport(recipient, request.requesterPosition).thenAccept(
teleportResult -> plugin.getTeleportManager().finishTeleport(recipient, teleportResult));
} else {
plugin.getTeleportManager().teleportToPlayerByName(recipient, request.requester.username);
plugin.getTeleportManager().teleportToPlayerByName(recipient, request.requesterName);
}
}
});
@@ -2,7 +2,6 @@ package net.william278.huskhomes.request;
import com.google.gson.annotations.SerializedName;
import net.william278.huskhomes.player.OnlineUser;
import net.william278.huskhomes.player.User;
import net.william278.huskhomes.position.Position;
import org.jetbrains.annotations.NotNull;
@@ -16,7 +15,8 @@ public class TeleportRequest {
/**
* The user making the request
*/
protected User requester;
@SerializedName("requester_name")
protected String requesterName;
/**
* The position of the requester, relevant in the case of a {@link RequestType#TPA_HERE} request
@@ -53,13 +53,14 @@ public class TeleportRequest {
*/
protected TeleportRequest(@NotNull OnlineUser requester, @NotNull RequestType requestType,
final long expiryTime) {
this.requester = requester;
this.requesterName = requester.username;
this.requesterPosition = requester.getPosition().join();
this.type = requestType;
this.status = RequestStatus.PENDING;
this.expiryTime = expiryTime;
}
@SuppressWarnings("unused")
private TeleportRequest() {
}
@@ -8,7 +8,6 @@ import net.william278.huskhomes.player.User;
import net.william278.huskhomes.position.Home;
import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.position.Warp;
import net.william278.huskhomes.util.MatcherUtil;
import net.william278.huskhomes.util.Permission;
import org.jetbrains.annotations.NotNull;
@@ -107,14 +106,30 @@ public class TeleportManager {
* @param targetPlayer the name of the target player
*/
public void teleportToPlayerByName(@NotNull OnlineUser onlineUser, @NotNull String targetPlayer) {
MatcherUtil.matchPlayerName(targetPlayer, plugin).ifPresentOrElse(playerName ->
getPlayerPositionByName(onlineUser, playerName).thenAccept(optionalPosition ->
optionalPosition.ifPresentOrElse(targetPosition ->
timedTeleport(onlineUser, targetPosition).thenAccept(result ->
finishTeleport(onlineUser, result)),
() -> plugin.getLocales().getLocale("error_invalid_player").
ifPresent(onlineUser::sendMessage))),
() -> plugin.getLocales().getLocale("error_invalid_player").ifPresent(onlineUser::sendMessage));
CompletableFuture.runAsync(() -> {
Optional<OnlineUser> localPlayer = plugin.findPlayer(targetPlayer);
if (localPlayer.isPresent()) {
timedTeleport(onlineUser, localPlayer.get().getPosition().join())
.thenAccept(result -> finishTeleport(onlineUser, result));
} else {
if (plugin.getSettings().crossServer) {
assert plugin.getNetworkMessenger() != null;
getPlayerPositionByName(onlineUser, targetPlayer).thenAccept(optionalPosition -> {
if (optionalPosition.isPresent()) {
timedTeleport(onlineUser, optionalPosition.get())
.thenAccept(teleportResult -> finishTeleport(onlineUser, teleportResult));
return;
}
plugin.getLocales().getLocale("error_player_not_found", targetPlayer)
.ifPresent(onlineUser::sendMessage);
});
}
return;
}
plugin.getLocales().getLocale("error_player_not_found", targetPlayer)
.ifPresent(onlineUser::sendMessage);
});
}
/**
@@ -183,7 +198,8 @@ public class TeleportManager {
* @param playerName the username of the player being requested
* @return future optionally supplying the player's position, if the player could be found
*/
private CompletableFuture<Optional<Position>> getPlayerPositionByName(@NotNull OnlineUser requester, @NotNull String playerName) {
private CompletableFuture<Optional<Position>> getPlayerPositionByName(@NotNull OnlineUser requester,
@NotNull String playerName) {
final Optional<OnlineUser> localPlayer = plugin.getOnlinePlayers().stream().filter(player ->
player.username.equalsIgnoreCase(playerName)).findFirst();
if (localPlayer.isPresent()) {
@@ -191,14 +207,21 @@ public class TeleportManager {
}
if (plugin.getSettings().crossServer) {
assert plugin.getNetworkMessenger() != null;
return plugin.getNetworkMessenger().sendMessage(requester,
new Message(Message.MessageType.POSITION_REQUEST,
requester.username,
playerName,
MessagePayload.empty(),
Message.RelayType.MESSAGE,
plugin.getSettings().clusterId))
.thenApply(reply -> Optional.ofNullable(reply.payload.position));
return plugin.getNetworkMessenger().findPlayer(requester, playerName).thenApply(foundPlayer -> {
if (foundPlayer.isEmpty()) {
return Optional.empty();
}
return plugin.getNetworkMessenger().sendMessage(requester,
new Message(Message.MessageType.POSITION_REQUEST,
requester.username,
playerName,
MessagePayload.empty(),
Message.RelayType.MESSAGE,
plugin.getSettings().clusterId))
.orTimeout(3, TimeUnit.SECONDS)
.exceptionally(throwable -> null)
.thenApply(reply -> Optional.ofNullable(reply.payload.position)).join();
});
}
return CompletableFuture.supplyAsync(Optional::empty);
}
@@ -1,35 +0,0 @@
package net.william278.huskhomes.util;
import net.william278.huskhomes.HuskHomes;
import org.jetbrains.annotations.NotNull;
import java.util.Comparator;
import java.util.Optional;
/**
* Utility for matching a username based on an input name
*/
public final class MatcherUtil {
/**
* Match a player's username from a user-supplied input name
*
* @param inputName the user input name
* @param plugin the implementing plugin
* @return an optional, containing a matched username if one was found
*/
public static Optional<String> matchPlayerName(@NotNull String inputName, @NotNull HuskHomes plugin) {
// Return an exact match if there is one present
final Optional<String> exactPlayer = plugin.getCache().players.stream().filter(username ->
username.equalsIgnoreCase(inputName)).findFirst();
if (exactPlayer.isPresent()) {
return exactPlayer;
}
// Return most likely match, if present
return plugin.getCache().players.stream().filter(username -> username.toLowerCase()
.startsWith(inputName.toLowerCase())).min(Comparator.comparingInt(username ->
username.length() - inputName.toLowerCase().length()));
}
}
@@ -103,7 +103,7 @@ public enum Permission {
/**
* Lets the user deny a tp request ({@code /tpdeny})
*/
COMMAND_TPDENY("huskhomes.command.tpdeny", DefaultAccess.EVERYONE),
COMMAND_TPDECLINE("huskhomes.command.tpdecline", DefaultAccess.EVERYONE),
/**
* Lets the user ignore incoming tp requests ({@code /tpignore})
*/
@@ -148,7 +148,6 @@ error_permission_restricted_warp: '[Error:](#ff3300) [You do not have permission
error_invalid_page_number: '[Error:](#ff3300) [Please specify a valid page number.](#ff7e5e)'
error_invalid_on_arrival: '[Error:](#ff3300) [Failed to finish teleportation as the target world could not be found.](#ff7e5e)'
error_no_offline_position: '[Error:](#ff3300) [Could not find where %1% last logged out.](#ff7e5e)'
error_invalid_player: '[Error:](#ff3300) [Please specify the name of a valid player!](#ff7e5e)'
error_no_homes_set_other: '[Error:](#ff3300) [%1% has not set any homes!](#ff7e5e)'
error_home_invalid_other: '[Error:](#ff3300) [%1% does not have a home set named "%2%"](#ff7e5e)'
error_edit_home_maximum_public_homes: '[Error:](#ff3300) [You can only make a maximum of %1% homes public.](#ff7e5e)'
@@ -13,7 +13,7 @@ public class MessageTests {
@Test
public void testMessageSerializationWithEmptyPayload() {
final Message message = new Message(
Message.MessageType.TP_REQUEST,
Message.MessageType.TELEPORT_REQUEST,
"TestSender",
"TestTarget",
MessagePayload.empty(),
@@ -49,5 +49,4 @@ public class MessageTests {
Assertions.assertEquals(message.payload.position.y, readMessage.payload.position.y);
Assertions.assertEquals(message.payload.position.z, readMessage.payload.position.z);
}
}