SteamWar/SpigotCore
Archiviert
13
0

Add Basic Packet System

Dieser Commit ist enthalten in:
Chaoscaot 2020-09-15 22:54:39 +02:00
Ursprung 7f6302a0ce
Commit 2dcecc5a62
8 geänderte Dateien mit 145 neuen und 1 gelöschten Zeilen

Datei anzeigen

@ -0,0 +1,18 @@
package de.steamwar.coms;
import de.steamwar.coms.sender.PacketSender;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public abstract class Packet implements Serializable {
public void send() {
PacketSender.sendPacket(this);
}
public abstract String getName();
public abstract void writeVars(ObjectOutputStream objectOutputStream) throws IOException;
}

Datei anzeigen

@ -0,0 +1,10 @@
package de.steamwar.coms.receiver;
import com.google.common.io.ByteArrayDataInput;
public abstract class Handler {
protected abstract String getName();
public abstract void handle(ByteArrayDataInput byteArrayDataInput) throws Exception;
}

Datei anzeigen

@ -0,0 +1,35 @@
package de.steamwar.coms.receiver;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteStreams;
import de.steamwar.coms.receiver.handlers.PingHandler;
import de.steamwar.coms.receiver.handlers.TestHandler;
import org.bukkit.entity.Player;
import org.bukkit.plugin.messaging.PluginMessageListener;
import java.util.HashMap;
import java.util.Map;
public class PacketHandler implements PluginMessageListener {
private static Map<String, Handler> handlerMap = new HashMap<>();
public static void registerHandler(Handler handler) {
handlerMap.put(handler.getName(), handler);
}
static {
registerHandler(new TestHandler());
registerHandler(new PingHandler());
}
@Override
public void onPluginMessageReceived(String s, Player player, byte[] bytes) {
ByteArrayDataInput in = ByteStreams.newDataInput(bytes);
try {
handlerMap.get(in.readUTF()).handle(in);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Datei anzeigen

@ -0,0 +1,29 @@
package de.steamwar.coms.receiver.handlers;
import com.google.common.io.ByteArrayDataInput;
import de.steamwar.coms.receiver.Handler;
import org.bukkit.Bukkit;
import org.bukkit.Sound;
import org.bukkit.SoundCategory;
import org.bukkit.entity.Player;
import java.util.UUID;
public class PingHandler extends Handler {
@Override
protected String getName() {
return "Ping";
}
@Override
public void handle(ByteArrayDataInput byteArrayDataInput) throws Exception {
String uuid = byteArrayDataInput.readUTF();
System.out.println(uuid);
if(Bukkit.getPlayer(UUID.fromString(uuid)).isOnline()) {
Player player = Bukkit.getPlayer(UUID.fromString(uuid));
player.playSound(player.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, SoundCategory.MASTER, 1, 1);
}
}
}

Datei anzeigen

@ -0,0 +1,17 @@
package de.steamwar.coms.receiver.handlers;
import com.google.common.io.ByteArrayDataInput;
import de.steamwar.coms.receiver.Handler;
import org.bukkit.Bukkit;
public class TestHandler extends Handler {
@Override
protected String getName() {
return "Test";
}
@Override
public void handle(ByteArrayDataInput byteArrayDataInput) throws Exception {
Bukkit.getOnlinePlayers().stream().forEach(player -> player.sendMessage("TEST!"));
}
}

Datei anzeigen

@ -0,0 +1,30 @@
package de.steamwar.coms.sender;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import de.steamwar.coms.Packet;
import de.steamwar.core.Core;
import org.bukkit.Bukkit;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.stream.Collectors;
public class PacketSender {
public static void sendPacket(Packet packet) {
try {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF(packet.getName());
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
packet.writeVars(objectOutputStream);
objectOutputStream.flush();
out.writeUTF(outputStream.toByteArray().toString());
Bukkit.getOnlinePlayers().stream().limit(1).collect(Collectors.toList()).get(0).sendPluginMessage(Core.getInstance(), "sw:bridge", out.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
}
}

Datei anzeigen

@ -19,6 +19,7 @@
package de.steamwar.core;
import de.steamwar.coms.receiver.PacketHandler;
import de.steamwar.core.events.ChattingEvent;
import de.steamwar.core.events.ChunkListener;
import de.steamwar.core.events.PlayerJoinedEvent;
@ -61,6 +62,9 @@ public class Core extends JavaPlugin{
ChunkListener.init();
if(version >= 12)
ErrorLogger.init();
getServer().getMessenger().registerIncomingPluginChannel( this, "sw:bridge", new PacketHandler());
getServer().getMessenger().registerOutgoingPluginChannel( this, "sw:bridge" );
}
@Override

Datei anzeigen

@ -19,13 +19,14 @@
package de.steamwar.sql;
import javax.xml.bind.DatatypeConverter;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.Instant;
import javax.xml.bind.DatatypeConverter;
public class DownloadSchematic {
private DownloadSchematic(){}