From 65e7a7bb739ecd4dc1652f4f467d2a5f6fabf96b Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Tue, 15 Sep 2020 22:47:58 +0200 Subject: [PATCH] Basic Packet System --- src/de/steamwar/bungeecore/BungeeCore.java | 28 ++++++++++++++++- .../bungeecore/commands/CheckCommand.java | 4 +-- .../bungeecore/coms/packets/PingPacket.java | 24 ++++++++++++++ .../bungeecore/coms/receiver/Handler.java | 10 ++++++ .../coms/receiver/PacketHandler.java | 31 +++++++++++++++++++ .../bungeecore/coms/sender/Packet.java | 20 ++++++++++++ .../bungeecore/coms/sender/PacketSender.java | 31 +++++++++++++++++++ .../bungeecore/listeners/ChatListener.java | 2 ++ 8 files changed, 147 insertions(+), 3 deletions(-) create mode 100644 src/de/steamwar/bungeecore/coms/packets/PingPacket.java create mode 100644 src/de/steamwar/bungeecore/coms/receiver/Handler.java create mode 100644 src/de/steamwar/bungeecore/coms/receiver/PacketHandler.java create mode 100644 src/de/steamwar/bungeecore/coms/sender/Packet.java create mode 100644 src/de/steamwar/bungeecore/coms/sender/PacketSender.java diff --git a/src/de/steamwar/bungeecore/BungeeCore.java b/src/de/steamwar/bungeecore/BungeeCore.java index ad9ced66..5d51f29c 100644 --- a/src/de/steamwar/bungeecore/BungeeCore.java +++ b/src/de/steamwar/bungeecore/BungeeCore.java @@ -39,9 +39,12 @@ import net.md_5.bungee.api.plugin.Plugin; import net.md_5.bungee.config.Configuration; import net.md_5.bungee.config.ConfigurationProvider; import net.md_5.bungee.config.YamlConfiguration; +import org.yaml.snakeyaml.error.YAMLException; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; +import java.nio.charset.MalformedInputException; import java.time.format.DateTimeFormatter; import java.util.HashMap; import java.util.List; @@ -127,6 +130,8 @@ public class BungeeCore extends Plugin { new TablistManager(); + getProxy().registerChannel("sw:bridge"); + getProxy().getScheduler().schedule(this, () -> { SteamwarUser.clearCache(); Team.clearCache(); @@ -208,7 +213,28 @@ public class BungeeCore extends Plugin { ProxyServer.getInstance().stop("Could not save conig file"); return; } - config = ConfigurationProvider.getProvider(YamlConfiguration.class).load(configFile); + try { + config = ConfigurationProvider.getProvider(YamlConfiguration.class).load(configFile); + } + catch (YAMLException e) { + e.printStackTrace(); + CHAT_PREFIX = "§eSteam§8War» §r"; + WORLD_FOLDER = "/home/minecraft/userworlds/"; + BAUWELT_PROTOTYP = "/home/minecraft/backbone/server/UserBau/Bauwelt2"; + LOBBY_SERVER = "Lobby"; + USERWORLDS15 = "/home/minecraft/userworlds15/"; + BAUWELT15 = "/home/minecraft/backbone/server/Bau15/Bauwelt"; + Broadcaster.setBroadCastMsgs(new String[] {""}); + Persistent.setChatPrefix(CHAT_PREFIX); + Persistent.setLobbyServer(LOBBY_SERVER); + + SQL.connect( + "jdbc:mysql://127.0.0.1:3306/developer", + "", + "" + ); + return; + } }catch(IOException e){ log("Could not save/load config.yml", e); ProxyServer.getInstance().stop(); diff --git a/src/de/steamwar/bungeecore/commands/CheckCommand.java b/src/de/steamwar/bungeecore/commands/CheckCommand.java index 2d03f4ef..24a11566 100644 --- a/src/de/steamwar/bungeecore/commands/CheckCommand.java +++ b/src/de/steamwar/bungeecore/commands/CheckCommand.java @@ -67,8 +67,8 @@ public class CheckCommand extends BasicCommand { ProxyServer.getInstance().getScheduler().schedule(BungeeCore.get(), () -> { List schematics = getSchemsToCheck(); - if(!schematics.isEmpty()) - ServerTeamchatCommand.sendToTeam("§7Es sind §e" + schematics.size() + " §7Schematics zu prüfen§8!"); + if(schematics.size() != currentCheckers.size()) + ServerTeamchatCommand.sendToTeam("§7Es sind §e" + (schematics.size() - currentCheckers.size()) + " §7Schematics zu prüfen§8!"); }, 10, 10, TimeUnit.MINUTES); } diff --git a/src/de/steamwar/bungeecore/coms/packets/PingPacket.java b/src/de/steamwar/bungeecore/coms/packets/PingPacket.java new file mode 100644 index 00000000..fe982327 --- /dev/null +++ b/src/de/steamwar/bungeecore/coms/packets/PingPacket.java @@ -0,0 +1,24 @@ +package de.steamwar.bungeecore.coms.packets; + +import com.google.common.io.ByteArrayDataOutput; +import de.steamwar.bungeecore.coms.sender.Packet; + +public class PingPacket extends Packet { + + String UUID; + + @Override + public String getName() { + return "Ping"; + } + + @Override + public void writeVars(ByteArrayDataOutput byteArrayDataOutput) { + byteArrayDataOutput.writeUTF(UUID); + } + + public PingPacket setUUID(String UUID) { + this.UUID = UUID; + return this; + } +} diff --git a/src/de/steamwar/bungeecore/coms/receiver/Handler.java b/src/de/steamwar/bungeecore/coms/receiver/Handler.java new file mode 100644 index 00000000..bccaf850 --- /dev/null +++ b/src/de/steamwar/bungeecore/coms/receiver/Handler.java @@ -0,0 +1,10 @@ +package de.steamwar.bungeecore.coms.receiver; + +import com.google.common.io.ByteArrayDataInput; + +public abstract class Handler { + + protected abstract String getName(); + + public abstract void handle(ByteArrayDataInput byteArrayDataInput); +} diff --git a/src/de/steamwar/bungeecore/coms/receiver/PacketHandler.java b/src/de/steamwar/bungeecore/coms/receiver/PacketHandler.java new file mode 100644 index 00000000..59d5c795 --- /dev/null +++ b/src/de/steamwar/bungeecore/coms/receiver/PacketHandler.java @@ -0,0 +1,31 @@ +package de.steamwar.bungeecore.coms.receiver; + +import com.google.common.io.ByteArrayDataInput; +import com.google.common.io.ByteStreams; +import de.steamwar.bungeecore.listeners.BasicListener; +import net.md_5.bungee.api.event.PluginMessageEvent; +import net.md_5.bungee.event.EventHandler; + +import java.util.HashMap; +import java.util.Map; + +public class PacketHandler extends BasicListener { + + private static Map handlerMap = new HashMap<>(); + + @EventHandler + public void onPluginMessage(PluginMessageEvent event) { + if(!event.getTag().equalsIgnoreCase("sw:bridge")) + return; + ByteArrayDataInput in = ByteStreams.newDataInput( event.getData() ); + handlerMap.get(in.readUTF()).handle(in); + } + + public static void registerHandler(Handler handler) { + handlerMap.put(handler.getName(), handler); + } + + static { + //TODO Register Handlers + } +} diff --git a/src/de/steamwar/bungeecore/coms/sender/Packet.java b/src/de/steamwar/bungeecore/coms/sender/Packet.java new file mode 100644 index 00000000..a7d92b6b --- /dev/null +++ b/src/de/steamwar/bungeecore/coms/sender/Packet.java @@ -0,0 +1,20 @@ +package de.steamwar.bungeecore.coms.sender; + +import com.google.common.io.ByteArrayDataOutput; +import net.md_5.bungee.api.connection.ProxiedPlayer; +import net.md_5.bungee.api.connection.Server; + +public abstract class Packet { + + public void send(Server server) { + PacketSender.sendPacket(this, server); + } + + public void send(ProxiedPlayer player) { + PacketSender.sendPacket(this, player); + } + + public abstract String getName(); + + public abstract void writeVars(ByteArrayDataOutput byteArrayDataOutput); +} diff --git a/src/de/steamwar/bungeecore/coms/sender/PacketSender.java b/src/de/steamwar/bungeecore/coms/sender/PacketSender.java new file mode 100644 index 00000000..5bd6f8df --- /dev/null +++ b/src/de/steamwar/bungeecore/coms/sender/PacketSender.java @@ -0,0 +1,31 @@ +package de.steamwar.bungeecore.coms.sender; + +import com.google.common.io.ByteArrayDataOutput; +import com.google.common.io.ByteStreams; +import net.md_5.bungee.api.connection.ProxiedPlayer; +import net.md_5.bungee.api.connection.Server; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectOutputStream; + +public class PacketSender { + + public static void sendPacket(Packet packet, ProxiedPlayer player) { + ByteArrayDataOutput out = ByteStreams.newDataOutput(); + out.writeUTF(packet.getName()); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + packet.writeVars(out); + out.writeUTF(outputStream.toByteArray().toString()); + player.getServer().sendData("sw:bridge", out.toByteArray()); + } + + public static void sendPacket(Packet packet, Server server) { + ByteArrayDataOutput out = ByteStreams.newDataOutput(); + out.writeUTF(packet.getName()); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + packet.writeVars(out); + out.writeUTF(outputStream.toByteArray().toString()); + server.sendData("sw:bridge", out.toByteArray()); + } +} diff --git a/src/de/steamwar/bungeecore/listeners/ChatListener.java b/src/de/steamwar/bungeecore/listeners/ChatListener.java index 554db08b..3faf3110 100644 --- a/src/de/steamwar/bungeecore/listeners/ChatListener.java +++ b/src/de/steamwar/bungeecore/listeners/ChatListener.java @@ -24,6 +24,7 @@ import de.steamwar.bungeecore.Servertype; import de.steamwar.bungeecore.Subserver; import de.steamwar.bungeecore.commands.BauCommand; import de.steamwar.bungeecore.commands.TpCommand; +import de.steamwar.bungeecore.coms.packets.PingPacket; import de.steamwar.bungeecore.sql.SteamwarUser; import de.steamwar.bungeecore.sql.Team; import net.md_5.bungee.api.ChatColor; @@ -188,6 +189,7 @@ public class ChatListener extends BasicListener { for (String curr : msgSplits) { if(curr.equalsIgnoreCase("@" + target.getName())) { builder.append("§e@").append(target.getName()).append(chatcolor).append(" "); + new PingPacket().setUUID(target.getUniqueId().toString()).send(target); }else { builder.append(curr).append(" "); }