From 519105f359b76d541f3804b6ca95e998de5ed2b9 Mon Sep 17 00:00:00 2001 From: William Date: Fri, 24 Mar 2023 01:00:07 +0000 Subject: [PATCH] Fix global player list propagation --- .../huskhomes/command/EditHomeCommand.java | 5 +- .../william278/huskhomes/config/Settings.java | 2 +- .../huskhomes/listener/EventListener.java | 50 ++++++++++++------- .../william278/huskhomes/network/Broker.java | 5 +- .../william278/huskhomes/network/Message.java | 8 +++ .../huskhomes/network/RedisBroker.java | 3 +- .../huskhomes/teleport/Teleport.java | 2 +- .../huskhomes/teleport/TeleportRequest.java | 1 + 8 files changed, 51 insertions(+), 25 deletions(-) diff --git a/common/src/main/java/net/william278/huskhomes/command/EditHomeCommand.java b/common/src/main/java/net/william278/huskhomes/command/EditHomeCommand.java index dcd66cb3..13cda5cc 100644 --- a/common/src/main/java/net/william278/huskhomes/command/EditHomeCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/EditHomeCommand.java @@ -12,7 +12,10 @@ import org.jetbrains.annotations.NotNull; import java.time.ZoneId; import java.time.format.DateTimeFormatter; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Optional; import java.util.regex.Pattern; public class EditHomeCommand extends SavedPositionCommand { diff --git a/common/src/main/java/net/william278/huskhomes/config/Settings.java b/common/src/main/java/net/william278/huskhomes/config/Settings.java index 43fb2fd6..e90698a7 100644 --- a/common/src/main/java/net/william278/huskhomes/config/Settings.java +++ b/common/src/main/java/net/william278/huskhomes/config/Settings.java @@ -361,7 +361,7 @@ public class Settings { return listItemsPerPage; } - public boolean isAsynchronousTeleports() { + public boolean doAsynchronousTeleports() { return asynchronousTeleports; } diff --git a/common/src/main/java/net/william278/huskhomes/listener/EventListener.java b/common/src/main/java/net/william278/huskhomes/listener/EventListener.java index a601d315..e6aface2 100644 --- a/common/src/main/java/net/william278/huskhomes/listener/EventListener.java +++ b/common/src/main/java/net/william278/huskhomes/listener/EventListener.java @@ -4,6 +4,7 @@ import de.themoep.minedown.adventure.MineDown; import net.william278.huskhomes.HuskHomes; import net.william278.huskhomes.command.BackCommand; import net.william278.huskhomes.command.Command; +import net.william278.huskhomes.network.Broker; import net.william278.huskhomes.network.Message; import net.william278.huskhomes.network.Payload; import net.william278.huskhomes.position.Position; @@ -11,6 +12,7 @@ import net.william278.huskhomes.teleport.Teleport; import net.william278.huskhomes.user.OnlineUser; import org.jetbrains.annotations.NotNull; +import java.util.List; import java.util.Optional; import java.util.logging.Level; @@ -40,21 +42,11 @@ public class EventListener { if (plugin.getSettings().isCrossServer()) { this.handleInboundTeleport(onlineUser); - // Send a player list update to other servers - this.sendPlayerListUpdates(onlineUser); + // Synchronize the global player list + plugin.runLater(() -> this.synchronizeGlobalPlayerList(onlineUser, plugin.getLocalPlayerList()), 40L); // Request updated player lists from other servers if (plugin.getOnlineUsers().size() == 1) { - plugin.runLater(() -> { - plugin.getGlobalPlayerList().clear(); - Message.builder() - .type(Message.Type.REQUEST_PLAYER_LIST) - .scope(Message.Scope.SERVER) - .target(Message.TARGET_ALL) - .build().send(plugin.getMessenger(), onlineUser); - }, 40L); - - // Update caches plugin.getManager().homes().updatePublicHomeCache(); plugin.getManager().warps().updateWarpCache(); } @@ -96,19 +88,19 @@ public class EventListener { .updateLastPosition(false) .toTeleport().execute(), 40L); } else { - teleporter.teleportLocally(spawn, plugin.getSettings().isAsynchronousTeleports()); + teleporter.teleportLocally(spawn, plugin.getSettings().doAsynchronousTeleports()); } teleporter.sendTranslatableMessage("block.minecraft.spawn.not_valid"); }); } else { - teleporter.teleportLocally(bedPosition.get(), plugin.getSettings().isAsynchronousTeleports()); + teleporter.teleportLocally(bedPosition.get(), plugin.getSettings().doAsynchronousTeleports()); } plugin.getDatabase().setCurrentTeleport(teleporter, null); plugin.getDatabase().setRespawnPosition(teleporter, bedPosition.orElse(null)); return; } - teleporter.teleportLocally((Position) teleport.getTarget(), plugin.getSettings().isAsynchronousTeleports()); + teleporter.teleportLocally((Position) teleport.getTarget(), plugin.getSettings().doAsynchronousTeleports()); plugin.getDatabase().setCurrentTeleport(teleporter, null); teleport.displayTeleportingComplete(teleporter); }); @@ -128,19 +120,41 @@ public class EventListener { // Update global lists if (plugin.getSettings().isCrossServer()) { + final List localPlayerList = plugin.getLocalPlayerList().stream() + .filter(player -> !player.equals(onlineUser.getUsername())) + .toList(); + + if (plugin.getSettings().getBrokerType() == Broker.Type.REDIS) { + this.synchronizeGlobalPlayerList(onlineUser, localPlayerList); + return; + } + plugin.getOnlineUsers().stream() .filter(user -> !user.equals(onlineUser)) .findAny() - .ifPresent(this::sendPlayerListUpdates); + .ifPresent(player -> this.synchronizeGlobalPlayerList(player, localPlayerList)); } } - private void sendPlayerListUpdates(@NotNull OnlineUser user) { + // Synchronize the global player list + private void synchronizeGlobalPlayerList(@NotNull OnlineUser user, @NotNull List localPlayerList) { + // Send this server's player list to all servers Message.builder() + .type(Message.Type.PLAYER_LIST) .scope(Message.Scope.SERVER) .target(Message.TARGET_ALL) - .payload(Payload.withStringList(plugin.getLocalPlayerList())) + .payload(Payload.withStringList(localPlayerList)) .build().send(plugin.getMessenger(), user); + + // Clear cached global player lists and request updated lists from all servers + if (plugin.getOnlineUsers().size() == 1) { + plugin.getGlobalPlayerList().clear(); + Message.builder() + .type(Message.Type.REQUEST_PLAYER_LIST) + .scope(Message.Scope.SERVER) + .target(Message.TARGET_ALL) + .build().send(plugin.getMessenger(), user); + } } /** diff --git a/common/src/main/java/net/william278/huskhomes/network/Broker.java b/common/src/main/java/net/william278/huskhomes/network/Broker.java index fd707397..475fcf39 100644 --- a/common/src/main/java/net/william278/huskhomes/network/Broker.java +++ b/common/src/main/java/net/william278/huskhomes/network/Broker.java @@ -6,7 +6,6 @@ import net.william278.huskhomes.position.Warp; import net.william278.huskhomes.teleport.Teleport; import net.william278.huskhomes.user.OnlineUser; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import java.util.Optional; import java.util.UUID; @@ -30,8 +29,8 @@ public abstract class Broker { * @param receiver The user who received the message, if a receiver exists * @param message The message */ - protected void handle(@Nullable OnlineUser receiver, @NotNull Message message) { - if (message.getSourceServer().equals(getServer()) || receiver == null) { + protected void handle(@NotNull OnlineUser receiver, @NotNull Message message) { + if (message.getSourceServer().equals(getServer())) { return; } switch (message.getType()) { diff --git a/common/src/main/java/net/william278/huskhomes/network/Message.java b/common/src/main/java/net/william278/huskhomes/network/Message.java index 8a0c86c3..5a38ed40 100644 --- a/common/src/main/java/net/william278/huskhomes/network/Message.java +++ b/common/src/main/java/net/william278/huskhomes/network/Message.java @@ -1,6 +1,7 @@ package net.william278.huskhomes.network; import com.google.gson.annotations.Expose; +import com.google.gson.annotations.SerializedName; import net.william278.huskhomes.user.OnlineUser; import org.jetbrains.annotations.NotNull; @@ -30,6 +31,7 @@ public class Message { @Expose private String sender; @Expose + @SerializedName("source_server") private String sourceServer; private Message(@NotNull Type type, @NotNull Scope scope, @NotNull String target, @NotNull Payload payload) { @@ -127,6 +129,12 @@ public class Message { @NotNull public Message build() { + if (type == null) { + throw new IllegalStateException("Message type must be set"); + } + if (target == null) { + throw new IllegalStateException("Message target must be set"); + } return new Message(type, scope, target, payload); } diff --git a/common/src/main/java/net/william278/huskhomes/network/RedisBroker.java b/common/src/main/java/net/william278/huskhomes/network/RedisBroker.java index 8c9862ee..a0451e6e 100644 --- a/common/src/main/java/net/william278/huskhomes/network/RedisBroker.java +++ b/common/src/main/java/net/william278/huskhomes/network/RedisBroker.java @@ -57,7 +57,8 @@ public class RedisBroker extends PluginMessageBroker { return; } - if (message.getTarget().equals(plugin.getServerName())) { + if (message.getTarget().equals(plugin.getServerName()) + || message.getTarget().equals(Message.TARGET_ALL)) { plugin.getOnlineUsers().stream() .findAny() .ifPresent(receiver -> handle(receiver, message)); 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 4a2e06c0..7675d3f8 100644 --- a/common/src/main/java/net/william278/huskhomes/teleport/Teleport.java +++ b/common/src/main/java/net/william278/huskhomes/teleport/Teleport.java @@ -36,7 +36,7 @@ public class Teleport { this.target = target; this.type = type; this.economyActions = actions; - this.async = plugin.getSettings().isAsynchronousTeleports(); + this.async = plugin.getSettings().doAsynchronousTeleports(); this.updateLastPosition = updateLastPosition && plugin.getCommand(BackCommand.class) .map(command -> executor.hasPermission(command.getPermission())) .orElse(false); diff --git a/common/src/main/java/net/william278/huskhomes/teleport/TeleportRequest.java b/common/src/main/java/net/william278/huskhomes/teleport/TeleportRequest.java index 54ff9839..e5efd864 100644 --- a/common/src/main/java/net/william278/huskhomes/teleport/TeleportRequest.java +++ b/common/src/main/java/net/william278/huskhomes/teleport/TeleportRequest.java @@ -27,6 +27,7 @@ public class TeleportRequest { @Expose private Status status; @Expose + @SerializedName("recipient_name") private String recipientName; /**