working database

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

View File

@ -7,11 +7,15 @@ import eu.mhsl.minecraft.pixelblocks.listeners.*;
import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.plugin.java.JavaPlugin;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
@ -39,6 +43,17 @@ public final class PixelBlocks extends JavaPlugin {
}
public void loadDB() {
List<Entity> entities = new ArrayList<>();
entities.addAll(Bukkit.getWorlds().get(0).getEntities().stream()
.filter(entity -> entity.getType().equals(EntityType.BLOCK_DISPLAY) || entity.getType().equals(EntityType.INTERACTION))
.toList());
entities.addAll(Bukkit.getWorlds().get(1).getEntities().stream()
.filter(entity -> entity.getType().equals(EntityType.BLOCK_DISPLAY) || entity.getType().equals(EntityType.INTERACTION))
.toList());
entities.addAll(Bukkit.getWorlds().get(2).getEntities().stream()
.filter(entity -> entity.getType().equals(EntityType.BLOCK_DISPLAY) || entity.getType().equals(EntityType.INTERACTION))
.toList());
String url = "jdbc:sqlite:pixelblocks.db";
String sql = "CREATE TABLE IF NOT EXISTS pixelblocks (" +
"uuid CHAR(36) PRIMARY KEY, " +
@ -56,17 +71,20 @@ public final class PixelBlocks extends JavaPlugin {
ResultSet pixelBlocksResult = stmt.executeQuery("SELECT * FROM pixelblocks;");
while (pixelBlocksResult.next()) {
Location newPixelBlockLocation = new Location(Bukkit.getWorld(pixelBlocksResult.getString("locationWorldName")),
Location newPixelBlockLocation = new Location(
Bukkit.getWorld(pixelBlocksResult.getString("locationWorldName")),
pixelBlocksResult.getDouble("locationX"),
pixelBlocksResult.getDouble("locationY"),
pixelBlocksResult.getDouble("locationZ"));
entities.stream().filter(entity -> entity.getLocation().clone().add(0, PixelBlock.hitboxOffset, 0).toBlockLocation().equals(newPixelBlockLocation)).forEach(Entity::remove);
PixelBlock newPixelBlock = new PixelBlock(
newPixelBlockLocation,
UUID.fromString(pixelBlocksResult.getString("owner")),
16
16,
UUID.fromString(pixelBlocksResult.getString("uuid"))
);
newPixelBlock.remove();
newPixelBlock.place(newPixelBlockLocation);
}
@ -74,5 +92,4 @@ public final class PixelBlocks extends JavaPlugin {
System.err.println(e.getMessage());
}
}
}

View File

@ -11,6 +11,7 @@ import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
import java.util.UUID;
public class CreatePixelBlockCommand implements CommandExecutor {
@Override
@ -21,7 +22,7 @@ public class CreatePixelBlockCommand implements CommandExecutor {
if(Arrays.stream(standardWorlds).toList().contains(playerWorld)) {
Location playerLocation = p.getLocation();
PixelBlock pixelBlock = new PixelBlock(playerLocation, p.getUniqueId(), 16);
PixelBlock pixelBlock = new PixelBlock(playerLocation, p.getUniqueId(), 16, UUID.randomUUID());
pixelBlock.place(playerLocation);
} else {
p.sendMessage("Du kannst nur in der Overworld oder im Nether Pixelblocks erstellen!");

View File

@ -48,9 +48,9 @@ public class PlayerInteractListener implements Listener {
pixelBlock.lastEntryLocation = event.getPlayer().getLocation();
pixelBlock.lastEntryTime = System.currentTimeMillis();
World blockWorld = Bukkit.getWorld(plugin.getDataFolder().getPath()+ "/"+pixelBlock.uuid);
World blockWorld = Bukkit.getWorld(plugin.getDataFolder().getPath()+ "/" +pixelBlock.uuid);
assert blockWorld != null;
event.getPlayer().teleport(blockWorld.getSpawnLocation());
event.getPlayer().teleport(blockWorld.getSpawnLocation().clone().subtract(0.5, 0, 0.5));
// Bukkit.getScheduler().cancelTask(pixelBlock.updateTaskID);
// pixelBlock.updateTaskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(

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!");
}
}
}