From e40b419e2ec73084179280d50ec1e2aab0100390 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Thu, 25 Jun 2020 16:19:37 +0200 Subject: [PATCH] Create a copy schem for automatic clipboard saving Signed-off-by: Lixfel --- .../schematicsystem/ClipboardListener.java | 48 +++++++++++++++++++ .../schematicsystem/SchematicSystem.java | 2 + 2 files changed, 50 insertions(+) create mode 100644 SchematicSystem_Main/src/de/steamwar/schematicsystem/ClipboardListener.java diff --git a/SchematicSystem_Main/src/de/steamwar/schematicsystem/ClipboardListener.java b/SchematicSystem_Main/src/de/steamwar/schematicsystem/ClipboardListener.java new file mode 100644 index 0000000..0f7eefe --- /dev/null +++ b/SchematicSystem_Main/src/de/steamwar/schematicsystem/ClipboardListener.java @@ -0,0 +1,48 @@ +package de.steamwar.schematicsystem; + +import de.steamwar.sql.NoClipboardException; +import de.steamwar.sql.Schematic; +import de.steamwar.sql.SchematicType; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerQuitEvent; + +import java.io.IOException; +import java.util.UUID; + +public class ClipboardListener implements Listener { + + private static final String CLIPBOARD_SCHEMNAME = "//copy"; + + @EventHandler + public void onLogin(PlayerJoinEvent e){ + Schematic schematic = Schematic.getSchemFromDB(CLIPBOARD_SCHEMNAME, e.getPlayer().getUniqueId()); + if(schematic != null){ + try { + schematic.loadToPlayer(e.getPlayer()); + } catch (IOException | NoClipboardException ex) { + //ignore + } + } + } + + @EventHandler + public void onLogout(PlayerQuitEvent e){ + UUID playerUUID = e.getPlayer().getUniqueId(); + Schematic schematic = Schematic.getSchemFromDB(CLIPBOARD_SCHEMNAME, playerUUID); + boolean newSchem = false; + if(schematic == null){ + Schematic.createSchem(CLIPBOARD_SCHEMNAME, playerUUID, "", SchematicType.Normal); + schematic = Schematic.getSchemFromDB(CLIPBOARD_SCHEMNAME, playerUUID); + newSchem = true; + } + + try{ + schematic.saveFromPlayer(e.getPlayer()); + } catch (IOException | NoClipboardException ex) { + if(newSchem) + schematic.remove(); + } + } +} diff --git a/SchematicSystem_Main/src/de/steamwar/schematicsystem/SchematicSystem.java b/SchematicSystem_Main/src/de/steamwar/schematicsystem/SchematicSystem.java index c1ec05d..7361642 100644 --- a/SchematicSystem_Main/src/de/steamwar/schematicsystem/SchematicSystem.java +++ b/SchematicSystem_Main/src/de/steamwar/schematicsystem/SchematicSystem.java @@ -29,5 +29,7 @@ public class SchematicSystem extends JavaPlugin { CommandRemover.removeAll("/schematic", "/schem", "//schematic", "//schem"); getCommand("schem").setExecutor(new SchematicCommand()); + + Bukkit.getPluginManager().registerEvents(new ClipboardListener(), this); } }