added rotation

This commit is contained in:
Lars Neuhaus 2024-07-20 16:38:13 +02:00
parent 315750475a
commit e48413d111
5 changed files with 51 additions and 10 deletions

View File

@ -1,5 +1,6 @@
package eu.mhsl.minecraft.pixelblocks; package eu.mhsl.minecraft.pixelblocks;
import eu.mhsl.minecraft.pixelblocks.pixelblock.Direction;
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;
@ -63,8 +64,8 @@ public class DataBase {
PreparedStatement prep = conn.prepareStatement( PreparedStatement prep = conn.prepareStatement(
"INSERT INTO pixelblocks(uuid, owner, " + "INSERT INTO pixelblocks(uuid, owner, " +
"locationWorldName, locationX, locationY, locationZ, " + "locationWorldName, locationX, locationY, locationZ, " +
"entryLocationWorldName, entryLocationX, entryLocationY, entryLocationZ) " + "entryLocationWorldName, entryLocationX, entryLocationY, entryLocationZ, direction) " +
"VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?);" "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"
); );
prep.setString(1, pixelBlock.uuid.toString()); prep.setString(1, pixelBlock.uuid.toString());
prep.setString(2, pixelBlock.ownerUID.toString()); prep.setString(2, pixelBlock.ownerUID.toString());
@ -86,6 +87,8 @@ public class DataBase {
prep.setDouble(10, Bukkit.getWorlds().getFirst().getSpawnLocation().getZ()); prep.setDouble(10, Bukkit.getWorlds().getFirst().getSpawnLocation().getZ());
} }
prep.setString(11, pixelBlock.direction.toString());
prep.executeUpdate(); prep.executeUpdate();
prep.close(); prep.close();
} catch (SQLException e) { } catch (SQLException e) {
@ -97,7 +100,7 @@ public class DataBase {
PreparedStatement prep = conn.prepareStatement( PreparedStatement prep = conn.prepareStatement(
"UPDATE pixelblocks " + "UPDATE pixelblocks " +
"SET owner=?, locationWorldName=?, locationX=?, locationY=?, locationZ=?," + "SET owner=?, locationWorldName=?, locationX=?, locationY=?, locationZ=?," +
"entryLocationWorldName=?, entryLocationX=?, entryLocationY=?, entryLocationZ=? " + "entryLocationWorldName=?, entryLocationX=?, entryLocationY=?, entryLocationZ=?, direction=? " +
"WHERE uuid=?;" "WHERE uuid=?;"
); );
prep.setString(1, pixelBlock.ownerUID.toString()); prep.setString(1, pixelBlock.ownerUID.toString());
@ -120,6 +123,7 @@ public class DataBase {
} }
prep.setString(10, pixelBlock.uuid.toString()); prep.setString(10, pixelBlock.uuid.toString());
prep.setString(11, pixelBlock.direction.toString());
prep.executeUpdate(); prep.executeUpdate();
prep.close(); prep.close();
@ -152,7 +156,8 @@ public class DataBase {
"entryLocationWorldName CHAR(36), " + "entryLocationWorldName CHAR(36), " +
"entryLocationX DOUBLE, " + "entryLocationX DOUBLE, " +
"entryLocationY DOUBLE, " + "entryLocationY DOUBLE, " +
"entryLocationZ DOUBLE" + "entryLocationZ DOUBLE, " +
"direction CHAR(36)" +
");"; ");";
try (var statement = getStatement()) { try (var statement = getStatement()) {
@ -193,7 +198,7 @@ public class DataBase {
UUID.fromString(pixelBlocksResult.getString("uuid")) UUID.fromString(pixelBlocksResult.getString("uuid"))
); );
newPixelBlock.lastEntryLocation = newEntryLocation; newPixelBlock.lastEntryLocation = newEntryLocation;
newPixelBlock.place(newPixelBlockLocation); newPixelBlock.place(newPixelBlockLocation, Direction.valueOf(pixelBlocksResult.getString("direction")));
} }
} catch (SQLException e) { } catch (SQLException e) {

View File

@ -1,5 +1,6 @@
package eu.mhsl.minecraft.pixelblocks.commands; package eu.mhsl.minecraft.pixelblocks.commands;
import eu.mhsl.minecraft.pixelblocks.pixelblock.Direction;
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;
@ -23,7 +24,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(), UUID.randomUUID()); PixelBlock pixelBlock = new PixelBlock(playerLocation, p.getUniqueId(), UUID.randomUUID());
pixelBlock.place(playerLocation); pixelBlock.place(playerLocation, Direction.POSITIVE_Z);
} 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!");
} }

