From ee7e68eaaecfd821da5f82012723a595b6f35b05 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Thu, 24 Sep 2020 14:09:19 +0200 Subject: [PATCH] Code Cleanup --- .../de/steamwar/comms/PacketIdManager.java | 19 ++++++++++++ .../comms/handlers/InventoryHandler.java | 30 ++----------------- .../packets/InventoryCallbackPacket.java | 18 +++++++++++ .../src/de/steamwar/inventory/SWItem.java | 27 +++++++++++++++++ 4 files changed, 67 insertions(+), 27 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/comms/PacketIdManager.java b/SpigotCore_Main/src/de/steamwar/comms/PacketIdManager.java index fb12d5c..27dbd81 100644 --- a/SpigotCore_Main/src/de/steamwar/comms/PacketIdManager.java +++ b/SpigotCore_Main/src/de/steamwar/comms/PacketIdManager.java @@ -1,3 +1,22 @@ +/* + 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 . +*/ + package de.steamwar.comms; public class PacketIdManager { diff --git a/SpigotCore_Main/src/de/steamwar/comms/handlers/InventoryHandler.java b/SpigotCore_Main/src/de/steamwar/comms/handlers/InventoryHandler.java index 9c3ccd6..74dd9e4 100644 --- a/SpigotCore_Main/src/de/steamwar/comms/handlers/InventoryHandler.java +++ b/SpigotCore_Main/src/de/steamwar/comms/handlers/InventoryHandler.java @@ -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 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)); }); 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(); } } diff --git a/SpigotCore_Main/src/de/steamwar/comms/packets/InventoryCallbackPacket.java b/SpigotCore_Main/src/de/steamwar/comms/packets/InventoryCallbackPacket.java index 0883033..6ba1c42 100644 --- a/SpigotCore_Main/src/de/steamwar/comms/packets/InventoryCallbackPacket.java +++ b/SpigotCore_Main/src/de/steamwar/comms/packets/InventoryCallbackPacket.java @@ -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; diff --git a/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java b/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java index 55aa8aa..d77ffab 100644 --- a/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java +++ b/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java @@ -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) { + 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 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);