added block rotation
This commit is contained in:
parent
d3c5a6c6b0
commit
b9cef8f567
@ -5,11 +5,13 @@ import eu.mhsl.minecraft.pixelblocks.utils.Direction;
|
||||
import eu.mhsl.minecraft.pixelblocks.utils.LocationUtil;
|
||||
import org.bukkit.*;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
import org.bukkit.block.data.Directional;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.generator.ChunkGenerator;
|
||||
import org.bukkit.util.Vector;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
@ -90,7 +92,7 @@ public class PixelBlockWorld {
|
||||
return getBorderOrigin().add(worldGrassBorderWidth + pixelsPerBlock, 0, worldGrassBorderWidth + pixelsPerBlock);
|
||||
}
|
||||
|
||||
public record PixelData(Vector relativeLocation, BlockData block, double scale) {
|
||||
public record PixelData(Vector relativeLocation, BlockData block, @Nullable Directional directional, double scale) {
|
||||
}
|
||||
|
||||
public List<PixelData> getPixels(Direction direction) {
|
||||
@ -112,10 +114,13 @@ public class PixelBlockWorld {
|
||||
case west ->
|
||||
blockLocation.add(relativeLocation.z(), relativeLocation.y(), (pixelsPerBlock - 1) - relativeLocation.x());
|
||||
}
|
||||
BlockData block = blockLocation.getBlock().getBlockData();
|
||||
BlockData blockData = blockLocation.getBlock().getBlockData();
|
||||
@Nullable Directional directional = blockData instanceof Directional face ? face : null;
|
||||
|
||||
if(!block.getMaterial().isEmpty()) {
|
||||
pixelData.add(new PixelData(relativeLocation.toVector(), block, (double) 1 / pixelsPerBlock));
|
||||
if(directional != null) System.out.println(directional);
|
||||
|
||||
if(!blockData.getMaterial().isEmpty()) {
|
||||
pixelData.add(new PixelData(relativeLocation.toVector(), blockData, directional, (double) 1 / pixelsPerBlock));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,12 +4,17 @@ import eu.mhsl.minecraft.pixelblocks.Main;
|
||||
import eu.mhsl.minecraft.pixelblocks.utils.ListUtil;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.block.BlockFace;
|
||||
import org.bukkit.block.data.Directional;
|
||||
import org.bukkit.entity.BlockDisplay;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
import org.bukkit.util.Transformation;
|
||||
import org.bukkit.util.Vector;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.joml.Vector3d;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
@ -34,12 +39,20 @@ public class Pixels {
|
||||
private void spawnPixel(PixelBlockWorld.PixelData pixelData) {
|
||||
Location pixelBlockLocation = this.parentBlock.getPixelBlockLocation();
|
||||
Location pixelLocation = pixelBlockLocation.add(pixelData.relativeLocation().multiply(pixelData.scale()));
|
||||
BlockDisplay entity = (BlockDisplay) pixelBlockLocation.getWorld().spawnEntity(pixelLocation, EntityType.BLOCK_DISPLAY);
|
||||
Vector3d centerOffset = new Vector3d(0.5, 0.5, 0.5).mul(pixelData.scale());
|
||||
BlockDisplay entity = (BlockDisplay) pixelBlockLocation.getWorld().spawnEntity(
|
||||
pixelLocation.add(Vector.fromJOML(centerOffset)),
|
||||
EntityType.BLOCK_DISPLAY
|
||||
);
|
||||
|
||||
entity.setBlock(pixelData.block());
|
||||
this.setEntityRotation(entity, pixelData.directional());
|
||||
|
||||
Transformation transform = entity.getTransformation();
|
||||
transform.getScale().set(pixelData.scale());
|
||||
transform.getTranslation().set(centerOffset.mul(-1));
|
||||
entity.setTransformation(transform);
|
||||
|
||||
entity.getPersistentDataContainer().set(pixelOfTag, PersistentDataType.STRING, this.parentBlock.getBlockUUID().toString());
|
||||
}
|
||||
|
||||
@ -59,4 +72,23 @@ public class Pixels {
|
||||
.sync(() -> pixels.forEach(Entity::remove))
|
||||
.execute());
|
||||
}
|
||||
|
||||
private void setEntityRotation(Entity entity, @Nullable Directional direction) {
|
||||
if(direction == null) return;
|
||||
BlockFace blockFace = direction.getFacing();
|
||||
float yaw = switch (blockFace) {
|
||||
case NORTH -> 0;
|
||||
case SOUTH -> 180;
|
||||
case WEST -> -90;
|
||||
case EAST -> 90;
|
||||
default -> entity.getLocation().getYaw();
|
||||
};
|
||||
|
||||
float pitch = switch (blockFace) {
|
||||
case UP -> -90;
|
||||
case DOWN -> 90;
|
||||
default -> 0;
|
||||
};
|
||||
entity.setRotation(yaw, pitch);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user