add BungeePlayers List / Event for Spigot Server
This commit is contained in:
JaTiTV 2022-12-24 19:05:18 +01:00
parent eb7fd03650
commit 1b12c2728b
14 changed files with 233 additions and 13 deletions

View File

@ -6,7 +6,7 @@
<groupId>net.t2code</groupId> <groupId>net.t2code</groupId>
<artifactId>T2CodeLib</artifactId> <artifactId>T2CodeLib</artifactId>
<version>13.9</version> <version>14.0</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>T2CodeLib</name> <name>T2CodeLib</name>

View File

@ -0,0 +1,99 @@
package net.t2code.t2codelib.BUNGEE.api.bungeePlayers;
import net.md_5.bungee.BungeeCord;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.event.PlayerDisconnectEvent;
import net.md_5.bungee.api.event.PluginMessageEvent;
import net.md_5.bungee.api.event.PostLoginEvent;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.event.EventHandler;
import net.t2code.t2codelib.BUNGEE.api.messages.T2CBsend;
import net.t2code.t2codelib.BUNGEE.system.T2CodeBMain;
import net.t2code.t2codelib.SPIGOT.api.bungeePlayers.T2CbungeePlayersEnum;
import java.io.*;
import java.util.logging.Logger;
public class T2CBbungeePlayers implements Listener {
@EventHandler
public void onJoin(PostLoginEvent e) {
sendToSpigotPlayer(e.getPlayer().getName(), T2CbungeePlayersEnum.JOIN, "");
}
@EventHandler
public void onDisconnect(PlayerDisconnectEvent e) {
sendToSpigotPlayer(e.getPlayer().getName(), T2CbungeePlayersEnum.QUIT, "");
}
public static void sendToSpigotPlayer(String name, T2CbungeePlayersEnum value, String uuid) {
switch (value) {
case JOIN:
case QUIT:
sendToSpigotPlayerExecute(name, value, uuid);
break;
case GIVEALL:
StringBuilder input = new StringBuilder();
for (ProxiedPlayer player : ProxyServer.getInstance().getPlayers()) {
input.append(player.getName()).append(";");
}
sendToSpigotPlayerExecute(input.toString(), T2CbungeePlayersEnum.GIVEALL, uuid);
break;
}
}
private static void sendToSpigotPlayerExecute(String name, T2CbungeePlayersEnum value, String uuid) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
DataOutputStream output = new DataOutputStream(stream);
try {
output.writeUTF(value.name());
output.writeUTF(name);
output.writeUTF(uuid);
} catch (IOException e) {
Logger.getLogger(e.getMessage());
}
T2CBsend.debug(T2CodeBMain.getPlugin(), "PluginMessage received channel: t2c:bonlp");
T2CBsend.debug(T2CodeBMain.getPlugin(), "PluginMessage received subChannel: " + value.name());
T2CBsend.debug(T2CodeBMain.getPlugin(), "PluginMessage received input: " + name);
T2CBsend.debug(T2CodeBMain.getPlugin(), "PluginMessage received input2/uuid: " + uuid);
BungeeCord.getInstance().getServers().values().stream().forEach((server) -> {
server.sendData("t2c:bonlp", stream.toByteArray());
});
}
public static void sendToSpigotDeleteAll() {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
DataOutputStream output = new DataOutputStream(stream);
try {
output.writeUTF(T2CbungeePlayersEnum.CLEAR.name());
output.writeUTF("");
output.writeUTF("");
} catch (IOException e) {
Logger.getLogger(e.getMessage());
}
BungeeCord.getInstance().getServers().values().stream().forEach((server) -> {
server.sendData("t2c:bonlp", stream.toByteArray());
});
}
@EventHandler
public void onPluginMessage(PluginMessageEvent event) {
if (event.getTag().equalsIgnoreCase("t2c:bonlp")) {
DataInputStream stream = new DataInputStream(new ByteArrayInputStream(event.getData()));
event.setCancelled(true);
try {
T2CbungeePlayersEnum subChannel = T2CbungeePlayersEnum.valueOf(stream.readUTF());
String input = stream.readUTF();
T2CBsend.debug(T2CodeBMain.getPlugin(), "PluginMessage received channel: " + event.getTag());
T2CBsend.debug(T2CodeBMain.getPlugin(), "PluginMessage received subChannel: " + subChannel.name());
T2CBsend.debug(T2CodeBMain.getPlugin(), "PluginMessage received input/uuid: " + input);
sendToSpigotPlayer("", subChannel, input);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

View File

@ -5,6 +5,7 @@ import net.t2code.t2codelib.BUNGEE.api.messages.T2CBsend;
import net.t2code.t2codelib.BUNGEE.api.update.T2CBupdateAPI; import net.t2code.t2codelib.BUNGEE.api.update.T2CBupdateAPI;
import net.t2code.t2codelib.BUNGEE.system.bstats.T2CBmetrics; import net.t2code.t2codelib.BUNGEE.system.bstats.T2CBmetrics;
import net.t2code.t2codelib.BUNGEE.system.config.T2CBlibConfig; import net.t2code.t2codelib.BUNGEE.system.config.T2CBlibConfig;
import net.t2code.t2codelib.BUNGEE.api.bungeePlayers.T2CBbungeePlayers;
import net.t2code.t2codelib.BUNGEE.system.pluginMessaging.T2CplmsgBcmd; import net.t2code.t2codelib.BUNGEE.system.pluginMessaging.T2CplmsgBcmd;
import net.t2code.t2codelib.BUNGEE.system.pluginMessaging.autoResponse.T2CapiAutoResponse; import net.t2code.t2codelib.BUNGEE.system.pluginMessaging.autoResponse.T2CapiAutoResponse;
import net.t2code.t2codelib.BUNGEE.system.pluginMessaging.commandgui.T2CapiCGUI; import net.t2code.t2codelib.BUNGEE.system.pluginMessaging.commandgui.T2CapiCGUI;
@ -39,6 +40,10 @@ public class T2CBload {
plugin.getProxy().registerChannel("t2c:bcmd"); plugin.getProxy().registerChannel("t2c:bcmd");
plugin.getProxy().getPluginManager().registerListener(plugin, new T2CplmsgBcmd()); plugin.getProxy().getPluginManager().registerListener(plugin, new T2CplmsgBcmd());
plugin.getProxy().registerChannel("t2c:bonlp");
plugin.getProxy().getPluginManager().registerListener(plugin, new T2CBbungeePlayers());
T2CBbungeePlayers.sendToSpigotDeleteAll();
if (T2CBlibConfig.getApiCommandGUIEnable()) { if (T2CBlibConfig.getApiCommandGUIEnable()) {
plugin.getProxy().registerChannel("t2c:cguiopl"); plugin.getProxy().registerChannel("t2c:cguiopl");
plugin.getProxy().getPluginManager().registerListener(plugin, new T2CapiCGUI()); plugin.getProxy().getPluginManager().registerListener(plugin, new T2CapiCGUI());

View File

@ -1,9 +1,9 @@
package net.t2code.t2codelib.BUNGEE.system; package net.t2code.t2codelib.BUNGEE.system;
import lombok.NonNull;
import net.kyori.adventure.platform.bungeecord.BungeeAudiences; import net.kyori.adventure.platform.bungeecord.BungeeAudiences;
import net.md_5.bungee.api.plugin.Plugin; import net.md_5.bungee.api.plugin.Plugin;
import net.t2code.t2codelib.BUNGEE.api.messages.T2CBsend; import net.t2code.t2codelib.BUNGEE.api.messages.T2CBsend;
import net.t2code.t2codelib.BUNGEE.api.bungeePlayers.T2CBbungeePlayers;
import net.t2code.t2codelib.Util; import net.t2code.t2codelib.Util;
public class T2CodeBMain extends Plugin { public class T2CodeBMain extends Plugin {
@ -51,6 +51,7 @@ public class T2CodeBMain extends Plugin {
@Override @Override
public void onDisable() { public void onDisable() {
// Plugin shutdown logic // Plugin shutdown logic
T2CBbungeePlayers.sendToSpigotDeleteAll();
if (mmIsLoad){ if (mmIsLoad){
if(adventure != null) { if(adventure != null) {
adventure.close(); adventure.close();

View File

@ -0,0 +1,84 @@
package net.t2code.t2codelib.SPIGOT.api.bungeePlayers;
import lombok.Getter;
import net.t2code.t2codelib.SPIGOT.api.messages.T2Csend;
import net.t2code.t2codelib.SPIGOT.system.T2CodeLibMain;
import net.t2code.t2codelib.Util;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.messaging.PluginMessageListener;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class T2CbungeePlayers implements PluginMessageListener {
@Getter
private static List<String> bungeePlayers = new ArrayList<>();
@Override
public void onPluginMessageReceived(String channel, Player player, byte[] message) {
DataInputStream stream = new DataInputStream(new ByteArrayInputStream(message));
T2Csend.debug(T2CodeLibMain.getPlugin(), "stream: " + stream.toString());
try {
T2CbungeePlayersEnum subChannel = T2CbungeePlayersEnum.valueOf(stream.readUTF());
String input = stream.readUTF();
String uuid = stream.readUTF();
T2Csend.debug(T2CodeLibMain.getPlugin(), "PluginMessage received channel: " + channel);
T2Csend.debug(T2CodeLibMain.getPlugin(), "PluginMessage received subChannel: " + subChannel.name());
T2Csend.debug(T2CodeLibMain.getPlugin(), "PluginMessage received input: " + input);
T2Csend.debug(T2CodeLibMain.getPlugin(), "PluginMessage received input2/uuid: " + uuid);
switch (subChannel) {
case JOIN:
bungeePlayers.add(input);
break;
case QUIT:
bungeePlayers.remove(input);
break;
case GIVEALL:
if (!Util.getServerUUID().toString().equals(uuid)) {
return;
}
bungeePlayers.clear();
String[] in = input.split(";");
bungeePlayers.addAll(Arrays.asList(in));
break;
case CLEAR:
bungeePlayers.clear();
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void callAllBungeePlayers() {
if (Bukkit.getOnlinePlayers().isEmpty()) {
Bukkit.getScheduler().runTaskLaterAsynchronously(T2CodeLibMain.getPlugin(), new Runnable() {
@Override
public void run() {
callAllBungeePlayers();
}
}, 20L);
return;
}
ByteArrayOutputStream stream = new ByteArrayOutputStream();
DataOutputStream output = new DataOutputStream(stream);
try {
output.writeUTF(T2CbungeePlayersEnum.GIVEALL.name());
output.writeUTF(Util.getServerUUID().toString());
} catch (IOException e) {
e.printStackTrace();
}
for (Player player : Bukkit.getOnlinePlayers()) {
player.sendPluginMessage(T2CodeLibMain.getPlugin(), "t2c:bonlp", stream.toByteArray());
T2Csend.debug(T2CodeLibMain.getPlugin(), "PluginMessage received channel: t2c:bonlp");
T2Csend.debug(T2CodeLibMain.getPlugin(), "PluginMessage send subChannel: " + T2CbungeePlayersEnum.GIVEALL.name());
T2Csend.debug(T2CodeLibMain.getPlugin(), "PluginMessage send output/uuid: " + Util.getServerUUID().toString());
return;
}
}
}

View File

@ -0,0 +1,8 @@
package net.t2code.t2codelib.SPIGOT.api.bungeePlayers;
public enum T2CbungeePlayersEnum {
JOIN,
QUIT,
GIVEALL,
CLEAR
}

View File

@ -1,24 +1,24 @@
package net.t2code.t2codelib.SPIGOT.api.commands; package net.t2code.t2codelib.SPIGOT.api.commands;
import net.t2code.t2codelib.SPIGOT.system.BCommandSenderReciver; import net.t2code.t2codelib.SPIGOT.system.T2CbungeeCommandSenderReciver;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
public class T2Ccmd { public class T2Ccmd {
public static void console(String cmd) { public static void console(String cmd) {
if (cmd.contains("!onBungee")){ if (cmd.contains("!onBungee")){
BCommandSenderReciver.sendToBungee(null, cmd.replace("!onBungee", ""), true); T2CbungeeCommandSenderReciver.sendToBungee(null, cmd.replace("!onBungee", ""), true);
}else Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), cmd); }else Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), cmd);
} }
public static void player(Player player, String cmd) { public static void player(Player player, String cmd) {
if (cmd.contains("!asConsole")) { if (cmd.contains("!asConsole")) {
if (cmd.contains("!onBungee")) { if (cmd.contains("!onBungee")) {
BCommandSenderReciver.sendToBungee(player, cmd.replace("!asConsole", "").replace("!onBungee", ""), true); T2CbungeeCommandSenderReciver.sendToBungee(player, cmd.replace("!asConsole", "").replace("!onBungee", ""), true);
} else console(cmd.replace("!asConsole", "")); } else console(cmd.replace("!asConsole", ""));
} else { } else {
if (cmd.contains("!onBungee")) { if (cmd.contains("!onBungee")) {
BCommandSenderReciver.sendToBungee(player, cmd.replace("!onBungee", ""), false); T2CbungeeCommandSenderReciver.sendToBungee(player, cmd.replace("!onBungee", ""), false);
} else player.chat("/" + cmd); } else player.chat("/" + cmd);
} }
} }

View File

@ -44,16 +44,20 @@ public class T2Csend {
public static void debug(Plugin plugin, String msg, Integer stage) { public static void debug(Plugin plugin, String msg, Integer stage) {
// if (!new File(Main.getPath(), "config.yml").exists()) return; // if (!new File(Main.getPath(), "config.yml").exists()) return;
if (stage == null) { if (stage == null) {
if (plugin.getConfig().getBoolean("Plugin.Debug")) if (plugin.getConfig().getBoolean("Plugin.Debug") || plugin.getConfig().getBoolean("plugin.debug") || plugin.getConfig().getBoolean("Debug") || plugin.getConfig().getBoolean("debug")){
info(plugin, "");
Bukkit.getConsoleSender().sendMessage(plugin.getDescription().getPrefix() + " §5DEBUG: §6" + msg); Bukkit.getConsoleSender().sendMessage(plugin.getDescription().getPrefix() + " §5DEBUG: §6" + msg);
}
return; return;
} }
if (plugin.getConfig().getInt("Plugin.Debug") >= stage) if (plugin.getConfig().getInt("Plugin.Debug") >= stage || plugin.getConfig().getInt("plugin.debug") >= stage || plugin.getConfig().getInt("Debug") >= stage || plugin.getConfig().getInt("debug") >= stage) {
info(plugin, "");
Bukkit.getConsoleSender().sendMessage(plugin.getDescription().getPrefix() + " §5DEBUG: §6" + msg); Bukkit.getConsoleSender().sendMessage(plugin.getDescription().getPrefix() + " §5DEBUG: §6" + msg);
} }
}
public static void debugmsg(Plugin plugin, String msg) { public static void debugmsg(Plugin plugin, String msg) {
warning(plugin,""); warning(plugin, "");
Bukkit.getConsoleSender().sendMessage("§e[" + plugin.getDescription().getPrefix() + "] §5DEBUG-MSG: §6" + msg); Bukkit.getConsoleSender().sendMessage("§e[" + plugin.getDescription().getPrefix() + "] §5DEBUG-MSG: §6" + msg);
} }

View File

@ -8,7 +8,7 @@ import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
public class BCommandSenderReciver { public class T2CbungeeCommandSenderReciver {
public static void sendToBungee(CommandSender sender, String information, Boolean console) { public static void sendToBungee(CommandSender sender, String information, Boolean console) {
ByteArrayOutputStream stream = new ByteArrayOutputStream(); ByteArrayOutputStream stream = new ByteArrayOutputStream();

View File

@ -4,6 +4,7 @@ import lombok.Getter;
import net.kyori.adventure.platform.bukkit.BukkitAudiences; import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.permission.Permission; import net.milkbowl.vault.permission.Permission;
import net.t2code.t2codelib.SPIGOT.api.bungeePlayers.T2CbungeePlayers;
import net.t2code.t2codelib.SPIGOT.api.items.T2CitemVersion; import net.t2code.t2codelib.SPIGOT.api.items.T2CitemVersion;
import net.t2code.t2codelib.SPIGOT.api.messages.T2Csend; import net.t2code.t2codelib.SPIGOT.api.messages.T2Csend;
import net.t2code.t2codelib.SPIGOT.api.messages.T2Ctemplate; import net.t2code.t2codelib.SPIGOT.api.messages.T2Ctemplate;
@ -50,6 +51,7 @@ public final class T2CodeLibMain extends JavaPlugin {
mmIsLoad = false; mmIsLoad = false;
} }
long long_ = T2Ctemplate.onLoadHeader(Util.getPrefix(), autor, version, Util.getSpigot(), Util.getDiscord()); long long_ = T2Ctemplate.onLoadHeader(Util.getPrefix(), autor, version, Util.getSpigot(), Util.getDiscord());
checkIsBungee();
String prefix = Util.getPrefix(); String prefix = Util.getPrefix();
try { try {
@ -106,11 +108,17 @@ public final class T2CodeLibMain extends JavaPlugin {
Metrics.Bstats(plugin, Util.getBstatsID()); Metrics.Bstats(plugin, Util.getBstatsID());
if (SelectLibConfig.getBungee()) { if (SelectLibConfig.getBungee()) {
Bukkit.getMessenger().registerOutgoingPluginChannel(plugin, "t2c:bcmd"); Bukkit.getMessenger().registerOutgoingPluginChannel(plugin, "t2c:bcmd");
Bukkit.getMessenger().registerOutgoingPluginChannel(plugin, "t2c:bonlp");
if (!Bukkit.getMessenger().isIncomingChannelRegistered(plugin, "t2c:bonlp")) {
T2Csend.debug(plugin, "registerIncomingPluginChannel §et2c:bonlp");
Bukkit.getMessenger().registerIncomingPluginChannel(plugin, "t2c:bonlp", new T2CbungeePlayers());
T2CbungeePlayers.callAllBungeePlayers();
}
} }
ReportLogStorage.load(); ReportLogStorage.load();
Bukkit.getServer().getPluginManager().registerEvents(new JoinEvent(), plugin); Bukkit.getServer().getPluginManager().registerEvents(new JoinEvent(), plugin);
checkIsBungee();
T2Ctemplate.onLoadFooter(prefix, long_); T2Ctemplate.onLoadFooter(prefix, long_);
load = true; load = true;
} }

View File

@ -2,6 +2,7 @@ package net.t2code.t2codelib.SPIGOT.system.cmd;
import net.t2code.t2codelib.SPIGOT.api.messages.T2Csend; import net.t2code.t2codelib.SPIGOT.api.messages.T2Csend;
import net.t2code.t2codelib.SPIGOT.api.update.T2CupdateAPI; import net.t2code.t2codelib.SPIGOT.api.update.T2CupdateAPI;
import net.t2code.t2codelib.SPIGOT.api.bungeePlayers.T2CbungeePlayers;
import net.t2code.t2codelib.T2CupdateObject; import net.t2code.t2codelib.T2CupdateObject;
import net.t2code.t2codelib.SPIGOT.system.config.config.SelectLibConfig; import net.t2code.t2codelib.SPIGOT.system.config.config.SelectLibConfig;
import org.bukkit.command.Command; import org.bukkit.command.Command;
@ -44,6 +45,9 @@ public class CmdExecuter implements CommandExecutor, TabCompleter {
case "debug": case "debug":
Commands.debug(sender,args); Commands.debug(sender,args);
return false; return false;
case "test":
T2Csend.sender(sender, T2CbungeePlayers.getBungeePlayers().toString());
return false;
default: default:
T2Csend.sender(sender, "§4Use: §7/t2code debug createReportLog"); T2Csend.sender(sender, "§4Use: §7/t2code debug createReportLog");

View File

@ -27,7 +27,7 @@ public class ConfigCreate {
T2Cconfig.set("Plugin.UpdateCheck.SeePreReleaseUpdates", true, yamlConfiguration); T2Cconfig.set("Plugin.UpdateCheck.SeePreReleaseUpdates", true, yamlConfiguration);
T2Cconfig.set("Plugin.language", "english", yamlConfiguration); T2Cconfig.set("Plugin.language", "english", yamlConfiguration);
T2Cconfig.set("BungeeCord.Enable", false, yamlConfiguration); T2Cconfig.set("BungeeCord.Enable", T2CodeLibMain.getIsBungee(), yamlConfiguration);
T2Cconfig.set("Player.Inventories.CloseByServerStop", true, yamlConfiguration); T2Cconfig.set("Player.Inventories.CloseByServerStop", true, yamlConfiguration);
try { try {

View File

@ -20,7 +20,7 @@ public class SelectLibConfig {
File config = new File(T2CodeLibMain.getPath(), "config.yml"); File config = new File(T2CodeLibMain.getPath(), "config.yml");
YamlConfiguration yamlConfiguration = YamlConfiguration.loadConfiguration(config); YamlConfiguration yamlConfiguration = YamlConfiguration.loadConfiguration(config);
if (yamlConfiguration.contains("t2cTestDevelopment")){ if (yamlConfiguration.contains("t2cTestDevelopment")) {
t2cTestDevelopment = yamlConfiguration.getBoolean("t2cTestDevelopment"); t2cTestDevelopment = yamlConfiguration.getBoolean("t2cTestDevelopment");
} }

View File

@ -1,10 +1,17 @@
package net.t2code.t2codelib; package net.t2code.t2codelib;
import lombok.Getter;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.UUID;
public class Util { public class Util {
@Getter
private static final UUID serverUUID = UUID.randomUUID();
public static String getInfoText() { public static String getInfoText() {
return ""; return "";
} }