own database class, added methods to PixelBlock class

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

View File

@ -0,0 +1,199 @@
package eu.mhsl.minecraft.pixelblocks;
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 java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import static eu.mhsl.minecraft.pixelblocks.PixelBlocks.plugin;
public class DataBase {
public static String url;
public DataBase(String url) {
DataBase.url = url;
}
public Connection getConnection() throws SQLException {
return DriverManager.getConnection(url);
}
public Statement getStatement() throws SQLException {
return getConnection().createStatement();
}
public void removePixelBlock(PixelBlock pixelBlock) {
Bukkit.getScheduler().runTask(plugin, () -> {
try (var conn = getConnection()) {
PreparedStatement prep = conn.prepareStatement("DELETE FROM pixelblocks WHERE uuid = ?");
prep.setString(1, pixelBlock.uuid.toString());
prep.executeUpdate();
prep.close();
} catch (SQLException e) {
System.err.println(e.getMessage());
}
});
}
public void savePixelBlock(PixelBlock pixelBlock) {
Bukkit.getScheduler().runTask(plugin, () -> {
List<UUID> uuids = new ArrayList<>();
try (var statement = getStatement()) {
ResultSet pixelBlocksResult = statement.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(pixelBlock.uuid)) {
try (var conn = getConnection()) {
PreparedStatement prep = conn.prepareStatement(
"INSERT INTO pixelblocks(uuid, owner, " +
"locationWorldName, locationX, locationY, locationZ, " +
"entryLocationWorldName, entryLocationX, entryLocationY, entryLocationZ) " +
"VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"
);
prep.setString(1, pixelBlock.uuid.toString());
prep.setString(2, pixelBlock.owner.toString());
prep.setString(3, pixelBlock.pixelBlockLocation.getWorld().getName());
prep.setDouble(4, pixelBlock.pixelBlockLocation.getX());
prep.setDouble(5, pixelBlock.pixelBlockLocation.getY());
prep.setDouble(6, pixelBlock.pixelBlockLocation.getZ());
if(pixelBlock.lastEntryLocation != null) {
prep.setString(7, pixelBlock.lastEntryLocation.getWorld().getName());
prep.setDouble(8, pixelBlock.lastEntryLocation.getX());
prep.setDouble(9, pixelBlock.lastEntryLocation.getY());
prep.setDouble(10, pixelBlock.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();
prep.close();
} catch (SQLException e) {
System.err.println(e.getMessage());
}
} else {
try (var conn = getConnection()) {
PreparedStatement prep = conn.prepareStatement(
"UPDATE pixelblocks " +
"SET owner=?, locationWorldName=?, locationX=?, locationY=?, locationZ=?," +
"entryLocationWorldName=?, entryLocationX=?, entryLocationY=?, entryLocationZ=? " +
"WHERE uuid=?;"
);
prep.setString(1, pixelBlock.owner.toString());
prep.setString(2, pixelBlock.pixelBlockLocation.getWorld().getName());
prep.setDouble(3, pixelBlock.pixelBlockLocation.getX());
prep.setDouble(4, pixelBlock.pixelBlockLocation.getY());
prep.setDouble(5, pixelBlock.pixelBlockLocation.getZ());
if(pixelBlock.lastEntryLocation != null) {
prep.setString(6, pixelBlock.lastEntryLocation.getWorld().getName());
prep.setDouble(7, pixelBlock.lastEntryLocation.getX());
prep.setDouble(8, pixelBlock.lastEntryLocation.getY());
prep.setDouble(9, pixelBlock.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, pixelBlock.uuid.toString());
prep.executeUpdate();
prep.close();
} catch (SQLException e) {
System.err.println(e.getMessage());
}
}
});
}
public void loadPixelBlocks() {
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 sql = "CREATE TABLE IF NOT EXISTS pixelblocks (" +
"uuid CHAR(36) PRIMARY KEY, " +
"owner CHAR(36), " +
"locationWorldName CHAR(36), " +
"locationX DOUBLE, " +
"locationY DOUBLE, " +
"locationZ DOUBLE, " +
"entryLocationWorldName CHAR(36), " +
"entryLocationX DOUBLE, " +
"entryLocationY DOUBLE, " +
"entryLocationZ DOUBLE" +
");";
try (var statement = getStatement()) {
statement.execute(sql);
ResultSet pixelBlocksResult = statement.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")
);
Location newEntryLocation = new Location(
Bukkit.getWorld(pixelBlocksResult.getString("entryLocationWorldName")),
pixelBlocksResult.getDouble("entryLocationX"),
pixelBlocksResult.getDouble("entryLocationY"),
pixelBlocksResult.getDouble("entryLocationZ")
);
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")),
UUID.fromString(pixelBlocksResult.getString("uuid"))
);
newPixelBlock.lastEntryLocation = newEntryLocation;
newPixelBlock.place(newPixelBlockLocation);
}
} catch (SQLException e) {
System.err.println(e.getMessage());
}
}
}

