diff --git a/src/main/java/eu/mhsl/minecraft/pixelblocks/DataBase.java b/src/main/java/eu/mhsl/minecraft/pixelblocks/DataBase.java index b39f207..e9a4c5b 100644 --- a/src/main/java/eu/mhsl/minecraft/pixelblocks/DataBase.java +++ b/src/main/java/eu/mhsl/minecraft/pixelblocks/DataBase.java @@ -1,5 +1,6 @@ package eu.mhsl.minecraft.pixelblocks; +import eu.mhsl.minecraft.pixelblocks.pixelblock.Direction; import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -63,8 +64,8 @@ public class DataBase { PreparedStatement prep = conn.prepareStatement( "INSERT INTO pixelblocks(uuid, owner, " + "locationWorldName, locationX, locationY, locationZ, " + - "entryLocationWorldName, entryLocationX, entryLocationY, entryLocationZ) " + - "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?);" + "entryLocationWorldName, entryLocationX, entryLocationY, entryLocationZ, direction) " + + "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);" ); prep.setString(1, pixelBlock.uuid.toString()); prep.setString(2, pixelBlock.ownerUID.toString()); @@ -86,6 +87,8 @@ public class DataBase { prep.setDouble(10, Bukkit.getWorlds().getFirst().getSpawnLocation().getZ()); } + prep.setString(11, pixelBlock.direction.toString()); + prep.executeUpdate(); prep.close(); } catch (SQLException e) { @@ -97,7 +100,7 @@ public class DataBase { PreparedStatement prep = conn.prepareStatement( "UPDATE pixelblocks " + "SET owner=?, locationWorldName=?, locationX=?, locationY=?, locationZ=?," + - "entryLocationWorldName=?, entryLocationX=?, entryLocationY=?, entryLocationZ=? " + + "entryLocationWorldName=?, entryLocationX=?, entryLocationY=?, entryLocationZ=?, direction=? " + "WHERE uuid=?;" ); prep.setString(1, pixelBlock.ownerUID.toString()); @@ -120,6 +123,7 @@ public class DataBase { } prep.setString(10, pixelBlock.uuid.toString()); + prep.setString(11, pixelBlock.direction.toString()); prep.executeUpdate(); prep.close(); @@ -152,7 +156,8 @@ public class DataBase { "entryLocationWorldName CHAR(36), " + "entryLocationX DOUBLE, " + "entryLocationY DOUBLE, " + - "entryLocationZ DOUBLE" + + "entryLocationZ DOUBLE, " + + "direction CHAR(36)" + ");"; try (var statement = getStatement()) { @@ -193,7 +198,7 @@ public class DataBase { UUID.fromString(pixelBlocksResult.getString("uuid")) ); newPixelBlock.lastEntryLocation = newEntryLocation; - newPixelBlock.place(newPixelBlockLocation); + newPixelBlock.place(newPixelBlockLocation, Direction.valueOf(pixelBlocksResult.getString("direction"))); } } catch (SQLException e) { diff --git a/src/main/java/eu/mhsl/minecraft/pixelblocks/commands/CreatePixelBlockCommand.java b/src/main/java/eu/mhsl/minecraft/pixelblocks/commands/CreatePixelBlockCommand.java index 53d026f..2b27576 100644 --- a/src/main/java/eu/mhsl/minecraft/pixelblocks/commands/CreatePixelBlockCommand.java +++ b/src/main/java/eu/mhsl/minecraft/pixelblocks/commands/CreatePixelBlockCommand.java @@ -1,5 +1,6 @@ package eu.mhsl.minecraft.pixelblocks.commands; +import eu.mhsl.minecraft.pixelblocks.pixelblock.Direction; import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -23,7 +24,7 @@ public class CreatePixelBlockCommand implements CommandExecutor { if(Arrays.stream(standardWorlds).toList().contains(playerWorld)) { Location playerLocation = p.getLocation(); PixelBlock pixelBlock = new PixelBlock(playerLocation, p.getUniqueId(), UUID.randomUUID()); - pixelBlock.place(playerLocation); + pixelBlock.place(playerLocation, Direction.POSITIVE_Z); } else { p.sendMessage("Du kannst nur in der Overworld oder im Nether Pixelblocks erstellen!"); } diff --git a/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/BlockPlaceListener.java b/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/BlockPlaceListener.java index 4ce2ddc..6126f76 100644 --- a/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/BlockPlaceListener.java +++ b/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/BlockPlaceListener.java @@ -33,8 +33,9 @@ public class BlockPlaceListener implements Listener { } PixelBlock pixelBlock = new PixelBlock(newBlockLocation, event.getPlayer().getUniqueId(), UUID.fromString(event.getItemInHand().getItemMeta().getItemName())); - if(pixelBlock.place(newBlockLocation)) { + if(pixelBlock.place(newBlockLocation, PixelBlock.vectorToDirection(event.getPlayer().getLocation().getDirection()))) { event.getPlayer().getInventory().remove(event.getItemInHand()); + System.out.println(event.getPlayer().getLocation().getDirection()); } else { event.getPlayer().sendMessage("Hier wurde bereits ein Pixelblock plaziert."); } diff --git a/src/main/java/eu/mhsl/minecraft/pixelblocks/pixelblock/Direction.java b/src/main/java/eu/mhsl/minecraft/pixelblocks/pixelblock/Direction.java new file mode 100644 index 0000000..67b63b5 --- /dev/null +++ b/src/main/java/eu/mhsl/minecraft/pixelblocks/pixelblock/Direction.java @@ -0,0 +1,8 @@ +package eu.mhsl.minecraft.pixelblocks.pixelblock; + +public enum Direction { + POSITIVE_X, + POSITIVE_Z, + NEGATIVE_X, + NEGATIVE_Z +} diff --git a/src/main/java/eu/mhsl/minecraft/pixelblocks/pixelblock/PixelBlock.java b/src/main/java/eu/mhsl/minecraft/pixelblocks/pixelblock/PixelBlock.java index b27c6f5..f123f19 100644 --- a/src/main/java/eu/mhsl/minecraft/pixelblocks/pixelblock/PixelBlock.java +++ b/src/main/java/eu/mhsl/minecraft/pixelblocks/pixelblock/PixelBlock.java @@ -9,6 +9,7 @@ import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.util.Vector; import org.codehaus.plexus.util.FileUtils; import java.io.File; @@ -16,6 +17,7 @@ import java.io.IOException; import java.util.*; import static eu.mhsl.minecraft.pixelblocks.PixelBlocks.*; +import static java.lang.Math.abs; public class PixelBlock { public static List placedPixelBlocks = new ArrayList<>(); @@ -25,6 +27,7 @@ public class PixelBlock { public static boolean liveUpdate = true; public Location pixelBlockLocation; + public Direction direction; public ArrayList pixels = new ArrayList<>(); public Interaction hitbox; public ItemDisplay barrier; @@ -38,6 +41,7 @@ public class PixelBlock { this.uuid = blockUUID; this.pixelBlockLocation = originLocation.toBlockLocation(); + this.direction = Direction.POSITIVE_Z; this.pixelBlockLocation.setYaw(0); this.pixelBlockLocation.setPitch(0); this.ownerUID = ownerUID; @@ -76,6 +80,22 @@ public class PixelBlock { return plugin.getDataFolder().getPath() + pathSeparator + pixelBlock.uuid.toString(); } + public static Direction vectorToDirection(Vector vector) { + if(abs(vector.getX()) > abs(vector.getZ())) { + if(vector.getX() >= 0) { + return Direction.POSITIVE_X; + } else { + return Direction.NEGATIVE_X; + } + } else { + if(vector.getZ() >= 0) { + return Direction.POSITIVE_Z; + } else { + return Direction.NEGATIVE_Z; + } + } + } + void createPixelWorld() { File file = new File(getWorldPathFromPixelblock(this)); @@ -327,8 +347,13 @@ public class PixelBlock { Location blockLocation = Objects.requireNonNull(Bukkit .getWorld(getWorldPathFromPixelblock(this))) .getSpawnLocation() - .clone() - .add(relativeLocation.x(), relativeLocation.y(), relativeLocation.z()); + .clone(); + switch (this.direction) { + case POSITIVE_Z -> blockLocation.add(relativeLocation.x(), relativeLocation.y(), relativeLocation.z()); + case NEGATIVE_Z -> blockLocation.add((pixelsPerBlock-1)-relativeLocation.x(), relativeLocation.y(), (pixelsPerBlock-1)-relativeLocation.z()); + case POSITIVE_X -> blockLocation.add((pixelsPerBlock-1)-relativeLocation.z(), relativeLocation.y(), relativeLocation.x()); + case NEGATIVE_X -> blockLocation.add(relativeLocation.z(), relativeLocation.y(), (pixelsPerBlock-1)-relativeLocation.x()); + } BlockData block = blockLocation.getBlock().getBlockData(); if(block.getMaterial() != Material.AIR) { @@ -407,13 +432,14 @@ public class PixelBlock { } } - public boolean place(Location placeLocation) { + public boolean place(Location placeLocation, Direction direction) { Location newLocation = placeLocation.toBlockLocation(); newLocation.setPitch(0); newLocation.setYaw(0); if(PixelBlock.getPixelBlockFromLocation(newLocation) == null || PixelBlock.getPixelBlockFromLocation(newLocation) == this) { this.pixelBlockLocation = newLocation; + this.direction = direction; update(); dataBase.savePixelBlock(this); placedPixelBlocks.add(this);