SteamWar/SpigotCore
Archiviert
13
0

Bungee GUI Improvements

Dieser Commit ist enthalten in:
Chaoscaot 2020-09-20 12:01:12 +02:00
Ursprung 00c08fc8ad
Commit 2155e386fd
14 geänderte Dateien mit 157 neuen und 231 gelöschten Zeilen

Datei anzeigen

@ -0,0 +1,51 @@
package de.steamwar.comms;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteStreams;
import de.steamwar.comms.handlers.BungeeHandler;
import de.steamwar.comms.handlers.InventoryHandler;
import de.steamwar.sql.SteamwarUser;
import org.bukkit.Bukkit;
import org.bukkit.Sound;
import org.bukkit.SoundCategory;
import org.bukkit.entity.Player;
import org.bukkit.plugin.messaging.PluginMessageListener;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class BungeeReceiver implements PluginMessageListener {
private static Map<Integer, BungeeHandler> handlerMap = new HashMap<>();
public static void registerHandler(int code, BungeeHandler handler) {
handlerMap.put(code, handler);
}
public static void registerHandler(String name, BungeeHandler handler) {
handlerMap.put(name.hashCode(), handler);
}
static {
registerHandler("PingHandler", byteArrayDataInput -> {
UUID uuid = SteamwarUser.get(byteArrayDataInput.readInt()).getUUID();
if(Bukkit.getPlayer(uuid).isOnline()) {
Player player = Bukkit.getPlayer(uuid);
player.playSound(player.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, SoundCategory.MASTER, 1, 1);
}
});
registerHandler("InventoryHandler", new InventoryHandler());
registerHandler("CloseInventory", byteArrayDataInput -> {
Player player = Bukkit.getPlayer(SteamwarUser.get(byteArrayDataInput.readInt()).getUUID());
player.closeInventory();
});
}
@Override
public void onPluginMessageReceived(String s, Player player, byte[] bytes) {
ByteArrayDataInput in = ByteStreams.newDataInput(bytes);
handlerMap.get(in.readInt()).handle(in);
}
}

Datei anzeigen

@ -0,0 +1,8 @@
package de.steamwar.comms.handlers;
import com.google.common.io.ByteArrayDataInput;
public interface BungeeHandler {
void handle(ByteArrayDataInput byteArrayDataInput);
}

Datei anzeigen

@ -0,0 +1,64 @@
package de.steamwar.comms.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.inventory.SWInventory;
import de.steamwar.inventory.SWItem;
import de.steamwar.sql.SteamwarUser;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import de.steamwar.comms.packets.*;
import java.util.*;
public class InventoryHandler implements BungeeHandler {
@Override
public void handle(ByteArrayDataInput byteArrayDataInput) {
JsonObject object = new JsonParser().parse(byteArrayDataInput.readUTF()).getAsJsonObject();
System.out.println(object.toString());
UUID player = SteamwarUser.get(object.get("id").getAsInt()).getUUID();
String title = object.get("title").getAsString();
int size = object.get("size").getAsInt();
int length = object.get("itemcount").getAsInt();
Map<Integer, SWItem> items = new HashMap<>();
JsonArray array = object.get("items").getAsJsonArray();
for (int i = 0; i < length; i++) {
JsonObject itemJson = array.get(i).getAsJsonObject();
SWItem item = null;
try {
item = new SWItem(Material.valueOf(itemJson.get("material").getAsString()), itemJson.get("title").getAsString());
}catch (IllegalArgumentException e) {
item = new SWItem(Material.STONE, itemJson.get("title").getAsString());
}
if(itemJson.has("skullOwner")) {
item = SWItem.getPlayerSkull(itemJson.get("skullOwner").getAsString());
item.setName(itemJson.get("title").getAsString());
}
if(itemJson.has("color")) {
item.getItemStack().setType(SWItem.getDye(itemJson.get("color").getAsInt()));
}
if(itemJson.has("enchanted"))
item.setEnchanted(true);
if(itemJson.has("lore")) {
List<String> lore = new ArrayList<>();
JsonArray loreArray = itemJson.getAsJsonArray("lore");
loreArray.forEach(jsonElement -> lore.add(jsonElement.getAsString()));
item.setLore(lore);
}
item.setCallback(click -> {
new InventoryCallbackPacket().setPosition(itemJson.get("position").getAsInt()).setClick(click)
.setCallback(InventoryCallbackPacket.CallbackType.CLICK)
.setOwner(SteamwarUser.get(player).getId()).send(Bukkit.getPlayer(player));
});
items.put(itemJson.get("position").getAsInt(), item);
}
SWInventory inventory = new SWInventory(Bukkit.getPlayer(player), size, title, items);
inventory.addCloseCallback(click -> new InventoryCallbackPacket()
.setCallback(InventoryCallbackPacket.CallbackType.CLOSE).setOwner(SteamwarUser.get(player).getId()).send(Bukkit.getPlayer(player)));
inventory.open();
}
}

Datei anzeigen