View File

@ -1,34 +1,25 @@
package eu.mhsl.minecraft.pixelblocks;
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.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;
public final class PixelBlocks extends JavaPlugin {
public static PixelBlocks plugin;
public static DataBase dataBase;
public static String pathSeparator = "/";
@Override
public void onEnable() {
PixelBlocks.plugin = this;
this.getLogger().info("PixelBlocks Plugin was enabled.");
this.loadDB();
getServer().getPluginManager().registerEvents(new PlayerJumpListener(), this);
dataBase = new DataBase("jdbc:sqlite:pixelblocks.db");
dataBase.loadPixelBlocks();
getServer().getPluginManager().registerEvents(new PlayerInteractListener(), this);
getServer().getPluginManager().registerEvents(new PlayerMoveListener(), this);
getServer().getPluginManager().registerEvents(new EntityDamageListener(), this);
@ -37,72 +28,8 @@ public final class PixelBlocks extends JavaPlugin {
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() {
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, " +
"owner CHAR(36), " +
"locationWorldName CHAR(36), " +
"locationX DOUBLE, " +
"locationY DOUBLE, " +
"locationZ DOUBLE, " +
"entryLocationWorldName CHAR(36), " +
"entryLocationX DOUBLE, " +
"entryLocationY DOUBLE, " +
"entryLocationZ 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")
);
Location newEntryLocation = new Location(
Bukkit.getWorld(pixelBlocksResult.getString("entryLocationWorldName")),
pixelBlocksResult.getDouble("entryLocationX"),
pixelBlocksResult.getDouble("entryLocationY"),
pixelBlocksResult.getDouble("entryLocationZ")
);
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,
UUID.fromString(pixelBlocksResult.getString("uuid"))
);
newPixelBlock.lastEntryLocation = newEntryLocation;
newPixelBlock.place(newPixelBlockLocation);
}
} catch (SQLException e) {
System.err.println(e.getMessage());
}
}
}

View File

@ -1,56 +0,0 @@
package eu.mhsl.minecraft.pixelblocks.commands;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.BlockDisplay;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.util.Transformation;
import org.jetbrains.annotations.NotNull;
public class ChessCommand 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().toBlockLocation();
double scale = (double) 1/16;
for (int i = 0; i < 16; i++) {
int finalI = i+1;
for (int j = 0; j < 16; j++) {
int finalJ = j+1;
double blockPositionX = playerLocation.x()+i*scale;
double blockPositionY = playerLocation.y();
double blockPositionZ = playerLocation.z()+j*scale;
BlockDisplay bd;
bd = (BlockDisplay) p.getWorld().spawnEntity(
new Location(p.getWorld(), blockPositionX, blockPositionY, blockPositionZ),
EntityType.BLOCK_DISPLAY
);
if(finalI % 2 == 0 && finalJ % 2 == 0) {
bd.setBlock(Material.BLACK_CONCRETE.createBlockData());
} else if(finalI % 2 != 0 && finalJ % 2 != 0) {
bd.setBlock(Material.BLACK_CONCRETE.createBlockData());
} else {
bd.setBlock(Material.WHITE_CONCRETE.createBlockData());
}
Transformation transform = bd.getTransformation();
transform.getScale().set(scale);
bd.setTransformation(transform);
}
}
} else {
sender.sendMessage("This command is only for real Players!");
}
return(true);
}
}

View File

