mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-03 14:38:53 +00:00
Fix issue with parallel plugin message requests
This commit is contained in:
1 parent
34881e4904
commit
14669c4ff8
3 files changed
+48
-40
No files matched your search
@@ -3,8 +3,8 @@ package net.william278.huskhomes.messenger;
|
|||||||
import com.google.common.io.ByteArrayDataInput;
|
import com.google.common.io.ByteArrayDataInput;
|
||||||
import com.google.common.io.ByteArrayDataOutput;
|
import com.google.common.io.ByteArrayDataOutput;
|
||||||
import com.google.common.io.ByteStreams;
|
import com.google.common.io.ByteStreams;
|
||||||
import net.william278.huskhomes.HuskHomes;
|
|
||||||
import net.william278.huskhomes.BukkitHuskHomes;
|
import net.william278.huskhomes.BukkitHuskHomes;
|
||||||
|
import net.william278.huskhomes.HuskHomes;
|
||||||
import net.william278.huskhomes.player.BukkitPlayer;
|
import net.william278.huskhomes.player.BukkitPlayer;
|
||||||
import net.william278.huskhomes.player.OnlineUser;
|
import net.william278.huskhomes.player.OnlineUser;
|
||||||
import net.william278.huskhomes.position.Server;
|
import net.william278.huskhomes.position.Server;
|
||||||
@@ -13,6 +13,8 @@ import org.bukkit.plugin.messaging.PluginMessageListener;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
||||||
@@ -55,7 +57,8 @@ public class PluginMessenger extends NetworkMessenger implements PluginMessageLi
|
|||||||
@SuppressWarnings("UnstableApiUsage")
|
@SuppressWarnings("UnstableApiUsage")
|
||||||
public CompletableFuture<String[]> getOnlinePlayerNames(@NotNull OnlineUser requester) {
|
public CompletableFuture<String[]> getOnlinePlayerNames(@NotNull OnlineUser requester) {
|
||||||
final BukkitHuskHomes plugin = (BukkitHuskHomes) this.plugin;
|
final BukkitHuskHomes plugin = (BukkitHuskHomes) this.plugin;
|
||||||
onlinePlayerNamesRequest = new CompletableFuture<>();
|
final CompletableFuture<String[]> future = new CompletableFuture<>();
|
||||||
|
onlinePlayerNamesRequests.add(future);
|
||||||
Bukkit.getScheduler().runTaskLater(plugin, () -> {
|
Bukkit.getScheduler().runTaskLater(plugin, () -> {
|
||||||
final ByteArrayDataOutput pluginMessageWriter = ByteStreams.newDataOutput();
|
final ByteArrayDataOutput pluginMessageWriter = ByteStreams.newDataOutput();
|
||||||
pluginMessageWriter.writeUTF("PlayerList");
|
pluginMessageWriter.writeUTF("PlayerList");
|
||||||
@@ -63,35 +66,39 @@ public class PluginMessenger extends NetworkMessenger implements PluginMessageLi
|
|||||||
((BukkitPlayer) requester).sendPluginMessage(plugin,
|
((BukkitPlayer) requester).sendPluginMessage(plugin,
|
||||||
BUNGEE_PLUGIN_CHANNEL_NAME, pluginMessageWriter.toByteArray());
|
BUNGEE_PLUGIN_CHANNEL_NAME, pluginMessageWriter.toByteArray());
|
||||||
}, PLUGIN_MESSAGE_DELAY_TICKS);
|
}, PLUGIN_MESSAGE_DELAY_TICKS);
|
||||||
return onlinePlayerNamesRequest;
|
return future;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("UnstableApiUsage")
|
@SuppressWarnings("UnstableApiUsage")
|
||||||
public CompletableFuture<String> getServerName(@NotNull OnlineUser requester) {
|
public CompletableFuture<String> getServerName(@NotNull OnlineUser requester) {
|
||||||
final BukkitHuskHomes plugin = (BukkitHuskHomes) this.plugin;
|
final BukkitHuskHomes plugin = (BukkitHuskHomes) this.plugin;
|
||||||
serverNameRequest = new CompletableFuture<>();
|
final CompletableFuture<String> future = new CompletableFuture<>();
|
||||||
|
serverNameRequests.add(future);
|
||||||
Bukkit.getScheduler().runTaskLater(plugin, () -> {
|
Bukkit.getScheduler().runTaskLater(plugin, () -> {
|
||||||
final ByteArrayDataOutput pluginMessageWriter = ByteStreams.newDataOutput();
|
final ByteArrayDataOutput pluginMessageWriter = ByteStreams.newDataOutput();
|
||||||
pluginMessageWriter.writeUTF("GetServer");
|
pluginMessageWriter.writeUTF("GetServer");
|
||||||
((BukkitPlayer) requester).sendPluginMessage(plugin,
|
((BukkitPlayer) requester).sendPluginMessage(plugin,
|
||||||
BUNGEE_PLUGIN_CHANNEL_NAME, pluginMessageWriter.toByteArray());
|
BUNGEE_PLUGIN_CHANNEL_NAME,
|
||||||
|
pluginMessageWriter.toByteArray());
|
||||||
}, PLUGIN_MESSAGE_DELAY_TICKS);
|
}, PLUGIN_MESSAGE_DELAY_TICKS);
|
||||||
return serverNameRequest;
|
return future;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("UnstableApiUsage")
|
@SuppressWarnings("UnstableApiUsage")
|
||||||
public CompletableFuture<String[]> getOnlineServers(@NotNull OnlineUser requester) {
|
public CompletableFuture<String[]> getOnlineServers(@NotNull OnlineUser requester) {
|
||||||
final BukkitHuskHomes plugin = (BukkitHuskHomes) this.plugin;
|
final BukkitHuskHomes plugin = (BukkitHuskHomes) this.plugin;
|
||||||
onlineServersRequest = new CompletableFuture<>();
|
final CompletableFuture<String[]> future = new CompletableFuture<>();
|
||||||
|
onlineServersRequests.add(future);
|
||||||
Bukkit.getScheduler().runTaskLater(plugin, () -> {
|
Bukkit.getScheduler().runTaskLater(plugin, () -> {
|
||||||
final ByteArrayDataOutput pluginMessageWriter = ByteStreams.newDataOutput();
|
final ByteArrayDataOutput pluginMessageWriter = ByteStreams.newDataOutput();
|
||||||
pluginMessageWriter.writeUTF("GetServers");
|
pluginMessageWriter.writeUTF("GetServers");
|
||||||
((BukkitPlayer) requester).sendPluginMessage(plugin,
|
((BukkitPlayer) requester).sendPluginMessage(plugin,
|
||||||
BUNGEE_PLUGIN_CHANNEL_NAME, pluginMessageWriter.toByteArray());
|
BUNGEE_PLUGIN_CHANNEL_NAME,
|
||||||
|
pluginMessageWriter.toByteArray());
|
||||||
}, PLUGIN_MESSAGE_DELAY_TICKS);
|
}, PLUGIN_MESSAGE_DELAY_TICKS);
|
||||||
return onlineServersRequest;
|
return future;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -99,17 +106,20 @@ public class PluginMessenger extends NetworkMessenger implements PluginMessageLi
|
|||||||
public CompletableFuture<Boolean> sendPlayer(@NotNull OnlineUser onlineUser, @NotNull Server server) {
|
public CompletableFuture<Boolean> sendPlayer(@NotNull OnlineUser onlineUser, @NotNull Server server) {
|
||||||
final BukkitHuskHomes plugin = (BukkitHuskHomes) this.plugin;
|
final BukkitHuskHomes plugin = (BukkitHuskHomes) this.plugin;
|
||||||
return getOnlineServers(onlineUser).thenApplyAsync(onlineServers -> {
|
return getOnlineServers(onlineUser).thenApplyAsync(onlineServers -> {
|
||||||
for (String onlineServerName : onlineServers) {
|
final Optional<String> targetServer = Arrays.stream(onlineServers)
|
||||||
if (onlineServerName.equals(server.name)) {
|
.filter(serverName -> serverName.equals(server.name))
|
||||||
Bukkit.getScheduler().runTask(plugin, () -> {
|
.findFirst();
|
||||||
final ByteArrayDataOutput pluginMessageWriter = ByteStreams.newDataOutput();
|
|
||||||
pluginMessageWriter.writeUTF("Connect");
|
if (targetServer.isPresent()) {
|
||||||
pluginMessageWriter.writeUTF(server.name);
|
Bukkit.getScheduler().runTask(plugin, () -> {
|
||||||
((BukkitPlayer) onlineUser).sendPluginMessage(plugin,
|
final ByteArrayDataOutput pluginMessageWriter = ByteStreams.newDataOutput();
|
||||||
BUNGEE_PLUGIN_CHANNEL_NAME, pluginMessageWriter.toByteArray());
|
pluginMessageWriter.writeUTF("Connect");
|
||||||
});
|
pluginMessageWriter.writeUTF(server.name);
|
||||||
return true;
|
((BukkitPlayer) onlineUser).sendPluginMessage(plugin,
|
||||||
}
|
BUNGEE_PLUGIN_CHANNEL_NAME,
|
||||||
|
pluginMessageWriter.toByteArray());
|
||||||
|
});
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
@@ -138,19 +148,20 @@ public class PluginMessenger extends NetworkMessenger implements PluginMessageLi
|
|||||||
messageWriter.writeUTF(NETWORK_MESSAGE_CHANNEL);
|
messageWriter.writeUTF(NETWORK_MESSAGE_CHANNEL);
|
||||||
|
|
||||||
// Write the plugin message
|
// Write the plugin message
|
||||||
try (ByteArrayOutputStream messageByteOutputStream = new ByteArrayOutputStream()) {
|
try (final ByteArrayOutputStream messageByteOutputStream = new ByteArrayOutputStream()) {
|
||||||
try (DataOutputStream messageDataOutputStream = new DataOutputStream(messageByteOutputStream)) {
|
try (DataOutputStream messageDataOutputStream = new DataOutputStream(messageByteOutputStream)) {
|
||||||
messageDataOutputStream.writeUTF(message.toJson());
|
messageDataOutputStream.writeUTF(message.toJson());
|
||||||
messageWriter.writeShort(messageByteOutputStream.toByteArray().length);
|
messageWriter.writeShort(messageByteOutputStream.toByteArray().length);
|
||||||
messageWriter.write(messageByteOutputStream.toByteArray());
|
messageWriter.write(messageByteOutputStream.toByteArray());
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
plugin.getLoggingAdapter().log(Level.SEVERE,
|
plugin.getLoggingAdapter().log(Level.SEVERE, "An error occurred dispatching a plugin message", e);
|
||||||
"An error occurred dispatching a plugin message", e);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send the written message
|
// Send the written message
|
||||||
player.sendPluginMessage(plugin, BUNGEE_PLUGIN_CHANNEL_NAME, messageWriter.toByteArray());
|
player.sendPluginMessage(plugin,
|
||||||
|
BUNGEE_PLUGIN_CHANNEL_NAME,
|
||||||
|
messageWriter.toByteArray());
|
||||||
}, PLUGIN_MESSAGE_DELAY_TICKS);
|
}, PLUGIN_MESSAGE_DELAY_TICKS);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -192,22 +203,16 @@ public class PluginMessenger extends NetworkMessenger implements PluginMessageLi
|
|||||||
}
|
}
|
||||||
case "GetServer" -> {
|
case "GetServer" -> {
|
||||||
final String serverName = pluginMessage.readUTF();
|
final String serverName = pluginMessage.readUTF();
|
||||||
if (serverNameRequest != null) {
|
serverNameRequests.forEach(future -> future.complete(serverName));
|
||||||
serverNameRequest.completeAsync(() -> serverName);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
case "PlayerList" -> {
|
case "PlayerList" -> {
|
||||||
pluginMessage.readUTF(); // Read the server name (unused)
|
pluginMessage.readUTF(); // Read the server name (unused)
|
||||||
final String[] playerNames = pluginMessage.readUTF().split(", ");
|
final String[] playerNames = pluginMessage.readUTF().split(", ");
|
||||||
if (onlinePlayerNamesRequest != null) {
|
onlinePlayerNamesRequests.forEach(future -> future.complete(playerNames));
|
||||||
onlinePlayerNamesRequest.completeAsync(() -> playerNames);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
case "GetServers" -> {
|
case "GetServers" -> {
|
||||||
final String[] serverNames = pluginMessage.readUTF().split(", ");
|
final String[] serverNames = pluginMessage.readUTF().split(", ");
|
||||||
if (onlineServersRequest != null) {
|
onlineServersRequests.forEach(future -> future.complete(serverNames));
|
||||||
onlineServersRequest.completeAsync(() -> serverNames);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,19 +22,19 @@ public abstract class NetworkMessenger {
|
|||||||
protected HashMap<UUID, CompletableFuture<Message>> processingMessages;
|
protected HashMap<UUID, CompletableFuture<Message>> processingMessages;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Future for processing {@link #getServerName(OnlineUser)} requests
|
* List of pending futures for processing {@link #getServerName(OnlineUser)} requests
|
||||||
*/
|
*/
|
||||||
protected CompletableFuture<String> serverNameRequest;
|
protected List<CompletableFuture<String>> serverNameRequests;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Future for processing {@link #getOnlinePlayerNames(OnlineUser)} requests
|
* List of pending futures for processing {@link #getOnlinePlayerNames(OnlineUser)} requests
|
||||||
*/
|
*/
|
||||||
protected CompletableFuture<String[]> onlinePlayerNamesRequest;
|
protected List<CompletableFuture<String[]>> onlinePlayerNamesRequests;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Future for processing {@link #getOnlineServers(OnlineUser)} requests
|
* List of pending futures for processing {@link #getOnlineServers(OnlineUser)} requests
|
||||||
*/
|
*/
|
||||||
protected CompletableFuture<String[]> onlineServersRequest;
|
protected List<CompletableFuture<String[]>> onlineServersRequests;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ID of the HuskHomes network cluster this server is on
|
* ID of the HuskHomes network cluster this server is on
|
||||||
@@ -53,6 +53,9 @@ public abstract class NetworkMessenger {
|
|||||||
*/
|
*/
|
||||||
public void initialize(@NotNull HuskHomes implementor) {
|
public void initialize(@NotNull HuskHomes implementor) {
|
||||||
this.processingMessages = new HashMap<>();
|
this.processingMessages = new HashMap<>();
|
||||||
|
this.serverNameRequests = new ArrayList<>();
|
||||||
|
this.onlinePlayerNamesRequests = new ArrayList<>();
|
||||||
|
this.onlineServersRequests = new ArrayList<>();
|
||||||
this.clusterId = implementor.getSettings().clusterId;
|
this.clusterId = implementor.getSettings().clusterId;
|
||||||
this.plugin = implementor;
|
this.plugin = implementor;
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -3,7 +3,7 @@ org.gradle.jvmargs='-Dfile.encoding=UTF-8'
|
|||||||
org.gradle.daemon=true
|
org.gradle.daemon=true
|
||||||
javaVersion=16
|
javaVersion=16
|
||||||
|
|
||||||
plugin_version=3.1
|
plugin_version=3.1.1
|
||||||
plugin_archive=huskhomes
|
plugin_archive=huskhomes
|
||||||
|
|
||||||
jedis_version=4.2.3
|
jedis_version=4.2.3
|
||||||
|
|||||||
Reference in new issue
Block a user