working database

This commit is contained in:
2024-07-13 19:24:26 +02:00
parent 216258955d
commit 5806788595
4 changed files with 78 additions and 53 deletions

View File

@@ -5,7 +5,10 @@ import org.bukkit.*;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Interaction;
import org.codehaus.plexus.util.FileUtils;
import java.io.File;
import java.io.IOException;
import java.sql.*;
import java.util.*;
@@ -26,9 +29,9 @@ public class PixelBlock {
public UUID owner;
public UUID uuid;
public PixelBlock(Location originLocation, UUID owner, double pixelsPerBlock) {
public PixelBlock(Location originLocation, UUID owner, double pixelsPerBlock, UUID blockUUID) {
PixelBlock.placedBlocks.add(this);
this.uuid = UUID.randomUUID();
this.uuid = blockUUID;
this.pixelBlockLocation = originLocation.toBlockLocation();
this.pixelBlockLocation.setYaw(0);
@@ -36,32 +39,17 @@ public class PixelBlock {
this.pixelsPerBlock = pixelsPerBlock;
this.owner = owner;
for (int x = 0; x < 16; x++) {
for (int z = 0; z < 16; z++) {
Pixel newPixel = new Pixel(
new Location(originLocation.getWorld(), x, 0, z),
Material.GRASS_BLOCK.createBlockData(),
(1/pixelsPerBlock)-0.0001,
0.00005*pixelsPerBlock
);
pixels.add(newPixel);
}
}
// pixelBlockLocation.getBlock().setType(Material.GLASS);
hitbox = (Interaction) pixelBlockLocation.getWorld().spawnEntity(
pixelBlockLocation.clone().add(0.5, -hitboxOffset, 0.5),
EntityType.INTERACTION
);
hitbox.setInteractionHeight(1F + 2*hitboxOffset);
hitbox.setInteractionWidth(1F + 2*hitboxOffset);
createWorld();
}
void createWorld() {
if(Bukkit.getWorld(plugin.getDataFolder().getPath() + "/" + this.uuid) == null) {
File file = new File(plugin.getDataFolder().getPath()+ "/" +this.uuid.toString());
if(!file.exists() || !file.isDirectory()) {
System.out.println("NEUE WELT WURDE ERSTELLT");
final WorldCreator worldCreator = new WorldCreator(plugin.getDataFolder().getPath() + "/" + this.uuid);
worldCreator.type(WorldType.FLAT);
worldCreator.generator(new EmptyChunkGenerator());
@@ -101,18 +89,6 @@ public class PixelBlock {
public void update() {
Bukkit.getScheduler().runTask(plugin, () -> {
// Player owner = null;
// for(Player player : Bukkit.getOnlinePlayers()) {
// if(player.getUniqueId() == this.owner) {
// owner = player;
// }
// }
//
// World[] standardWorlds = {Bukkit.getWorlds().get(0), Bukkit.getWorlds().get(1), Bukkit.getWorlds().get(2)};
// if(owner == null || Arrays.stream(standardWorlds).toList().contains(owner.getWorld())) {
// Bukkit.getScheduler().cancelTask(this.updateTaskID);
// }
for(Pixel pixel : this.pixels) { pixel.remove(); }
pixels.clear();
@@ -142,14 +118,20 @@ public class PixelBlock {
});
}
public void place(Location placeLocation) {
Bukkit.getScheduler().runTask(plugin, () -> {
for(Pixel pixel : pixels) {
pixel.spawn(placeLocation.toBlockLocation());
public void saveToDB() {
String url = "jdbc:sqlite:pixelblocks.db";
List<UUID> uuids = new ArrayList<>();
try (var conn = DriverManager.getConnection(url);
var stmt = conn.createStatement()) {
ResultSet pixelBlocksResult = stmt.executeQuery("SELECT * FROM pixelblocks;");
while (pixelBlocksResult.next()) {
uuids.add(UUID.fromString(pixelBlocksResult.getString("uuid")));
}
} catch (SQLException e) {
System.err.println(e.getMessage());
}
String url = "jdbc:sqlite:pixelblocks.db";
if(!uuids.contains(this.uuid)) {
try (var conn = DriverManager.getConnection(url)) {
PreparedStatement prep = conn.prepareStatement(
"INSERT INTO pixelblocks(uuid, owner, locationWorldName, locationX, locationY, locationZ)" +
@@ -163,17 +145,13 @@ public class PixelBlock {
prep.setDouble(6, this.pixelBlockLocation.getZ());
prep.executeUpdate();
conn.close();
prep.close();
} catch (SQLException e) {
System.err.println(e.getMessage());
}
});
// this.updateTaskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, this::update, 100L, 100L);
}
}
public void remove() {
public void removeFromDB() {
Bukkit.getScheduler().runTask(plugin, () -> {
String url = "jdbc:sqlite:pixelblocks.db";
try (var conn = DriverManager.getConnection(url)) {
@@ -187,10 +165,39 @@ public class PixelBlock {
System.err.println(e.getMessage());
}
});
}
public void place(Location placeLocation) {
Bukkit.getScheduler().runTask(plugin, () -> {
hitbox = (Interaction) pixelBlockLocation.getWorld().spawnEntity(
pixelBlockLocation.clone().add(0.5, -hitboxOffset, 0.5),
EntityType.INTERACTION
);
hitbox.setInteractionHeight(1F + 2*hitboxOffset);
hitbox.setInteractionWidth(1F + 2*hitboxOffset);
this.saveToDB();
update();
});
// this.updateTaskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, this::update, 100L, 100L);
}
public void remove() {
this.removeFromDB();
this.pixels.forEach(Pixel::remove);
hitbox.remove();
// pixelBlockLocation.getBlock().setType(Material.AIR);
placedBlocks.remove(this);
}
public void delete() {
this.remove();
Bukkit.unloadWorld(plugin.getDataFolder().getPath() + "/" + this.uuid, true);
try {
FileUtils.deleteDirectory(plugin.getDataFolder().getPath() + "/" + this.uuid);
} catch (IOException e) {
System.err.println("World could not be deleted!");
}
}
}