@ -22,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, UUID.randomUUID());
PixelBlock pixelBlock = new PixelBlock(playerLocation, p.getUniqueId(), UUID.randomUUID());
pixelBlock.place(playerLocation);
} else {
p.sendMessage("Du kannst nur in der Overworld oder im Nether Pixelblocks erstellen!");

View File

@ -20,6 +20,7 @@ public class ExitWorldCommand implements CommandExecutor {
World[] standardWorlds = {Bukkit.getWorlds().get(0), Bukkit.getWorlds().get(1), Bukkit.getWorlds().get(2)};
if(!Arrays.stream(standardWorlds).toList().contains(playerWorld)) {
List<PixelBlock> ownedBlocks = PixelBlock.placedBlocks.stream()
.filter(pixelBlock -> pixelBlock.owner.equals(p.getUniqueId()))
.sorted(Comparator.comparing(pixelBlock -> pixelBlock.lastEntryTime))
@ -32,6 +33,7 @@ public class ExitWorldCommand implements CommandExecutor {
newLocation = Bukkit.getWorlds().getFirst().getSpawnLocation();
}
p.teleport(newLocation);
} else {
p.sendMessage("Dieser Command ist nur für PixelBlock Welten verfügbar!");
}

View File

@ -1,7 +1,5 @@
package eu.mhsl.minecraft.pixelblocks.listeners;
import eu.mhsl.minecraft.pixelblocks.PixelBlocks;
import eu.mhsl.minecraft.pixelblocks.pixelblock.Pixel;
import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock;
import org.bukkit.Bukkit;
import org.bukkit.Location;
@ -11,7 +9,6 @@ import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import java.util.Arrays;
import java.util.List;
public class BlockBreakListener implements Listener {
@EventHandler
@ -20,33 +17,10 @@ public class BlockBreakListener implements Listener {
World[] standardWorlds = {Bukkit.getWorlds().get(0), Bukkit.getWorlds().get(1), Bukkit.getWorlds().get(2)};
if(!Arrays.stream(standardWorlds).toList().contains(blockLocation.getWorld())) {
Location spawnLocation = blockLocation.getWorld().getSpawnLocation();
PixelBlock pixelBlock = PixelBlock.getPixelBlockFromWorld(blockLocation.getWorld());
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, () -> {
String blockUUID = Arrays.stream(blockLocation.getWorld().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();
Location relativeLocation = blockLocation.subtract(spawnLocation.x(), spawnLocation.y()-1, spawnLocation.z());
relativeLocation.setWorld(pixelBlock.pixelBlockLocation.getWorld());
List<Pixel> pixels = pixelBlock.pixels.stream().filter(pixel -> pixel.relativeLocation.equals(relativeLocation)).toList();
if(!pixels.isEmpty()) {
Pixel pixel = pixels.getFirst();
pixel.remove();
pixelBlock.pixels.remove(pixel);
}
}
});
assert pixelBlock != null;
pixelBlock.handleBlockBreak(event, true);
}
}
}

View File

@ -1,7 +1,5 @@
package eu.mhsl.minecraft.pixelblocks.listeners;
import eu.mhsl.minecraft.pixelblocks.PixelBlocks;
import eu.mhsl.minecraft.pixelblocks.pixelblock.Pixel;
import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock;
import org.bukkit.Bukkit;
import org.bukkit.Location;
@ -11,7 +9,6 @@ import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
import java.util.Arrays;
import java.util.List;
public class BlockPlaceListener implements Listener {
@EventHandler
@ -20,34 +17,10 @@ public class BlockPlaceListener implements Listener {
World[] standardWorlds = {Bukkit.getWorlds().get(0), Bukkit.getWorlds().get(1), Bukkit.getWorlds().get(2)};
if(!Arrays.stream(standardWorlds).toList().contains(blockLocation.getWorld())) {
Location spawnLocation = blockLocation.getWorld().getSpawnLocation();
PixelBlock pixelBlock = PixelBlock.getPixelBlockFromWorld(blockLocation.getWorld());
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, () -> {
String blockUUID = Arrays.stream(blockLocation.getWorld().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();
Location relativeLocation = blockLocation.subtract(spawnLocation.x(), spawnLocation.y()-1, spawnLocation.z());
relativeLocation.setWorld(pixelBlock.pixelBlockLocation.getWorld());
Pixel newPixel = new Pixel(
relativeLocation,
event.getBlock().getBlockData(),
(1/pixelBlock.pixelsPerBlock)-0.0001,
0.00005*pixelBlock.pixelsPerBlock);
pixelBlock.pixels.add(newPixel);
newPixel.spawn(pixelBlock.pixelBlockLocation);
}
});
assert pixelBlock != null;
pixelBlock.handleBlockPlace(event, true);
}
}
}

View File

@ -8,7 +8,7 @@ import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerChangedWorldEvent;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
public class PlayerChangeWorldListener implements Listener {
@EventHandler
@ -17,13 +17,7 @@ public class PlayerChangeWorldListener implements Listener {
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();
}
Objects.requireNonNull(PixelBlock.getPixelBlockFromWorld(worldLeft)).update();
}
}
}

View File

