SteamWar/SpigotCore
Archiviert
13
0

Packet System + Bungee GUI #59

Manuell gemergt
YoyoNow hat 10 Commits von packet-system nach master 2020-09-26 09:20:52 +02:00 zusammengeführt
8 geänderte Dateien mit 340 neuen und 0 gelöschten Zeilen

Datei anzeigen

@ -0,0 +1,67 @@
/*
This file is a part of the SteamWar software.
Copyright (C) 2020 SteamWar.de-Serverteam
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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<Byte, BungeeHandler> handlerMap = new HashMap<>();
public static void registerHandler(Byte code, BungeeHandler handler) {
handlerMap.put(code, handler);
}
static {
registerHandler(PacketIdManager.PING_PACKET, 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(PacketIdManager.INVENTORY_PACKET, new InventoryHandler());
registerHandler(PacketIdManager.INVENTORY_CLOSE_PACKET, 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);
Byte handler = in.readByte();
handlerMap.get(handler).handle(in);
}
}

Datei anzeigen

@ -0,0 +1,30 @@
/*
Veraltet
Review

Lizenzheader fehlt.

Lizenzheader fehlt.
This file is a part of the SteamWar software.
Copyright (C) 2020 SteamWar.de-Serverteam
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.comms;
public class PacketIdManager {
//0x0(X) Standalone Packets
public final static byte PING_PACKET = 0x01;
//0x1(X) Bungee Inventory
public final static byte INVENTORY_PACKET = 0x10;
public final static byte INVENTORY_CALLBACK_PACKET = 0x11;
public final static byte INVENTORY_CLOSE_PACKET = 0x12;
}

Datei anzeigen

@ -0,0 +1,27 @@
/*
This file is a part of the SteamWar software.
Copyright (C) 2020 SteamWar.de-Serverteam
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.comms.handlers;
import com.google.common.io.ByteArrayDataInput;
public interface BungeeHandler {
void handle(ByteArrayDataInput byteArrayDataInput);
}

Datei anzeigen

@ -0,0 +1,56 @@
/*
This file is a part of the SteamWar software.
Copyright (C) 2020 SteamWar.de-Serverteam
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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 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();
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 = SWItem.getItemFromJson(itemJson);
item.setCallback(click -> new InventoryCallbackPacket(itemJson, click, player).send(Bukkit.getPlayer(player)));
items.put(itemJson.get("position").getAsInt(), item);
}
Review

Evtl. einfach einen neuen SWItem-Konstruktor erstellen, der als einziges Argument ein JsonObject nimmt und dann sich die Parameter aus dem JsonObject entnimmt? Das wäre meiner Meinung nach eine elegantere Lösung.

Evtl. einfach einen neuen SWItem-Konstruktor erstellen, der als einziges Argument ein JsonObject nimmt und dann sich die Parameter aus dem JsonObject entnimmt? Das wäre meiner Meinung nach eine elegantere Lösung.
SWInventory inventory = new SWInventory(Bukkit.getPlayer(player), size, title, items);
inventory.addCloseCallback(click -> new InventoryCallbackPacket(player).send(Bukkit.getPlayer(player)));
inventory.open();
}
}

Datei anzeigen

@ -0,0 +1,90 @@
/*
This file is a part of the SteamWar software.
Copyright (C) 2020 SteamWar.de-Serverteam
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.comms.packets;
import com.google.common.io.ByteArrayDataOutput;
import com.google.gson.JsonObject;
import de.steamwar.comms.PacketIdManager;
import de.steamwar.sql.SteamwarUser;
import org.bukkit.event.inventory.ClickType;
import java.util.UUID;
public class InventoryCallbackPacket extends SpigotPacket {
int position = -1;
ClickType clickType;
CallbackType callbackType;
int owner;
@Override
public int getName() {
return PacketIdManager.INVENTORY_CALLBACK_PACKET;
}
@Override
public void writeVars(ByteArrayDataOutput byteArrayDataOutput) {
byteArrayDataOutput.writeInt(owner);
byteArrayDataOutput.writeUTF(callbackType.name());
if(position != -1){
byteArrayDataOutput.writeInt(position);
byteArrayDataOutput.writeUTF(clickType.name());
}
}
public InventoryCallbackPacket() {}
public InventoryCallbackPacket(JsonObject itemJson, ClickType click, UUID player) {
setPosition(itemJson.get("position").getAsInt());
setClick(click);
setCallback(InventoryCallbackPacket.CallbackType.CLICK);
setOwner(SteamwarUser.get(player).getId());
}
public InventoryCallbackPacket(UUID player) {
setCallback(CallbackType.CLOSE);
setOwner(SteamwarUser.get(player).getId());
}
public InventoryCallbackPacket setPosition(int position) {
this.position = position;
return this;
}
public InventoryCallbackPacket setClick(ClickType type){
this.clickType = type;
return this;
}
public InventoryCallbackPacket setCallback(CallbackType callback) {
callbackType = callback;
return this;
}
public InventoryCallbackPacket setOwner(int id) {
this.owner = id;
return this;
}
public enum CallbackType {
CLICK,
CLOSE,
}
}

Datei anzeigen

@ -0,0 +1,39 @@
/*
This file is a part of the SteamWar software.
Copyright (C) 2020 SteamWar.de-Serverteam
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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.writeByte(getName());
writeVars(out);
player.sendPluginMessage(Core.getInstance(), "sw:bridge", out.toByteArray());
}
public abstract int getName();
public abstract void writeVars(ByteArrayDataOutput byteArrayDataOutput);
}

Datei anzeigen

@ -19,6 +19,7 @@
package de.steamwar.core;
import de.steamwar.comms.BungeeReceiver;
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 BungeeReceiver());
getServer().getMessenger().registerOutgoingPluginChannel(this, "sw:bridge");
Veraltet
Review

Wozu braucht es den Channel?

Wozu braucht es den Channel?
}
@Override

Datei anzeigen

@ -19,6 +19,8 @@
package de.steamwar.inventory;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import de.steamwar.core.Core;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
@ -137,6 +139,31 @@ public class SWItem {
callback = c;
}
public static SWItem getItemFromJson(JsonObject itemJson) {
Veraltet
Review

Üblicherweise hiden wir immer, daher kann das glaube ich private bleiben

Üblicherweise hiden wir immer, daher kann das glaube ich private bleiben
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);
}
return item;
}
private void hideAttributes() {
if (itemMeta == null) return;
itemMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);