Add /tphere, make /tp instantaneous, add images, slight refactoring

This commit is contained in:
William committed 2022-08-18 20:00:07 +01:00
1 parent e37a55b214
commit 6149ebe983
17 files changed
+198 -50

No files matched your search

@@ -21,6 +21,7 @@ public enum BukkitCommandType {
DEL_WARP_COMMAND(new DelWarpCommand(BukkitHuskHomes.getInstance())), DEL_WARP_COMMAND(new DelWarpCommand(BukkitHuskHomes.getInstance())),
EDIT_WARP_COMMAND(new EditWarpCommand(BukkitHuskHomes.getInstance())), EDIT_WARP_COMMAND(new EditWarpCommand(BukkitHuskHomes.getInstance())),
TP_COMMAND(new TpCommand(BukkitHuskHomes.getInstance())), TP_COMMAND(new TpCommand(BukkitHuskHomes.getInstance())),
TP_HERE_COMMAND(new TpHereCommand(BukkitHuskHomes.getInstance())),
BACK_COMMAND(new BackCommand(BukkitHuskHomes.getInstance())); BACK_COMMAND(new BackCommand(BukkitHuskHomes.getInstance()));
public final CommandBase commandBase; public final CommandBase commandBase;
@@ -106,13 +106,13 @@ public class PluginMessenger extends NetworkMessenger implements PluginMessageLi
} }
@Override @Override
protected CompletableFuture<Void> sendReply(@NotNull OnlineUser replier, @NotNull Message reply) { protected void sendReply(@NotNull OnlineUser replier, @NotNull Message reply) {
return sendPluginMessage((BukkitPlayer) replier, reply); sendPluginMessage((BukkitPlayer) replier, reply);
} }
@SuppressWarnings("UnstableApiUsage") @SuppressWarnings("UnstableApiUsage")
private CompletableFuture<Void> sendPluginMessage(@NotNull BukkitPlayer player, @NotNull Message message) { private void sendPluginMessage(@NotNull BukkitPlayer player, @NotNull Message message) {
return CompletableFuture.runAsync(() -> { CompletableFuture.runAsync(() -> {
final ByteArrayDataOutput messageWriter = ByteStreams.newDataOutput(); final ByteArrayDataOutput messageWriter = ByteStreams.newDataOutput();
messageWriter.writeUTF("ForwardToPlayer"); messageWriter.writeUTF("ForwardToPlayer");
messageWriter.writeUTF(message.targetPlayer); messageWriter.writeUTF(message.targetPlayer);
@@ -61,8 +61,8 @@ public class RedisMessenger extends PluginMessenger {
} }
@Override @Override
protected CompletableFuture<Void> sendReply(@NotNull OnlineUser replier, @NotNull Message reply) { protected void sendReply(@NotNull OnlineUser replier, @NotNull Message reply) {
return redisWorker.sendMessage(reply); redisWorker.sendMessage(reply);
} }
@Override @Override
+4 -1
View File
@@ -40,7 +40,10 @@ commands:
editwarp: editwarp:
usage: /editwarp <name> [rename|description|relocate] [args] usage: /editwarp <name> [rename|description|relocate] [args]
tp: tp:
usage: /tp <name> usage: /tp <target> [destination]
aliases: [tpo] aliases: [tpo]
tphere:
usage: /tphere <player>
aliases: [tpohere]
back: back:
usage: /back usage: /back
@@ -2,7 +2,6 @@ package net.william278.huskhomes.command;
import net.william278.huskhomes.HuskHomes; import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.player.OnlineUser; import net.william278.huskhomes.player.OnlineUser;
import net.william278.huskhomes.player.User;
import net.william278.huskhomes.util.Permission; import net.william278.huskhomes.util.Permission;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@@ -15,7 +14,7 @@ public class BackCommand extends CommandBase {
@Override @Override
public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) { public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
plugin.getDatabase().getLastPosition(onlineUser).thenAccept(lastPosition -> plugin.getDatabase().getLastPosition(onlineUser).thenAccept(lastPosition ->
lastPosition.ifPresentOrElse(position -> plugin.getTeleportManager().teleport(onlineUser, position) lastPosition.ifPresentOrElse(position -> plugin.getTeleportManager().timedTeleport(onlineUser, position)
.thenAccept(result -> plugin.getTeleportManager().finishTeleport(onlineUser, result)), .thenAccept(result -> plugin.getTeleportManager().finishTeleport(onlineUser, result)),
() -> plugin.getLocales().getLocale("error_no_last_position").ifPresent(onlineUser::sendMessage))); () -> plugin.getLocales().getLocale("error_no_last_position").ifPresent(onlineUser::sendMessage)));
} }
@@ -23,7 +23,7 @@ public class HomeCommand extends CommandBase implements TabCompletable, ConsoleE
// Send the home list if they have homes set. If they have just one home set, teleport the player // Send the home list if they have homes set. If they have just one home set, teleport the player
switch (homes.size()) { switch (homes.size()) {
case 0 -> plugin.getLocales().getLocale("error_no_homes_set").ifPresent(onlineUser::sendMessage); case 0 -> plugin.getLocales().getLocale("error_no_homes_set").ifPresent(onlineUser::sendMessage);
case 1 -> plugin.getTeleportManager().teleport(onlineUser, homes.get(0)).thenAccept(result -> case 1 -> plugin.getTeleportManager().timedTeleport(onlineUser, homes.get(0)).thenAccept(result ->
plugin.getTeleportManager().finishTeleport(onlineUser, result)).join(); plugin.getTeleportManager().finishTeleport(onlineUser, result)).join();
default -> onlineUser.sendMessage(plugin.getCache().getHomeList(onlineUser, onlineUser, default -> onlineUser.sendMessage(plugin.getCache().getHomeList(onlineUser, onlineUser,
plugin.getLocales(), homes, plugin.getSettings().listItemsPerPage,1)); plugin.getLocales(), homes, plugin.getSettings().listItemsPerPage,1));
@@ -35,10 +35,10 @@ public class HomeCommand extends CommandBase implements TabCompletable, ConsoleE
RegexUtil.matchDisambiguatedHomeIdentifier(homeName).ifPresentOrElse( RegexUtil.matchDisambiguatedHomeIdentifier(homeName).ifPresentOrElse(
homeIdentifier -> plugin.getDatabase().getUserDataByName(homeIdentifier.ownerName()) homeIdentifier -> plugin.getDatabase().getUserDataByName(homeIdentifier.ownerName())
.thenAccept(optionalUserData -> optionalUserData.ifPresentOrElse( .thenAccept(optionalUserData -> optionalUserData.ifPresentOrElse(
userData -> plugin.getTeleportManager().teleportToHome(onlineUser, userData.user(), homeIdentifier.homeName()), userData -> plugin.getTeleportManager().teleportToHomeByName(onlineUser, userData.user(), homeIdentifier.homeName()),
() -> plugin.getLocales().getLocale("error_home_invalid_other", homeIdentifier.ownerName(), homeIdentifier.homeName()) () -> plugin.getLocales().getLocale("error_home_invalid_other", homeIdentifier.ownerName(), homeIdentifier.homeName())
.ifPresent(onlineUser::sendMessage))), .ifPresent(onlineUser::sendMessage))),
() -> plugin.getTeleportManager().teleportToHome(onlineUser, onlineUser, homeName)); () -> plugin.getTeleportManager().teleportToHomeByName(onlineUser, onlineUser, homeName));
} }
default -> plugin.getLocales().getLocale("error_invalid_syntax", "/home [name]") default -> plugin.getLocales().getLocale("error_invalid_syntax", "/home [name]")
.ifPresent(onlineUser::sendMessage); .ifPresent(onlineUser::sendMessage);
@@ -38,7 +38,7 @@ public class PublicHomeCommand extends CommandBase implements TabCompletable, Co
RegexUtil.matchDisambiguatedHomeIdentifier(homeName).ifPresentOrElse( RegexUtil.matchDisambiguatedHomeIdentifier(homeName).ifPresentOrElse(
homeIdentifier -> plugin.getDatabase().getUserDataByName(homeIdentifier.ownerName()) homeIdentifier -> plugin.getDatabase().getUserDataByName(homeIdentifier.ownerName())
.thenAccept(optionalUserData -> optionalUserData.ifPresentOrElse( .thenAccept(optionalUserData -> optionalUserData.ifPresentOrElse(
userData -> plugin.getTeleportManager().teleportToHome(onlineUser, userData.user(), homeIdentifier.homeName()), userData -> plugin.getTeleportManager().teleportToHomeByName(onlineUser, userData.user(), homeIdentifier.homeName()),
() -> plugin.getLocales().getLocale("error_home_invalid_other", homeIdentifier.ownerName(), homeIdentifier.homeName()) () -> plugin.getLocales().getLocale("error_home_invalid_other", homeIdentifier.ownerName(), homeIdentifier.homeName())
.ifPresent(onlineUser::sendMessage))), .ifPresent(onlineUser::sendMessage))),
() -> plugin.getDatabase().getPublicHomes().thenAccept(publicHomes -> { () -> plugin.getDatabase().getPublicHomes().thenAccept(publicHomes -> {
@@ -22,7 +22,7 @@ public class TpCommand extends CommandBase implements TabCompletable, ConsoleExe
@Override @Override
public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) { public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
OnlineUser userToTeleport = onlineUser; String teleportingUserName = onlineUser.username;
TeleportCommandTarget targetPosition; TeleportCommandTarget targetPosition;
// Validate argument length // Validate argument length
@@ -40,13 +40,7 @@ public class TpCommand extends CommandBase implements TabCompletable, ConsoleExe
int coordinatesIndex = ((args.length > 3 && (isCoordinate(args[1]) && isCoordinate(args[2]) && isCoordinate(args[3]))) int coordinatesIndex = ((args.length > 3 && (isCoordinate(args[1]) && isCoordinate(args[2]) && isCoordinate(args[3])))
|| (args.length == 2)) ? 1 : 0; || (args.length == 2)) ? 1 : 0;
if (coordinatesIndex == 1) { if (coordinatesIndex == 1) {
userToTeleport = plugin.getOnlinePlayers().stream().filter(user -> user.username.equalsIgnoreCase(args[0])) teleportingUserName = args[0];
.findFirst().orElse(plugin.getOnlinePlayers().stream().filter(user -> user.username.startsWith(args[0]))
.findFirst().orElse(null));
if (userToTeleport == null) {
plugin.getLocales().getLocale("error_player_not_found", args[0]).ifPresent(onlineUser::sendMessage);
return;
}
} }
// Determine the target position (player or coordinates) // Determine the target position (player or coordinates)
@@ -73,15 +67,25 @@ public class TpCommand extends CommandBase implements TabCompletable, ConsoleExe
} }
// Execute the teleport // Execute the teleport
final OnlineUser teleportingUser = userToTeleport; final String playerToTeleportName = teleportingUserName;
if (targetPosition.targetType == TeleportCommandTarget.TargetType.PLAYER) { if (targetPosition.targetType == TeleportCommandTarget.TargetType.PLAYER) {
// Teleport players by usernames
assert targetPosition.targetPlayer != null; assert targetPosition.targetPlayer != null;
CompletableFuture.runAsync(() -> plugin.getTeleportManager().teleportToPlayer(teleportingUser, targetPosition.targetPlayer)); CompletableFuture.runAsync(() -> plugin.getTeleportManager()
.teleportPlayerToPlayerByName(playerToTeleportName, targetPosition.targetPlayer, onlineUser)
.thenAccept(resultIfPlayerExists -> resultIfPlayerExists.ifPresentOrElse(
result -> plugin.getTeleportManager().finishTeleport(onlineUser, result),
() -> plugin.getLocales().getLocale("error_player_not_found", playerToTeleportName)
.ifPresent(onlineUser::sendMessage))));
return; return;
} else if (targetPosition.targetType == TeleportCommandTarget.TargetType.POSITION) { } else if (targetPosition.targetType == TeleportCommandTarget.TargetType.POSITION) {
// Teleport players by specified position
assert targetPosition.targetPosition != null; assert targetPosition.targetPosition != null;
plugin.getTeleportManager().teleport(teleportingUser, targetPosition.targetPosition).thenAccept( plugin.getTeleportManager().teleportPlayerByName(playerToTeleportName, targetPosition.targetPosition, onlineUser)
result -> plugin.getTeleportManager().finishTeleport(teleportingUser, result)); .thenAccept(resultIfPlayerExists -> resultIfPlayerExists.ifPresentOrElse(
result -> plugin.getTeleportManager().finishTeleport(onlineUser, result),
() -> plugin.getLocales().getLocale("error_player_not_found", playerToTeleportName)
.ifPresent(onlineUser::sendMessage)));
return; return;
} }
throw new HuskHomesException("Attempted to execute invalid teleport command operation"); throw new HuskHomesException("Attempted to execute invalid teleport command operation");
@@ -0,0 +1,34 @@
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;
public class TpHereCommand extends CommandBase implements TabCompletable {
protected TpHereCommand(@NotNull HuskHomes implementor) {
super("tphere", Permission.COMMAND_TPHERE, implementor, "tpohere");
}
@Override
public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
if (args.length != 1) {
plugin.getLocales().getLocale("error_invalid_syntax", "/tphere <player>")
.ifPresent(onlineUser::sendMessage);
return;
}
plugin.getTeleportManager().teleportPlayerByName(args[0], onlineUser.getPosition().join(), onlineUser);
}
@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();
}
}
@@ -27,7 +27,7 @@ public class WarpCommand extends CommandBase implements TabCompletable, ConsoleE
}); });
case 1 -> { case 1 -> {
final String warpName = args[0]; final String warpName = args[0];
plugin.getTeleportManager().teleportToWarp(onlineUser, warpName); plugin.getTeleportManager().teleportToWarpByName(onlineUser, warpName);
} }
default -> plugin.getLocales().getLocale("error_invalid_syntax", "/warp [name]") default -> plugin.getLocales().getLocale("error_invalid_syntax", "/warp [name]")
.ifPresent(onlineUser::sendMessage); .ifPresent(onlineUser::sendMessage);
@@ -82,6 +82,7 @@ public class Message {
*/ */
public enum MessageType { public enum MessageType {
TP_REQUEST, TP_REQUEST,
TP_TO_POSITION_REQUEST,
POSITION_REQUEST POSITION_REQUEST
} }
@@ -1,6 +1,8 @@
package net.william278.huskhomes.messenger; package net.william278.huskhomes.messenger;
import com.google.gson.annotations.SerializedName;
import net.william278.huskhomes.position.Position; import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.teleport.TeleportResult;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@@ -15,6 +17,13 @@ public class MessagePayload {
@Nullable @Nullable
public Position position; public Position position;
/**
* A teleport result field
*/
@Nullable
@SerializedName("teleport_result")
public TeleportResult teleportResult;
/** /**
* Returns an empty cross-server message payload * Returns an empty cross-server message payload
* *
@@ -38,6 +47,19 @@ public class MessagePayload {
return payload; return payload;
} }
/**
* Returns a payload containing a {@link TeleportResult}
*
* @param teleportResult the teleport to send
* @return a payload containing the teleport result
*/
@NotNull
public static MessagePayload withTeleportResult(@NotNull TeleportResult teleportResult) {
final MessagePayload payload = new MessagePayload();
payload.teleportResult = teleportResult;
return payload;
}
private MessagePayload() { private MessagePayload() {
} }
@@ -42,6 +42,11 @@ public abstract class NetworkMessenger {
*/ */
protected String clusterId; protected String clusterId;
/**
* The implementing HuskHomes plugin instance
*/
private HuskHomes plugin;
/** /**
* Initialize the network messenger * Initialize the network messenger
* *
@@ -50,6 +55,7 @@ public abstract class NetworkMessenger {
public void initialize(@NotNull HuskHomes implementor) { public void initialize(@NotNull HuskHomes implementor) {
this.processingMessages = new HashMap<>(); this.processingMessages = new HashMap<>();
this.clusterId = implementor.getSettings().clusterId; this.clusterId = implementor.getSettings().clusterId;
this.plugin = implementor;
} }
/** /**
@@ -97,9 +103,8 @@ public abstract class NetworkMessenger {
* Send a reply to a received {@link Message} * Send a reply to a received {@link Message}
* *
* @param reply The reply {@link Message} to send * @param reply The reply {@link Message} to send
* @return A future completing when the message has been sent
*/ */
protected abstract CompletableFuture<Void> sendReply(@NotNull OnlineUser replier, @NotNull Message reply); protected abstract void sendReply(@NotNull OnlineUser replier, @NotNull Message reply);
/** /**
* Handle and action received network {@link Message}s * Handle and action received network {@link Message}s
@@ -128,6 +133,14 @@ public abstract class NetworkMessenger {
case TP_REQUEST -> { case TP_REQUEST -> {
//todo //todo
} }
case TP_TO_POSITION_REQUEST -> {
if (message.payload.position != null) {
message.payload = MessagePayload.withTeleportResult(plugin.getTeleportManager()
.teleport(receiver, message.payload.position).join());
} else {
message.payload = MessagePayload.empty();
}
}
case POSITION_REQUEST -> message.payload = MessagePayload.withPosition(receiver.getPosition().join()); case POSITION_REQUEST -> message.payload = MessagePayload.withPosition(receiver.getPosition().join());
} }
@@ -48,7 +48,7 @@ public class TeleportManager {
* @param homeOwner the {@link User} who owns the home * @param homeOwner the {@link User} who owns the home
* @param homeName the name of the home * @param homeName the name of the home
*/ */
public void teleportToHome(@NotNull OnlineUser onlineUser, @NotNull User homeOwner, @NotNull String homeName) { public void teleportToHomeByName(@NotNull OnlineUser onlineUser, @NotNull User homeOwner, @NotNull String homeName) {
plugin.getDatabase().getHome(homeOwner, homeName).thenAccept(optionalHome -> plugin.getDatabase().getHome(homeOwner, homeName).thenAccept(optionalHome ->
optionalHome.ifPresentOrElse(home -> teleportToHome(onlineUser, home), () -> { optionalHome.ifPresentOrElse(home -> teleportToHome(onlineUser, home), () -> {
if (homeOwner.uuid.equals(onlineUser.uuid)) { if (homeOwner.uuid.equals(onlineUser.uuid)) {
@@ -73,7 +73,7 @@ public class TeleportManager {
return; return;
} }
} }
teleport(onlineUser, home).thenAccept(result -> finishTeleport(onlineUser, result)); timedTeleport(onlineUser, home).thenAccept(result -> finishTeleport(onlineUser, result));
} }
/** /**
@@ -82,7 +82,7 @@ public class TeleportManager {
* @param onlineUser the {@link OnlineUser} to teleport * @param onlineUser the {@link OnlineUser} to teleport
* @param warpName the name of the warp * @param warpName the name of the warp
*/ */
public void teleportToWarp(@NotNull OnlineUser onlineUser, @NotNull String warpName) { public void teleportToWarpByName(@NotNull OnlineUser onlineUser, @NotNull String warpName) {
plugin.getDatabase().getWarp(warpName).thenAccept(optionalWarp -> plugin.getDatabase().getWarp(warpName).thenAccept(optionalWarp ->
optionalWarp.ifPresentOrElse(warp -> //todo permission restricted warps optionalWarp.ifPresentOrElse(warp -> //todo permission restricted warps
teleportToWarp(onlineUser, warp), () -> teleportToWarp(onlineUser, warp), () ->
@@ -97,26 +97,85 @@ public class TeleportManager {
* @param warp the {@link Warp} to teleport to * @param warp the {@link Warp} to teleport to
*/ */
public void teleportToWarp(@NotNull OnlineUser onlineUser, @NotNull Warp warp) { public void teleportToWarp(@NotNull OnlineUser onlineUser, @NotNull Warp warp) {
teleport(onlineUser, warp).thenAccept(result -> finishTeleport(onlineUser, result)); timedTeleport(onlineUser, warp).thenAccept(result -> finishTeleport(onlineUser, result));
} }
/** /**
* Teleport a {@link OnlineUser} to another player by given input name * Teleport a {@link OnlineUser} to another player by username
* *
* @param onlineUser the {@link OnlineUser} to teleport * @param onlineUser the {@link OnlineUser} to teleport
* @param targetPlayer the name of the target player * @param targetPlayer the name of the target player
*/ */
public void teleportToPlayer(@NotNull OnlineUser onlineUser, @NotNull String targetPlayer) { public void teleportToPlayerByName(@NotNull OnlineUser onlineUser, @NotNull String targetPlayer) {
MatcherUtil.matchPlayerName(targetPlayer, plugin).ifPresentOrElse(playerName -> MatcherUtil.matchPlayerName(targetPlayer, plugin).ifPresentOrElse(playerName ->
getPlayerPosition(onlineUser, playerName).thenAccept(optionalPosition -> getPlayerPositionByName(onlineUser, playerName).thenAccept(optionalPosition ->
optionalPosition.ifPresentOrElse(targetPosition -> optionalPosition.ifPresentOrElse(targetPosition ->
teleport(onlineUser, targetPosition).thenAccept(result -> timedTeleport(onlineUser, targetPosition).thenAccept(result ->
finishTeleport(onlineUser, result)), finishTeleport(onlineUser, result)),
() -> plugin.getLocales().getLocale("error_invalid_player"). () -> plugin.getLocales().getLocale("error_invalid_player").
ifPresent(onlineUser::sendMessage))), ifPresent(onlineUser::sendMessage))),
() -> plugin.getLocales().getLocale("error_invalid_player").ifPresent(onlineUser::sendMessage)); () -> plugin.getLocales().getLocale("error_invalid_player").ifPresent(onlineUser::sendMessage));
} }
/**
* Immediately teleport a player by username to a {@link Position} by name
*
* @param playerName username of the target player to teleport
* @param position the {@link Position} to teleport to
* @param requester the {@link OnlineUser} performing the teleport action
* @return a {@link CompletableFuture} that completes when the teleport is complete with the {@link TeleportResult},
* if it was processed, otherwise an empty {@link Optional} if the player was not found
*/
public CompletableFuture<Optional<TeleportResult>> teleportPlayerByName(@NotNull String playerName, @NotNull Position position,
@NotNull OnlineUser requester) {
final Optional<OnlineUser> localPlayer = plugin.getOnlinePlayers().stream().filter(player ->
player.username.equalsIgnoreCase(playerName)).findFirst();
if (localPlayer.isPresent()) {
return teleport(localPlayer.get(), position).thenApply(Optional::of);
}
if (plugin.getSettings().crossServer) {
assert plugin.getNetworkMessenger() != null;
return plugin.getNetworkMessenger().sendMessage(requester,
new Message(Message.MessageType.TP_TO_POSITION_REQUEST,
requester.username,
playerName,
MessagePayload.withPosition(position),
Message.RelayType.MESSAGE,
plugin.getSettings().clusterId))
.thenApply(result -> {
if (result.payload.teleportResult == null) {
return Optional.empty();
}
return Optional.of(result.payload.teleportResult);
});
}
return CompletableFuture.supplyAsync(Optional::empty);
}
/**
* Teleport two players by username
*
* @param playerName the name of the player to teleport
* @param targetPlayer the name of the target player
* @param requester the {@link OnlineUser} performing the teleport action
* @return a {@link CompletableFuture} that completes when the teleport is complete with the {@link TeleportResult},
*/
public CompletableFuture<Optional<TeleportResult>> teleportPlayerToPlayerByName(@NotNull String playerName,
@NotNull String targetPlayer,
@NotNull OnlineUser requester) {
final Optional<Position> localPositionTarget = plugin.getOnlinePlayers().stream().filter(player ->
player.username.equalsIgnoreCase(targetPlayer)).findFirst().map(user -> user.getPosition().join());
if (localPositionTarget.isPresent()) {
return teleportPlayerByName(playerName, localPositionTarget.get(), requester);
}
return getPlayerPositionByName(requester, targetPlayer).thenApply(position -> {
if (position.isEmpty()) {
return Optional.empty();
}
return teleportPlayerByName(playerName, position.get(), requester).join();
});
}
/** /**
* Gets the position of a player by their username, including players on other servers * Gets the position of a player by their username, including players on other servers
* *
@@ -124,7 +183,7 @@ public class TeleportManager {
* @param playerName the username of the player being requested * @param playerName the username of the player being requested
* @return future optionally supplying the player's position, if the player could be found * @return future optionally supplying the player's position, if the player could be found
*/ */
private CompletableFuture<Optional<Position>> getPlayerPosition(@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 -> final Optional<OnlineUser> localPlayer = plugin.getOnlinePlayers().stream().filter(player ->
player.username.equalsIgnoreCase(playerName)).findFirst(); player.username.equalsIgnoreCase(playerName)).findFirst();
if (localPlayer.isPresent()) { if (localPlayer.isPresent()) {
@@ -145,12 +204,12 @@ public class TeleportManager {
} }
/** /**
* Teleport a {@link OnlineUser} to a specified {@link Position}. Respects timed teleport. * Teleport a {@link OnlineUser} to a specified {@link Position} after a warmup period
* *
* @param onlineUser the {@link OnlineUser} to teleport * @param onlineUser the {@link OnlineUser} to teleport
* @param position the target {@link Position} to teleport to * @param position the target {@link Position} to teleport to
*/ */
public CompletableFuture<TeleportResult> teleport(@NotNull OnlineUser onlineUser, @NotNull Position position) { public CompletableFuture<TeleportResult> timedTeleport(@NotNull OnlineUser onlineUser, @NotNull Position position) {
final int teleportWarmupTime = plugin.getSettings().teleportWarmupTime; final int teleportWarmupTime = plugin.getSettings().teleportWarmupTime;
if (!onlineUser.hasPermission(Permission.BYPASS_TELEPORT_WARMUP.node) && teleportWarmupTime > 0) { if (!onlineUser.hasPermission(Permission.BYPASS_TELEPORT_WARMUP.node) && teleportWarmupTime > 0) {
if (currentlyOnWarmup.contains(onlineUser.uuid)) { if (currentlyOnWarmup.contains(onlineUser.uuid)) {
@@ -159,20 +218,20 @@ public class TeleportManager {
return CompletableFuture.supplyAsync(() -> processTeleportWarmup(new TimedTeleport(onlineUser, position, teleportWarmupTime)) return CompletableFuture.supplyAsync(() -> processTeleportWarmup(new TimedTeleport(onlineUser, position, teleportWarmupTime))
.thenApply(teleport -> { .thenApply(teleport -> {
if (!teleport.cancelled) { if (!teleport.cancelled) {
return teleportNow(onlineUser, position).join(); return teleport(onlineUser, position).join();
} else { } else {
return TeleportResult.CANCELLED; return TeleportResult.CANCELLED;
} }
}).join()); }).join());
} else { } else {
return teleportNow(onlineUser, position); return teleport(onlineUser, position);
} }
} }
/** /**
* Handles a completed {@link OnlineUser}'s {@link TeleportResult} with the appropriate message * Handles a completed {@link OnlineUser}'s {@link TeleportResult} with the appropriate message
* *
* @param onlineUser the {@link OnlineUser} who just completed a teleport * @param onlineUser the {@link OnlineUser} to send the teleport completion message to
* @param teleportResult the {@link TeleportResult} to handle * @param teleportResult the {@link TeleportResult} to handle
*/ */
public void finishTeleport(@NotNull OnlineUser onlineUser, @NotNull TeleportResult teleportResult) { public void finishTeleport(@NotNull OnlineUser onlineUser, @NotNull TeleportResult teleportResult) {
@@ -194,12 +253,14 @@ public class TeleportManager {
} }
/** /**
* Executes a teleport now, teleporting a {@link OnlineUser} to a specified {@link Position} * Carries out a teleport, teleporting a {@link OnlineUser} to a specified {@link Position} and returning
* a future that will return a {@link TeleportResult}
* *
* @param onlineUser the {@link OnlineUser} to teleport * @param onlineUser the {@link OnlineUser} to teleport
* @param position the target {@link Position} to teleport to * @param position the target {@link Position} to teleport to#
* @return a {@link CompletableFuture} that completes when the teleport is complete with the {@link TeleportResult}
*/ */
private CompletableFuture<TeleportResult> teleportNow(@NotNull OnlineUser onlineUser, @NotNull Position position) { public CompletableFuture<TeleportResult> teleport(@NotNull OnlineUser onlineUser, @NotNull Position position) {
final Teleport teleport = new Teleport(onlineUser, position); final Teleport teleport = new Teleport(onlineUser, position);
return onlineUser.getPosition().thenApply(preTeleportPosition -> plugin.getDatabase() return onlineUser.getPosition().thenApply(preTeleportPosition -> plugin.getDatabase()
.setLastPosition(onlineUser, preTeleportPosition) // Update the player's last position .setLastPosition(onlineUser, preTeleportPosition) // Update the player's last position
@@ -213,7 +274,17 @@ public class TeleportManager {
}).join()).join()); }).join()).join());
} }
private CompletableFuture<TeleportResult> teleportCrossServer(OnlineUser onlineUser, Teleport teleport) { /**
* Handles a cross-server teleport, setting database parameters and dispatching a player across the network
*
* @param onlineUser the {@link OnlineUser} to teleport
* @param teleport the {@link Teleport} to carry out
* @return future completing when the teleport is complete with a {@link TeleportResult}.
* Successful cross-server teleports will return {@link TeleportResult#COMPLETED_CROSS_SERVER}.
* <p>Note that cross-server teleports will return with a {@link TeleportResult#FAILED_INVALID_SERVER} result if the
* target server is not online
*/
private CompletableFuture<TeleportResult> teleportCrossServer(@NotNull OnlineUser onlineUser, @NotNull Teleport teleport) {
assert plugin.getNetworkMessenger() != null; assert plugin.getNetworkMessenger() != null;
return plugin.getDatabase().setCurrentTeleport(teleport.player, teleport) return plugin.getDatabase().setCurrentTeleport(teleport.player, teleport)
.thenApply(ignored -> plugin.getNetworkMessenger().sendPlayer(onlineUser, teleport.target.server) .thenApply(ignored -> plugin.getNetworkMessenger().sendPlayer(onlineUser, teleport.target.server)
+4 -4
View File
@@ -113,10 +113,10 @@ error_warp_description_length: '[Error:](#ff3300) [Warp descriptions must be les
error_warp_description_characters: '[Error:](#ff3300) [Warp descriptions may only contain alphanumeric (A-Z, 0-9) characters, spaces, hyphens (-) and underscores (_)](#ff7e5e)' error_warp_description_characters: '[Error:](#ff3300) [Warp descriptions may only contain alphanumeric (A-Z, 0-9) characters, spaces, hyphens (-) and underscores (_)](#ff7e5e)'
error_no_permission: '[Error:](#ff3300) [You do not have permission to execute that command.](#ff7e5e)' error_no_permission: '[Error:](#ff3300) [You do not have permission to execute that command.](#ff7e5e)'
error_already_teleporting: '[Error:](#ff3300) [Please wait for your current teleport to finish!](#ff7e5e)' error_already_teleporting: '[Error:](#ff3300) [Please wait for the current teleport to finish!](#ff7e5e)'
error_no_last_position: '[Error:](#ff3300) [You have no last position to return to!](#ff7e5e)' error_no_last_position: '[Error:](#ff3300) [You have no last position to return to!](#ff7e5e)'
error_in_game_only: '[Error:](#ff3300) [That command can only be run in-game.](#ff7e5e)' error_in_game_only: '[Error:](#ff3300) [That command can only be run in-game.](#ff7e5e)'
error_invalid_server: '[Error:](#ff3300) [Failed to finish your teleportation as the target server could not be found or was offline.](#ff7e5e)' error_invalid_server: '[Error:](#ff3300) [Failed to finish teleportation as the target server could not be found or was offline.](#ff7e5e)'
error_home_invalid: '[Error:](#ff3300) [You do not have a home named "%1%"](#ff7e5e)' error_home_invalid: '[Error:](#ff3300) [You do not have a home named "%1%"](#ff7e5e)'
error_warp_invalid: '[Error:](#ff3300) [There is no warp set named "%1%"](#ff7e5e)' error_warp_invalid: '[Error:](#ff3300) [There is no warp set named "%1%"](#ff7e5e)'
error_invalid_syntax: '[Error:](#ff3300) [Incorrect syntax. Usage: %1%](#ff7e5e)' error_invalid_syntax: '[Error:](#ff3300) [Incorrect syntax. Usage: %1%](#ff7e5e)'
@@ -142,14 +142,14 @@ error_tp_invalid_world: '[Error:](#ff3300) [Please specify a valid server world.
error_rtp_randomization_timeout: '[Error:](#ff3300) [Failed to find a safe random location to teleport you.](#ff7e5e)' error_rtp_randomization_timeout: '[Error:](#ff3300) [Failed to find a safe random location to teleport you.](#ff7e5e)'
error_permission_restricted_warp: '[Error:](#ff3300) [You do not have permission to warp to %1%!](#ff7e5e)' error_permission_restricted_warp: '[Error:](#ff3300) [You do not have permission to warp to %1%!](#ff7e5e)'
error_invalid_page_number: '[Error:](#ff3300) [Please specify a valid page number.](#ff7e5e)' error_invalid_page_number: '[Error:](#ff3300) [Please specify a valid page number.](#ff7e5e)'
error_invalid_on_arrival: '[Error:](#ff3300) [Failed to finish your teleportation as the target world could not be found.](#ff7e5e)' error_invalid_on_arrival: '[Error:](#ff3300) [Failed to finish teleportation as the target world could not be found.](#ff7e5e)'
error_ignoring_teleport_requests: '[Error:](#ff3300) [You are currently ignoring teleport requests!](#ff7e5e) [(Click to toggle)](#ff7e5e show_text=&#ff7e5e&Click to listen for incoming teleport requests run_command=/huskhomes:tpignore)' error_ignoring_teleport_requests: '[Error:](#ff3300) [You are currently ignoring teleport requests!](#ff7e5e) [(Click to toggle)](#ff7e5e show_text=&#ff7e5e&Click to listen for incoming teleport requests run_command=/huskhomes:tpignore)'
error_no_offline_position: '[Error:](#ff3300) [Could not find where %1% last logged out.](#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_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_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_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)' error_edit_home_maximum_public_homes: '[Error:](#ff3300) [You can only make a maximum of %1% homes public.](#ff7e5e)'
error_illegal_target_coordinates: '[Error:](#ff3300) [Aborted teleport as it exceeds the possible game world limits.](#ff7e5e)' error_illegal_target_coordinates: '[Error:](#ff3300) [Cancelled teleport as it exceeds the possible game world limits.](#ff7e5e)'
error_no_other_online_players: '[Error:](#ff3300) [There are no other online players who could teleport to you.](#ff7e5e)' error_no_other_online_players: '[Error:](#ff3300) [There are no other online players who could teleport to you.](#ff7e5e)'
home_command_description: 'Teleport to one of your homes' home_command_description: 'Teleport to one of your homes'
Binary file not shown.

After

Width:  |  Height:  |  Size: 904 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 788 KiB