Add cross server list tracking and handling

This commit is contained in:
BuildTools committed 2021-03-09 13:06:09 +00:00
1 parent fa85860c22
commit 0e9527ce2a
7 files changed
+170 -12

No files matched your search

@@ -0,0 +1,39 @@
package me.william278.huskhomes2;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import java.util.HashMap;
import java.util.HashSet;
public class CrossServerListHandler {
// A HashMap mapping all the HuskHomes servers on the Bungee Network
// (with the same Cluster ID) with their list of players
private static final HashMap<String,HashSet<String>> playerList = new HashMap<>();
// Returns a HashSet of all players on the Network
public static HashSet<String> getGlobalPlayerList() {
HashSet<String> players = getOtherServerPlayerList();
for (Player p : Bukkit.getOnlinePlayers()) {
players.add(p.getName());
}
return players;
}
public static HashSet<String> getOtherServerPlayerList() {
HashSet<String> players = new HashSet<>();
for (String server : playerList.keySet()) {
players.addAll(playerList.get(server));
}
return players;
}
public static void updateHashset(String server, HashSet<String> players) {
playerList.put(server, players);
}
public static void requestHashsetUpdate() {
PluginMessageHandler.requestPlayerLists();
}
}
@@ -176,9 +176,10 @@ public final class HuskHomes extends JavaPlugin {
VaultIntegration.initializeEconomy();
}
// Set up bungee channels if bungee mode is enabled
// Set up bungee channels if bungee mode is enabled and update global player list
if (getSettings().doBungee()) {
setupBungeeChannels();
CrossServerListHandler.requestHashsetUpdate();
}
// Register commands and their associated tab completers
@@ -9,6 +9,7 @@ import me.william278.huskhomes2.teleport.TeleportManager;
import me.william278.huskhomes2.teleport.TeleportRequest;
import me.william278.huskhomes2.teleport.TeleportRequestHandler;
import me.william278.huskhomes2.teleport.points.TeleportationPoint;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.messaging.PluginMessageListener;
@@ -18,6 +19,8 @@ import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
public class PluginMessageHandler implements PluginMessageListener {
@@ -31,7 +34,7 @@ public class PluginMessageHandler implements PluginMessageListener {
p.sendPluginMessage(plugin, "BungeeCord", out.toByteArray());
}
// Send a plugin message
// Send a plugin message (to a specific player)
public static void sendPluginMessage(Player sender, String targetPlayerName, String messageType, String messageData) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
int clusterID = HuskHomes.getSettings().getServerClusterID();
@@ -61,6 +64,83 @@ public class PluginMessageHandler implements PluginMessageListener {
sender.sendPluginMessage(plugin, "BungeeCord", out.toByteArray());
}
// Sends a plugin message request asking for all the player lists on servers
public static void requestPlayerLists() {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
int clusterID = HuskHomes.getSettings().getServerClusterID();
// Send a plugin message to the specified player name
out.writeUTF("Forward");
out.writeUTF("ONLINE");
// Send the HuskHomes message with a specific type
out.writeUTF("HuskHomes:" + clusterID + ":send_online_players");
ByteArrayOutputStream messageBytes = new ByteArrayOutputStream();
DataOutputStream messageOut = new DataOutputStream(messageBytes);
// Send the message data; output an exception if there's an error
try {
messageOut.writeUTF(HuskHomes.getSettings().getServerID());
} catch (IOException e) {
Bukkit.getLogger().warning("An error occurred trying to send a plugin message (" + e.getCause() + ")");
e.printStackTrace();
}
// Write the messages to the output packet
out.writeShort(messageBytes.toByteArray().length);
out.write(messageBytes.toByteArray());
// Send the constructed plugin message packet
for (Player p : Bukkit.getOnlinePlayers()) {
p.sendPluginMessage(plugin, "BungeeCord", out.toByteArray());
break;
}
}
public static void sendPlayerLists() {
sendPlayerList(null);
}
// Sends a plugin message with player list
public static void sendPlayerList(String returnTo) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
int clusterID = HuskHomes.getSettings().getServerClusterID();
// Send a plugin message to the specified player name
out.writeUTF("Forward");
if (returnTo == null) {
returnTo = "ONLINE";
}
out.writeUTF(returnTo);
// Send the HuskHomes message with a specific type
out.writeUTF("HuskHomes:" + clusterID + ":return_online_players");
ByteArrayOutputStream messageBytes = new ByteArrayOutputStream();
DataOutputStream messageOut = new DataOutputStream(messageBytes);
// Send the message data; output an exception if there's an error
try {
StringBuilder playerList = new StringBuilder();
for (Player p : Bukkit.getOnlinePlayers()) {
playerList.append(p.getName()).append(",");
}
messageOut.writeUTF(HuskHomes.getSettings().getServerID() + "/" + StringUtils.removeEnd(playerList.toString(), ","));
} catch (IOException e) {
Bukkit.getLogger().warning("An error occurred trying to send a plugin message (" + e.getCause() + ")");
e.printStackTrace();
}
// Write the messages to the output packet
out.writeShort(messageBytes.toByteArray().length);
out.write(messageBytes.toByteArray());
// Send the constructed plugin message packet
for (Player p : Bukkit.getOnlinePlayers()) {
p.sendPluginMessage(plugin, "BungeeCord", out.toByteArray());
break;
}
}
// When a plugin message is received
@Override
public void onPluginMessageReceived(String channel, Player recipient, byte[] message) {
@@ -162,6 +242,17 @@ public class PluginMessageHandler implements PluginMessageListener {
case "teleport_to_me":
TeleportManager.teleportPlayer(recipient, messageData);
break;
case "send_online_players":
if (Bukkit.getOnlinePlayers().size() > 0) {
sendPlayerList(messageData);
}
break;
case "return_online_players":
String server = messageData.split("/")[0];
String players = messageData.split("/")[1];
HashSet<String> playerList = new HashSet<>(Arrays.asList(players.split(",")));
CrossServerListHandler.updateHashset(server, playerList);
break;
}
}
@@ -1,5 +1,6 @@
package me.william278.huskhomes2.commands;
import me.william278.huskhomes2.CrossServerListHandler;
import me.william278.huskhomes2.HuskHomes;
import me.william278.huskhomes2.MessageManager;
import me.william278.huskhomes2.integrations.VanishChecker;
@@ -75,7 +76,6 @@ public class TpCommand extends CommandBase {
} else {
MessageManager.sendMessage(p, "error_invalid_syntax", command.getUsage());
}
return;
}
}
@@ -89,6 +89,7 @@ public class TpCommand extends CommandBase {
players.add(p.getName());
}
}
players.addAll(CrossServerListHandler.getOtherServerPlayerList());
}
final List<String> tabCompletions = new ArrayList<>();
StringUtil.copyPartialMatches(args[0], players, tabCompletions);
@@ -2,6 +2,7 @@ package me.william278.huskhomes2.listeners;
import me.william278.huskhomes2.HuskHomes;
import me.william278.huskhomes2.MessageManager;
import me.william278.huskhomes2.PluginMessageHandler;
import me.william278.huskhomes2.commands.HomeCommand;
import me.william278.huskhomes2.data.DataManager;
import me.william278.huskhomes2.teleport.TeleportManager;
@@ -12,11 +13,13 @@ import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.chat.hover.content.Text;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
public class PlayerListener implements Listener {
@@ -65,6 +68,19 @@ public class PlayerListener implements Listener {
if (DataManager.getPlayerTeleporting(p)) {
TeleportManager.teleportPlayer(p);
}
// Update player lists globally
if (Bukkit.getOnlinePlayers().size() == 1) {
PluginMessageHandler.requestPlayerLists();
}
PluginMessageHandler.sendPlayerLists();
}
}
@EventHandler
public void onPlayerQuit(PlayerQuitEvent e) {
if (HuskHomes.getSettings().doBungee()) {
PluginMessageHandler.sendPlayerLists();
}
}
@@ -1,5 +1,6 @@
package me.william278.huskhomes2.teleport;
import me.william278.huskhomes2.CrossServerListHandler;
import me.william278.huskhomes2.HuskHomes;
import me.william278.huskhomes2.MessageManager;
import me.william278.huskhomes2.PluginMessageHandler;
@@ -16,7 +17,7 @@ import java.util.Set;
public class TeleportManager {
private static Set<TimedTeleport> queuedTeleports = new HashSet<>();
private static final Set<TimedTeleport> queuedTeleports = new HashSet<>();
private static TeleportationPoint spawnLocation;
@@ -113,10 +114,12 @@ public class TeleportManager {
}
} else {
if (HuskHomes.getSettings().doBungee()) {
teleportHereCrossServer(requester, targetPlayerName);
} else {
MessageManager.sendMessage(requester, "error_player_not_found", targetPlayerName);
if (CrossServerListHandler.getGlobalPlayerList().contains(targetPlayerName)) {
teleportHereCrossServer(requester, targetPlayerName);
return;
}
}
MessageManager.sendMessage(requester, "error_player_not_found", targetPlayerName);
}
}
@@ -140,10 +143,12 @@ public class TeleportManager {
}
} else {
if (HuskHomes.getSettings().doBungee()) {
setTeleportationDestinationCrossServer(requester, targetPlayerName);
} else {
MessageManager.sendMessage(requester, "error_player_not_found", targetPlayerName);
if (CrossServerListHandler.getGlobalPlayerList().contains(targetPlayerName)) {
setTeleportationDestinationCrossServer(requester, targetPlayerName);
return;
}
}
MessageManager.sendMessage(requester, "error_player_not_found", targetPlayerName);
}
}
@@ -1,5 +1,6 @@
package me.william278.huskhomes2.teleport;
import me.william278.huskhomes2.CrossServerListHandler;
import me.william278.huskhomes2.HuskHomes;
import me.william278.huskhomes2.MessageManager;
import me.william278.huskhomes2.PluginMessageHandler;
@@ -83,8 +84,12 @@ public class TeleportRequestHandler {
}
} else {
if (HuskHomes.getSettings().doBungee()) {
sendTeleportRequestCrossServer(requester, targetPlayerName, "tpa");
MessageManager.sendMessage(requester, "tpa_request_sent", targetPlayerName);
if (CrossServerListHandler.getGlobalPlayerList().contains(targetPlayerName)) {
sendTeleportRequestCrossServer(requester, targetPlayerName, "tpa");
MessageManager.sendMessage(requester, "tpa_request_sent", targetPlayerName);
} else {
MessageManager.sendMessage(requester, "error_player_not_found", targetPlayerName);
}
} else {
MessageManager.sendMessage(requester, "error_player_not_found", targetPlayerName);
}