fetchOnlineServerList(@NotNull OnlineUser requester);
-
- /**
- * Send a {@link OnlineUser} to a target {@link Server} on the proxy network
- *
- * @param server The {@link Server} to send the {@link OnlineUser} to
- * @return A future, returning {@code true} if the player was sent.
- * If the player could not be sent (i.e. the server was offline), this will return {@code false}
- */
- public abstract CompletableFuture sendPlayer(@NotNull OnlineUser onlineUser, @NotNull Server server);
-
- /**
- * Send a network message
- *
- * @param sender {@link OnlineUser} sending the message
- * @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
- */
- protected final CompletableFuture> 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(request.getUuid());
- return Optional.ofNullable(result);
- });
- }
-
- /**
- * Dispatch a {@link Request} via the appropriate handler
- *
- * @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 dispatchMessage(@NotNull OnlineUser sender, @NotNull Request request);
-
- /**
- * Send a reply to a received {@link Request}
- *
- * @param reply The reply {@link Request} to send
- */
- protected abstract void sendReply(@NotNull OnlineUser replier, @NotNull Request reply);
-
- /**
- * Handle and action received network {@link Request}s
- *
- * @param receiver The online {@link OnlineUser} receiving the message
- * @param request The received {@link Request}
- */
- protected final void handleMessage(@NotNull OnlineUser receiver, @NotNull Request request) {
- switch (request.getRelayType()) {
- // Handle a message and send reply
- case MESSAGE -> handleRequest(receiver, request);
- // Handle a reply message
- case REPLY -> {
- 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");
- }
- }
- }
-
- /**
- * Handle different message types, executed needed operations and dispatches a reply message
- * to the sender
- *
- * @param receiver The {@link OnlineUser} receiving the message
- * @param request The received {@link Request}
- */
- 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);
- }
- 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
- */
- @Override
- public abstract void close();
-
-}
\ No newline at end of file
diff --git a/common/src/main/java/net/william278/huskhomes/network/Payload.java b/common/src/main/java/net/william278/huskhomes/network/Payload.java
index 85f01ed8..7e67bde7 100644
--- a/common/src/main/java/net/william278/huskhomes/network/Payload.java
+++ b/common/src/main/java/net/william278/huskhomes/network/Payload.java
@@ -12,25 +12,16 @@ import org.jetbrains.annotations.Nullable;
*/
public class Payload {
- /**
- * A position field
- */
@Nullable
- public Position position;
+ private Position position;
- /**
- * A teleport result field
- */
@Nullable
@SerializedName("teleport_result")
- public TeleportResult resultState;
+ private TeleportResult resultState;
- /**
- * A teleport request field
- */
@Nullable
@SerializedName("teleport_request")
- public TeleportRequest teleportRequest;
+ private TeleportRequest teleportRequest;
/**
* Returns an empty cross-server message payload
@@ -84,4 +75,27 @@ public class Payload {
private Payload() {
}
+ /**
+ * A position field
+ */
+ @Nullable
+ public Position getPosition() {
+ return position;
+ }
+
+ /**
+ * A teleport result field
+ */
+ @Nullable
+ public TeleportResult getResultState() {
+ return resultState;
+ }
+
+ /**
+ * A teleport request field
+ */
+ @Nullable
+ public TeleportRequest getTeleportRequest() {
+ return teleportRequest;
+ }
}
diff --git a/common/src/main/java/net/william278/huskhomes/network/PluginMessageBroker.java b/common/src/main/java/net/william278/huskhomes/network/PluginMessageBroker.java
new file mode 100644
index 00000000..d1babbba
--- /dev/null
+++ b/common/src/main/java/net/william278/huskhomes/network/PluginMessageBroker.java
@@ -0,0 +1,101 @@
+package net.william278.huskhomes.network;
+
+import com.google.common.io.ByteArrayDataInput;
+import com.google.common.io.ByteArrayDataOutput;
+import com.google.common.io.ByteStreams;
+import net.william278.huskhomes.HuskHomes;
+import net.william278.huskhomes.user.OnlineUser;
+import org.jetbrains.annotations.NotNull;
+
+import java.io.*;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.logging.Level;
+
+/**
+ * Plugin Messaging channel message
+ * broker implementation
+ */
+public class PluginMessageBroker extends Broker {
+
+ /**
+ * The name of BungeeCord's provided plugin channel.
+ *
+ * @implNote Technically, the effective identifier of this channel is {@code bungeecord:main}, but Spigot remaps
+ * {@code BungeeCord} automatically to the new one (source).
+ * Spigot's official documentation
+ * still instructs usage of {@code BungeeCord} as the name to use, however. It's all a bit inconsistent, so just in case
+ * it's best to leave it how it is for to maintain backwards compatibility.
+ */
+ public static final String BUNGEE_CHANNEL_ID = "BungeeCord";
+
+ public PluginMessageBroker(@NotNull HuskHomes plugin) {
+ super(plugin);
+ }
+
+ @Override
+ public void initialize() throws RuntimeException {
+ plugin.initializePluginChannels();
+ }
+
+ @SuppressWarnings("UnstableApiUsage")
+ public final void onReceive(@NotNull String channel, @NotNull OnlineUser user, byte[] message) {
+ if (!channel.equals(BUNGEE_CHANNEL_ID)) {
+ return;
+ }
+
+ final ByteArrayDataInput inputStream = ByteStreams.newDataInput(message);
+ final String subChannelId = inputStream.readUTF();
+ if (!subChannelId.equals(getSubChannelId())) {
+ return;
+ }
+
+ short messageLength = inputStream.readShort();
+ byte[] messageBody = new byte[messageLength];
+ inputStream.readFully(messageBody);
+
+ try (final DataInputStream messageReader = new DataInputStream(new ByteArrayInputStream(messageBody))) {
+ super.handle(user, plugin.getGson().fromJson(messageReader.readUTF(), Message.class));
+ } catch (IOException e) {
+ plugin.getLoggingAdapter().log(Level.SEVERE, "Failed to fully read plugin message", e);
+ }
+ }
+
+ @Override
+ @SuppressWarnings("UnstableApiUsage")
+ protected void send(@NotNull Message message, @NotNull OnlineUser sender) {
+ final ByteArrayDataOutput messageWriter = ByteStreams.newDataOutput();
+ messageWriter.writeUTF("ForwardToPlayer");
+ messageWriter.writeUTF(message.getTarget());
+ messageWriter.writeUTF(getSubChannelId());
+
+ // Write the plugin message
+ try (final ByteArrayOutputStream messageByteStream = new ByteArrayOutputStream()) {
+ try (DataOutputStream messageDataStream = new DataOutputStream(messageByteStream)) {
+ messageDataStream.writeUTF(plugin.getGson().toJson(message));
+ messageWriter.writeShort(messageByteStream.toByteArray().length);
+ messageWriter.write(messageByteStream.toByteArray());
+ }
+ } catch (IOException e) {
+ plugin.getLoggingAdapter().log(Level.SEVERE, "Exception dispatching plugin message", e);
+ return;
+ }
+
+ sender.sendPluginMessage(BUNGEE_CHANNEL_ID, messageWriter.toByteArray());
+ }
+
+ @Override
+ @SuppressWarnings("UnstableApiUsage")
+ public void changeServer(@NotNull OnlineUser user, @NotNull String server) {
+ final ByteArrayDataOutput outputStream = ByteStreams.newDataOutput();
+
+ outputStream.writeUTF("Connect");
+ outputStream.writeUTF(server);
+
+ user.sendPluginMessage(BUNGEE_CHANNEL_ID, outputStream.toByteArray());
+ }
+
+ @Override
+ public void close() {
+ }
+}
\ No newline at end of file
diff --git a/common/src/main/java/net/william278/huskhomes/network/RedisBroker.java b/common/src/main/java/net/william278/huskhomes/network/RedisBroker.java
new file mode 100644
index 00000000..735f4be6
--- /dev/null
+++ b/common/src/main/java/net/william278/huskhomes/network/RedisBroker.java
@@ -0,0 +1,77 @@
+package net.william278.huskhomes.network;
+
+import net.william278.huskhomes.HuskHomes;
+import net.william278.huskhomes.user.OnlineUser;
+import org.jetbrains.annotations.NotNull;
+import redis.clients.jedis.Jedis;
+import redis.clients.jedis.JedisPool;
+import redis.clients.jedis.JedisPoolConfig;
+import redis.clients.jedis.JedisPubSub;
+
+import java.util.logging.Level;
+
+/**
+ * Redis message broker implementation
+ */
+public class RedisBroker extends PluginMessageBroker {
+ private JedisPool jedisPool;
+
+ public RedisBroker(@NotNull HuskHomes plugin) {
+ super(plugin);
+ }
+
+ @Override
+ public void initialize() throws RuntimeException {
+ super.initialize();
+
+ final String password = plugin.getSettings().getRedisPassword();
+ final String host = plugin.getSettings().getRedisHost();
+ final int port = plugin.getSettings().getRedisPort();
+ final boolean useSSL = plugin.getSettings().useRedisSsl();
+
+ this.jedisPool = password.isEmpty() ? new JedisPool(new JedisPoolConfig(), host, port, 0, useSSL)
+ : new JedisPool(new JedisPoolConfig(), host, port, 0, password, useSSL);
+
+ new Thread(getSubscriber(), plugin.getKey("redis_subscriber").toString()).start();
+
+ plugin.getLoggingAdapter().log(Level.INFO, "Initialized Redis connection pool");
+ }
+
+ @NotNull
+ private Runnable getSubscriber() {
+ return () -> {
+ try (Jedis jedis = jedisPool.getResource()) {
+ jedis.subscribe(new JedisPubSub() {
+ @Override
+ public void onMessage(@NotNull String channel, @NotNull String encodedMessage) {
+ if (!channel.equals(getSubChannelId())) {
+ return;
+ }
+
+ final Message message = plugin.getGson().fromJson(encodedMessage, Message.class);
+ plugin.getOnlineUsers().stream()
+ .filter(online -> online.getUsername().equalsIgnoreCase(message.getTarget()))
+ .findFirst()
+ .ifPresent(receiver -> handle(receiver, message));
+ }
+ }, getSubChannelId());
+ }
+ };
+ }
+
+ @Override
+ protected void send(@NotNull Message message, @NotNull OnlineUser sender) {
+ try (Jedis jedis = jedisPool.getResource()) {
+ jedis.publish(getSubChannelId(), plugin.getGson().toJson(message));
+ }
+ }
+
+ @Override
+ public void close() {
+ super.close();
+ if (jedisPool != null) {
+ jedisPool.close();
+ }
+ }
+
+}
\ No newline at end of file
diff --git a/common/src/main/java/net/william278/huskhomes/network/Request.java b/common/src/main/java/net/william278/huskhomes/network/Request.java
deleted file mode 100644
index e573d9da..00000000
--- a/common/src/main/java/net/william278/huskhomes/network/Request.java
+++ /dev/null
@@ -1,185 +0,0 @@
-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> send(@NotNull OnlineUser sender, @NotNull HuskHomes plugin) {
- this.sender = sender.username;
- this.clusterId = plugin.getSettings().getClusterId();
- 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().getClusterId();
- 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);
- }
-
-}
diff --git a/common/src/main/java/net/william278/huskhomes/network/RequestBuilder.java b/common/src/main/java/net/william278/huskhomes/network/RequestBuilder.java
deleted file mode 100644
index b2c2a96b..00000000
--- a/common/src/main/java/net/william278/huskhomes/network/RequestBuilder.java
+++ /dev/null
@@ -1,40 +0,0 @@
-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);
- }
-
-}
diff --git a/common/src/main/java/net/william278/huskhomes/position/Home.java b/common/src/main/java/net/william278/huskhomes/position/Home.java
index 590b6652..fd31da83 100644
--- a/common/src/main/java/net/william278/huskhomes/position/Home.java
+++ b/common/src/main/java/net/william278/huskhomes/position/Home.java
@@ -1,6 +1,6 @@
package net.william278.huskhomes.position;
-import net.william278.huskhomes.player.User;
+import net.william278.huskhomes.user.User;
import org.jetbrains.annotations.NotNull;
import java.util.UUID;
diff --git a/common/src/main/java/net/william278/huskhomes/position/Position.java b/common/src/main/java/net/william278/huskhomes/position/Position.java
index f68da3f9..3888e1e4 100644
--- a/common/src/main/java/net/william278/huskhomes/position/Position.java
+++ b/common/src/main/java/net/william278/huskhomes/position/Position.java
@@ -10,15 +10,15 @@ import java.util.UUID;
*/
public class Position extends Location {
- private Server server;
+ private String server;
public Position(double x, double y, double z, float yaw, float pitch,
- @NotNull World world, @NotNull Server server) {
+ @NotNull World world, @NotNull String server) {
super(x, y, z, yaw, pitch, world);
this.setServer(server);
}
- public Position(@NotNull Location location, @NotNull Server server) {
+ public Position(@NotNull Location location, @NotNull String server) {
super(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch(), location.getWorld());
this.setServer(server);
}
@@ -61,7 +61,7 @@ public class Position extends Location {
final double y = parseCoordinate(args[1], relativeTo.getY());
final double z = parseCoordinate(args[2], relativeTo.getZ());
final World world = args.length > 3 ? new World(args[3], UUID.randomUUID()) : relativeTo.getWorld();
- final Server server = args.length > 4 ? new Server(args[4]) : relativeTo.getServer();
+ final String server = args.length > 4 ? args[4] : relativeTo.getServer();
return Optional.of(new Position(x, y, z, relativeTo.getYaw(), relativeTo.getPitch(), world, server));
} catch (NumberFormatException ignored) {
return Optional.empty();
@@ -90,13 +90,14 @@ public class Position extends Location {
}
/**
- * The {@link Server} the position is on
+ * The name of the server the position is on
*/
- public Server getServer() {
+ @NotNull
+ public String getServer() {
return server;
}
- public void setServer(Server server) {
+ public void setServer(@NotNull String server) {
this.server = server;
}
}
diff --git a/common/src/main/java/net/william278/huskhomes/position/Warp.java b/common/src/main/java/net/william278/huskhomes/position/Warp.java
index add1b5ac..a84bc36c 100644
--- a/common/src/main/java/net/william278/huskhomes/position/Warp.java
+++ b/common/src/main/java/net/william278/huskhomes/position/Warp.java
@@ -1,6 +1,6 @@
package net.william278.huskhomes.position;
-import net.william278.huskhomes.player.OnlineUser;
+import net.william278.huskhomes.user.OnlineUser;
import net.william278.huskhomes.util.Permission;
import org.intellij.lang.annotations.Subst;
import org.jetbrains.annotations.NotNull;
diff --git a/common/src/main/java/net/william278/huskhomes/redis/RedisWorker.java b/common/src/main/java/net/william278/huskhomes/redis/RedisWorker.java
deleted file mode 100644
index c49469e3..00000000
--- a/common/src/main/java/net/william278/huskhomes/redis/RedisWorker.java
+++ /dev/null
@@ -1,64 +0,0 @@
-package net.william278.huskhomes.redis;
-
-import net.william278.huskhomes.config.Settings;
-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;
-import redis.clients.jedis.JedisPoolConfig;
-
-import java.util.concurrent.CompletableFuture;
-
-/**
- * Handles Redis database pub/sub network messaging requests over Jedis
- */
-public class RedisWorker {
-
- public String host;
-
- public int port;
-
- public String password;
-
- public boolean ssl;
- public JedisPool jedisPool;
-
- public RedisWorker(@NotNull Settings settings) {
- this.host = settings.getRedisHost();
- this.port = settings.getRedisPort();
- this.password = settings.getRedisPassword() != null ? settings.getRedisPassword() : "";
- this.ssl = settings.isRedisUseSsl() && !(this.host.equalsIgnoreCase("localhost")
- || this.host.equalsIgnoreCase("127.0.0.1"));
- }
-
- /**
- * Initialize the JedisPool connection
- */
- public void initialize() {
- if (password.isEmpty()) {
- jedisPool = new JedisPool(new JedisPoolConfig(), host, port, 0, ssl);
- } else {
- jedisPool = new JedisPool(new JedisPoolConfig(), host, port, 0, password, ssl);
- }
- }
-
- public CompletableFuture sendMessage(@NotNull Request request) {
- return CompletableFuture.runAsync(() -> {
- try (Jedis jedis = jedisPool.getResource()) {
- jedis.publish(Messenger.NETWORK_MESSAGE_CHANNEL, request.toJson());
- }
- });
- }
-
- /**
- * Close the JedisPool connection
- */
- public void terminate() {
- if (jedisPool != null) {
- if (!jedisPool.isClosed()) {
- jedisPool.close();
- }
- }
- }
-}
diff --git a/common/src/main/java/net/william278/huskhomes/request/RequestManager.java b/common/src/main/java/net/william278/huskhomes/request/RequestManager.java
index 37689346..3202fb61 100644
--- a/common/src/main/java/net/william278/huskhomes/request/RequestManager.java
+++ b/common/src/main/java/net/william278/huskhomes/request/RequestManager.java
@@ -3,8 +3,8 @@ package net.william278.huskhomes.request;
import net.william278.huskhomes.HuskHomes;
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.user.OnlineUser;
+import net.william278.huskhomes.user.User;
import net.william278.huskhomes.teleport.Teleport;
import net.william278.huskhomes.teleport.TimedTeleport;
import org.jetbrains.annotations.NotNull;
@@ -45,9 +45,9 @@ public class RequestManager {
*/
public void setIgnoringRequests(@NotNull User user, boolean ignoring) {
if (ignoring) {
- this.ignoringRequests.add(user.uuid);
+ this.ignoringRequests.add(user.getUuid());
} else {
- this.ignoringRequests.remove(user.uuid);
+ this.ignoringRequests.remove(user.getUuid());
}
}
@@ -58,7 +58,7 @@ public class RequestManager {
* @return {@code true} if the user is ignoring tpa requests
*/
public boolean isIgnoringRequests(@NotNull User user) {
- return this.ignoringRequests.contains(user.uuid);
+ return this.ignoringRequests.contains(user.getUuid());
}
/**
@@ -68,7 +68,7 @@ public class RequestManager {
* @param recipient the {@link User} recipient of the request
*/
public void addTeleportRequest(@NotNull TeleportRequest request, @NotNull User recipient) {
- this.requests.computeIfAbsent(recipient.uuid, uuid -> new LinkedList<>()).addFirst(request);
+ this.requests.computeIfAbsent(recipient.getUuid(), uuid -> new LinkedList<>()).addFirst(request);
}
/**
@@ -78,7 +78,7 @@ public class RequestManager {
* @param recipient the {@link User} recipient of the request
*/
public void removeTeleportRequest(@NotNull String requesterName, @NotNull User recipient) {
- this.requests.computeIfPresent(recipient.uuid, (uuid, requests) -> {
+ this.requests.computeIfPresent(recipient.getUuid(), (uuid, requests) -> {
requests.removeIf(teleportRequest -> teleportRequest.requesterName.equalsIgnoreCase(requesterName));
return requests.isEmpty() ? null : requests;
});
@@ -91,7 +91,7 @@ public class RequestManager {
* @return the last received request, if present
*/
public Optional getLastTeleportRequest(@NotNull User recipient) {
- return this.requests.getOrDefault(recipient.uuid, new LinkedList<>()).stream().findFirst();
+ return this.requests.getOrDefault(recipient.getUuid(), new LinkedList<>()).stream().findFirst();
}
/**
@@ -106,11 +106,11 @@ public class RequestManager {
* @return the last unexpired teleport request received from the requester, if present
*/
public Optional getTeleportRequest(@NotNull String requesterName, @NotNull User recipient) {
- return this.requests.getOrDefault(recipient.uuid, new LinkedList<>()).stream()
+ return this.requests.getOrDefault(recipient.getUuid(), new LinkedList<>()).stream()
.filter(request -> request.requesterName.equalsIgnoreCase(requesterName))
.filter(request -> !request.hasExpired())
.findFirst()
- .or(() -> this.requests.getOrDefault(recipient.uuid, new LinkedList<>()).stream()
+ .or(() -> this.requests.getOrDefault(recipient.getUuid(), new LinkedList<>()).stream()
.filter(request -> request.requesterName.equalsIgnoreCase(requesterName))
.findFirst());
}
@@ -133,7 +133,7 @@ public class RequestManager {
if (localTarget.get().equals(requester)) {
return CompletableFuture.completedFuture(Optional.empty());
}
- request.recipientName = localTarget.get().username;
+ request.recipientName = localTarget.get().getUsername();
return CompletableFuture.completedFuture(sendLocalTeleportRequest(request, localTarget.get()));
}
@@ -154,7 +154,7 @@ public class RequestManager {
.build().send(requester, plugin)
.thenApply(reply -> {
if (reply.isPresent()) {
- final TeleportRequest result = reply.get().getPayload().teleportRequest;
+ final TeleportRequest result = reply.get().getPayload().getTeleportRequest();
if (result == null || result.status == TeleportRequest.RequestStatus.IGNORED) {
return null;
}
diff --git a/common/src/main/java/net/william278/huskhomes/request/TeleportRequest.java b/common/src/main/java/net/william278/huskhomes/request/TeleportRequest.java
index 9b1600a8..080e0d6c 100644
--- a/common/src/main/java/net/william278/huskhomes/request/TeleportRequest.java
+++ b/common/src/main/java/net/william278/huskhomes/request/TeleportRequest.java
@@ -1,7 +1,7 @@
package net.william278.huskhomes.request;
import com.google.gson.annotations.SerializedName;
-import net.william278.huskhomes.player.OnlineUser;
+import net.william278.huskhomes.user.OnlineUser;
import net.william278.huskhomes.position.Position;
import org.jetbrains.annotations.NotNull;
@@ -53,7 +53,7 @@ public class TeleportRequest {
*/
protected TeleportRequest(@NotNull OnlineUser requester, @NotNull RequestType requestType,
final long expiryTime) {
- this.requesterName = requester.username;
+ this.requesterName = requester.getUsername();
this.requesterPosition = requester.getPosition();
this.type = requestType;
this.status = RequestStatus.PENDING;
diff --git a/common/src/main/java/net/william278/huskhomes/teleport/CompletedTeleport.java b/common/src/main/java/net/william278/huskhomes/teleport/CompletedTeleport.java
index 551acf32..0a380afd 100644
--- a/common/src/main/java/net/william278/huskhomes/teleport/CompletedTeleport.java
+++ b/common/src/main/java/net/william278/huskhomes/teleport/CompletedTeleport.java
@@ -1,8 +1,8 @@
package net.william278.huskhomes.teleport;
import net.william278.huskhomes.config.Locales;
-import net.william278.huskhomes.player.OnlineUser;
-import net.william278.huskhomes.player.User;
+import net.william278.huskhomes.user.OnlineUser;
+import net.william278.huskhomes.user.User;
import net.william278.huskhomes.position.Position;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
diff --git a/common/src/main/java/net/william278/huskhomes/teleport/Teleport.java b/common/src/main/java/net/william278/huskhomes/teleport/Teleport.java
index 858c2ae5..1ff3ab6e 100644
--- a/common/src/main/java/net/william278/huskhomes/teleport/Teleport.java
+++ b/common/src/main/java/net/william278/huskhomes/teleport/Teleport.java
@@ -2,18 +2,18 @@ package net.william278.huskhomes.teleport;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.config.Settings;
-import net.william278.huskhomes.network.Request;
+import net.william278.huskhomes.hook.EconomyHook;
+import net.william278.huskhomes.network.Message;
import net.william278.huskhomes.network.Payload;
-import net.william278.huskhomes.player.OnlineUser;
-import net.william278.huskhomes.player.User;
import net.william278.huskhomes.position.Position;
+import net.william278.huskhomes.user.OnlineUser;
+import net.william278.huskhomes.user.User;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
+import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.TimeUnit;
-import java.util.logging.Level;
/**
* Represents a teleport in the process of being executed
@@ -55,10 +55,10 @@ public class Teleport {
public final TeleportType type;
/**
- * {@link Settings.EconomyAction}s to be checked against the {@link #executor executor}'s balance
+ * {@link EconomyHook.EconomyAction}s to be checked against the {@link #executor executor}'s balance
*/
@NotNull
- public final Set economyActions;
+ public final Set economyActions;
/**
* Whether to update the {@link #teleporter teleporter}'s last position (i.e. their {@code /back} position)
@@ -69,7 +69,7 @@ public class Teleport {
* Internal - use TeleportBuilder to instantiate a teleport
*/
protected Teleport(@Nullable User teleporter, @NotNull OnlineUser executor, @Nullable Position target,
- @NotNull TeleportType type, @NotNull Set economyActions,
+ @NotNull TeleportType type, @NotNull Set economyActions,
final boolean updateLastPosition, @NotNull HuskHomes plugin) {
this.teleporter = teleporter;
this.executor = executor;
@@ -112,7 +112,7 @@ public class Teleport {
}
// Check economy actions
- for (Settings.EconomyAction economyAction : economyActions) {
+ for (EconomyHook.EconomyAction economyAction : economyActions) {
if (!plugin.validateEconomyCheck(executor, economyAction)) {
return CompletableFuture.completedFuture(TeleportResult.CANCELLED_ECONOMY)
.thenApply(resultState -> CompletedTeleport.from(resultState, this));
@@ -183,20 +183,9 @@ public class Teleport {
}
// If the target position is on another server, execute a cross-server teleport
- final CompletableFuture teleportFuture = new CompletableFuture<>();
plugin.getDatabase().setCurrentTeleport(teleporter, this);
- plugin.getMessenger()
- .sendPlayer(teleporter, target.getServer())
- .thenApply(completed -> completed
- ? TeleportResult.COMPLETED_CROSS_SERVER
- : TeleportResult.FAILED_INVALID_SERVER)
- .orTimeout(10, TimeUnit.SECONDS)
- .exceptionally(throwable -> {
- plugin.getLoggingAdapter().log(Level.WARNING, "Cross-server teleport timed out for " + teleporter.username);
- plugin.getDatabase().setCurrentTeleport(teleporter, null);
- return TeleportResult.FAILED_INVALID_SERVER;
- });
- return teleportFuture;
+ plugin.getMessenger().changeServer(teleporter, target.getServer());
+ return CompletableFuture.completedFuture(TeleportResult.COMPLETED_CROSS_SERVER);
}
/**
@@ -212,20 +201,13 @@ public class Teleport {
assert teleporter != null;
// Send a network message to a user on another server to teleport them to the target position
- 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 Request reply = result.get();
- if (reply.getPayload().resultState != null) {
- return reply.getPayload().resultState;
- }
- }
- return TeleportResult.FAILED_TELEPORTER_NOT_RESOLVED;
- });
+ return Message.builder()
+ .type(Message.Type.TELEPORT_TO_POSITION_REQUEST)
+ .payload(Payload.withPosition(target))
+ .target(teleporter.getUsername())
+ .build().send(plugin.getMessenger(), executor)
+ .thenApply(result -> Optional.ofNullable(result.getPayload().getResultState())
+ .orElse(TeleportResult.FAILED_TELEPORTER_NOT_RESOLVED));
}
}
diff --git a/common/src/main/java/net/william278/huskhomes/teleport/TeleportBuilder.java b/common/src/main/java/net/william278/huskhomes/teleport/TeleportBuilder.java
index a1a3bc70..40b5f7ed 100644
--- a/common/src/main/java/net/william278/huskhomes/teleport/TeleportBuilder.java
+++ b/common/src/main/java/net/william278/huskhomes/teleport/TeleportBuilder.java
@@ -1,10 +1,10 @@
package net.william278.huskhomes.teleport;
import net.william278.huskhomes.HuskHomes;
-import net.william278.huskhomes.config.Settings;
+import net.william278.huskhomes.hook.EconomyHook;
import net.william278.huskhomes.network.Request;
-import net.william278.huskhomes.player.OnlineUser;
-import net.william278.huskhomes.player.User;
+import net.william278.huskhomes.user.OnlineUser;
+import net.william278.huskhomes.user.User;
import net.william278.huskhomes.position.Location;
import net.william278.huskhomes.position.Position;
import org.jetbrains.annotations.NotNull;
@@ -43,12 +43,12 @@ public class TeleportBuilder {
private CompletableFuture target;
/**
- * List of {@link Settings.EconomyAction}s to check against.
+ * List of {@link EconomyHook.EconomyAction}s to check against.
*
* Note that these are checked against the {@link #executor executor} of the teleport;
* not necessarily the one doing the teleporting
*/
- private final Set economyActions = new HashSet<>();
+ private final Set economyActions = new HashSet<>();
/**
* The type of teleport. Defaults to {@link TeleportType#TELEPORT}
@@ -85,18 +85,7 @@ public class TeleportBuilder {
* @return The {@link TeleportBuilder} instance
*/
public TeleportBuilder setTeleporter(@NotNull String teleporterUsername) {
- this.teleporter = CompletableFuture.supplyAsync(() -> plugin
- .findOnlinePlayer(teleporterUsername)
- .map(onlineUser -> (User) onlineUser)
- .or(() -> {
- if (plugin.getSettings().isCrossServer()) {
- return plugin.getMessenger()
- .findPlayer(executor, teleporterUsername).join()
- .map(username -> new User(UUID.randomUUID(), username));
- }
- return Optional.empty();
- })
- .orElse(null));
+ this.teleporter = teleporterUsername;
return this;
}
@@ -148,12 +137,12 @@ public class TeleportBuilder {
/**
* Set the economy actions to check against during the teleport
*
- * @param economyActions The {@link Settings.EconomyAction}s to check against
+ * @param economyActions The {@link EconomyHook.EconomyAction}s to check against
* @return The {@link TeleportBuilder} instance
* @implNote These are checked against the {@link #executor executor} of the teleport;
* not necessarily the one doing the teleporting
*/
- public TeleportBuilder setEconomyActions(@NotNull Set economyActions) {
+ public TeleportBuilder setEconomyActions(@NotNull Set economyActions) {
this.economyActions.addAll(economyActions);
return this;
}
@@ -161,12 +150,12 @@ public class TeleportBuilder {
/**
* Set the economy actions to check against during the teleport
*
- * @param economyActions The {@link Settings.EconomyAction}s to check against
+ * @param economyActions The {@link EconomyHook.EconomyAction}s to check against
* @return The {@link TeleportBuilder} instance
* @implNote These are checked against the {@link #executor executor} of the teleport;
* not necessarily the one doing the teleporting
*/
- public TeleportBuilder setEconomyActions(@NotNull Settings.EconomyAction... economyActions) {
+ public TeleportBuilder setEconomyActions(@NotNull EconomyHook.EconomyAction... economyActions) {
this.economyActions.addAll(Arrays.asList(economyActions));
return this;
}
@@ -243,7 +232,7 @@ public class TeleportBuilder {
.withType(Request.MessageType.POSITION_REQUEST)
.withTargetPlayer(playerName)
.build().send(executor, plugin)
- .thenApply(reply -> reply.map(message -> message.getPayload().position)).join();
+ .thenApply(reply -> reply.map(message -> message.getPayload().getPosition())).join();
});
}
return CompletableFuture.supplyAsync(Optional::empty);
diff --git a/common/src/main/java/net/william278/huskhomes/teleport/TeleportResult.java b/common/src/main/java/net/william278/huskhomes/teleport/TeleportResult.java
index 7038e42b..6b8ee66e 100644
--- a/common/src/main/java/net/william278/huskhomes/teleport/TeleportResult.java
+++ b/common/src/main/java/net/william278/huskhomes/teleport/TeleportResult.java
@@ -1,7 +1,7 @@
package net.william278.huskhomes.teleport;
import net.william278.huskhomes.config.Locales;
-import net.william278.huskhomes.player.OnlineUser;
+import net.william278.huskhomes.user.OnlineUser;
import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.position.World;
import org.jetbrains.annotations.NotNull;
diff --git a/common/src/main/java/net/william278/huskhomes/teleport/TimedTeleport.java b/common/src/main/java/net/william278/huskhomes/teleport/TimedTeleport.java
index fd79f1bb..c7bfc695 100644
--- a/common/src/main/java/net/william278/huskhomes/teleport/TimedTeleport.java
+++ b/common/src/main/java/net/william278/huskhomes/teleport/TimedTeleport.java
@@ -3,7 +3,8 @@ package net.william278.huskhomes.teleport;
import de.themoep.minedown.adventure.MineDown;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.config.Settings;
-import net.william278.huskhomes.player.OnlineUser;
+import net.william278.huskhomes.hook.EconomyHook;
+import net.william278.huskhomes.user.OnlineUser;
import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.util.Permission;
import org.jetbrains.annotations.NotNull;
@@ -24,7 +25,7 @@ public class TimedTeleport extends Teleport {
private boolean cancelled = false;
protected TimedTeleport(@NotNull OnlineUser teleporter, @NotNull OnlineUser executor, @NotNull Position target,
- @NotNull TeleportType type, int warmupTime, @NotNull Set economyActions,
+ @NotNull TeleportType type, int warmupTime, @NotNull Set economyActions,
final boolean updateLastPosition, @NotNull HuskHomes plugin) {
super(teleporter, executor, target, type, economyActions, updateLastPosition, plugin);
this.plugin = plugin;
@@ -53,13 +54,13 @@ public class TimedTeleport extends Teleport {
}
// Check if the teleporter is already warming up to teleport
- if (plugin.getCache().getCurrentlyOnWarmup().contains(teleporter.uuid)) {
+ if (plugin.getCache().getCurrentlyOnWarmup().contains(teleporter.getUuid())) {
return CompletableFuture.completedFuture(TeleportResult.FAILED_ALREADY_TELEPORTING)
.thenApply(resultState -> CompletedTeleport.from(resultState, this));
}
// Check economy actions
- for (Settings.EconomyAction economyAction : economyActions) {
+ for (EconomyHook.EconomyAction economyAction : economyActions) {
if (!plugin.validateEconomyCheck(executor, economyAction)) {
return CompletableFuture.completedFuture(TeleportResult.CANCELLED_ECONOMY)
.thenApply(resultState -> CompletedTeleport.from(resultState, this));
@@ -98,7 +99,7 @@ public class TimedTeleport extends Teleport {
}
// Mark the player as warming up and display the message
- plugin.getCache().getCurrentlyOnWarmup().add(teleporter.uuid);
+ plugin.getCache().getCurrentlyOnWarmup().add(teleporter.getUuid());
plugin.getLocales().getLocale("teleporting_warmup_start", Integer.toString(timeLeft))
.ifPresent(teleporter::sendMessage);
@@ -118,7 +119,7 @@ public class TimedTeleport extends Teleport {
// Tick (decrement) the timed teleport timer and end it if done
if (tickWarmup()) {
- plugin.getCache().getCurrentlyOnWarmup().remove(teleporter.uuid);
+ plugin.getCache().getCurrentlyOnWarmup().remove(teleporter.getUuid());
timedTeleportFuture.complete(null);
executor.shutdown();
}
diff --git a/common/src/main/java/net/william278/huskhomes/player/OnlineUser.java b/common/src/main/java/net/william278/huskhomes/user/OnlineUser.java
similarity index 97%
rename from common/src/main/java/net/william278/huskhomes/player/OnlineUser.java
rename to common/src/main/java/net/william278/huskhomes/user/OnlineUser.java
index d0d2be90..f653e168 100644
--- a/common/src/main/java/net/william278/huskhomes/player/OnlineUser.java
+++ b/common/src/main/java/net/william278/huskhomes/user/OnlineUser.java
@@ -1,4 +1,4 @@
-package net.william278.huskhomes.player;
+package net.william278.huskhomes.user;
import de.themoep.minedown.adventure.MineDown;
import de.themoep.minedown.adventure.MineDownParser;
@@ -11,8 +11,6 @@ import net.kyori.adventure.title.TitlePart;
import net.william278.huskhomes.position.Location;
import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.teleport.TeleportResult;
-import net.william278.huskhomes.util.Permission;
-import org.intellij.lang.annotations.Pattern;
import org.intellij.lang.annotations.Subst;
import org.jetbrains.annotations.NotNull;
@@ -28,7 +26,6 @@ public abstract class OnlineUser extends User {
super(uuid, username);
}
-
/**
* Returns the current {@link Position} of this player
*
@@ -146,6 +143,13 @@ public abstract class OnlineUser extends User {
*/
public abstract CompletableFuture teleportLocally(@NotNull Location location, boolean asynchronous);
+ /**
+ * Send a plugin message to the user
+ * @param channel channel to send it on
+ * @param message byte array of message data
+ */
+ public abstract void sendPluginMessage(@NotNull String channel, byte[] message);
+
/**
* Returns if a player is moving (i.e. they have momentum)
*
diff --git a/common/src/main/java/net/william278/huskhomes/player/User.java b/common/src/main/java/net/william278/huskhomes/user/User.java
similarity index 68%
rename from common/src/main/java/net/william278/huskhomes/player/User.java
rename to common/src/main/java/net/william278/huskhomes/user/User.java
index dcafcf0e..7c6db9fe 100644
--- a/common/src/main/java/net/william278/huskhomes/player/User.java
+++ b/common/src/main/java/net/william278/huskhomes/user/User.java
@@ -1,4 +1,4 @@
-package net.william278.huskhomes.player;
+package net.william278.huskhomes.user;
import org.jetbrains.annotations.NotNull;
@@ -10,10 +10,10 @@ import java.util.UUID;
public class User {
@NotNull
- public final UUID uuid;
+ private final UUID uuid;
@NotNull
- public final String username;
+ private final String username;
/**
* Get a user from a {@link UUID} and username
@@ -29,8 +29,18 @@ public class User {
@Override
public boolean equals(@NotNull Object obj) {
if (obj instanceof User user) {
- return user.uuid.equals(uuid);
+ return user.getUuid().equals(getUuid());
}
return super.equals(obj);
}
+
+ @NotNull
+ public UUID getUuid() {
+ return uuid;
+ }
+
+ @NotNull
+ public String getUsername() {
+ return username;
+ }
}
diff --git a/common/src/main/java/net/william278/huskhomes/player/UserData.java b/common/src/main/java/net/william278/huskhomes/user/UserData.java
similarity index 77%
rename from common/src/main/java/net/william278/huskhomes/player/UserData.java
rename to common/src/main/java/net/william278/huskhomes/user/UserData.java
index e19f576d..2057f976 100644
--- a/common/src/main/java/net/william278/huskhomes/player/UserData.java
+++ b/common/src/main/java/net/william278/huskhomes/user/UserData.java
@@ -1,4 +1,4 @@
-package net.william278.huskhomes.player;
+package net.william278.huskhomes.user;
import org.jetbrains.annotations.NotNull;
@@ -12,11 +12,11 @@ public record UserData(@NotNull User user, int homeSlots, boolean ignoringTelepo
@NotNull
public UUID getUserUuid() {
- return user.uuid;
+ return user.getUuid();
}
public String getUsername() {
- return user.username;
+ return user.getUsername();
}
}
diff --git a/common/src/test/java/net/william278/huskhomes/network/RequestTests.java b/common/src/test/java/net/william278/huskhomes/network/RequestTests.java
index 556ff6c5..6e3bcfbf 100644
--- a/common/src/test/java/net/william278/huskhomes/network/RequestTests.java
+++ b/common/src/test/java/net/william278/huskhomes/network/RequestTests.java
@@ -40,11 +40,11 @@ public class RequestTests {
final Request readRequest = Request.fromJson(DUMMY_PAYLOAD.toJson());
Assertions.assertEquals(DUMMY_PAYLOAD.getUuid(), readRequest.getUuid());
- Assertions.assertNotNull(readRequest.getPayload().position);
+ Assertions.assertNotNull(readRequest.getPayload().getPosition());
- assert readRequest.getPayload().position != null && DUMMY_PAYLOAD.getPayload().position != null;
- Assertions.assertEquals(DUMMY_PAYLOAD.getPayload().position.getX(), readRequest.getPayload().position.getX());
- Assertions.assertEquals(DUMMY_PAYLOAD.getPayload().position.getY(), readRequest.getPayload().position.getY());
- Assertions.assertEquals(DUMMY_PAYLOAD.getPayload().position.getZ(), readRequest.getPayload().position.getZ());
+ assert readRequest.getPayload().getPosition() != null && DUMMY_PAYLOAD.getPayload().getPosition() != null;
+ Assertions.assertEquals(DUMMY_PAYLOAD.getPayload().getPosition().getX(), readRequest.getPayload().getPosition().getX());
+ Assertions.assertEquals(DUMMY_PAYLOAD.getPayload().getPosition().getY(), readRequest.getPayload().getPosition().getY());
+ Assertions.assertEquals(DUMMY_PAYLOAD.getPayload().getPosition().getZ(), readRequest.getPayload().getPosition().getZ());
}
}