started with database

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

View File

@ -4,9 +4,16 @@ import eu.mhsl.minecraft.pixelblocks.commands.ChessCommand;
import eu.mhsl.minecraft.pixelblocks.commands.CreatePixelBlockCommand;
import eu.mhsl.minecraft.pixelblocks.commands.ExitWorldCommand;
import eu.mhsl.minecraft.pixelblocks.listeners.*;
import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.plugin.java.JavaPlugin;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Objects;
import java.util.UUID;
public final class PixelBlocks extends JavaPlugin {
public static PixelBlocks plugin;
@ -14,8 +21,8 @@ public final class PixelBlocks extends JavaPlugin {
@Override
public void onEnable() {
PixelBlocks.plugin = this;
this.getLogger().info("Hello World");
// new File(plugin.getDataFolder().getPath()).mkdir();
this.getLogger().info("PixelBlocks Plugin was enabled.");
this.loadDB();
getServer().getPluginManager().registerEvents(new PlayerJumpListener(), this);
getServer().getPluginManager().registerEvents(new PlayerInteractListener(), this);
@ -24,10 +31,48 @@ public final class PixelBlocks extends JavaPlugin {
getServer().getPluginManager().registerEvents(new BlockBreakListener(), this);
getServer().getPluginManager().registerEvents(new BlockPlaceListener(), this);
getServer().getPluginManager().registerEvents(new CreatureSpawnListener(), this);
getServer().getPluginManager().registerEvents(new PlayerChangeWorldListener(), this);
Objects.requireNonNull(getCommand("chess")).setExecutor(new ChessCommand());
Objects.requireNonNull(getCommand("createpixelblock")).setExecutor(new CreatePixelBlockCommand());
Objects.requireNonNull(getCommand("exitworld")).setExecutor(new ExitWorldCommand());
}
public void loadDB() {
String url = "jdbc:sqlite:pixelblocks.db";
String sql = "CREATE TABLE IF NOT EXISTS pixelblocks (" +
"uuid CHAR(36) PRIMARY KEY, " +
"owner CHAR(36), " +
"locationWorldName CHAR(36), " +
"locationX DOUBLE, " +
"locationY DOUBLE, " +
"locationZ DOUBLE" +
");";
try (var conn = DriverManager.getConnection(url);
var stmt = conn.createStatement()) {
stmt.execute(sql);
ResultSet pixelBlocksResult = stmt.executeQuery("SELECT * FROM pixelblocks;");
while (pixelBlocksResult.next()) {
Location newPixelBlockLocation = new Location(Bukkit.getWorld(pixelBlocksResult.getString("locationWorldName")),
pixelBlocksResult.getDouble("locationX"),
pixelBlocksResult.getDouble("locationY"),
pixelBlocksResult.getDouble("locationZ"));
PixelBlock newPixelBlock = new PixelBlock(
newPixelBlockLocation,
UUID.fromString(pixelBlocksResult.getString("owner")),
16
);
newPixelBlock.remove();
newPixelBlock.place(newPixelBlockLocation);
}
} catch (SQLException e) {
System.err.println(e.getMessage());
}
}
}

View File

@ -3,19 +3,29 @@ package eu.mhsl.minecraft.pixelblocks.commands;
import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
public class CreatePixelBlockCommand implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if(sender instanceof Player p) {
Location playerLocation = p.getLocation();
PixelBlock pixelBlock = new PixelBlock(playerLocation, p.getUniqueId(), 16);
pixelBlock.place(playerLocation);
World playerWorld = p.getWorld();
World[] standardWorlds = {Bukkit.getWorlds().get(0), Bukkit.getWorlds().get(1)};
if(Arrays.stream(standardWorlds).toList().contains(playerWorld)) {
Location playerLocation = p.getLocation();
PixelBlock pixelBlock = new PixelBlock(playerLocation, p.getUniqueId(), 16);
pixelBlock.place(playerLocation);
} else {
p.sendMessage("Du kannst nur in der Overworld oder im Nether Pixelblocks erstellen!");
}
}
return(true);

View File

@ -21,7 +21,7 @@ public class ExitWorldCommand implements CommandExecutor {
if(!Arrays.stream(standardWorlds).toList().contains(playerWorld)) {
List<PixelBlock> ownedBlocks = PixelBlock.placedBlocks.stream()
.filter(pixelBlock -> pixelBlock.owner == p.getUniqueId())
.filter(pixelBlock -> pixelBlock.owner.equals(p.getUniqueId()))
.sorted(Comparator.comparing(pixelBlock -> pixelBlock.lastEntryTime))
.toList();

View File

@ -1,13 +1,29 @@
package eu.mhsl.minecraft.pixelblocks.listeners;
import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerChangedWorldEvent;
import java.util.Arrays;
import java.util.List;
public class PlayerChangeWorldListener implements Listener {
@EventHandler
static void onPlayerChangeWorld(PlayerChangedWorldEvent event) {
World worldLeft = event.getFrom();
World[] standardWorlds = {Bukkit.getWorlds().get(0), Bukkit.getWorlds().get(1), Bukkit.getWorlds().get(2)};
if(!Arrays.stream(standardWorlds).toList().contains(worldLeft)) {
String blockUUID = Arrays.stream(worldLeft.getName().split("/")).toList().getLast();
List<PixelBlock> pixelBlocks = PixelBlock.placedBlocks.stream().filter(block -> block.uuid.toString().equals(blockUUID)).toList();
if(!pixelBlocks.isEmpty()) {
PixelBlock pixelBlock = pixelBlocks.getFirst();
pixelBlock.update();
}
}
}
}

View File

@ -44,21 +44,21 @@ public class PlayerInteractListener implements Listener {
.orElseThrow();
UUID blockOwner = pixelBlock.owner;
if(playerUID == blockOwner) {
if(playerUID.equals(blockOwner)) {
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());
Bukkit.getScheduler().cancelTask(pixelBlock.updateTaskID);
pixelBlock.updateTaskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(
PixelBlocks.plugin,
pixelBlock::update,
100L,
100L
);
// Bukkit.getScheduler().cancelTask(pixelBlock.updateTaskID);
// pixelBlock.updateTaskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(
// PixelBlocks.plugin,
// pixelBlock::update,
// 100L,
// 100L
// );
} else {
event.getPlayer().sendMessage("Dieser Block gehört nicht dir!");
}
@ -79,7 +79,7 @@ public class PlayerInteractListener implements Listener {
.orElseThrow();
UUID blockOwner = pixelBlock.owner;
if(playerUID == blockOwner) {
if(playerUID.equals(blockOwner)) {
Bukkit.getScheduler().runTask(PixelBlocks.plugin, pixelBlock::remove);
} else {
event.getPlayer().sendMessage("Dieser Block gehört nicht dir!");

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