working database
This commit is contained in:
parent
216258955d
commit
5806788595
@ -7,11 +7,15 @@ import eu.mhsl.minecraft.pixelblocks.listeners.*;
|
|||||||
import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock;
|
import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
|
import org.bukkit.entity.Entity;
|
||||||
|
import org.bukkit.entity.EntityType;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
import java.sql.DriverManager;
|
import java.sql.DriverManager;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@ -39,6 +43,17 @@ public final class PixelBlocks extends JavaPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void loadDB() {
|
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 url = "jdbc:sqlite:pixelblocks.db";
|
||||||
String sql = "CREATE TABLE IF NOT EXISTS pixelblocks (" +
|
String sql = "CREATE TABLE IF NOT EXISTS pixelblocks (" +
|
||||||
"uuid CHAR(36) PRIMARY KEY, " +
|
"uuid CHAR(36) PRIMARY KEY, " +
|
||||||
@ -56,17 +71,20 @@ public final class PixelBlocks extends JavaPlugin {
|
|||||||
ResultSet pixelBlocksResult = stmt.executeQuery("SELECT * FROM pixelblocks;");
|
ResultSet pixelBlocksResult = stmt.executeQuery("SELECT * FROM pixelblocks;");
|
||||||
while (pixelBlocksResult.next()) {
|
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("locationX"),
|
||||||
pixelBlocksResult.getDouble("locationY"),
|
pixelBlocksResult.getDouble("locationY"),
|
||||||
pixelBlocksResult.getDouble("locationZ"));
|
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(
|
PixelBlock newPixelBlock = new PixelBlock(
|
||||||
newPixelBlockLocation,
|
newPixelBlockLocation,
|
||||||
UUID.fromString(pixelBlocksResult.getString("owner")),
|
UUID.fromString(pixelBlocksResult.getString("owner")),
|
||||||
16
|
16,
|
||||||
|
UUID.fromString(pixelBlocksResult.getString("uuid"))
|
||||||
);
|
);
|
||||||
newPixelBlock.remove();
|
|
||||||
newPixelBlock.place(newPixelBlockLocation);
|
newPixelBlock.place(newPixelBlockLocation);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,5 +92,4 @@ public final class PixelBlocks extends JavaPlugin {
|
|||||||
System.err.println(e.getMessage());
|
System.err.println(e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,7 @@ import org.bukkit.entity.Player;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
public class CreatePixelBlockCommand implements CommandExecutor {
|
public class CreatePixelBlockCommand implements CommandExecutor {
|
||||||
@Override
|
@Override
|
||||||
@ -21,7 +22,7 @@ public class CreatePixelBlockCommand implements CommandExecutor {
|
|||||||
|
|
||||||
if(Arrays.stream(standardWorlds).toList().contains(playerWorld)) {
|
if(Arrays.stream(standardWorlds).toList().contains(playerWorld)) {
|
||||||
Location playerLocation = p.getLocation();
|
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);
|
pixelBlock.place(playerLocation);
|
||||||
} else {
|
} else {
|
||||||
p.sendMessage("Du kannst nur in der Overworld oder im Nether Pixelblocks erstellen!");
|
p.sendMessage("Du kannst nur in der Overworld oder im Nether Pixelblocks erstellen!");
|
||||||
|
@ -48,9 +48,9 @@ public class PlayerInteractListener implements Listener {
|
|||||||
pixelBlock.lastEntryLocation = event.getPlayer().getLocation();
|
pixelBlock.lastEntryLocation = event.getPlayer().getLocation();
|
||||||
pixelBlock.lastEntryTime = System.currentTimeMillis();
|
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;
|
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);
|
// Bukkit.getScheduler().cancelTask(pixelBlock.updateTaskID);
|
||||||
// pixelBlock.updateTaskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(
|
// pixelBlock.updateTaskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(
|
||||||
|
@ -5,7 +5,10 @@ import org.bukkit.*;
|
|||||||
import org.bukkit.block.data.BlockData;
|
import org.bukkit.block.data.BlockData;
|
||||||
import org.bukkit.entity.EntityType;
|
import org.bukkit.entity.EntityType;
|
||||||
import org.bukkit.entity.Interaction;
|
import org.bukkit.entity.Interaction;
|
||||||
|
import org.codehaus.plexus.util.FileUtils;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
import java.sql.*;
|
import java.sql.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
@ -26,9 +29,9 @@ public class PixelBlock {
|
|||||||
public UUID owner;
|
public UUID owner;
|
||||||
public UUID uuid;
|
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);
|
PixelBlock.placedBlocks.add(this);
|
||||||
this.uuid = UUID.randomUUID();
|
this.uuid = blockUUID;
|
||||||
|
|
||||||
this.pixelBlockLocation = originLocation.toBlockLocation();
|
this.pixelBlockLocation = originLocation.toBlockLocation();
|
||||||
this.pixelBlockLocation.setYaw(0);
|
this.pixelBlockLocation.setYaw(0);
|
||||||
@ -36,32 +39,17 @@ public class PixelBlock {
|
|||||||
this.pixelsPerBlock = pixelsPerBlock;
|
this.pixelsPerBlock = pixelsPerBlock;
|
||||||
this.owner = owner;
|
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);
|
// 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();
|
createWorld();
|
||||||
}
|
}
|
||||||
|
|
||||||
void 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);
|
final WorldCreator worldCreator = new WorldCreator(plugin.getDataFolder().getPath() + "/" + this.uuid);
|
||||||
worldCreator.type(WorldType.FLAT);
|
worldCreator.type(WorldType.FLAT);
|
||||||
worldCreator.generator(new EmptyChunkGenerator());
|
worldCreator.generator(new EmptyChunkGenerator());
|
||||||
@ -101,18 +89,6 @@ public class PixelBlock {
|
|||||||
|
|
||||||
public void update() {
|
public void update() {
|
||||||
Bukkit.getScheduler().runTask(plugin, () -> {
|
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(); }
|
for(Pixel pixel : this.pixels) { pixel.remove(); }
|
||||||
pixels.clear();
|
pixels.clear();
|
||||||
|
|
||||||
@ -142,14 +118,20 @@ public class PixelBlock {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void place(Location placeLocation) {
|
public void saveToDB() {
|
||||||
Bukkit.getScheduler().runTask(plugin, () -> {
|
String url = "jdbc:sqlite:pixelblocks.db";
|
||||||
for(Pixel pixel : pixels) {
|
List<UUID> uuids = new ArrayList<>();
|
||||||
pixel.spawn(placeLocation.toBlockLocation());
|
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());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(!uuids.contains(this.uuid)) {
|
||||||
String url = "jdbc:sqlite:pixelblocks.db";
|
|
||||||
try (var conn = DriverManager.getConnection(url)) {
|
try (var conn = DriverManager.getConnection(url)) {
|
||||||
PreparedStatement prep = conn.prepareStatement(
|
PreparedStatement prep = conn.prepareStatement(
|
||||||
"INSERT INTO pixelblocks(uuid, owner, locationWorldName, locationX, locationY, locationZ)" +
|
"INSERT INTO pixelblocks(uuid, owner, locationWorldName, locationX, locationY, locationZ)" +
|
||||||
@ -163,17 +145,13 @@ public class PixelBlock {
|
|||||||
prep.setDouble(6, this.pixelBlockLocation.getZ());
|
prep.setDouble(6, this.pixelBlockLocation.getZ());
|
||||||
|
|
||||||
prep.executeUpdate();
|
prep.executeUpdate();
|
||||||
|
|
||||||
conn.close();
|
|
||||||
prep.close();
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
System.err.println(e.getMessage());
|
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, () -> {
|
Bukkit.getScheduler().runTask(plugin, () -> {
|
||||||
String url = "jdbc:sqlite:pixelblocks.db";
|
String url = "jdbc:sqlite:pixelblocks.db";
|
||||||
try (var conn = DriverManager.getConnection(url)) {
|
try (var conn = DriverManager.getConnection(url)) {
|
||||||
@ -187,10 +165,39 @@ public class PixelBlock {
|
|||||||
System.err.println(e.getMessage());
|
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);
|
this.pixels.forEach(Pixel::remove);
|
||||||
hitbox.remove();
|
hitbox.remove();
|
||||||
// pixelBlockLocation.getBlock().setType(Material.AIR);
|
// pixelBlockLocation.getBlock().setType(Material.AIR);
|
||||||
placedBlocks.remove(this);
|
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!");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user