own database class, added methods to PixelBlock class

This commit is contained in:
2024-07-16 14:06:49 +02:00
parent 89de8b6ab5
commit 5c8bc57123
12 changed files with 378 additions and 416 deletions

View File

@@ -1,26 +1,29 @@
package eu.mhsl.minecraft.pixelblocks.pixelblock;
import eu.mhsl.minecraft.pixelblocks.PixelBlocks;
import eu.mhsl.minecraft.pixelblocks.chunkGenerators.EmptyChunkGenerator;
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 org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.codehaus.plexus.util.FileUtils;
import java.io.File;
import java.io.IOException;
import java.sql.*;
import java.util.*;
import static eu.mhsl.minecraft.pixelblocks.PixelBlocks.plugin;
import static eu.mhsl.minecraft.pixelblocks.PixelBlocks.*;
public class PixelBlock {
public static List<PixelBlock> placedBlocks = new ArrayList<>();
public static float hitboxOffset = 0.005F;
public static int worldGrassBorderWidth = 5;
public static int pixelsPerBlock = 16;
public Location pixelBlockLocation;
public double pixelsPerBlock;
public ArrayList<Pixel> pixels = new ArrayList<>();
public Interaction hitbox;
@@ -29,28 +32,51 @@ public class PixelBlock {
public UUID owner;
public UUID uuid;
public PixelBlock(Location originLocation, UUID owner, double pixelsPerBlock, UUID blockUUID) {
public PixelBlock(Location originLocation, UUID owner, UUID blockUUID) {
PixelBlock.placedBlocks.add(this);
this.uuid = blockUUID;
this.pixelBlockLocation = originLocation.toBlockLocation();
this.pixelBlockLocation.setYaw(0);
this.pixelBlockLocation.setPitch(0);
this.pixelsPerBlock = pixelsPerBlock;
this.owner = owner;
// pixelBlockLocation.getBlock().setType(Material.GLASS);
createWorld();
createPixelWorld();
}
void createWorld() {
File file = new File(plugin.getDataFolder().getPath()+ "/" +this.uuid.toString());
public static PixelBlock getPixelBlockFromWorld(World world) {
String blockUUID = Arrays.stream(world.getName().split(pathSeparator)).toList().getLast();
List<PixelBlock> pixelBlocks = placedBlocks.stream().filter(block -> block.uuid.toString().equals(blockUUID)).toList();
if(!pixelBlocks.isEmpty()) {
return pixelBlocks.getFirst();
} else {
return null;
}
}
public static PixelBlock getPixelBlockFromLocation(Location location) {
Location loc = location.clone().toBlockLocation();
loc.setPitch(0);
loc.setYaw(0);
List<PixelBlock> pixelBlocks = placedBlocks.stream()
.filter(block -> block.pixelBlockLocation.equals(loc))
.toList();
if(pixelBlocks.isEmpty()) {
return null;
} else {
return pixelBlocks.getFirst();
}
}
void createPixelWorld() {
File file = new File(plugin.getDataFolder().getPath()+ pathSeparator +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);
final WorldCreator worldCreator = new WorldCreator(plugin.getDataFolder().getPath() + pathSeparator + this.uuid);
worldCreator.type(WorldType.FLAT);
worldCreator.generator(new EmptyChunkGenerator());
World newWorld = Bukkit.createWorld(worldCreator);
@@ -85,21 +111,110 @@ public class PixelBlock {
borderStartLocation.getBlock().setType(Material.WHITE_CONCRETE);
});
} else {
new WorldCreator(plugin.getDataFolder().getPath() + "/" + this.uuid).createWorld();
new WorldCreator(plugin.getDataFolder().getPath() + pathSeparator + this.uuid).createWorld();
}
}
public void handleInteraction(Player player) {
if(!player.getUniqueId().equals(owner)) {
player.sendMessage("Dieser Pixelblock gehört nicht dir!");
return;
}
this.lastEntryLocation = player.getLocation();
this.lastEntryTime = System.currentTimeMillis();
dataBase.savePixelBlock(this);
player.teleport(getPlayerSpawnLocation(player));
}
public void handleAttack(Player player) {
if(!player.getUniqueId().equals(owner)) {
player.sendMessage("Dieser Pixelblock gehört nicht dir!");
return;
}
this.delete();
}
public void handleBlockBreak(BlockBreakEvent event, boolean liveUpdate) {
Location blockLocation = event.getBlock().getLocation();
Location spawnLocation = blockLocation.getWorld().getSpawnLocation();
if(blockLocation.x() < spawnLocation.x() || blockLocation.z() < spawnLocation.z() || blockLocation.y() < spawnLocation.y()-1) {
event.setCancelled(true);
return;
} else if(blockLocation.x() > spawnLocation.x()+15 || blockLocation.z() > spawnLocation.z()+15 || blockLocation.y() > spawnLocation.y()+14) {
event.setCancelled(true);
return;
}
Bukkit.getScheduler().runTask(plugin, () -> {
if(liveUpdate) {
Location relativeLocation = blockLocation.subtract(spawnLocation.x(), spawnLocation.y()-1, spawnLocation.z());
relativeLocation.setWorld(this.pixelBlockLocation.getWorld());
List<Pixel> pixels = this.pixels.stream().filter(pixel -> pixel.relativeLocation.equals(relativeLocation)).toList();
if(!pixels.isEmpty()) {
Pixel pixel = pixels.getFirst();
pixel.remove();
this.pixels.remove(pixel);
}
}
});
}
public void handleBlockPlace(BlockPlaceEvent event, boolean liveUpdate) {
Location blockLocation = event.getBlock().getLocation();
Location spawnLocation = blockLocation.getWorld().getSpawnLocation();
if(blockLocation.x() < spawnLocation.x() || blockLocation.z() < spawnLocation.z() || blockLocation.y() < spawnLocation.y()-1) {
event.setCancelled(true);
return;
} else if(blockLocation.x() > spawnLocation.x()+15 || blockLocation.z() > spawnLocation.z()+15 || blockLocation.y() > spawnLocation.y()+14) {
event.setCancelled(true);
return;
}
Bukkit.getScheduler().runTask(PixelBlocks.plugin, () -> {
if(liveUpdate) {
Location relativeLocation = blockLocation.subtract(spawnLocation.x(), spawnLocation.y()-1, spawnLocation.z());
relativeLocation.setWorld(this.pixelBlockLocation.getWorld());
Pixel newPixel = new Pixel(relativeLocation, event.getBlock().getBlockData(), ((double) 1 /pixelsPerBlock), 0);
this.pixels.add(newPixel);
newPixel.spawn(this.pixelBlockLocation);
}
});
}
public Location getPlayerSpawnLocation(Player player) {
Location spawnLocation = getPixelWorld().getSpawnLocation().clone().subtract(0.5, 0, 0.5);
spawnLocation.setYaw(player.getLocation().getYaw());
spawnLocation.setPitch(player.getLocation().getPitch());
return spawnLocation;
}
public World getPixelWorld() {
return Bukkit.getWorld(plugin.getDataFolder().getPath() + pathSeparator + this.uuid);
}
public void update() {
Bukkit.getScheduler().runTask(plugin, () -> {
for(Pixel pixel : this.pixels) { pixel.remove(); }
pixels.clear();
clearEntities();
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);
for (int x = 0; x < 16; x++) {
for (int y = 0; y < 16; y++) {
for (int z = 0; z < 16; z++) {
Location relativeLocation = new Location(pixelBlockLocation.getWorld(), x, y, z);
Location blockLocation = Objects.requireNonNull(Bukkit
.getWorld(plugin.getDataFolder().getPath() + "/" + this.uuid))
.getWorld(plugin.getDataFolder().getPath() + pathSeparator + this.uuid))
.getSpawnLocation()
.clone()
.subtract(0, 1, 0)
@@ -107,7 +222,7 @@ public class PixelBlock {
BlockData block = blockLocation.getBlock().getBlockData();
if(block.getMaterial() != Material.AIR) {
Pixel newPixel = new Pixel(relativeLocation, block, (1/pixelsPerBlock)-0.0001, 0.00005*pixelsPerBlock);
Pixel newPixel = new Pixel(relativeLocation, block, ((double) 1 /pixelsPerBlock), 0);
pixels.add(newPixel);
}
}
@@ -120,134 +235,47 @@ public class PixelBlock {
});
}
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());
public void clearEntities() {
if(!pixels.isEmpty()) {
this.pixels.forEach(Pixel::remove);
pixels.clear();
}
if(!uuids.contains(this.uuid)) {
try (var conn = DriverManager.getConnection(url)) {
PreparedStatement prep = conn.prepareStatement(
"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());
}
if(hitbox != null) {
hitbox.remove();
hitbox = null;
}
}
public void removeFromDB() {
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();
public boolean place(Location placeLocation) {
Location newLocation = placeLocation.toBlockLocation();
newLocation.setPitch(0);
newLocation.setYaw(0);
conn.close();
prep.close();
} catch (SQLException e) {
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();
if(PixelBlock.getPixelBlockFromLocation(newLocation) == null || PixelBlock.getPixelBlockFromLocation(newLocation) == this) {
this.pixelBlockLocation = newLocation;
update();
});
// this.updateTaskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, this::update, 100L, 100L);
dataBase.savePixelBlock(this);
return true;
}
return false;
}
public void remove() {
this.removeFromDB();
dataBase.removePixelBlock(this);
this.pixels.forEach(Pixel::remove);
hitbox.remove();
// pixelBlockLocation.getBlock().setType(Material.AIR);
clearEntities();
placedBlocks.remove(this);
}
public void delete() {
this.remove();
Bukkit.unloadWorld(plugin.getDataFolder().getPath() + "/" + this.uuid, true);
Bukkit.unloadWorld(getPixelWorld(), true);
try {
FileUtils.deleteDirectory(plugin.getDataFolder().getPath() + "/" + this.uuid);
FileUtils.deleteDirectory(plugin.getDataFolder().getPath() + pathSeparator + this.uuid);
} catch (IOException e) {
System.err.println("World could not be deleted!");
System.err.println("World could not be deleted: " + e.getMessage());
}
}
}