From 00cfa1676b56ba402c663d4dc2970350c9260ef1 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 4 Feb 2022 17:39:43 +0100 Subject: [PATCH] Update Laufbau Signed-off-by: yoyosource --- .../features/script/ScriptListener.java | 2 +- .../features/slaves/laufbau/Laufbau.java | 250 +++++++++++++++--- 2 files changed, 213 insertions(+), 39 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptListener.java index 870e931c..f2c94291 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptListener.java @@ -59,6 +59,6 @@ public class ScriptListener implements Listener { } public static Context getGlobalContext(Player player) { - return GLOBAL_CONTEXT.get(player); + return GLOBAL_CONTEXT.computeIfAbsent(player, ignore -> new Context()); } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/Laufbau.java b/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/Laufbau.java index b6cb5bcf..20044aeb 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/Laufbau.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/Laufbau.java @@ -21,19 +21,71 @@ package de.steamwar.bausystem.features.slaves.laufbau; import de.steamwar.bausystem.features.tracer.TNTPosition; import de.steamwar.bausystem.features.tracer.show.StoredRecords; +import de.steamwar.bausystem.region.Point; import de.steamwar.bausystem.utils.FlatteningWrapper; import lombok.AllArgsConstructor; import lombok.Data; +import lombok.Getter; +import lombok.ToString; +import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.World; +import org.bukkit.block.data.Bisected; +import org.bukkit.block.data.BlockData; +import org.bukkit.block.data.type.Slab; +import org.bukkit.block.data.type.TrapDoor; import org.bukkit.util.Vector; -import java.util.HashSet; -import java.util.Set; +import java.util.*; +import java.util.logging.Level; public class Laufbau { + @AllArgsConstructor + @ToString + @Getter + private static class BlockDataWithBoundingBox { + private BlockData blockData; + private Cuboid cuboid; + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof BlockDataWithBoundingBox)) return false; + BlockDataWithBoundingBox that = (BlockDataWithBoundingBox) o; + return that.blockData.getClass() == this.blockData.getClass(); + } + + @Override + public int hashCode() { + return blockData.getClass().hashCode(); + } + } + + private static Set elements = new HashSet<>(); + + static { + elements.add(new BlockDataWithBoundingBox(Material.AIR.createBlockData(), new Cuboid(0, 0, 0, 0, 0, 0))); + elements.add(new BlockDataWithBoundingBox(Material.END_STONE.createBlockData(), new Cuboid(0, 0, 0, 1, 1, 1))); + + Slab bottomSlab = (Slab) Material.END_STONE_BRICK_SLAB.createBlockData(); + bottomSlab.setType(Slab.Type.BOTTOM); + elements.add(new BlockDataWithBoundingBox(bottomSlab, new Cuboid(0, 0, 0, 1, 0.5, 1))); + + Slab topSlab = (Slab) Material.END_STONE_BRICK_SLAB.createBlockData(); + topSlab.setType(Slab.Type.TOP); + elements.add(new BlockDataWithBoundingBox(topSlab, new Cuboid(0, 0.5, 0, 1, 0.5, 1))); + + TrapDoor bottomTrapDoor = (TrapDoor) Material.IRON_TRAPDOOR.createBlockData(); + bottomTrapDoor.setHalf(Bisected.Half.BOTTOM); + elements.add(new BlockDataWithBoundingBox(bottomTrapDoor, new Cuboid(0, 0, 0, 1, 0.1875, 1))); + + TrapDoor topTrapDoor = (TrapDoor) Material.IRON_TRAPDOOR.createBlockData(); + topTrapDoor.setHalf(Bisected.Half.TOP); + elements.add(new BlockDataWithBoundingBox(topTrapDoor, new Cuboid(0, 0.8125, 0, 1, 0.1875, 1))); + } + private Set tntPositions = new HashSet<>(); private Set cuboidList = new HashSet<>(); @@ -42,6 +94,41 @@ public class Laufbau { private Location pos2; public Laufbau(Location pos1, Location pos2, Material blockMaterial, Material slabMaterial) { + if (false) { + { + Cuboid c1 = new Cuboid(0, 0, 0, 1, 1, 1); + System.out.println(c1.intersects(c1)); + } + { + // FALSE! -> Wrong for now + Cuboid c1 = new Cuboid(0, 0, 0, 1, 1, 1); + Cuboid c2 = new Cuboid(1, 0, 0, 1, 1, 1); + System.out.println(c1.intersects(c2)); + } + { + // FALSE! + Cuboid c1 = new Cuboid(0, 0, 0, 1, 1, 1); + Cuboid c2 = new Cuboid(1.1, 0, 0, 1, 1, 1); + System.out.println(c1.intersects(c2)); + } + { + Cuboid c1 = new Cuboid(0, 0, 0, 3, 3, 3); + Cuboid c2 = new Cuboid(1, 1, 1, 1, 1, 1); + System.out.println(c1.intersects(c2)); + } + { + Cuboid c1 = new Cuboid(0, 0, 0, 3, 1, 3); + Cuboid c2 = new Cuboid(0, 0, 1, 1, 1, 3); + System.out.println(c1.intersects(c2)); + } + { + Cuboid c1 = new Cuboid(0, 0, 0, 3, 1, 3); + Cuboid c2 = new Cuboid(0, 0.5, 0, 1, 1, 1); + System.out.println(c1.intersects(c2)); + } + return; + } + this.world = pos1.getWorld(); this.pos1 = new Location(world, Math.min(pos1.getBlockX(), pos2.getBlockX()), Math.min(pos1.getBlockY(), pos2.getBlockY()), Math.min(pos1.getBlockZ(), pos2.getBlockZ())); this.pos2 = new Location(world, Math.max(pos1.getBlockX(), pos2.getBlockX()), Math.max(pos1.getBlockY(), pos2.getBlockY()), Math.max(pos1.getBlockZ(), pos2.getBlockZ())); @@ -80,40 +167,93 @@ public class Laufbau { } } } - System.out.println(cuboidList); - System.out.println(cuboidList.size()); + // System.out.println(cuboidList); + // System.out.println(cuboidList.size()); + Set blockMap = new HashSet<>(); cuboidList.forEach(cuboid -> { - System.out.println(cuboid); - }); - - /* - BlockData fullBlock = Material.STONE.createBlockData(); - Slab topBlock = (Slab) Material.STONE_SLAB.createBlockData(); - Slab bottomBlock = (Slab) Material.STONE_SLAB.createBlockData(); - topBlock.setType(Slab.Type.TOP); - bottomBlock.setType(Slab.Type.BOTTOM); - - for (int x = this.pos1.getBlockX(); x <= this.pos2.getBlockX(); x++) { - for (int y = this.pos1.getBlockY(); y <= this.pos2.getBlockY(); y++) { - for (int z = this.pos1.getBlockZ(); z <= this.pos2.getBlockZ(); z++) { - Point point = new Point(x, y, z); - Type type = laufMap.getOrDefault(point, Type.EMPTY); - if (type == Type.FULL) continue; - Block block = world.getBlockAt(x, y, z); - if (!block.getType().isAir()) continue; - if (type == Type.HALF_TOP) { - block.setBlockData(bottomBlock); - } else if (type == Type.HALF_BOTTOM) { - block.setBlockData(topBlock); - } else { - block.setBlockData(fullBlock); + for (double x = cuboid.getX() - 2; x < cuboid.getX() + cuboid.getDx() + 2; x++) { + for (double y = cuboid.getY() - 2; y < cuboid.getY() + cuboid.getDy() + 2; y++) { + for (double z = cuboid.getZ() - 2; z < cuboid.getZ() + cuboid.getDz() + 2; z++) { + Location location = new Location(world, x, y, z); + if (inRegion(new Vector(location.getBlockX(), location.getBlockY(), location.getBlockZ()), 0)) { + blockMap.add(new Point(location.getBlockX(), location.getBlockY(), location.getBlockZ())); + } } } } + }); + + Set blockMap2 = new HashSet<>(); + Set blockMap3 = new HashSet<>(); + for (Point point : blockMap) { + boolean isInCuboid = false; + Cuboid tempCuboid = new Cuboid(0, 0, 0, 1, 1, 1).add(point); + for (Cuboid cuboid : cuboidList) { + if (cuboid.intersects(tempCuboid)) { + isInCuboid = true; + break; + } + } + if (!isInCuboid) { + blockMap2.add(point); + } else { + blockMap3.add(point); + } } - */ - // System.out.println(laufMap); + + blockMap2.removeIf(point -> { + Point p1 = new Point(point.getX() - 1, point.getY(), point.getZ()); + Point p2 = new Point(point.getX() + 1, point.getY(), point.getZ()); + Point p3 = new Point(point.getX(), point.getY() - 1, point.getZ()); + Point p4 = new Point(point.getX(), point.getY() + 1, point.getZ()); + Point p5 = new Point(point.getX(), point.getY(), point.getZ() - 1); + Point p6 = new Point(point.getX(), point.getY(), point.getZ() + 1); + + return !blockMap3.contains(p1) && !blockMap3.contains(p2) && !blockMap3.contains(p3) && !blockMap3.contains(p4) && !blockMap3.contains(p5) && !blockMap3.contains(p6); + }); + + Map blockDataMap = new HashMap<>(); + blockMap3.forEach(point -> { + List blockDataWithBoundingBoxList = new ArrayList<>(); + for (BlockDataWithBoundingBox blockDataWithBoundingBox : elements) { + Cuboid tempCuboid = blockDataWithBoundingBox.cuboid.add(point); + // Bukkit.getLogger().log(Level.INFO, tempCuboid.toString()); + boolean isInCuboid = false; + for (Cuboid cuboid : cuboidList) { + if (cuboid.intersects(tempCuboid)) { + isInCuboid = true; + } + } + if (!isInCuboid) { + blockDataWithBoundingBoxList.add(blockDataWithBoundingBox); + } + } + if (blockDataWithBoundingBoxList.isEmpty()) { + return; + } + if (blockDataWithBoundingBoxList.size() == 1) { + blockDataMap.put(point, blockDataWithBoundingBoxList.get(0)); + return; + } + Bukkit.getLogger().log(Level.INFO, blockDataWithBoundingBoxList.size() + " " + blockDataWithBoundingBoxList); + // System.out.println(blockDataWithBoundingBoxList.size() + " " + blockDataWithBoundingBoxList); + }); + System.out.println(blockMap3.size()); + + display(blockMap2); + // System.out.println(blockMap2); + // System.out.println(blockMap2.size()); + } + + private void display(Set blockMap) { + BlockData fullBlock = Material.STONE.createBlockData(); + blockMap.forEach(point -> { + Location location = new Location(world, point.getX(), point.getY(), point.getZ()); + Bukkit.getOnlinePlayers().forEach(player -> { + player.sendBlockChange(location, fullBlock); + }); + }); } private boolean inRegion(Location location, int expansion) { @@ -147,15 +287,49 @@ public class Laufbau { private double dy; private double dz; - private boolean inCuboid(Vector vector) { - if (vector.getX() > x && vector.getX() < x + dx) { - if (vector.getY() > y && vector.getY() < y + dy) { - if (vector.getZ() > z && vector.getZ() < z + dz) { - return true; - } - } + private boolean intersects(Cuboid cuboid) { + Cuboid helpCuboid = helpIntersect(cuboid); + Cuboid c1 = helpIntersect(helpCuboid); + Cuboid c2 = cuboid.helpIntersect(c1); + + if (c1.dx == 0 && c2.dx == 0) { + return false; } - return false; + if (c1.dy == 0 && c2.dy == 0) { + return false; + } + if (c1.dz == 0 && c2.dz == 0) { + return false; + } + return c1.equals(c2); + } + + private Cuboid helpIntersect(Cuboid cuboid) { + double minX = Math.max(x, cuboid.x); + double minY = Math.max(y, cuboid.y); + double minZ = Math.max(z, cuboid.z); + double maxX = Math.min(x + dx, cuboid.x + cuboid.dx); + double maxY = Math.min(y + dy, cuboid.y + cuboid.dy); + double maxZ = Math.min(z + dz, cuboid.z + cuboid.dz); + + double nx = round(Math.min(minX, maxX)); + double ny = round(Math.min(minY, maxY)); + double nz = round(Math.min(minZ, maxZ)); + double dx = round(Math.max(minX, maxX) - nx); + double dy = round(Math.max(minY, maxY) - ny); + double dz = round(Math.max(minZ, maxZ) - nz); + return new Cuboid(nx, ny, nz, dx, dy, dz); + } + + private double round(double value) { + value *= 100000000000000L; + value = Math.round(value); + value /= 100000000000000L; + return value; + } + + public Cuboid add(Point point) { + return new Cuboid(x + point.getX(), y + point.getY(), z + point.getZ(), dx, dy, dz); } } }