From 89de8b6ab5e030c67fc8381c817bc1477e09bbfd Mon Sep 17 00:00:00 2001
From: lars <larslukasneuhaus@gmx.de>
Date: Sun, 14 Jul 2024 13:50:23 +0200
Subject: [PATCH] save entry location in database

---
 .../minecraft/pixelblocks/PixelBlocks.java    | 17 +++++-
 .../listeners/PlayerInteractListener.java     | 27 +++++-----
 .../pixelblocks/pixelblock/PixelBlock.java    | 52 ++++++++++++++++++-
 3 files changed, 79 insertions(+), 17 deletions(-)

diff --git a/src/main/java/eu/mhsl/minecraft/pixelblocks/PixelBlocks.java b/src/main/java/eu/mhsl/minecraft/pixelblocks/PixelBlocks.java
index 17d5db1..5f8dde3 100644
--- a/src/main/java/eu/mhsl/minecraft/pixelblocks/PixelBlocks.java
+++ b/src/main/java/eu/mhsl/minecraft/pixelblocks/PixelBlocks.java
@@ -61,7 +61,11 @@ public final class PixelBlocks extends JavaPlugin {
             "locationWorldName CHAR(36), " +
             "locationX DOUBLE, " +
             "locationY DOUBLE, " +
-            "locationZ DOUBLE" +
+            "locationZ DOUBLE, " +
+            "entryLocationWorldName CHAR(36), " +
+            "entryLocationX DOUBLE, " +
+            "entryLocationY DOUBLE, " +
+            "entryLocationZ DOUBLE" +
             ");";
 
         try (var conn = DriverManager.getConnection(url);
@@ -75,7 +79,15 @@ public final class PixelBlocks extends JavaPlugin {
                     Bukkit.getWorld(pixelBlocksResult.getString("locationWorldName")),
                     pixelBlocksResult.getDouble("locationX"),
                     pixelBlocksResult.getDouble("locationY"),
-                    pixelBlocksResult.getDouble("locationZ"));
+                    pixelBlocksResult.getDouble("locationZ")
+                );
+
+                Location newEntryLocation = new Location(
+                    Bukkit.getWorld(pixelBlocksResult.getString("entryLocationWorldName")),
+                    pixelBlocksResult.getDouble("entryLocationX"),
+                    pixelBlocksResult.getDouble("entryLocationY"),
+                    pixelBlocksResult.getDouble("entryLocationZ")
+                );
 
                 entities.stream().filter(entity -> entity.getLocation().clone().add(0, PixelBlock.hitboxOffset, 0).toBlockLocation().equals(newPixelBlockLocation)).forEach(Entity::remove);
 
@@ -85,6 +97,7 @@ public final class PixelBlocks extends JavaPlugin {
                     16,
                     UUID.fromString(pixelBlocksResult.getString("uuid"))
                 );
+                newPixelBlock.lastEntryLocation = newEntryLocation;
                 newPixelBlock.place(newPixelBlockLocation);
             }
 
