From 29d7cf081991a755e69883d412d43d86a61843f2 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sat, 26 Sep 2020 12:44:31 +0200 Subject: [PATCH] Refactored Some Code --- src/de/steamwar/bungeecore/commands/BauCommand.java | 4 ++-- .../comms/handlers/InventoryCallbackHandler.java | 9 +++++++++ .../bungeecore/comms/packets/CloseInventoryPacket.java | 8 +++----- src/de/steamwar/bungeecore/inventory/SWInventory.java | 7 ++++++- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/de/steamwar/bungeecore/commands/BauCommand.java b/src/de/steamwar/bungeecore/commands/BauCommand.java index e5bc49a..b433f72 100644 --- a/src/de/steamwar/bungeecore/commands/BauCommand.java +++ b/src/de/steamwar/bungeecore/commands/BauCommand.java @@ -229,8 +229,8 @@ public class BauCommand { private static void delete(ProxiedPlayer p, String[] command){ SWInventory inventory = new SWInventory(p, 9, "§eWirklich Welt löschen?"); - inventory.addItem(8, new SWItem(8, "§cAbbrechen", 1), click -> inventory.close()); - inventory.addItem(0, new SWItem(0, "§aLöschen", 10), click -> { + inventory.addItem(new SWItem(8, "§cAbbrechen", 1), click -> inventory.close()); + inventory.addItem(new SWItem(0, "§aLöschen", 10), click -> { if(bau15(p, command, 2)){ SteamwarUser user = SteamwarUser.get(p.getUniqueId()); deleteWorld(p, BungeeCore.USERWORLDS15 + user.getId()); diff --git a/src/de/steamwar/bungeecore/comms/handlers/InventoryCallbackHandler.java b/src/de/steamwar/bungeecore/comms/handlers/InventoryCallbackHandler.java index e39b668..385a6bd 100644 --- a/src/de/steamwar/bungeecore/comms/handlers/InventoryCallbackHandler.java +++ b/src/de/steamwar/bungeecore/comms/handlers/InventoryCallbackHandler.java @@ -20,10 +20,13 @@ package de.steamwar.bungeecore.comms.handlers; import com.google.common.io.ByteArrayDataInput; +import de.steamwar.bungeecore.BungeeCore; import de.steamwar.bungeecore.comms.SpigotHandler; +import de.steamwar.bungeecore.comms.packets.CloseInventoryPacket; import de.steamwar.bungeecore.inventory.InvCallback; import de.steamwar.bungeecore.inventory.SWInventory; import de.steamwar.bungeecore.sql.SteamwarUser; +import net.md_5.bungee.api.ProxyServer; import java.util.HashMap; import java.util.Map; @@ -36,6 +39,12 @@ public class InventoryCallbackHandler implements SpigotHandler { public void handle(ByteArrayDataInput byteArrayDataInput) { SteamwarUser owner = SteamwarUser.get(byteArrayDataInput.readInt()); CallbackType type = CallbackType.valueOf(byteArrayDataInput.readUTF()); + if(!inventoryHashMap.containsKey(owner.getId())) { + BungeeCore.send(ProxyServer.getInstance().getPlayer(owner.getUuid()), BungeeCore.CHAT_PREFIX + "§eAuf grund eines neuladens ist dein Inventar verloren gegangen."); + if(type == CallbackType.CLICK) { + new CloseInventoryPacket(owner.getId()).send(ProxyServer.getInstance().getPlayer(owner.getUuid())); + } + } if(type == CallbackType.CLICK) { int pos = byteArrayDataInput.readInt(); InvCallback.ClickType clickType = InvCallback.ClickType.valueOf(byteArrayDataInput.readUTF()); diff --git a/src/de/steamwar/bungeecore/comms/packets/CloseInventoryPacket.java b/src/de/steamwar/bungeecore/comms/packets/CloseInventoryPacket.java index 974661a..0445c10 100644 --- a/src/de/steamwar/bungeecore/comms/packets/CloseInventoryPacket.java +++ b/src/de/steamwar/bungeecore/comms/packets/CloseInventoryPacket.java @@ -22,24 +22,22 @@ package de.steamwar.bungeecore.comms.packets; import com.google.common.io.ByteArrayDataOutput; import de.steamwar.bungeecore.comms.BungeePacket; import de.steamwar.bungeecore.comms.PacketIdManager; -import de.steamwar.bungeecore.sql.SteamwarUser; -import net.md_5.bungee.api.connection.ProxiedPlayer; public class CloseInventoryPacket extends BungeePacket { - final ProxiedPlayer player; + final int player; @Override public int getId() { return PacketIdManager.INVENTORY_CLOSE_PACKET; } - public CloseInventoryPacket(ProxiedPlayer player) { + public CloseInventoryPacket(int player) { this.player = player; } @Override public void writeVars(ByteArrayDataOutput byteArrayDataOutput) { - byteArrayDataOutput.writeInt(SteamwarUser.get(player).getId()); + byteArrayDataOutput.writeInt(player); } } diff --git a/src/de/steamwar/bungeecore/inventory/SWInventory.java b/src/de/steamwar/bungeecore/inventory/SWInventory.java index 6a308cf..1f3d166 100644 --- a/src/de/steamwar/bungeecore/inventory/SWInventory.java +++ b/src/de/steamwar/bungeecore/inventory/SWInventory.java @@ -50,6 +50,11 @@ public class SWInventory { itemMap.put(item.getPosition(), item); } + public void addItem(SWItem item, InvCallback callback) { + item.setCallback(callback); + itemMap.put(item.getPosition(), item); + } + public void addItem(int position, SWItem item) { itemMap.put(position, item); } @@ -116,7 +121,7 @@ public class SWInventory { } public void close() { - new CloseInventoryPacket(player).send(player); + new CloseInventoryPacket(SteamwarUser.get(player).getId()).send(player); } public void setClose(InvCallback close) {