New player list caching system, further refinements and fixes

This commit is contained in:
William committed 2023-03-21 12:49:36 +00:00
1 parent 054811676d
commit 93dd15007a
59 files changed
+445 -317

No files matched your search

@@ -1,8 +1,8 @@
package net.william278.huskhomes.network;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.user.OnlineUser;
import net.william278.huskhomes.teleport.Teleport;
import net.william278.huskhomes.user.OnlineUser;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -54,6 +54,15 @@ public abstract class Broker {
.getTeleportRequest()
.ifPresent(teleportRequest -> plugin.getManager().requests()
.handleLocalRequestResponse(receiver, teleportRequest));
case REQUEST_PLAYER_LIST -> Message.builder()
.type(Message.Type.PLAYER_LIST)
.scope(Message.Scope.SERVER)
.target(message.getSourceServer())
.payload(Payload.withStringList(plugin.getLocalPlayerList()))
.build().send(this, receiver);
case PLAYER_LIST -> message.getPayload()
.getStringList()
.ifPresent(players -> plugin.setPlayerList(message.getSourceServer(), players));
}
}
@@ -96,23 +105,23 @@ public abstract class Broker {
return plugin.getServerName();
}
/**
* Identifies types of message brokers
*/
public enum Type {
PLUGIN_MESSAGE("Plugin Messages"),
REDIS("Redis");
@NotNull
private final String displayName;
/**
* Identifies types of message brokers
*/
public enum Type {
PLUGIN_MESSAGE("Plugin Messages"),
REDIS("Redis");
@NotNull
private final String displayName;
Type(@NotNull String displayName) {
this.displayName = displayName;
}
Type(@NotNull String displayName) {
this.displayName = displayName;
}
@NotNull
public String getDisplayName() {
return displayName;
@NotNull
public String getDisplayName() {
return displayName;
}
}
}
}
@@ -22,6 +22,8 @@ public class Message {
@Expose
private Type type;
@Expose
private Scope scope;
@Expose
private String target;
@Expose
private Payload payload;
@@ -30,8 +32,9 @@ public class Message {
@Expose
private String sourceServer;
private Message(@NotNull Type type, @NotNull String target, @NotNull Payload payload) {
private Message(@NotNull Type type, @NotNull Scope scope, @NotNull String target, @NotNull Payload payload) {
this.type = type;
this.scope = scope;
this.target = target;
this.payload = payload;
this.id = UUID.randomUUID();
@@ -56,6 +59,11 @@ public class Message {
return type;
}
@NotNull
public Scope getScope() {
return scope;
}
@NotNull
public String getTarget() {
return target;
@@ -86,6 +94,7 @@ public class Message {
*/
public static class Builder {
private Type type;
private Scope scope = Scope.PLAYER;
private Payload payload = Payload.empty();
private String target;
@@ -98,6 +107,12 @@ public class Message {
return this;
}
@NotNull
public Builder scope(@NotNull Scope scope) {
this.scope = scope;
return this;
}
@NotNull
public Builder payload(@NotNull Payload payload) {
this.payload = payload;
@@ -112,7 +127,7 @@ public class Message {
@NotNull
public Message build() {
return new Message(type, target, payload);
return new Message(type, scope, target, payload);
}
}
@@ -125,7 +140,31 @@ public class Message {
TELEPORT_TO_NETWORKED_POSITION,
TELEPORT_REQUEST,
TELEPORT_TO_NETWORKED_USER,
TELEPORT_REQUEST_RESPONSE
TELEPORT_REQUEST_RESPONSE,
REQUEST_PLAYER_LIST,
PLAYER_LIST,
}
public enum Scope {
/**
* The target is a server name, or "all" to indicate all servers.
*/
SERVER("Forward"),
/**
* The target is a player name, or "all" to indicate all players.
*/
PLAYER("ForwardToPlayer");
private final String pluginMessageChannel;
Scope(@NotNull String pluginMessageChannel) {
this.pluginMessageChannel = pluginMessageChannel;
}
@NotNull
public String getPluginMessageChannel() {
return pluginMessageChannel;
}
}
}
@@ -4,10 +4,10 @@ import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.teleport.TeleportRequest;
import net.william278.huskhomes.teleport.TeleportResult;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.Optional;
/**
@@ -28,6 +28,10 @@ public class Payload {
@Expose
private String string;
@Nullable
@Expose
private List<String> stringList;
/**
* Returns an empty cross-server message payload
*
@@ -74,6 +78,16 @@ public class Payload {
return payload;
}
/**
* A string list field
*/
@NotNull
public static Payload withStringList(@NotNull List<String> target) {
final Payload payload = new Payload();
payload.stringList = target;
return payload;
}
private Payload() {
}
@@ -85,6 +99,9 @@ public class Payload {
}
/**
* A teleport request field
*/
public Optional<TeleportRequest> getTeleportRequest() {
return Optional.ofNullable(teleportRequest);
}
@@ -95,4 +112,12 @@ public class Payload {
public Optional<String> getString() {
return Optional.ofNullable(string);
}
/**
* A string list field
*/
public Optional<List<String>> getStringList() {
return Optional.ofNullable(stringList);
}
}
@@ -63,7 +63,7 @@ public class PluginMessageBroker extends Broker {
@SuppressWarnings("UnstableApiUsage")
protected void send(@NotNull Message message, @NotNull OnlineUser sender) {
final ByteArrayDataOutput messageWriter = ByteStreams.newDataOutput();
messageWriter.writeUTF("ForwardToPlayer");
messageWriter.writeUTF(message.getScope().getPluginMessageChannel());
messageWriter.writeUTF(message.getTarget());
messageWriter.writeUTF(getSubChannelId());
@@ -49,10 +49,19 @@ public class RedisBroker extends PluginMessageBroker {
}
final Message message = plugin.getGson().fromJson(encodedMessage, Message.class);
plugin.getOnlineUsers().stream()
.filter(online -> message.getTarget().equals(Message.TARGET_ALL)
|| online.getUsername().equals(message.getTarget()))
.forEach(receiver -> handle(receiver, message));
if (message.getScope() == Message.Scope.PLAYER) {
plugin.getOnlineUsers().stream()
.filter(online -> message.getTarget().equals(Message.TARGET_ALL)
|| online.getUsername().equals(message.getTarget()))
.forEach(receiver -> handle(receiver, message));
return;
}
if (message.getTarget().equals(plugin.getServerName())) {
plugin.getOnlineUsers().stream()
.findAny()
.ifPresent(receiver -> handle(receiver, message));
}
}
}, getSubChannelId());
}