diff --git a/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/PlayerInteractListener.java b/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/PlayerInteractListener.java
index 36c650c..e03d73b 100644
--- a/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/PlayerInteractListener.java
+++ b/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/PlayerInteractListener.java
@@ -16,19 +16,19 @@ import java.util.*;
 import static eu.mhsl.minecraft.pixelblocks.PixelBlocks.plugin;
 
 public class PlayerInteractListener implements Listener {
-    @EventHandler
-    static void onPlayerInteract(PlayerInteractEvent event) {
-        if(event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getClickedBlock() != null) {
-            if(event.getClickedBlock().getType() == Material.GLASS) {
-//                final WorldCreator worldCreator = new WorldCreator(plugin.getDataFolder().getPath()+"/"+event.getPlayer().getUniqueId());
-//                worldCreator.type(WorldType.FLAT);
-//
-//                World newWorld = Bukkit.createWorld(worldCreator);
-//
-//                event.getPlayer().teleport(newWorld.getSpawnLocation());
-            }
-        }
-    }
+//    @EventHandler
+//    static void onPlayerInteract(PlayerInteractEvent event) {
+//        if(event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getClickedBlock() != null) {
+//            if(event.getClickedBlock().getType() == Material.GLASS) {
+////                final WorldCreator worldCreator = new WorldCreator(plugin.getDataFolder().getPath()+"/"+event.getPlayer().getUniqueId());
+////                worldCreator.type(WorldType.FLAT);
+////
+////                World newWorld = Bukkit.createWorld(worldCreator);
+////
+////                event.getPlayer().teleport(newWorld.getSpawnLocation());
+//            }
+//        }
+//    }
 
     @EventHandler
     static void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
@@ -47,6 +47,7 @@ public class PlayerInteractListener implements Listener {
             if(playerUID.equals(blockOwner)) {
                 pixelBlock.lastEntryLocation = event.getPlayer().getLocation();
                 pixelBlock.lastEntryTime = System.currentTimeMillis();
+                pixelBlock.saveToDB();
 
                 World blockWorld = Bukkit.getWorld(plugin.getDataFolder().getPath()+ "/" +pixelBlock.uuid);
                 assert blockWorld != null;
diff --git a/src/main/java/eu/mhsl/minecraft/pixelblocks/pixelblock/PixelBlock.java b/src/main/java/eu/mhsl/minecraft/pixelblocks/pixelblock/PixelBlock.java
index b3dad61..df8dcaa 100644
--- a/src/main/java/eu/mhsl/minecraft/pixelblocks/pixelblock/PixelBlock.java
+++ b/src/main/java/eu/mhsl/minecraft/pixelblocks/pixelblock/PixelBlock.java
@@ -136,16 +136,64 @@ public class PixelBlock {
         if(!uuids.contains(this.uuid)) {
             try (var conn = DriverManager.getConnection(url)) {
                 PreparedStatement prep = conn.prepareStatement(
-                    "INSERT INTO pixelblocks(uuid, owner, locationWorldName, locationX, locationY, locationZ)" +
-                        " VALUES(?, ?, ?, ?, ?, ?);"
+                "INSERT INTO pixelblocks(uuid, owner, " +
+                    "locationWorldName, locationX, locationY, locationZ, " +
+                    "entryLocationWorldName, entryLocationX, entryLocationY, entryLocationZ) " +
+                    "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"
                 );
                 prep.setString(1, this.uuid.toString());
                 prep.setString(2, this.owner.toString());
+
                 prep.setString(3, this.pixelBlockLocation.getWorld().getName());
                 prep.setDouble(4, this.pixelBlockLocation.getX());
                 prep.setDouble(5, this.pixelBlockLocation.getY());
                 prep.setDouble(6, this.pixelBlockLocation.getZ());
 
+                if(this.lastEntryLocation != null) {
+                    prep.setString(7, this.lastEntryLocation.getWorld().getName());
+                    prep.setDouble(8, this.lastEntryLocation.getX());
+                    prep.setDouble(9, this.lastEntryLocation.getY());
+                    prep.setDouble(10, this.lastEntryLocation.getZ());
+                } else {
+                    prep.setString(7, Bukkit.getWorlds().getFirst().getName());
+                    prep.setDouble(8, Bukkit.getWorlds().getFirst().getSpawnLocation().getX());
+                    prep.setDouble(9, Bukkit.getWorlds().getFirst().getSpawnLocation().getY());
+                    prep.setDouble(10, Bukkit.getWorlds().getFirst().getSpawnLocation().getZ());
+                }
+
+                prep.executeUpdate();
+            } catch (SQLException e) {
+                System.err.println(e.getMessage());
+            }
+        } else {
+            try (var conn = DriverManager.getConnection(url)) {
+                PreparedStatement prep = conn.prepareStatement(
+                    "UPDATE pixelblocks " +
+                        "SET owner=?, locationWorldName=?, locationX=?, locationY=?, locationZ=?," +
+                        "entryLocationWorldName=?, entryLocationX=?, entryLocationY=?, entryLocationZ=? " +
+                        "WHERE uuid=?;"
+                );
+                prep.setString(1, this.owner.toString());
+
+                prep.setString(2, this.pixelBlockLocation.getWorld().getName());
+                prep.setDouble(3, this.pixelBlockLocation.getX());
+                prep.setDouble(4, this.pixelBlockLocation.getY());
+                prep.setDouble(5, this.pixelBlockLocation.getZ());
+
+                if(this.lastEntryLocation != null) {
+                    prep.setString(6, this.lastEntryLocation.getWorld().getName());
+                    prep.setDouble(7, this.lastEntryLocation.getX());
+                    prep.setDouble(8, this.lastEntryLocation.getY());
+                    prep.setDouble(9, this.lastEntryLocation.getZ());
+                } else {
+                    prep.setString(6, Bukkit.getWorlds().getFirst().getName());
+                    prep.setDouble(7, Bukkit.getWorlds().getFirst().getSpawnLocation().getX());
+                    prep.setDouble(8, Bukkit.getWorlds().getFirst().getSpawnLocation().getY());
+                    prep.setDouble(9, Bukkit.getWorlds().getFirst().getSpawnLocation().getZ());
+                }
+
+                prep.setString(10, this.uuid.toString());
+
                 prep.executeUpdate();
             } catch (SQLException e) {
                 System.err.println(e.getMessage());