diff --git a/SpigotCore_Main/src/de/steamwar/comms/BungeeReceiver.java b/SpigotCore_Main/src/de/steamwar/comms/BungeeReceiver.java index 7c1ba11..f70c147 100644 --- a/SpigotCore_Main/src/de/steamwar/comms/BungeeReceiver.java +++ b/SpigotCore_Main/src/de/steamwar/comms/BungeeReceiver.java @@ -23,24 +23,34 @@ 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 de.steamwar.comms.packets.MaterialsReturnPacket; +import de.steamwar.core.Core; +import de.steamwar.sql.*; import org.bukkit.Bukkit; +import org.bukkit.Material; import org.bukkit.Sound; import org.bukkit.SoundCategory; import org.bukkit.entity.Player; import org.bukkit.plugin.messaging.PluginMessageListener; +import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.UUID; +import java.util.logging.Level; +import java.util.stream.Collectors; public class BungeeReceiver implements PluginMessageListener { private static Map handlerMap = new HashMap<>(); + private static Map backupMap = new HashMap<>(); public static void registerHandler(Byte code, BungeeHandler handler) { handlerMap.put(code, handler); } + public static void registerBackup(Byte code, BungeeHandler handler) { + backupMap.put(code, handler); + } static { registerHandler(PacketIdManager.PING_PACKET, byteArrayDataInput -> { @@ -56,12 +66,68 @@ public class BungeeReceiver implements PluginMessageListener { Player player = Bukkit.getPlayer(SteamwarUser.get(byteArrayDataInput.readInt()).getUUID()); player.closeInventory(); }); + registerHandler(PacketIdManager.MATERIALS_GET_PACKET, byteArrayDataInput -> { + UUID id = UUID.fromString(byteArrayDataInput.readUTF()); + MaterialsReturnPacket packet = new MaterialsReturnPacket(id, Material.values()); + packet.send(Bukkit.getOnlinePlayers().stream().limit(1).collect(Collectors.toList()).get(1)); + }); + registerHandler(PacketIdManager.LOAD_SCHEMATIC, byteArrayDataInput -> { + Player player = Bukkit.getPlayer(SteamwarUser.get(byteArrayDataInput.readInt()).getUUID()); + Schematic schematic = Schematic.getSchemFromDB(byteArrayDataInput.readInt()); + if(SchematicMember.getAccessibleSchems(player.getUniqueId()).contains(schematic)) { + try { + schematic.loadToPlayer(player); + player.sendMessage("§eSchematic§8» §7" + "Schematic §e" + schematic.getSchemName() + " §7geladen"); + }catch (IOException | NoClipboardException e){ + Bukkit.getLogger().log(Level.SEVERE, "Schematic konnte nicht geladen werden", e); + player.sendMessage("§eSchematic§8» §7" + "§cFehler beim Laden der Schematic. Bitte wende dich an einen Developer"); + } + } + }); + registerHandler(PacketIdManager.SAVE_SCHEMATIC, byteArrayDataInput -> { + Player player = Bukkit.getPlayer(SteamwarUser.get(byteArrayDataInput.readInt()).getUUID()); + String name = byteArrayDataInput.readUTF(); + Schematic schematic = Schematic.getSchemFromDB(name, player.getUniqueId()); + boolean newSchem = false; + if(schematic == null || schematic.getSchemOwner() != SteamwarUser.get(player.getUniqueId()).getId()){ + newSchem = true; + Schematic.createSchem(name, player.getUniqueId(), "", SchematicType.Normal); + schematic = Schematic.getSchemFromDB(name, player.getUniqueId()); + } + try { + schematic.saveFromPlayer(player); + }catch(IOException ex){ + Bukkit.getLogger().log(Level.SEVERE, "Could not save schematic", ex); + player.sendMessage(Core.SCHEMATIC_PREFIX + "§cFehler beim Speichern der Schematic. Bitte wende dich an einen Developer"); + if(newSchem) + schematic.remove(); + return; + }catch (NoClipboardException e) { + player.sendMessage(Core.SCHEMATIC_PREFIX + "§cDein Clipboard ist leer"); + if(newSchem) + schematic.remove(); + return; + } + if (newSchem) + player.sendMessage(Core.SCHEMATIC_PREFIX + "Schematic §e" + name + " §7gespeichert"); + else + player.sendMessage(Core.SCHEMATIC_PREFIX + "Schematic §e" + name + " §7überschrieben"); + }); + registerBackup((byte) 0x25, byteArrayDataInput -> { + byteArrayDataInput.readInt(); + Player player = Bukkit.getPlayer(SteamwarUser.get(byteArrayDataInput.readInt()).getUUID()); + player.sendMessage(Core.SCHEMATIC_PREFIX + "§cDieser Server kann keine Schematics prüfen"); + }); } @Override public void onPluginMessageReceived(String s, Player player, byte[] bytes) { ByteArrayDataInput in = ByteStreams.newDataInput(bytes); Byte handler = in.readByte(); - handlerMap.get(handler).handle(in); + if(handlerMap.containsKey(handler)) + handlerMap.get(handler).handle(in); + else if(backupMap.containsKey(handler)) + backupMap.get(handler).handle(in); + } } diff --git a/SpigotCore_Main/src/de/steamwar/comms/PacketIdManager.java b/SpigotCore_Main/src/de/steamwar/comms/PacketIdManager.java index 27dbd81..6ec81d3 100644 --- a/SpigotCore_Main/src/de/steamwar/comms/PacketIdManager.java +++ b/SpigotCore_Main/src/de/steamwar/comms/PacketIdManager.java @@ -27,4 +27,9 @@ public class PacketIdManager { public final static byte INVENTORY_PACKET = 0x10; public final static byte INVENTORY_CALLBACK_PACKET = 0x11; public final static byte INVENTORY_CLOSE_PACKET = 0x12; + //0x2(X) Schematic System + public final static byte MATERIALS_GET_PACKET = 0x21; + public final static byte MATERIALS_RETURN_PACKET = 0x22; + public static final byte LOAD_SCHEMATIC = 0x23; + public static final byte SAVE_SCHEMATIC = 0x24; } diff --git a/SpigotCore_Main/src/de/steamwar/comms/packets/MaterialsReturnPacket.java b/SpigotCore_Main/src/de/steamwar/comms/packets/MaterialsReturnPacket.java new file mode 100644 index 0000000..1b4b0fd --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/comms/packets/MaterialsReturnPacket.java @@ -0,0 +1,36 @@ +package de.steamwar.comms.packets; + +import com.google.common.io.ByteArrayDataOutput; +import com.google.gson.JsonArray; +import de.steamwar.comms.PacketIdManager; +import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; + +import java.util.UUID; + +public class MaterialsReturnPacket extends SpigotPacket{ + + final UUID id; + final Material[] array; + + public MaterialsReturnPacket(UUID id, Material[] array) { + this.id = id; + this.array = array; + } + + @Override + public int getName() { + return PacketIdManager.MATERIALS_RETURN_PACKET; + } + + @Override + public void writeVars(ByteArrayDataOutput byteArrayDataOutput) { + byteArrayDataOutput.writeUTF(id.toString()); + JsonArray array = new JsonArray(); + for (int i = 0; i < this.array.length; i++) { + if(new ItemStack(this.array[i]).getItemMeta() != null && this.array[i].isItem()) + array.add(this.array[i].name()); + } + byteArrayDataOutput.writeUTF(array.toString()); + } +} diff --git a/SpigotCore_Main/src/de/steamwar/core/Core.java b/SpigotCore_Main/src/de/steamwar/core/Core.java index 893e53a..9bc14ef 100644 --- a/SpigotCore_Main/src/de/steamwar/core/Core.java +++ b/SpigotCore_Main/src/de/steamwar/core/Core.java @@ -32,6 +32,7 @@ import org.bukkit.plugin.java.JavaPlugin; public class Core extends JavaPlugin{ private static Core instance; private static final int version; + public static final String SCHEMATIC_PREFIX = "§eSchematic§8» §7"; static{ String packageName = Bukkit.getServer().getClass().getPackage().getName();