mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-01 14:38:53 +00:00
networking/refactor: Rename messages to requests, use builder class
This commit is contained in:
1 parent
10a3eebc55
commit
0c1ad832ea
16 files changed
+429
-342
No files matched your search
@@ -106,7 +106,7 @@ public class Cache {
|
||||
*/
|
||||
public CompletableFuture<List<String>> updatePlayerListCache(@NotNull HuskHomes plugin, @NotNull OnlineUser requester) {
|
||||
if (plugin.getSettings().crossServer) {
|
||||
return plugin.getNetworkMessenger().getOnlinePlayerNames(requester).thenApply(returnedPlayerList -> {
|
||||
return plugin.getMessenger().getOnlinePlayerNames(requester).thenApply(returnedPlayerList -> {
|
||||
players.clear();
|
||||
players.addAll(List.of(returnedPlayerList));
|
||||
return players;
|
||||
|
||||
@@ -11,7 +11,7 @@ import net.william278.huskhomes.event.EventDispatcher;
|
||||
import net.william278.huskhomes.hook.EconomyHook;
|
||||
import net.william278.huskhomes.hook.MapHook;
|
||||
import net.william278.huskhomes.hook.PluginHook;
|
||||
import net.william278.huskhomes.messenger.NetworkMessenger;
|
||||
import net.william278.huskhomes.network.Messenger;
|
||||
import net.william278.huskhomes.migrator.Migrator;
|
||||
import net.william278.huskhomes.player.OnlineUser;
|
||||
import net.william278.huskhomes.position.*;
|
||||
@@ -86,7 +86,7 @@ public interface HuskHomes {
|
||||
return CompletableFuture.completedFuture(Optional.of(localPlayer.get().username));
|
||||
}
|
||||
if (getSettings().crossServer) {
|
||||
return getNetworkMessenger().findPlayer(requester, targetPlayerName);
|
||||
return getMessenger().findPlayer(requester, targetPlayerName);
|
||||
}
|
||||
return CompletableFuture.completedFuture(Optional.empty());
|
||||
}
|
||||
@@ -139,12 +139,12 @@ public interface HuskHomes {
|
||||
SavedPositionManager getSavedPositionManager();
|
||||
|
||||
/**
|
||||
* The {@link NetworkMessenger} that sends cross-network messages
|
||||
* The {@link Messenger} that sends cross-network messages
|
||||
*
|
||||
* @return the {@link NetworkMessenger} implementation
|
||||
* @return the {@link Messenger} implementation
|
||||
*/
|
||||
@NotNull
|
||||
NetworkMessenger getNetworkMessenger() throws HuskHomesException;
|
||||
Messenger getMessenger() throws HuskHomesException;
|
||||
|
||||
/**
|
||||
* The {@link RandomTeleportEngine} that manages random teleports
|
||||
|
||||
@@ -1,112 +0,0 @@
|
||||
package net.william278.huskhomes.messenger;
|
||||
|
||||
import com.google.gson.*;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* A message sent cross-proxy to a player
|
||||
*/
|
||||
public class Message {
|
||||
|
||||
/**
|
||||
* A unique ID representing this message
|
||||
*/
|
||||
@NotNull
|
||||
public UUID uuid;
|
||||
|
||||
/**
|
||||
* The type of the message
|
||||
*/
|
||||
@NotNull
|
||||
public MessageType type;
|
||||
|
||||
/**
|
||||
* Represents the context under which this message is being relayed; an outbound {@link RelayType#MESSAGE} or a {@link RelayType#REPLY} to one.
|
||||
*/
|
||||
@SerializedName("relay_type")
|
||||
@NotNull
|
||||
public RelayType relayType;
|
||||
|
||||
/**
|
||||
* Username of the target player the message is bound for
|
||||
*/
|
||||
@SerializedName("target_player")
|
||||
@NotNull
|
||||
public String targetPlayer;
|
||||
|
||||
/**
|
||||
* Username of the sender of the message
|
||||
*/
|
||||
@NotNull
|
||||
public String sender;
|
||||
|
||||
/**
|
||||
* ID of the cluster this message is for
|
||||
*/
|
||||
@SerializedName("cluster_id")
|
||||
@NotNull
|
||||
public String clusterId;
|
||||
|
||||
/**
|
||||
* Data to send in the message
|
||||
*/
|
||||
@NotNull
|
||||
public MessagePayload payload;
|
||||
|
||||
public Message(@NotNull MessageType type, @NotNull String sender, @NotNull String targetPlayer,
|
||||
@NotNull MessagePayload payload, @NotNull Message.RelayType relayType, @NotNull String clusterId) {
|
||||
this.type = type;
|
||||
this.sender = sender;
|
||||
this.targetPlayer = targetPlayer;
|
||||
this.payload = payload;
|
||||
this.relayType = relayType;
|
||||
this.clusterId = clusterId;
|
||||
this.uuid = UUID.randomUUID();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String toJson() {
|
||||
return new GsonBuilder().create().toJson(this);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Message fromJson(@NotNull String json) {
|
||||
return new GsonBuilder().create().fromJson(json, Message.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Identifies the type of message or reply
|
||||
*/
|
||||
public enum MessageType {
|
||||
TELEPORT_TO_POSITION_REQUEST,
|
||||
POSITION_REQUEST,
|
||||
TELEPORT_REQUEST,
|
||||
TELEPORT_REQUEST_RESPONSE
|
||||
}
|
||||
|
||||
/**
|
||||
* Identifies the source of the message being relayed - a {@link RelayType#MESSAGE} or a {@link RelayType#REPLY} to a message.
|
||||
*/
|
||||
public enum RelayType {
|
||||
/**
|
||||
* An outbound message to/from a server
|
||||
*/
|
||||
MESSAGE,
|
||||
/**
|
||||
* A reply to an outbound message
|
||||
*/
|
||||
REPLY
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(@NotNull Object other) {
|
||||
if (other instanceof Message message) {
|
||||
return message.uuid.equals(this.uuid);
|
||||
}
|
||||
return super.equals(other);
|
||||
}
|
||||
|
||||
}
|
||||
+59
-75
@@ -1,4 +1,4 @@
|
||||
package net.william278.huskhomes.messenger;
|
||||
package net.william278.huskhomes.network;
|
||||
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.player.OnlineUser;
|
||||
@@ -11,7 +11,7 @@ import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public abstract class NetworkMessenger {
|
||||
public abstract class Messenger implements AutoCloseable {
|
||||
|
||||
/**
|
||||
* Name of the network message channel for HuskHomes messages
|
||||
@@ -21,7 +21,7 @@ public abstract class NetworkMessenger {
|
||||
/**
|
||||
* Map of message UUIDs to messages being processed
|
||||
*/
|
||||
protected HashMap<UUID, CompletableFuture<Message>> processingMessages;
|
||||
protected HashMap<UUID, CompletableFuture<Request>> processingMessages;
|
||||
|
||||
/**
|
||||
* List of pending futures for processing {@link #fetchServerName(OnlineUser)} requests
|
||||
@@ -123,54 +123,54 @@ public abstract class NetworkMessenger {
|
||||
* Send a network message
|
||||
*
|
||||
* @param sender {@link OnlineUser} sending the message
|
||||
* @param message {@link Message} to send
|
||||
* @return Future returning the {@link Message} sent, that will time out after
|
||||
* @apiNote This method invokes {@link #dispatchMessage(OnlineUser, Message)}, applying a time-to-live timeOut
|
||||
* @param request {@link Request} to send
|
||||
* @return Future returning the {@link Request} sent, that will time out after
|
||||
* @apiNote This method invokes {@link #dispatchMessage(OnlineUser, Request)}, applying a time-to-live timeOut
|
||||
*/
|
||||
public final CompletableFuture<Optional<Message>> sendMessage(@NotNull OnlineUser sender, @NotNull Message message) {
|
||||
return dispatchMessage(sender, message)
|
||||
protected final CompletableFuture<Optional<Request>> sendMessage(@NotNull OnlineUser sender, @NotNull Request request) {
|
||||
return dispatchMessage(sender, request)
|
||||
.orTimeout(MESSAGE_TIME_OUT, TimeUnit.SECONDS)
|
||||
.exceptionally(e -> {
|
||||
plugin.getLoggingAdapter().log(Level.WARNING, "Message dispatch after " + MESSAGE_TIME_OUT + " seconds", e);
|
||||
return null;
|
||||
})
|
||||
.thenApply(result -> {
|
||||
processingMessages.remove(message.uuid);
|
||||
processingMessages.remove(request.getUuid());
|
||||
return Optional.ofNullable(result);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispatch a {@link Message} via the appropriate handler
|
||||
* Dispatch a {@link Request} via the appropriate handler
|
||||
*
|
||||
* @param sender The {@link OnlineUser} that sent the {@link Message}
|
||||
* @param message The {@link Message} to dispatch
|
||||
* @return A future containing a reply to the {@link Message} that was sent
|
||||
* @param sender The {@link OnlineUser} that sent the {@link Request}
|
||||
* @param request The {@link Request} to dispatch
|
||||
* @return A future containing a reply to the {@link Request} that was sent
|
||||
*/
|
||||
protected abstract CompletableFuture<Message> dispatchMessage(@NotNull OnlineUser sender, @NotNull Message message);
|
||||
protected abstract CompletableFuture<Request> dispatchMessage(@NotNull OnlineUser sender, @NotNull Request request);
|
||||
|
||||
/**
|
||||
* Send a reply to a received {@link Message}
|
||||
* Send a reply to a received {@link Request}
|
||||
*
|
||||
* @param reply The reply {@link Message} to send
|
||||
* @param reply The reply {@link Request} to send
|
||||
*/
|
||||
protected abstract void sendReply(@NotNull OnlineUser replier, @NotNull Message reply);
|
||||
protected abstract void sendReply(@NotNull OnlineUser replier, @NotNull Request reply);
|
||||
|
||||
/**
|
||||
* Handle and action received network {@link Message}s
|
||||
* Handle and action received network {@link Request}s
|
||||
*
|
||||
* @param receiver The online {@link OnlineUser} receiving the message
|
||||
* @param message The received {@link Message}
|
||||
* @param request The received {@link Request}
|
||||
*/
|
||||
protected final void handleMessage(@NotNull OnlineUser receiver, @NotNull Message message) {
|
||||
switch (message.relayType) {
|
||||
protected final void handleMessage(@NotNull OnlineUser receiver, @NotNull Request request) {
|
||||
switch (request.getRelayType()) {
|
||||
// Handle a message and send reply
|
||||
case MESSAGE -> prepareReply(receiver, message).thenAccept(reply -> sendReply(receiver, message));
|
||||
case MESSAGE -> handleRequest(receiver, request);
|
||||
// Handle a reply message
|
||||
case REPLY -> {
|
||||
if (processingMessages.containsKey(message.uuid)) {
|
||||
final Message finalMessage = message;
|
||||
processingMessages.get(message.uuid).completeAsync(() -> finalMessage);
|
||||
if (processingMessages.containsKey(request.getUuid())) {
|
||||
final Request finalRequest = request;
|
||||
processingMessages.get(request.getUuid()).completeAsync(() -> finalRequest);
|
||||
return;
|
||||
}
|
||||
plugin.getLoggingAdapter().log(Level.WARNING, "Received a reply to a message that was not sent by this server");
|
||||
@@ -183,63 +183,47 @@ public abstract class NetworkMessenger {
|
||||
* to the sender
|
||||
*
|
||||
* @param receiver The {@link OnlineUser} receiving the message
|
||||
* @param message The received {@link Message}
|
||||
* @return A future containing the reply {@link Message}
|
||||
* @param request The received {@link Request}
|
||||
*/
|
||||
private CompletableFuture<Message> prepareReply(@NotNull final OnlineUser receiver, @NotNull final Message message) {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
switch (message.type) {
|
||||
case TELEPORT_TO_POSITION_REQUEST -> {
|
||||
if (message.payload.position != null) {
|
||||
message.payload = MessagePayload.withTeleportResult(Teleport.builder(plugin, receiver)
|
||||
.setTarget(message.payload.position)
|
||||
.toTeleport().join().execute().join().getState());
|
||||
} else {
|
||||
message.payload = MessagePayload.empty();
|
||||
}
|
||||
}
|
||||
case POSITION_REQUEST -> message.payload = MessagePayload.withPosition(receiver.getPosition());
|
||||
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();
|
||||
}
|
||||
private void handleRequest(@NotNull OnlineUser receiver, @NotNull Request request) {
|
||||
switch (request.getType()) {
|
||||
case TELEPORT_TO_POSITION_REQUEST -> {
|
||||
if (request.getPayload().position != null) {
|
||||
Teleport.builder(plugin, receiver)
|
||||
.setTarget(request.getPayload().position).toTeleport()
|
||||
.thenAccept(teleport -> teleport.execute()
|
||||
.thenAccept(result -> request.reply(receiver,
|
||||
Payload.withTeleportResult(result.getState()), plugin)));
|
||||
return;
|
||||
}
|
||||
request.reply(receiver, Payload.empty(), plugin);
|
||||
}
|
||||
return message;
|
||||
}).exceptionally(exception -> {
|
||||
plugin.getLoggingAdapter().log(Level.WARNING, "Failed to prepare message reply", exception);
|
||||
message.payload = MessagePayload.empty();
|
||||
return message;
|
||||
}).thenApply(reply -> {
|
||||
formatReplyMessage(message, receiver);
|
||||
return reply;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a {@link Message} to be sent as a reply to a received {@link Message}
|
||||
*
|
||||
* @param message The received {@link Message}
|
||||
* @param receiver The {@link OnlineUser} receiving the message
|
||||
*/
|
||||
private void formatReplyMessage(@NotNull Message message, @NotNull OnlineUser receiver) {
|
||||
message.targetPlayer = message.sender;
|
||||
message.sender = receiver.username;
|
||||
message.relayType = Message.RelayType.REPLY;
|
||||
case POSITION_REQUEST -> request.reply(receiver, Payload.withPosition(receiver.getPosition()), plugin);
|
||||
case TELEPORT_REQUEST -> {
|
||||
if (request.getPayload().teleportRequest != null) {
|
||||
request.reply(receiver, plugin.getRequestManager()
|
||||
.sendLocalTeleportRequest(request.getPayload().teleportRequest, receiver)
|
||||
.map(Payload::withTeleportRequest)
|
||||
.orElse(Payload.empty()), plugin);
|
||||
return;
|
||||
}
|
||||
request.reply(receiver, Payload.empty(), plugin);
|
||||
}
|
||||
case TELEPORT_REQUEST_RESPONSE -> {
|
||||
if (request.getPayload().teleportRequest != null) {
|
||||
plugin.getRequestManager().handleLocalRequestResponse(receiver, request.getPayload().teleportRequest);
|
||||
request.reply(receiver, plugin);
|
||||
return;
|
||||
}
|
||||
request.reply(receiver, Payload.empty(), plugin);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the network messenger
|
||||
*/
|
||||
public abstract void terminate();
|
||||
@Override
|
||||
public abstract void close();
|
||||
|
||||
}
|
||||
+12
-12
@@ -1,4 +1,4 @@
|
||||
package net.william278.huskhomes.messenger;
|
||||
package net.william278.huskhomes.network;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import net.william278.huskhomes.position.Position;
|
||||
@@ -8,9 +8,9 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* Represents a payload sent in a cross-server {@link Message}
|
||||
* Represents a payload sent in a cross-server {@link Request}
|
||||
*/
|
||||
public class MessagePayload {
|
||||
public class Payload {
|
||||
|
||||
/**
|
||||
* A position field
|
||||
@@ -38,8 +38,8 @@ public class MessagePayload {
|
||||
* @return an empty payload
|
||||
*/
|
||||
@NotNull
|
||||
public static MessagePayload empty() {
|
||||
return new MessagePayload();
|
||||
public static Payload empty() {
|
||||
return new Payload();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -49,8 +49,8 @@ public class MessagePayload {
|
||||
* @return a payload containing the position
|
||||
*/
|
||||
@NotNull
|
||||
public static MessagePayload withPosition(@NotNull Position position) {
|
||||
final MessagePayload payload = new MessagePayload();
|
||||
public static Payload withPosition(@NotNull Position position) {
|
||||
final Payload payload = new Payload();
|
||||
payload.position = position;
|
||||
return payload;
|
||||
}
|
||||
@@ -62,8 +62,8 @@ public class MessagePayload {
|
||||
* @return a payload containing the teleport result
|
||||
*/
|
||||
@NotNull
|
||||
public static MessagePayload withTeleportResult(@NotNull TeleportResult resultState) {
|
||||
final MessagePayload payload = new MessagePayload();
|
||||
public static Payload withTeleportResult(@NotNull TeleportResult resultState) {
|
||||
final Payload payload = new Payload();
|
||||
payload.resultState = resultState;
|
||||
return payload;
|
||||
}
|
||||
@@ -75,13 +75,13 @@ public class MessagePayload {
|
||||
* @return a payload containing the teleport request
|
||||
*/
|
||||
@NotNull
|
||||
public static MessagePayload withTeleportRequest(@NotNull TeleportRequest teleportRequest) {
|
||||
final MessagePayload payload = new MessagePayload();
|
||||
public static Payload withTeleportRequest(@NotNull TeleportRequest teleportRequest) {
|
||||
final Payload payload = new Payload();
|
||||
payload.teleportRequest = teleportRequest;
|
||||
return payload;
|
||||
}
|
||||
|
||||
private MessagePayload() {
|
||||
private Payload() {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,185 @@
|
||||
package net.william278.huskhomes.network;
|
||||
|
||||
import com.google.gson.*;
|
||||
import com.google.gson.annotations.Expose;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.player.OnlineUser;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
/**
|
||||
* A message sent cross-proxy to a player
|
||||
*/
|
||||
public class Request {
|
||||
|
||||
/**
|
||||
* A unique ID representing this message
|
||||
*/
|
||||
@NotNull
|
||||
@Expose
|
||||
private UUID uuid;
|
||||
|
||||
/**
|
||||
* The type of the message
|
||||
*/
|
||||
@NotNull
|
||||
@Expose
|
||||
private MessageType type;
|
||||
|
||||
/**
|
||||
* Represents the context under which this message is being relayed; an outbound {@link RelayType#MESSAGE} or a {@link RelayType#REPLY} to one.
|
||||
*/
|
||||
@SerializedName("relay_type")
|
||||
@NotNull
|
||||
@Expose
|
||||
private RelayType relayType;
|
||||
|
||||
/**
|
||||
* Username of the target player the message is bound for
|
||||
*/
|
||||
@SerializedName("target_player")
|
||||
@NotNull
|
||||
@Expose
|
||||
private String targetPlayer;
|
||||
|
||||
/**
|
||||
* ID of the cluster this message is for
|
||||
*/
|
||||
@SerializedName("cluster_id")
|
||||
@NotNull
|
||||
@Expose
|
||||
private String clusterId;
|
||||
|
||||
/**
|
||||
* Data to send in the message
|
||||
*/
|
||||
@NotNull
|
||||
@Expose
|
||||
private Payload payload;
|
||||
|
||||
/**
|
||||
* Username of the sender of the message
|
||||
*/
|
||||
@NotNull
|
||||
@Expose
|
||||
private String sender;
|
||||
|
||||
protected Request(@NotNull MessageType type, @NotNull String targetPlayer, @NotNull Payload payload,
|
||||
@NotNull Request.RelayType relayType) {
|
||||
this.type = type;
|
||||
this.targetPlayer = targetPlayer;
|
||||
this.payload = payload;
|
||||
this.relayType = relayType;
|
||||
this.uuid = UUID.randomUUID();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private Request() {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static RequestBuilder builder() {
|
||||
return new RequestBuilder();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String toJson() {
|
||||
return new GsonBuilder().create().toJson(this);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Request fromJson(@NotNull String json) {
|
||||
return new GsonBuilder().create().fromJson(json, Request.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Identifies the type of message or reply
|
||||
*/
|
||||
public enum MessageType {
|
||||
TELEPORT_TO_POSITION_REQUEST,
|
||||
POSITION_REQUEST,
|
||||
TELEPORT_REQUEST,
|
||||
TELEPORT_REQUEST_RESPONSE
|
||||
}
|
||||
|
||||
/**
|
||||
* Identifies the source of the message being relayed - a {@link RelayType#MESSAGE} or a {@link RelayType#REPLY} to a message.
|
||||
*/
|
||||
public enum RelayType {
|
||||
/**
|
||||
* An outbound message to/from a server
|
||||
*/
|
||||
MESSAGE,
|
||||
/**
|
||||
* A reply to an outbound message
|
||||
*/
|
||||
REPLY
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public UUID getUuid() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public MessageType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public RelayType getRelayType() {
|
||||
return relayType;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getTargetPlayer() {
|
||||
return targetPlayer;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getSender() {
|
||||
return sender;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getClusterId() {
|
||||
return clusterId;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Payload getPayload() {
|
||||
return payload;
|
||||
}
|
||||
|
||||
public CompletableFuture<Optional<Request>> send(@NotNull OnlineUser sender, @NotNull HuskHomes plugin) {
|
||||
this.sender = sender.username;
|
||||
this.clusterId = plugin.getSettings().clusterId;
|
||||
return plugin.getMessenger().sendMessage(sender, this);
|
||||
}
|
||||
|
||||
public void reply(@NotNull OnlineUser sender, @NotNull Payload payload, @NotNull HuskHomes plugin) {
|
||||
this.targetPlayer = this.sender;
|
||||
this.sender = sender.username;
|
||||
this.clusterId = plugin.getSettings().clusterId;
|
||||
this.payload = payload;
|
||||
this.relayType = RelayType.REPLY;
|
||||
plugin.getMessenger().sendReply(sender, this);
|
||||
}
|
||||
|
||||
public void reply(@NotNull OnlineUser sender, @NotNull HuskHomes plugin) {
|
||||
this.reply(sender, this.payload, plugin);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(@NotNull Object other) {
|
||||
if (other instanceof Request request) {
|
||||
return request.uuid.equals(this.uuid);
|
||||
}
|
||||
return super.equals(other);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package net.william278.huskhomes.network;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class RequestBuilder {
|
||||
|
||||
private Request.MessageType type;
|
||||
|
||||
private String targetPlayer;
|
||||
|
||||
@NotNull
|
||||
private Payload payload = Payload.empty();
|
||||
|
||||
protected RequestBuilder() {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public RequestBuilder withType(Request.MessageType type) {
|
||||
this.type = type;
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public RequestBuilder withTargetPlayer(String targetPlayer) {
|
||||
this.targetPlayer = targetPlayer;
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public RequestBuilder withPayload(Payload payload) {
|
||||
this.payload = payload;
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Request build() {
|
||||
return new Request(type, targetPlayer, payload, Request.RelayType.MESSAGE);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
package net.william278.huskhomes.redis;
|
||||
|
||||
import net.william278.huskhomes.config.Settings;
|
||||
import net.william278.huskhomes.messenger.Message;
|
||||
import net.william278.huskhomes.messenger.NetworkMessenger;
|
||||
import net.william278.huskhomes.network.Request;
|
||||
import net.william278.huskhomes.network.Messenger;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisPool;
|
||||
@@ -43,10 +43,10 @@ public class RedisWorker {
|
||||
}
|
||||
}
|
||||
|
||||
public CompletableFuture<Void> sendMessage(@NotNull Message message) {
|
||||
public CompletableFuture<Void> sendMessage(@NotNull Request request) {
|
||||
return CompletableFuture.runAsync(() -> {
|
||||
try (Jedis jedis = jedisPool.getResource()) {
|
||||
jedis.publish(NetworkMessenger.NETWORK_MESSAGE_CHANNEL, message.toJson());
|
||||
jedis.publish(Messenger.NETWORK_MESSAGE_CHANNEL, request.toJson());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package net.william278.huskhomes.request;
|
||||
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.messenger.Message;
|
||||
import net.william278.huskhomes.messenger.MessagePayload;
|
||||
import net.william278.huskhomes.network.Request;
|
||||
import net.william278.huskhomes.network.Payload;
|
||||
import net.william278.huskhomes.player.OnlineUser;
|
||||
import net.william278.huskhomes.player.User;
|
||||
import net.william278.huskhomes.teleport.Teleport;
|
||||
@@ -140,23 +140,21 @@ public class RequestManager {
|
||||
// If the player couldn't be found locally, send the request cross-server
|
||||
if (plugin.getSettings().crossServer) {
|
||||
// Find the matching networked target
|
||||
return plugin.getNetworkMessenger().findPlayer(requester, targetUser).thenApplyAsync(networkedTarget -> {
|
||||
return plugin.getMessenger().findPlayer(requester, targetUser).thenApplyAsync(networkedTarget -> {
|
||||
if (networkedTarget.isEmpty()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
// Use the network messenger to send the request
|
||||
request.recipientName = networkedTarget.get();
|
||||
return plugin.getNetworkMessenger()
|
||||
.sendMessage(requester, new Message(Message.MessageType.TELEPORT_REQUEST,
|
||||
requester.username,
|
||||
networkedTarget.get(),
|
||||
MessagePayload.withTeleportRequest(request),
|
||||
Message.RelayType.MESSAGE,
|
||||
plugin.getSettings().clusterId))
|
||||
return Request.builder()
|
||||
.withType(Request.MessageType.TELEPORT_REQUEST)
|
||||
.withPayload(Payload.withTeleportRequest(request))
|
||||
.withTargetPlayer(networkedTarget.get())
|
||||
.build().send(requester, plugin)
|
||||
.thenApply(reply -> {
|
||||
if (reply.isPresent()) {
|
||||
final TeleportRequest result = reply.get().payload.teleportRequest;
|
||||
final TeleportRequest result = reply.get().getPayload().teleportRequest;
|
||||
if (result == null || result.status == TeleportRequest.RequestStatus.IGNORED) {
|
||||
return null;
|
||||
}
|
||||
@@ -196,7 +194,7 @@ public class RequestManager {
|
||||
// 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.requesterName)
|
||||
+ "_request_received", request.requesterName)
|
||||
.ifPresent(recipient::sendMessage);
|
||||
plugin.getLocales().getLocale("teleport_request_buttons", request.requesterName)
|
||||
.ifPresent(recipient::sendMessage);
|
||||
@@ -283,7 +281,7 @@ public class RequestManager {
|
||||
handleLocalRequestResponse(localRequester.get(), request);
|
||||
} else if (plugin.getSettings().crossServer) {
|
||||
// Ensure the sender is still online
|
||||
plugin.getNetworkMessenger()
|
||||
plugin.getMessenger()
|
||||
.findPlayer(recipient, request.requesterName)
|
||||
.thenApplyAsync(networkedTarget -> {
|
||||
if (networkedTarget.isEmpty()) {
|
||||
@@ -292,13 +290,11 @@ public class RequestManager {
|
||||
return false;
|
||||
}
|
||||
|
||||
return plugin.getNetworkMessenger()
|
||||
.sendMessage(recipient, new Message(Message.MessageType.TELEPORT_REQUEST_RESPONSE,
|
||||
recipient.username,
|
||||
networkedTarget.get(),
|
||||
MessagePayload.withTeleportRequest(request),
|
||||
Message.RelayType.MESSAGE,
|
||||
plugin.getSettings().clusterId))
|
||||
return Request.builder()
|
||||
.withType(Request.MessageType.TELEPORT_REQUEST_RESPONSE)
|
||||
.withPayload(Payload.withTeleportRequest(request))
|
||||
.withTargetPlayer(networkedTarget.get())
|
||||
.build().send(recipient, plugin)
|
||||
.thenApply(Optional::isPresent)
|
||||
.join();
|
||||
})
|
||||
|
||||
@@ -2,8 +2,8 @@ package net.william278.huskhomes.teleport;
|
||||
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.config.Settings;
|
||||
import net.william278.huskhomes.messenger.Message;
|
||||
import net.william278.huskhomes.messenger.MessagePayload;
|
||||
import net.william278.huskhomes.network.Request;
|
||||
import net.william278.huskhomes.network.Payload;
|
||||
import net.william278.huskhomes.player.OnlineUser;
|
||||
import net.william278.huskhomes.player.User;
|
||||
import net.william278.huskhomes.position.Position;
|
||||
@@ -187,7 +187,7 @@ public class Teleport {
|
||||
plugin.getDatabase()
|
||||
.setCurrentTeleport(teleporter, this)
|
||||
.thenApply(ignored -> plugin
|
||||
.getNetworkMessenger()
|
||||
.getMessenger()
|
||||
.sendPlayer(teleporter, target.server)
|
||||
.thenApply(completed -> completed
|
||||
? TeleportResult.COMPLETED_CROSS_SERVER
|
||||
@@ -215,19 +215,16 @@ public class Teleport {
|
||||
assert teleporter != null;
|
||||
|
||||
// Send a network message to a user on another server to teleport them to the target position
|
||||
return plugin.getNetworkMessenger()
|
||||
.sendMessage(executor,
|
||||
new Message(Message.MessageType.TELEPORT_TO_POSITION_REQUEST,
|
||||
executor.username,
|
||||
teleporter.username,
|
||||
MessagePayload.withPosition(target),
|
||||
Message.RelayType.MESSAGE,
|
||||
plugin.getSettings().clusterId))
|
||||
return Request.builder()
|
||||
.withType(Request.MessageType.TELEPORT_TO_POSITION_REQUEST)
|
||||
.withPayload(Payload.withPosition(target))
|
||||
.withTargetPlayer(teleporter.username)
|
||||
.build().send(executor, plugin)
|
||||
.thenApply(result -> {
|
||||
if (result.isPresent()) {
|
||||
final Message reply = result.get();
|
||||
if (reply.payload.resultState != null) {
|
||||
return reply.payload.resultState;
|
||||
final Request reply = result.get();
|
||||
if (reply.getPayload().resultState != null) {
|
||||
return reply.getPayload().resultState;
|
||||
}
|
||||
}
|
||||
return TeleportResult.FAILED_TELEPORTER_NOT_RESOLVED;
|
||||
|
||||
@@ -2,8 +2,7 @@ package net.william278.huskhomes.teleport;
|
||||
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.config.Settings;
|
||||
import net.william278.huskhomes.messenger.Message;
|
||||
import net.william278.huskhomes.messenger.MessagePayload;
|
||||
import net.william278.huskhomes.network.Request;
|
||||
import net.william278.huskhomes.player.OnlineUser;
|
||||
import net.william278.huskhomes.player.User;
|
||||
import net.william278.huskhomes.position.Location;
|
||||
@@ -91,7 +90,7 @@ public class TeleportBuilder {
|
||||
.map(onlineUser -> (User) onlineUser)
|
||||
.or(() -> {
|
||||
if (plugin.getSettings().crossServer) {
|
||||
return plugin.getNetworkMessenger()
|
||||
return plugin.getMessenger()
|
||||
.findPlayer(executor, teleporterUsername).join()
|
||||
.map(username -> new User(UUID.randomUUID(), username));
|
||||
}
|
||||
@@ -234,17 +233,17 @@ public class TeleportBuilder {
|
||||
return CompletableFuture.supplyAsync(() -> Optional.of(localPlayer.get().getPosition()));
|
||||
}
|
||||
if (plugin.getSettings().crossServer) {
|
||||
return plugin.getNetworkMessenger()
|
||||
return plugin.getMessenger()
|
||||
.findPlayer(executor, playerName)
|
||||
.thenApplyAsync(foundPlayer -> {
|
||||
if (foundPlayer.isEmpty()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return plugin.getNetworkMessenger()
|
||||
.sendMessage(executor, new Message(Message.MessageType.POSITION_REQUEST,
|
||||
executor.username, playerName, MessagePayload.empty(),
|
||||
Message.RelayType.MESSAGE, plugin.getSettings().clusterId))
|
||||
.thenApply(reply -> reply.map(message -> message.payload.position)).join();
|
||||
return Request.builder()
|
||||
.withType(Request.MessageType.POSITION_REQUEST)
|
||||
.withTargetPlayer(playerName)
|
||||
.build().send(executor, plugin)
|
||||
.thenApply(reply -> reply.map(message -> message.getPayload().position)).join();
|
||||
});
|
||||
}
|
||||
return CompletableFuture.supplyAsync(Optional::empty);
|
||||
|
||||
Reference in new issue
Block a user