SteamWar/SpigotCore
Archiviert
13
0

Add Bungee GUI Prototype

Dieser Commit ist enthalten in:
Chaoscaot 2020-09-19 12:39:22 +02:00
Ursprung 691abd0dea
Commit 00c08fc8ad
8 geänderte Dateien mit 88 neuen und 46 gelöschten Zeilen

Datei anzeigen

@ -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

Datei anzeigen

@ -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();
}
}

Datei anzeigen

@ -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();
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"))
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);
inventory.addCloseCallback(click -> new InventoryCallbackPacket()
.setCallback(InventoryCallbackPacket.CallbackType.CLOSE).setOwner(player).send(Bukkit.getPlayer(UUID.fromString(player))));
inventory.open();
}
}

Datei anzeigen

@ -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 {
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);
}

Datei anzeigen

@ -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 {
public static void sendPacket(Packet packet, Player player) {
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();
}
packet.writeVars(out);
player.sendPluginMessage(Core.getInstance(), "sw:return", out.toByteArray());
}
}

Datei anzeigen

@ -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 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 setTitle(String title) {
this.title = title;
return this;
}
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,
}
}

Datei anzeigen

@ -63,7 +63,7 @@ public class Core extends JavaPlugin{
if(version >= 12)
ErrorLogger.init();
getServer().getMessenger().registerIncomingPluginChannel(this, "sw:bridge", new PacketHandler());
getServer().getMessenger().registerOutgoingPluginChannel( this, "sw:bridge" );
getServer().getMessenger().registerOutgoingPluginChannel(this, "sw:return");
}

Datei anzeigen

@ -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();
}
}