@ -1,91 +1,39 @@
package eu.mhsl.minecraft.pixelblocks.listeners;
import eu.mhsl.minecraft.pixelblocks.PixelBlocks;
import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock;
import io.papermc.paper.event.player.PrePlayerAttackEntityEvent;
import org.bukkit.*;
import org.bukkit.entity.Interaction;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import java.util.*;
import static eu.mhsl.minecraft.pixelblocks.PixelBlocks.plugin;
public class PlayerInteractListener implements Listener {
// @EventHandler
// static void onPlayerInteract(PlayerInteractEvent event) {
// if(event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getClickedBlock() != null) {
// if(event.getClickedBlock().getType() == Material.GLASS) {
//// final WorldCreator worldCreator = new WorldCreator(plugin.getDataFolder().getPath()+"/"+event.getPlayer().getUniqueId());
//// worldCreator.type(WorldType.FLAT);
////
//// World newWorld = Bukkit.createWorld(worldCreator);
////
//// event.getPlayer().teleport(newWorld.getSpawnLocation());
// }
// }
// }
@EventHandler
static void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
if(event.getRightClicked() instanceof Interaction) {
Location interactionLocation = event.getRightClicked().getLocation().add(0, PixelBlock.hitboxOffset+0.5, 0).toBlockLocation();
Location interactionLocation = event.getRightClicked().getLocation().clone().add(0, PixelBlock.hitboxOffset, 0).toBlockLocation();
interactionLocation.setYaw(0);
interactionLocation.setPitch(0);
UUID playerUID = event.getPlayer().getUniqueId();
PixelBlock pixelBlock = PixelBlock.placedBlocks.stream()
.filter(block -> block.pixelBlockLocation.equals(interactionLocation))
.findFirst()
.orElseThrow();
UUID blockOwner = pixelBlock.owner;
PixelBlock pixelBlock = PixelBlock.getPixelBlockFromLocation(interactionLocation);
if(playerUID.equals(blockOwner)) {
pixelBlock.lastEntryLocation = event.getPlayer().getLocation();
pixelBlock.lastEntryTime = System.currentTimeMillis();
pixelBlock.saveToDB();
World blockWorld = Bukkit.getWorld(plugin.getDataFolder().getPath()+ "/" +pixelBlock.uuid);
assert blockWorld != null;
event.getPlayer().teleport(blockWorld.getSpawnLocation().clone().subtract(0.5, 0, 0.5));
// 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!");
}
assert pixelBlock != null;
pixelBlock.handleInteraction(event.getPlayer());
}
}
@EventHandler
static void onPlayerAttackEntity(PrePlayerAttackEntityEvent event) {
if(event.getAttacked() instanceof Interaction) {
Location blockLocation = event.getAttacked().getLocation().add(0, PixelBlock.hitboxOffset+0.5, 0).toBlockLocation();
Location blockLocation = event.getAttacked().getLocation().add(0, PixelBlock.hitboxOffset, 0).toBlockLocation();
blockLocation.setYaw(0);
blockLocation.setPitch(0);
UUID playerUID = event.getPlayer().getUniqueId();
PixelBlock pixelBlock = PixelBlock.placedBlocks.stream()
.filter(block -> block.pixelBlockLocation.equals(blockLocation))
.findFirst()
.orElseThrow();
UUID blockOwner = pixelBlock.owner;
if(playerUID.equals(blockOwner)) {
Bukkit.getScheduler().runTask(PixelBlocks.plugin, pixelBlock::remove);
} else {
event.getPlayer().sendMessage("Dieser Block gehört nicht dir!");
}
PixelBlock pixelBlock = PixelBlock.getPixelBlockFromLocation(blockLocation);
assert pixelBlock != null;
pixelBlock.handleAttack(event.getPlayer());
}
}
}

View File

@ -1,12 +0,0 @@
package eu.mhsl.minecraft.pixelblocks.listeners;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
public class PlayerJoinListener implements Listener {
@EventHandler
static void onPlayerJoin(PlayerJoinEvent event) {
}
}

View File

@ -1,15 +0,0 @@
package eu.mhsl.minecraft.pixelblocks.listeners;
import com.destroystokyo.paper.event.player.PlayerJumpEvent;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
public class PlayerJumpListener implements Listener {
@EventHandler
static void onPlayerJump(PlayerJumpEvent event) {
// event.getPlayer().getWorld().spawn(event.getPlayer().getLocation(), BlockDisplay.class, blockDisplay -> {
// blockDisplay.setBlock(Material.OAK_LOG.createBlockData());
// });
}
}

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