Archiviert
1
0

Add Bungee GUI Prototype

Dieser Commit ist enthalten in:
Chaoscaot 2020-09-19 12:37:26 +02:00
Ursprung 4565f7da7e
Commit 34534b49d8
9 geänderte Dateien mit 284 neuen und 26 gelöschten Zeilen

Datei anzeigen

@ -20,6 +20,7 @@
package de.steamwar.bungeecore; package de.steamwar.bungeecore;
import de.steamwar.bungeecore.commands.*; import de.steamwar.bungeecore.commands.*;
import de.steamwar.bungeecore.coms.receiver.PacketHandler;
import de.steamwar.bungeecore.listeners.*; import de.steamwar.bungeecore.listeners.*;
import de.steamwar.bungeecore.listeners.mods.Fabric; import de.steamwar.bungeecore.listeners.mods.Fabric;
import de.steamwar.bungeecore.listeners.mods.Forge; import de.steamwar.bungeecore.listeners.mods.Forge;
@ -72,6 +73,10 @@ public class BungeeCore extends Plugin {
@Override @Override
public void onEnable(){ public void onEnable(){
getProxy().registerChannel("sw:bridge");
getProxy().registerChannel("sw:return");
setInstance(this); setInstance(this);
loadConfig(); loadConfig();
@ -124,14 +129,15 @@ public class BungeeCore extends Plugin {
new IgnoreCommand(); new IgnoreCommand();
new UnIgnoreCommand(); new UnIgnoreCommand();
new TestCommand();
new EventStarter(); new EventStarter();
new Broadcaster(); new Broadcaster();
new SessionManager(); new SessionManager();
new PacketHandler();
new TablistManager(); new TablistManager();
getProxy().registerChannel("sw:bridge");
getProxy().getScheduler().schedule(this, () -> { getProxy().getScheduler().schedule(this, () -> {
SteamwarUser.clearCache(); SteamwarUser.clearCache();
Team.clearCache(); Team.clearCache();

Datei anzeigen

@ -0,0 +1,33 @@
package de.steamwar.bungeecore.commands;
import de.steamwar.bungeecore.coms.packets.inventory.SWInventory;
import de.steamwar.bungeecore.coms.packets.inventory.SWItem;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.connection.ProxiedPlayer;
public class TestCommand extends BasicCommand {
public TestCommand() {
super("test", null);
}
@Override
public void execute(CommandSender commandSender, String[] strings) {
if(!(commandSender instanceof ProxiedPlayer))
return;
SWInventory inventory = new SWInventory((ProxiedPlayer) commandSender, 3*9, "Test Inventory");
SWItem item = new SWItem("DIRT", 13, "§e§lTest Item");
item.setSkullOwner(commandSender.getName());
for (ProxiedPlayer player:
((ProxiedPlayer) commandSender).getServer().getInfo().getPlayers()) {
item.addLore("§6" + player.getDisplayName());
}
item.setEnchanted(true);
item.setCallback(click -> {
inventory.close();
commandSender.sendMessage("TEST!!! FJOnbds");
});
inventory.addItem(item);
inventory.open();
}
}

Datei anzeigen

@ -1,9 +1,18 @@
package de.steamwar.bungeecore.coms.handlers; package de.steamwar.bungeecore.coms.handlers;
import com.google.common.io.ByteArrayDataInput; import com.google.common.io.ByteArrayDataInput;
import de.steamwar.bungeecore.coms.packets.inventory.InvCallback;
import de.steamwar.bungeecore.coms.packets.inventory.SWInventory;
import de.steamwar.bungeecore.coms.packets.inventory.SWItem;
import de.steamwar.bungeecore.coms.receiver.Handler; import de.steamwar.bungeecore.coms.receiver.Handler;
import java.util.HashMap;
import java.util.Map;
public class InventoryCallbackHandler extends Handler { public class InventoryCallbackHandler extends Handler {
public static final Map<String, SWInventory> inventoryHashMap = new HashMap<>();
@Override @Override
protected String getName() { protected String getName() {
return "InventoryCallback"; return "InventoryCallback";
@ -11,6 +20,20 @@ public class InventoryCallbackHandler extends Handler {
@Override @Override
public void handle(ByteArrayDataInput byteArrayDataInput) { public void handle(ByteArrayDataInput byteArrayDataInput) {
String owner = byteArrayDataInput.readUTF();
CallbackType type = CallbackType.valueOf(byteArrayDataInput.readUTF());
if(type == CallbackType.CLICK) {
SWItem item = SWItem.getFromJson(byteArrayDataInput.readUTF());
InvCallback.ClickType clickType = InvCallback.ClickType.valueOf(byteArrayDataInput.readUTF());
System.out.println(item.writeToString());
inventoryHashMap.get(owner).handleCallback(clickType, item);
}else if(type == CallbackType.CLOSE) {
inventoryHashMap.get(owner).handleClose();
}
}
public enum CallbackType {
CLICK,
CLOSE,
} }
} }

Datei anzeigen

@ -0,0 +1,25 @@
package de.steamwar.bungeecore.coms.packets;
import com.google.common.io.ByteArrayDataOutput;
import de.steamwar.bungeecore.coms.sender.Packet;
import net.md_5.bungee.api.connection.ProxiedPlayer;
public class CloseInventoryPacket extends Packet {
ProxiedPlayer player;
@Override
public String getName() {
return "InvClose";
}
@Override
public void writeVars(ByteArrayDataOutput byteArrayDataOutput) {
byteArrayDataOutput.writeUTF(player.getUniqueId().toString());
}
public CloseInventoryPacket setPlayer(ProxiedPlayer player) {
this.player = player;
return this;
}
}

Datei anzeigen

@ -5,19 +5,19 @@ import de.steamwar.bungeecore.coms.packets.inventory.SWItem;
import de.steamwar.bungeecore.coms.sender.Packet; import de.steamwar.bungeecore.coms.sender.Packet;
import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.connection.ProxiedPlayer;
import java.util.HashMap;
public class InventoryPacket extends Packet { public class InventoryPacket extends Packet {
String title; String title;
String name;
String player; String player;
int size; int size;
SWItem[] items; HashMap<Integer, SWItem> items;
public InventoryPacket(int size, String title, String name, ProxiedPlayer player) { public InventoryPacket(int size, String title, ProxiedPlayer player) {
items = new SWItem[size]; items = new HashMap<>();
this.title = title; this.title = title;
this.size = size; this.size = size;
this.name = name;
this.player = player.getUniqueId().toString(); this.player = player.getUniqueId().toString();
} }
@ -28,28 +28,26 @@ public class InventoryPacket extends Packet {
@Override @Override
public void writeVars(ByteArrayDataOutput byteArrayDataOutput) { public void writeVars(ByteArrayDataOutput byteArrayDataOutput) {
byteArrayDataOutput.writeUTF(name);
byteArrayDataOutput.writeUTF(player); byteArrayDataOutput.writeUTF(player);
byteArrayDataOutput.writeUTF(title); byteArrayDataOutput.writeUTF(title);
byteArrayDataOutput.writeInt(size); byteArrayDataOutput.writeInt(size);
byteArrayDataOutput.writeInt(items.length); byteArrayDataOutput.writeInt(items.size());
for (SWItem item: for (int i = 0; i < size; i++) {
items) { if(items.get(i) != null)
if(item == null) byteArrayDataOutput.writeUTF(items.get(i).writeToString());
byteArrayDataOutput.writeUTF(item.writeToString());
} }
} }
public void addItem(SWItem item) { public void addItem(SWItem item) {
for (int i = 0; i < items.length; i++) { for (int i = 0; i < items.size(); i++) {
if (items[i] != null) { if (items.get(i) != null) {
items[i] = item; items.put(i, item);
return; return;
} }
} }
} }
public void setItem(int index, SWItem item) { public void setItem(int index, SWItem item) {
items[index] = item; items.put(index, item);
} }
} }

Datei anzeigen

@ -0,0 +1,45 @@
package de.steamwar.bungeecore.coms.packets.inventory;
public interface InvCallback {
void clicked(ClickType click);
public static enum ClickType {
LEFT,
SHIFT_LEFT,
RIGHT,
SHIFT_RIGHT,
WINDOW_BORDER_LEFT,
WINDOW_BORDER_RIGHT,
MIDDLE,
NUMBER_KEY,
DOUBLE_CLICK,
DROP,
CONTROL_DROP,
CREATIVE,
UNKNOWN;
private ClickType() {
}
public boolean isKeyboardClick() {
return this == NUMBER_KEY || this == DROP || this == CONTROL_DROP;
}
public boolean isCreativeAction() {
return this == MIDDLE || this == CREATIVE;
}
public boolean isRightClick() {
return this == RIGHT || this == SHIFT_RIGHT;
}
public boolean isLeftClick() {
return this == LEFT || this == SHIFT_LEFT || this == DOUBLE_CLICK || this == CREATIVE;
}
public boolean isShiftClick() {
return this == SHIFT_LEFT || this == SHIFT_RIGHT || this == CONTROL_DROP;
}
}
}

Datei anzeigen

@ -1,4 +1,75 @@
package de.steamwar.bungeecore.coms.packets.inventory; package de.steamwar.bungeecore.coms.packets.inventory;
import de.steamwar.bungeecore.coms.handlers.InventoryCallbackHandler;
import de.steamwar.bungeecore.coms.packets.CloseInventoryPacket;
import de.steamwar.bungeecore.coms.packets.InventoryPacket;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import java.util.HashMap;
import java.util.Map;
public class SWInventory { public class SWInventory {
Map<Integer, SWItem> itemMap;
InvCallback close;
ProxiedPlayer player;
int size;
String title;
public SWInventory(ProxiedPlayer proxiedPlayer, int size, String title) {
itemMap = new HashMap<>();
InventoryCallbackHandler.inventoryHashMap.put(proxiedPlayer.getUniqueId().toString(), this);
this.player = proxiedPlayer;
this.size = size;
this.title = title;
}
public void addItem(SWItem item) {
itemMap.put(item.position, item);
}
public void removeItem(SWItem item) {
itemMap.remove(item.position);
}
public void removeItem(int position) {
itemMap.remove(position);
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public void handleCallback(InvCallback.ClickType type, SWItem item) {
itemMap.get(item.position).callback.clicked(type);
}
public void handleClose() {
if(close != null)
close.clicked(null);
}
public void open() {
InventoryPacket inv = new InventoryPacket(size, title, player);
for (Map.Entry<Integer, SWItem> entry : itemMap.entrySet()) {
inv.setItem(entry.getKey(), entry.getValue());
}
inv.send(player);
}
public void close() {
new CloseInventoryPacket().setPlayer(player).send(player);
}
} }

Datei anzeigen

@ -2,6 +2,7 @@ package de.steamwar.bungeecore.coms.packets.inventory;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -12,20 +13,49 @@ public class SWItem {
boolean enchanted, hideAttributes; boolean enchanted, hideAttributes;
int position; int position;
List<String> lore; List<String> lore;
InvCallback callback;
public SWItem(String material, int position, String title) { public SWItem(String material, int position, String title) {
this.material = material; this.material = material.toUpperCase();
lore = new ArrayList<>(); lore = new ArrayList<>();
this.position = position; this.position = position;
this.title = title; this.title = title;
} }
private SWItem(String json) {
JsonObject object = new JsonParser().parse(json).getAsJsonObject();
material = object.get("material").getAsString();
position = object.get("position").getAsInt();
title = object.get("title").getAsString();
if(object.has("skullOwner")) {
skullOwner = object.get("skullOwner").getAsString();
}
if(object.has("enchanted"))
enchanted = true;
if(object.has("hideAttributes"))
hideAttributes = true;
if(object.has("lore")) {
List<String> lore = new ArrayList<>();
JsonArray array = object.getAsJsonArray("lore");
array.forEach(jsonElement -> lore.add(jsonElement.getAsString()));
this.lore = lore;
}
}
public void setCallback(InvCallback callback) {
this.callback = callback;
}
public InvCallback getCallback() {
return callback;
}
public String getMaterial() { public String getMaterial() {
return material; return material;
} }
public void setMaterial(String material) { public void setMaterial(String material) {
this.material = material; this.material = material.toUpperCase();
} }
public String getSkullOwner() { public String getSkullOwner() {
@ -67,7 +97,7 @@ public class SWItem {
object.addProperty("enchanted", true); object.addProperty("enchanted", true);
if(hideAttributes) if(hideAttributes)
object.addProperty("hideAttributes", true); object.addProperty("hideAttributes", true);
if(!lore.isEmpty()) { if(lore != null) {
JsonArray array = new JsonArray(); JsonArray array = new JsonArray();
for (String lores: for (String lores:
lore) { lore) {
@ -76,6 +106,30 @@ public class SWItem {
object.add("lore", array); object.add("lore", array);
} }
return object.getAsString(); return object.toString();
}
public static SWItem getFromJson(String json) {
return new SWItem(json);
}
@Override
public boolean equals(Object o) {
if(!(o instanceof SWItem))
return false;
SWItem item = (SWItem) o;
if(item.enchanted != enchanted)
return false;
if(item.hideAttributes != hideAttributes)
return false;
if(!item.material.equals(material))
return false;
if(!item.lore.equals(lore))
return false;
if(!item.skullOwner.equals(skullOwner))
return false;
if(item.position != position)
return false;
return true;
} }
} }

Datei anzeigen

@ -2,6 +2,7 @@ package de.steamwar.bungeecore.coms.receiver;
import com.google.common.io.ByteArrayDataInput; import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
import de.steamwar.bungeecore.coms.handlers.InventoryCallbackHandler;
import de.steamwar.bungeecore.listeners.BasicListener; import de.steamwar.bungeecore.listeners.BasicListener;
import net.md_5.bungee.api.event.PluginMessageEvent; import net.md_5.bungee.api.event.PluginMessageEvent;
import net.md_5.bungee.event.EventHandler; import net.md_5.bungee.event.EventHandler;
@ -11,14 +12,16 @@ import java.util.Map;
public class PacketHandler extends BasicListener { public class PacketHandler extends BasicListener {
private static Map<String, Handler> handlerMap = new HashMap<>(); private static final Map<String, Handler> handlerMap = new HashMap<>();
@EventHandler @EventHandler
public void onPluginMessage(PluginMessageEvent event) { public void onPluginMessage(PluginMessageEvent event) {
if(!event.getTag().equalsIgnoreCase("sw:bridge")) System.out.println(event.toString());
if(!event.getTag().equalsIgnoreCase("sw:return"))
return; return;
ByteArrayDataInput in = ByteStreams.newDataInput( event.getData() ); ByteArrayDataInput in = ByteStreams.newDataInput(event.getData());
handlerMap.get(in.readUTF()).handle(in); String handler = in.readUTF();
handlerMap.get(handler).handle(in);
} }
public static void registerHandler(Handler handler) { public static void registerHandler(Handler handler) {
@ -26,6 +29,6 @@ public class PacketHandler extends BasicListener {
} }
static { static {
//TODO Register Handlers registerHandler(new InventoryCallbackHandler());
} }
} }