Add RequestManager, handle networked requests, new locales

This commit is contained in:
William committed 2022-08-18 22:08:21 +01:00
1 parent 6149ebe983
commit 11d05efcfa
13 files changed
+464 -43

No files matched your search

@@ -81,9 +81,10 @@ public class Message {
* Identifies the type of message or reply
*/
public enum MessageType {
TP_REQUEST,
TP_TO_POSITION_REQUEST,
POSITION_REQUEST
TELEPORT_TO_POSITION_REQUEST,
POSITION_REQUEST,
TELEPORT_REQUEST,
TELEPORT_REQUEST_RESPONSE
}
/**
@@ -2,6 +2,7 @@ package net.william278.huskhomes.messenger;
import com.google.gson.annotations.SerializedName;
import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.request.TeleportRequest;
import net.william278.huskhomes.teleport.TeleportResult;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -24,6 +25,13 @@ public class MessagePayload {
@SerializedName("teleport_result")
public TeleportResult teleportResult;
/**
* A teleport request field
*/
@Nullable
@SerializedName("teleport_request")
public TeleportRequest teleportRequest;
/**
* Returns an empty cross-server message payload
*
@@ -60,6 +68,19 @@ public class MessagePayload {
return payload;
}
/**
* Returns a payload containing a {@link TeleportRequest}
*
* @param teleportRequest the teleport to send
* @return a payload containing the teleport request
*/
@NotNull
public static MessagePayload withTeleportRequest(@NotNull TeleportRequest teleportRequest) {
final MessagePayload payload = new MessagePayload();
payload.teleportRequest = teleportRequest;
return payload;
}
private MessagePayload() {
}
@@ -5,9 +5,7 @@ import net.william278.huskhomes.player.OnlineUser;
import net.william278.huskhomes.position.Server;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.CompletableFuture;
public abstract class NetworkMessenger {
@@ -45,7 +43,7 @@ public abstract class NetworkMessenger {
/**
* The implementing HuskHomes plugin instance
*/
private HuskHomes plugin;
protected HuskHomes plugin;
/**
* Initialize the network messenger
@@ -66,6 +64,25 @@ public abstract class NetworkMessenger {
*/
public abstract CompletableFuture<String[]> getOnlinePlayerNames(@NotNull OnlineUser requester);
/**
* Find a player on the proxy network by username to validate if they exist.
*
* @param requester {@link OnlineUser} to send the request
* @param playerName Approximate name of the player to find. If an exact match is not found,
* this will attempt to return the closest match
* @return Future returning an {@link Optional} of the player's canonical username if found;
* otherwise an empty {@link Optional}
*/
public final CompletableFuture<Optional<String>> findPlayer(@NotNull OnlineUser requester, @NotNull String playerName) {
return getOnlinePlayerNames(requester).thenApply(networkedPlayers ->
Optional.ofNullable(Arrays.stream(networkedPlayers)
.filter(user -> user.equalsIgnoreCase(playerName))
.findFirst()
.orElse(Arrays.stream(networkedPlayers)
.filter(user -> user.toLowerCase().startsWith(playerName))
.findFirst().orElse(null))));
}
/**
* Fetch the name of this server on the proxy network
*
@@ -130,10 +147,7 @@ public abstract class NetworkMessenger {
return CompletableFuture.supplyAsync(() -> {
// Handle different message types and apply changes for reply message
switch (message.type) {
case TP_REQUEST -> {
//todo
}
case TP_TO_POSITION_REQUEST -> {
case TELEPORT_TO_POSITION_REQUEST -> {
if (message.payload.position != null) {
message.payload = MessagePayload.withTeleportResult(plugin.getTeleportManager()
.teleport(receiver, message.payload.position).join());
@@ -142,6 +156,20 @@ public abstract class NetworkMessenger {
}
}
case POSITION_REQUEST -> message.payload = MessagePayload.withPosition(receiver.getPosition().join());
case TELEPORT_REQUEST -> {
if (message.payload.teleportRequest != null) {
plugin.getRequestManager().sendLocalTeleportRequest(message.payload.teleportRequest, receiver);
} else {
message.payload = MessagePayload.empty();
}
}
case TELEPORT_REQUEST_RESPONSE -> {
if (message.payload.teleportRequest != null) {
plugin.getRequestManager().handleLocalRequestResponse(receiver, message.payload.teleportRequest);
} else {
message.payload = MessagePayload.empty();
}
}
}
// Prepare reply message