Packet System + Bungee GUI #59
@ -2,6 +2,8 @@ package de.steamwar.coms.receiver;
|
||||
|
||||
import com.google.common.io.ByteArrayDataInput;
|
||||
import com.google.common.io.ByteStreams;
|
||||
import de.steamwar.coms.receiver.handlers.CloseInventoryHandler;
|
||||
import de.steamwar.coms.receiver.handlers.InventoryHandler;
|
||||
import de.steamwar.coms.receiver.handlers.PingHandler;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.messaging.PluginMessageListener;
|
||||
@ -19,6 +21,8 @@ public class PacketHandler implements PluginMessageListener {
|
||||
|
||||
static {
|
||||
registerHandler(new PingHandler());
|
||||
registerHandler(new InventoryHandler());
|
||||
registerHandler(new CloseInventoryHandler());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -0,0 +1,21 @@
|
||||
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.entity.Player;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class CloseInventoryHandler extends Handler {
|
||||
@Override
|
||||
protected String getName() {
|
||||
return "InvClose";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(ByteArrayDataInput byteArrayDataInput) {
|
||||
Player player = Bukkit.getPlayer(UUID.fromString(byteArrayDataInput.readUTF()));
|
||||
|
||||
player.closeInventory();
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package de.steamwar.coms.receiver.handlers;
|
||||
|
||||
import com.google.common.io.ByteArrayDataInput;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import de.steamwar.coms.receiver.Handler;
|
||||
@ -14,8 +15,6 @@ import java.util.*;
|
||||
|
||||
public class InventoryHandler extends Handler {
|
||||
|
||||
private static HashMap<String, String> callbacks = new HashMap<>();
|
||||
|
||||
@Override
|
||||
protected String getName() {
|
||||
return "Inventory";
|
||||
@ -23,13 +22,12 @@ public class InventoryHandler extends Handler {
|
||||
|
||||
@Override
|
||||
public void handle(ByteArrayDataInput byteArrayDataInput) {
|
||||
String name = byteArrayDataInput.readUTF();
|
||||
String player = byteArrayDataInput.readUTF();
|
||||
String title = byteArrayDataInput.readUTF();
|
||||
int size = byteArrayDataInput.readInt();
|
||||
int lenght = byteArrayDataInput.readInt();
|
||||
int length = byteArrayDataInput.readInt();
|
||||
Map<Integer, SWItem> items = new HashMap<>();
|
||||
for (int i = 0; i < lenght; i++) {
|
||||
for (int i = 0; i < length; i++) {
|
||||
JsonObject object = new JsonParser().parse(byteArrayDataInput.readUTF()).getAsJsonObject();
|
||||
Lixfel
hat
Wenn wir hier schon JSON verwenden, könnte man nicht stattdessen das gesamte Paket als JSON übertragen? (Oder das ganze Protokoll als JSON aufbauen?). Dann sparst du dir length, weil das einfach eine JSON-Liste ist. Wenn wir hier schon JSON verwenden, könnte man nicht stattdessen das gesamte Paket als JSON übertragen? (Oder das ganze Protokoll als JSON aufbauen?). Dann sparst du dir length, weil das einfach eine JSON-Liste ist.
|
||||
SWItem item = new SWItem(Material.valueOf(object.get("material").getAsString()), object.get("title").getAsString());
|
||||
if(object.has("skullOwner")) {
|
||||
@ -40,14 +38,25 @@ public class InventoryHandler extends Handler {
|
||||
item.setEnchanted(true);
|
||||
if(object.has("hideAttributes"))
|
||||
Lixfel
hat
Ich glaube, die Verzauberungen verstecken wir immer. Ich glaube, die Verzauberungen verstecken wir immer.
|
||||
item.hideAttributes();
|
||||
if(object.has("lore")) {
|
||||
List<String> lore = new ArrayList<>();
|
||||
JsonArray array = object.getAsJsonArray("lore");
|
||||
array.forEach(jsonElement -> lore.add(jsonElement.getAsString()));
|
||||
item.setLore(lore);
|
||||
}
|
||||
SWItem finalItem = item;
|
||||
item.setCallback(click -> {
|
||||
new InventoryCallbackPacket().setItem(finalItem).setPosition(object.get("position").getAsInt()).setTitle(title).send();
|
||||
new InventoryCallbackPacket().setItem(finalItem).setClick(click)
|
||||
.setPosition(object.get("position").getAsInt())
|
||||
.setCallback(InventoryCallbackPacket.CallbackType.CLICK)
|
||||
.setOwner(player).send(Bukkit.getPlayer(UUID.fromString(player)));
|
||||
});
|
||||
items.put(object.get("position").getAsInt(), item);
|
||||
}
|
||||
|
||||
SWInventory inventory = new SWInventory(Bukkit.getPlayer(UUID.fromString(player)), size, title,items);
|
||||
SWInventory inventory = new SWInventory(Bukkit.getPlayer(UUID.fromString(player)), size, title, items);
|
||||
inventory.addCloseCallback(click -> new InventoryCallbackPacket()
|
||||
.setCallback(InventoryCallbackPacket.CallbackType.CLOSE).setOwner(player).send(Bukkit.getPlayer(UUID.fromString(player))));
|
||||
inventory.open();
|
||||
}
|
||||
}
|
||||
|
@ -1,18 +1,17 @@
|
||||
package de.steamwar.coms.sender;
|
||||
|
||||
import de.steamwar.coms.sender.PacketSender;
|
||||
import com.google.common.io.ByteArrayDataOutput;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.Serializable;
|
||||
|
||||
public abstract class Packet implements Serializable {
|
||||
Lixfel
hat
Packet. Auch wieder sehr generisch. Packet. Auch wieder sehr generisch.
|
||||
|
||||
public void send() {
|
||||
PacketSender.sendPacket(this);
|
||||
public void send(Player player) {
|
||||
PacketSender.sendPacket(this, player);
|
||||
}
|
||||
|
||||
public abstract String getName();
|
||||
|
||||
public abstract void writeVars(ObjectOutputStream objectOutputStream) throws IOException;
|
||||
public abstract void writeVars(ByteArrayDataOutput byteArrayDataOutput);
|
||||
}
|
||||
|
@ -3,27 +3,14 @@ package de.steamwar.coms.sender;
|
||||
import com.google.common.io.ByteArrayDataOutput;
|
||||
import com.google.common.io.ByteStreams;
|
||||
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;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
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();
|
||||
}
|
||||
public static void sendPacket(Packet packet, Player player) {
|
||||
Lixfel
hat
Die Funktion würde ich eher in Packet integrieren, dafür braucht es nicht eine extra Klasse. Die Funktion würde ich eher in Packet integrieren, dafür braucht es nicht eine extra Klasse.
|
||||
ByteArrayDataOutput out = ByteStreams.newDataOutput();
|
||||
out.writeUTF(packet.getName());
|
||||
packet.writeVars(out);
|
||||
player.sendPluginMessage(Core.getInstance(), "sw:return", out.toByteArray());
|
||||
}
|
||||
}
|
@ -1,15 +1,17 @@
|
||||
package de.steamwar.coms.sender.packets;
|
||||
|
||||
import com.google.common.io.ByteArrayDataOutput;
|
||||
import de.steamwar.coms.sender.Packet;
|
||||
import de.steamwar.inventory.SWItem;
|
||||
|
||||
import java.io.ObjectOutputStream;
|
||||
import org.bukkit.event.inventory.ClickType;
|
||||
|
||||
public class InventoryCallbackPacket extends Packet {
|
||||
|
||||
String title;
|
||||
int position;
|
||||
SWItem item;
|
||||
ClickType clickType;
|
||||
CallbackType callbackType;
|
||||
String owner;
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
@ -17,13 +19,13 @@ public class InventoryCallbackPacket extends Packet {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeVars(ObjectOutputStream objectOutputStream) {
|
||||
|
||||
}
|
||||
|
||||
public InventoryCallbackPacket setTitle(String title) {
|
||||
this.title = title;
|
||||
return this;
|
||||
public void writeVars(ByteArrayDataOutput byteArrayDataOutput) {
|
||||
byteArrayDataOutput.writeUTF(owner);
|
||||
byteArrayDataOutput.writeUTF(callbackType.name());
|
||||
if(item != null){
|
||||
byteArrayDataOutput.writeUTF(item.parseToJson(position));
|
||||
byteArrayDataOutput.writeUTF(clickType.name());
|
||||
}
|
||||
}
|
||||
|
||||
public InventoryCallbackPacket setPosition(int position) {
|
||||
@ -35,4 +37,24 @@ public class InventoryCallbackPacket extends Packet {
|
||||
this.item = item;
|
||||
return this;
|
||||
}
|
||||
|
||||
public InventoryCallbackPacket setClick(ClickType type){
|
||||
this.clickType = type;
|
||||
return this;
|
||||
}
|
||||
|
||||
public InventoryCallbackPacket setCallback(CallbackType callback) {
|
||||
callbackType = callback;
|
||||
return this;
|
||||
}
|
||||
|
||||
public InventoryCallbackPacket setOwner(String uuid) {
|
||||
this.owner = uuid;
|
||||
return this;
|
||||
}
|
||||
|
||||
public enum CallbackType {
|
||||
CLICK,
|
||||
CLOSE,
|
||||
}
|
||||
}
|
||||
|
@ -62,8 +62,8 @@ 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" );
|
||||
getServer().getMessenger().registerIncomingPluginChannel(this, "sw:bridge", new PacketHandler());
|
||||
getServer().getMessenger().registerOutgoingPluginChannel(this, "sw:return");
|
||||
Lixfel
hat
Wozu braucht es den Channel? Wozu braucht es den Channel?
|
||||
|
||||
}
|
||||
|
||||
|
@ -205,7 +205,7 @@ public class SWItem {
|
||||
object.addProperty("enchanted", true);
|
||||
if(itemMeta.hasItemFlag(ItemFlag.HIDE_ENCHANTS))
|
||||
object.addProperty("hideAttributes", true);
|
||||
if(!itemMeta.getLore().isEmpty()) {
|
||||
if(itemMeta.getLore() != null) {
|
||||
JsonArray array = new JsonArray();
|
||||
for (String lores:
|
||||
itemMeta.getLore()) {
|
||||
@ -214,6 +214,6 @@ public class SWItem {
|
||||
object.add("lore", array);
|
||||
}
|
||||
|
||||
return object.getAsString();
|
||||
return object.toString();
|
||||
}
|
||||
}
|
||||
|
In neuem Issue referenzieren
Einen Benutzer sperren
Überlegung: Statt String für den Spieler die SteamWarUser-id?