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
4 geänderte Dateien mit 67 neuen und 27 gelöschten Zeilen
Nur Änderungen aus Commit ee7e68eaae werden angezeigt - Alle Commits anzeigen

Datei anzeigen

@ -1,3 +1,22 @@
/*
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 {

Datei anzeigen

@ -27,7 +27,6 @@ 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.*;
@ -45,38 +44,15 @@ public class InventoryHandler implements BungeeHandler {
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);
}
SWItem item = SWItem.getItemFromJson(itemJson);
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));
new InventoryCallbackPacket(itemJson, click, player).send(Bukkit.getPlayer(player));
});
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.
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.addCloseCallback(click -> new InventoryCallbackPacket(player).send(Bukkit.getPlayer(player)));
inventory.open();
}
}

Datei anzeigen

@ -20,9 +20,13 @@
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;
@ -45,6 +49,20 @@ public class InventoryCallbackPacket extends SpigotPacket {
}
}
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;

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);