started with database

This commit is contained in:
2024-07-12 19:00:56 +02:00
parent 992cea92ef
commit 216258955d
6 changed files with 169 additions and 61 deletions

View File

@@ -5,8 +5,8 @@ import org.bukkit.*;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Interaction;
import org.bukkit.entity.Player;
import java.sql.*;
import java.util.*;
import static eu.mhsl.minecraft.pixelblocks.PixelBlocks.plugin;
@@ -25,7 +25,6 @@ public class PixelBlock {
public Location lastEntryLocation;
public UUID owner;
public UUID uuid;
public int updateTaskID;
public PixelBlock(Location originLocation, UUID owner, double pixelsPerBlock) {
PixelBlock.placedBlocks.add(this);
@@ -62,53 +61,57 @@ public class PixelBlock {
}
void createWorld() {
final WorldCreator worldCreator = new WorldCreator(plugin.getDataFolder().getPath()+"/"+this.uuid);
worldCreator.type(WorldType.FLAT);
worldCreator.generator(new EmptyChunkGenerator());
World newWorld = Bukkit.createWorld(worldCreator);
if(Bukkit.getWorld(plugin.getDataFolder().getPath() + "/" + this.uuid) == null) {
final WorldCreator worldCreator = new WorldCreator(plugin.getDataFolder().getPath() + "/" + this.uuid);
worldCreator.type(WorldType.FLAT);
worldCreator.generator(new EmptyChunkGenerator());
World newWorld = Bukkit.createWorld(worldCreator);
assert newWorld != null;
Location borderStartLocation = newWorld.getSpawnLocation().clone().subtract(1, 1, 1);
Location grassStartLocation = borderStartLocation.clone().subtract(worldGrassBorderWidth, 0, worldGrassBorderWidth);
assert newWorld != null;
newWorld.setGameRule(GameRule.DO_DAYLIGHT_CYCLE, false);
Bukkit.getScheduler().runTask(plugin, () -> {
for (int x = 0; x < 18+2*worldGrassBorderWidth; x++) {
for (int z = 0; z < 18+2*worldGrassBorderWidth; z++) {
grassStartLocation.clone().add(x, 0, z).getBlock().setType(Material.GRASS_BLOCK);
}
}
for (int x = 0; x < 18+2*worldGrassBorderWidth; x++) {
for (int z = 0; z < 18+2*worldGrassBorderWidth; z++) {
grassStartLocation.clone().add(x, -1, z).getBlock().setType(Material.DIRT);
}
}
for (int x = 0; x < 18; x++) {
for (int z = 0; z < 18; z++) {
Location currentLocation = borderStartLocation.clone().add(x, 0, z);
if(currentLocation.x() == borderStartLocation.x() || currentLocation.z() == borderStartLocation.z()) {
currentLocation.getBlock().setType(Material.RED_CONCRETE);
} else if (currentLocation.x() == borderStartLocation.x()+17 || currentLocation.z() == borderStartLocation.z()+17) {
currentLocation.getBlock().setType(Material.RED_CONCRETE);
Location borderStartLocation = newWorld.getSpawnLocation().clone().subtract(1, 1, 1);
Location grassStartLocation = borderStartLocation.clone().subtract(worldGrassBorderWidth, 0, worldGrassBorderWidth);
Bukkit.getScheduler().runTask(plugin, () -> {
for (int x = 0; x < 18 + 2 * worldGrassBorderWidth; x++) {
for (int z = 0; z < 18 + 2 * worldGrassBorderWidth; z++) {
grassStartLocation.clone().add(x, 0, z).getBlock().setType(Material.GRASS_BLOCK);
}
}
}
borderStartLocation.getBlock().setType(Material.WHITE_CONCRETE);
});
for (int x = 0; x < 18 + 2 * worldGrassBorderWidth; x++) {
for (int z = 0; z < 18 + 2 * worldGrassBorderWidth; z++) {
grassStartLocation.clone().add(x, -1, z).getBlock().setType(Material.DIRT);
}
}
for (int x = 0; x < 18; x++) {
for (int z = 0; z < 18; z++) {
Location currentLocation = borderStartLocation.clone().add(x, 0, z);
if (currentLocation.x() == borderStartLocation.x() || currentLocation.z() == borderStartLocation.z()) {
currentLocation.getBlock().setType(Material.RED_CONCRETE);
} else if (currentLocation.x() == borderStartLocation.x() + 17 || currentLocation.z() == borderStartLocation.z() + 17) {
currentLocation.getBlock().setType(Material.RED_CONCRETE);
}
}
}
borderStartLocation.getBlock().setType(Material.WHITE_CONCRETE);
});
}
}
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);
}
// 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();
@@ -144,16 +147,50 @@ public class PixelBlock {
for(Pixel pixel : pixels) {
pixel.spawn(placeLocation.toBlockLocation());
}
});
this.updateTaskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, this::update, 100L, 100L);
String url = "jdbc:sqlite:pixelblocks.db";
try (var conn = DriverManager.getConnection(url)) {
PreparedStatement prep = conn.prepareStatement(
"INSERT INTO pixelblocks(uuid, owner, locationWorldName, locationX, locationY, locationZ)" +
" 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());
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() {
Bukkit.getScheduler().runTask(plugin, () -> {
String url = "jdbc:sqlite:pixelblocks.db";
try (var conn = DriverManager.getConnection(url)) {
PreparedStatement prep = conn.prepareStatement("DELETE FROM pixelblocks WHERE uuid = ?");
prep.setString(1, this.uuid.toString());
prep.executeUpdate();
conn.close();
prep.close();
} catch (SQLException e) {
System.err.println(e.getMessage());
}
});
this.pixels.forEach(Pixel::remove);
hitbox.remove();
pixelBlockLocation.getBlock().setType(Material.AIR);
// pixelBlockLocation.getBlock().setType(Material.AIR);
placedBlocks.remove(this);
Bukkit.getScheduler().cancelTask(this.updateTaskID);
}
}