@ -1,29 +1,26 @@
package de.steamwar.coms.sender.packets; package de.steamwar.comms.packets;
import com.google.common.io.ByteArrayDataOutput; import com.google.common.io.ByteArrayDataOutput;
import de.steamwar.coms.sender.Packet;
import de.steamwar.inventory.SWItem;
import org.bukkit.event.inventory.ClickType; import org.bukkit.event.inventory.ClickType;
public class InventoryCallbackPacket extends Packet { public class InventoryCallbackPacket extends SpigotPacket {
int position; int position;
SWItem item;
ClickType clickType; ClickType clickType;
CallbackType callbackType; CallbackType callbackType;
String owner; int owner;
@Override @Override
public String getName() { public int getName() {
return "InventoryCallback"; return "InventoryCallback".hashCode();
} }
@Override @Override
public void writeVars(ByteArrayDataOutput byteArrayDataOutput) { public void writeVars(ByteArrayDataOutput byteArrayDataOutput) {
byteArrayDataOutput.writeUTF(owner); byteArrayDataOutput.writeInt(owner);
byteArrayDataOutput.writeUTF(callbackType.name()); byteArrayDataOutput.writeUTF(callbackType.name());
if(item != null){ if(position != 0){
byteArrayDataOutput.writeUTF(item.parseToJson(position)); byteArrayDataOutput.writeInt(position);
byteArrayDataOutput.writeUTF(clickType.name()); byteArrayDataOutput.writeUTF(clickType.name());
} }
} }
@ -33,11 +30,6 @@ public class InventoryCallbackPacket extends Packet {
return this; return this;
} }
public InventoryCallbackPacket setItem(SWItem item) {
this.item = item;
return this;
}
public InventoryCallbackPacket setClick(ClickType type){ public InventoryCallbackPacket setClick(ClickType type){
this.clickType = type; this.clickType = type;
return this; return this;
@ -48,8 +40,8 @@ public class InventoryCallbackPacket extends Packet {
return this; return this;
} }
public InventoryCallbackPacket setOwner(String uuid) { public InventoryCallbackPacket setOwner(int id) {
this.owner = uuid; this.owner = id;
return this; return this;
} }

Datei anzeigen

@ -0,0 +1,20 @@
package de.steamwar.comms.packets;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import de.steamwar.core.Core;
import org.bukkit.entity.Player;
public abstract class SpigotPacket {
public void send(Player player) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeInt(getName());
writeVars(out);
player.sendPluginMessage(Core.getInstance(), "sw:bridge", out.toByteArray());
}
public abstract int getName();
public abstract void writeVars(ByteArrayDataOutput byteArrayDataOutput);
}

Datei anzeigen

@ -1,10 +0,0 @@
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);
}

Datei anzeigen

@ -1,33 +0,0 @@
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;
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 PingHandler());
registerHandler(new InventoryHandler());
registerHandler(new CloseInventoryHandler());
}
@Override
public void onPluginMessageReceived(String s, Player player, byte[] bytes) {
ByteArrayDataInput in = ByteStreams.newDataInput(bytes);
handlerMap.get(in.readUTF()).handle(in);
}
}

Datei anzeigen

@ -1,21 +0,0 @@
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,62 +0,0 @@
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;
import de.steamwar.coms.sender.packets.InventoryCallbackPacket;
import de.steamwar.inventory.SWInventory;
import de.steamwar.inventory.SWItem;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import java.util.*;
public class InventoryHandler extends Handler {
@Override
protected String getName() {
return "Inventory";
}
@Override
public void handle(ByteArrayDataInput byteArrayDataInput) {
String player = byteArrayDataInput.readUTF();
String title = byteArrayDataInput.readUTF();
int size = byteArrayDataInput.readInt();
int length = byteArrayDataInput.readInt();
Map<Integer, SWItem> items = new HashMap<>();
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")) {
item = SWItem.getPlayerSkull(object.get("skullOwner").getAsString());
item.setName(object.get("title").getAsString());
}
if(object.has("enchanted"))
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).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,29 +0,0 @@
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) {
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

@ -1,17 +0,0 @@
package de.steamwar.coms.sender;
import com.google.common.io.ByteArrayDataOutput;
import org.bukkit.entity.Player;
import java.io.Serializable;
public abstract class Packet implements Serializable {
public void send(Player player) {
PacketSender.sendPacket(this, player);
}
public abstract String getName();
public abstract void writeVars(ByteArrayDataOutput byteArrayDataOutput);
}

Datei anzeigen

@ -1,16 +0,0 @@
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.entity.Player;
public class PacketSender {
public static void sendPacket(Packet packet, Player player) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF(packet.getName());
packet.writeVars(out);
player.sendPluginMessage(Core.getInstance(), "sw:return", out.toByteArray());
}
}

Datei anzeigen

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

Datei anzeigen

@ -139,7 +139,7 @@ public class SWItem {
callback = c; callback = c;
} }
public void hideAttributes() { private void hideAttributes() {
if (itemMeta == null) return; if (itemMeta == null) return;
itemMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES); itemMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
itemMeta.addItemFlags(ItemFlag.HIDE_DESTROYS); itemMeta.addItemFlags(ItemFlag.HIDE_DESTROYS);
@ -195,25 +195,4 @@ public class SWItem {
} }
itemStack.setItemMeta(itemMeta); itemStack.setItemMeta(itemMeta);
} }
public String parseToJson(int position) {
JsonObject object = new JsonObject();
object.addProperty("material", itemStack.getType().toString());
object.addProperty("position", position);
object.addProperty("title", itemMeta.getDisplayName());
if(itemMeta.hasEnchant(Enchantment.DURABILITY))
object.addProperty("enchanted", true);
if(itemMeta.hasItemFlag(ItemFlag.HIDE_ENCHANTS))
object.addProperty("hideAttributes", true);
if(itemMeta.getLore() != null) {
JsonArray array = new JsonArray();
for (String lores:
itemMeta.getLore()) {
array.add(lores);
}
object.add("lore", array);
}
return object.toString();
}
} }