View File

@ -33,8 +33,9 @@ public class BlockPlaceListener implements Listener {
} }
PixelBlock pixelBlock = new PixelBlock(newBlockLocation, event.getPlayer().getUniqueId(), UUID.fromString(event.getItemInHand().getItemMeta().getItemName())); 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()); event.getPlayer().getInventory().remove(event.getItemInHand());
System.out.println(event.getPlayer().getLocation().getDirection());
} else { } else {
event.getPlayer().sendMessage("Hier wurde bereits ein Pixelblock plaziert."); event.getPlayer().sendMessage("Hier wurde bereits ein Pixelblock plaziert.");
} }

View File

@ -0,0 +1,8 @@
package eu.mhsl.minecraft.pixelblocks.pixelblock;
public enum Direction {
POSITIVE_X,
POSITIVE_Z,
NEGATIVE_X,
NEGATIVE_Z
}

View File

@ -9,6 +9,7 @@ import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.util.Vector;
import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.FileUtils;
import java.io.File; import java.io.File;
@ -16,6 +17,7 @@ import java.io.IOException;
import java.util.*; import java.util.*;
import static eu.mhsl.minecraft.pixelblocks.PixelBlocks.*; import static eu.mhsl.minecraft.pixelblocks.PixelBlocks.*;
import static java.lang.Math.abs;
public class PixelBlock { public class PixelBlock {
public static List<PixelBlock> placedPixelBlocks = new ArrayList<>(); public static List<PixelBlock> placedPixelBlocks = new ArrayList<>();
@ -25,6 +27,7 @@ public class PixelBlock {
public static boolean liveUpdate = true; public static boolean liveUpdate = true;
public Location pixelBlockLocation; public Location pixelBlockLocation;
public Direction direction;
public ArrayList<Pixel> pixels = new ArrayList<>(); public ArrayList<Pixel> pixels = new ArrayList<>();
public Interaction hitbox; public Interaction hitbox;
public ItemDisplay barrier; public ItemDisplay barrier;
@ -38,6 +41,7 @@ public class PixelBlock {
this.uuid = blockUUID; this.uuid = blockUUID;
this.pixelBlockLocation = originLocation.toBlockLocation(); this.pixelBlockLocation = originLocation.toBlockLocation();
this.direction = Direction.POSITIVE_Z;
this.pixelBlockLocation.setYaw(0); this.pixelBlockLocation.setYaw(0);
this.pixelBlockLocation.setPitch(0); this.pixelBlockLocation.setPitch(0);
this.ownerUID = ownerUID; this.ownerUID = ownerUID;
@ -76,6 +80,22 @@ public class PixelBlock {
return plugin.getDataFolder().getPath() + pathSeparator + pixelBlock.uuid.toString(); 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() { void createPixelWorld() {
File file = new File(getWorldPathFromPixelblock(this)); File file = new File(getWorldPathFromPixelblock(this));
@ -327,8 +347,13 @@ public class PixelBlock {
Location blockLocation = Objects.requireNonNull(Bukkit Location blockLocation = Objects.requireNonNull(Bukkit
.getWorld(getWorldPathFromPixelblock(this))) .getWorld(getWorldPathFromPixelblock(this)))
.getSpawnLocation() .getSpawnLocation()
.clone() .clone();
.add(relativeLocation.x(), relativeLocation.y(), relativeLocation.z()); 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(); BlockData block = blockLocation.getBlock().getBlockData();
if(block.getMaterial() != Material.AIR) { 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(); Location newLocation = placeLocation.toBlockLocation();
newLocation.setPitch(0); newLocation.setPitch(0);
newLocation.setYaw(0); newLocation.setYaw(0);
if(PixelBlock.getPixelBlockFromLocation(newLocation) == null || PixelBlock.getPixelBlockFromLocation(newLocation) == this) { if(PixelBlock.getPixelBlockFromLocation(newLocation) == null || PixelBlock.getPixelBlockFromLocation(newLocation) == this) {
this.pixelBlockLocation = newLocation; this.pixelBlockLocation = newLocation;
this.direction = direction;
update(); update();
dataBase.savePixelBlock(this); dataBase.savePixelBlock(this);
placedPixelBlocks.add(this); placedPixelBlocks.add(this);