save entry location in database

This commit is contained in:
Lars Neuhaus 2024-07-14 13:50:23 +02:00
parent 4b2203e5ae
commit 89de8b6ab5
3 changed files with 79 additions and 17 deletions

View File

@ -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);
}

View File

@ -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;

View File

@ -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());