Fix global player list propagation

This commit is contained in:
William committed 2023-03-24 01:00:07 +00:00
1 parent c4320edb2d
commit 519105f359
8 files changed
+51 -25

No files matched your search

@@ -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<Home> {
@@ -361,7 +361,7 @@ public class Settings {
return listItemsPerPage;
}
public boolean isAsynchronousTeleports() {
public boolean doAsynchronousTeleports() {
return asynchronousTeleports;
}
@@ -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<String> 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<String> 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);
}
}
/**
@@ -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()) {
@@ -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);
}
@@ -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));
@@ -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);
@@ -27,6 +27,7 @@ public class TeleportRequest {
@Expose
private Status status;
@Expose
@SerializedName("recipient_name")
private String recipientName;
/**