From 02c68a8b0b23685ef5da6d708e8128798ca500f2 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Thu, 31 Aug 2023 21:30:34 +0200 Subject: [PATCH] Push current state Signed-off-by: yoyosource --- .../preview/AxisMovementLimiter.java | 16 ++++++++--- .../features/simulator/preview/Explosion.java | 6 ++--- .../simulator/preview/Simulator19.java | 13 +++++---- .../features/simulator/preview/TNT.java | 12 ++++----- .../simulator/TNTSimulatorListener.java | 15 ++++++++++- .../simulator/preview/PreviewRecord.java | 27 ++++++++++++++++--- 6 files changed, 67 insertions(+), 22 deletions(-) diff --git a/BauSystem_19/src/de/steamwar/bausystem/features/simulator/preview/AxisMovementLimiter.java b/BauSystem_19/src/de/steamwar/bausystem/features/simulator/preview/AxisMovementLimiter.java index 49841c17..2e288071 100644 --- a/BauSystem_19/src/de/steamwar/bausystem/features/simulator/preview/AxisMovementLimiter.java +++ b/BauSystem_19/src/de/steamwar/bausystem/features/simulator/preview/AxisMovementLimiter.java @@ -28,6 +28,9 @@ import java.util.List; public class AxisMovementLimiter { + private double x; + private double y; + private double z; private Axis axis; private double movement; @@ -39,6 +42,9 @@ public class AxisMovementLimiter { private double maxZ; public AxisMovementLimiter(double x, double y, double z, Axis axis, double movement) { + this.x = x; + this.y = y; + this.z = z; this.axis = axis; this.movement = movement; @@ -75,6 +81,7 @@ public class AxisMovementLimiter { } break; } + System.out.println(axis + " " + minX + " -> " + maxX + " " + minY + " -> " + maxY + " " + minZ + " -> " + maxZ); } private List possibleCollisions() { @@ -98,6 +105,8 @@ public class AxisMovementLimiter { // TODO: This can be optimized by optimizing the x,y,z loop layout public double run() { + if (movement == 0.0) return 0.0; + BoundingBox movementBoundingBox = new BoundingBox(minX, minY, minZ, maxX, maxY, maxZ); List poss = possibleCollisions(); @@ -151,13 +160,14 @@ public class AxisMovementLimiter { if (collision == null) { return movement; } else { + System.out.println(axis + " " + movement + " " + collision); switch (axis) { case X: - return movement + (collision - minX); + return x - collision; case Y: - return movement + (collision - minY); + return y - collision; case Z: - return movement + (collision - minZ); + return z - collision; default: throw new IllegalStateException("Unexpected value: " + axis); } diff --git a/BauSystem_19/src/de/steamwar/bausystem/features/simulator/preview/Explosion.java b/BauSystem_19/src/de/steamwar/bausystem/features/simulator/preview/Explosion.java index 6caffe0e..282464b1 100644 --- a/BauSystem_19/src/de/steamwar/bausystem/features/simulator/preview/Explosion.java +++ b/BauSystem_19/src/de/steamwar/bausystem/features/simulator/preview/Explosion.java @@ -65,14 +65,12 @@ public class Explosion { private final double y; private final double z; - private final List entities; - public Explosion(TNT tnt, double x, double y, double z, List entities) { + public Explosion(TNT tnt, double x, double y, double z) { this.tnt = tnt; this.x = x; this.y = y; this.z = z; - this.entities = entities; } public void calculate() { @@ -120,7 +118,7 @@ public class Explosion { int t = floor(z - q - 1.0D); int u = floor(z + q + 1.0D); - for (TNT currentTNT : entities) { + for (TNT currentTNT : Simulator19.tntList) { if (currentTNT == tnt) continue; if (!(currentTNT.getX() >= k && currentTNT.getY() >= r && currentTNT.getZ() >= t && currentTNT.getX() <= l && currentTNT.getY() <= s && currentTNT.getZ() <= u)) { continue; diff --git a/BauSystem_19/src/de/steamwar/bausystem/features/simulator/preview/Simulator19.java b/BauSystem_19/src/de/steamwar/bausystem/features/simulator/preview/Simulator19.java index eefa7e10..e1d00dd2 100644 --- a/BauSystem_19/src/de/steamwar/bausystem/features/simulator/preview/Simulator19.java +++ b/BauSystem_19/src/de/steamwar/bausystem/features/simulator/preview/Simulator19.java @@ -26,6 +26,7 @@ import org.bukkit.Material; import org.bukkit.World; import org.bukkit.block.data.BlockData; import org.bukkit.util.BoundingBox; +import org.bukkit.util.Vector; import org.bukkit.util.VoxelShape; import java.util.*; @@ -51,6 +52,7 @@ public class Simulator19 implements Simulator { private static long accessed = 0; private static long cacheMisses = 0; private static long aired = 0; + static final List tntList = new ArrayList<>(); private static final Set AIR_BLOCKS = new HashSet<>(); private static final Map BLOCK_TYPES_MAP = new HashMap<>(); private static final Map BLOCK_DATA_MAP = new HashMap<>(); @@ -59,7 +61,6 @@ public class Simulator19 implements Simulator { @Override public synchronized PreviewRecord run(Pair>>>> toCalculate) { PreviewRecord previewRecord = new PreviewRecord(); - List tntList = new ArrayList<>(); int currentTick = 0; long time = System.currentTimeMillis(); @@ -73,7 +74,7 @@ public class Simulator19 implements Simulator { pairs.forEach(pair -> { if (pair.getValue() > 0) { hasSomeLeft.set(true); - TNT tnt = new TNT(pair.getKey().location.getX(), pair.getKey().location.getY(), pair.getKey().location.getZ()); + TNT tnt = new TNT(pair.getKey().location.getX() - 0.49, pair.getKey().location.getY(), pair.getKey().location.getZ() - 0.49); if (!pair.getKey().xVelocity) tnt.setVx(0.0); if (!pair.getKey().yVelocity) tnt.setVy(0.0); if (!pair.getKey().zVelocity) tnt.setVz(0.0); @@ -96,6 +97,8 @@ public class Simulator19 implements Simulator { } tntList.removeAll(remove); } + System.out.println(AIR_BLOCKS); + AIR_BLOCKS.forEach(pos -> previewRecord.addAir(new Vector(pos.x, pos.y, pos.z))); System.out.println("Time: " + (System.currentTimeMillis() - time) + "ms " + cacheMisses + "/" + accessed + "/" + aired); AIR_BLOCKS.clear(); @@ -142,10 +145,10 @@ public class Simulator19 implements Simulator { public static void clearBlock(Pos pos) { aired++; AIR_BLOCKS.add(pos); - BLOCK_DATA_MAP.put(pos, AIR_BLOCK_DATA); + BlockData blockData = BLOCK_DATA_MAP.put(pos, AIR_BLOCK_DATA); } - public static void clearBlocks(Set pos) { - + public static void clearBlocks(Set poss) { // TODO: Optimize + poss.forEach(Simulator19::clearBlock); } } diff --git a/BauSystem_19/src/de/steamwar/bausystem/features/simulator/preview/TNT.java b/BauSystem_19/src/de/steamwar/bausystem/features/simulator/preview/TNT.java index 0cecafe3..a01b4203 100644 --- a/BauSystem_19/src/de/steamwar/bausystem/features/simulator/preview/TNT.java +++ b/BauSystem_19/src/de/steamwar/bausystem/features/simulator/preview/TNT.java @@ -82,7 +82,7 @@ public class TNT { this.fuse--; if (this.fuse <= 0) { - Explosion explosion = new Explosion(this, x, y + 0.98 * 0.0625, z, new ArrayList<>()); + Explosion explosion = new Explosion(this, x, y + 0.98 * 0.0625, z); explosion.calculate(); return true; } @@ -165,16 +165,16 @@ public class TNT { return movement; } - double mY = new OptimizedAxisMovementLimiter(x, y, z, Axis.Y, movement.getY()).run(); + double mY = new AxisMovementLimiter(x, y, z, Axis.Y, movement.getY()).run(); boolean bl = Math.abs(movement.getX()) < Math.abs(movement.getZ()); if (bl) { - double mZ = new OptimizedAxisMovementLimiter(x, y + mY, z, Axis.Z, movement.getZ()).run(); - double mX = new OptimizedAxisMovementLimiter(x, y + mY, z + mZ, Axis.X, movement.getX()).run(); + double mZ = new AxisMovementLimiter(x, y + mY, z, Axis.Z, movement.getZ()).run(); + double mX = new AxisMovementLimiter(x, y + mY, z + mZ, Axis.X, movement.getX()).run(); return new Vector(mX, mY, mZ); } else { - double mX = new OptimizedAxisMovementLimiter(x, y + mY, z, Axis.X, movement.getX()).run(); - double mZ = new OptimizedAxisMovementLimiter(x + mX, y + mY, z, Axis.Z, movement.getZ()).run(); + double mX = new AxisMovementLimiter(x, y + mY, z, Axis.X, movement.getX()).run(); + double mZ = new AxisMovementLimiter(x + mX, y + mY, z, Axis.Z, movement.getZ()).run(); return new Vector(mX, mY, mZ); } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/TNTSimulatorListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/TNTSimulatorListener.java index 41d6e1ed..930acf7a 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/TNTSimulatorListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/TNTSimulatorListener.java @@ -36,6 +36,7 @@ import org.bukkit.event.player.*; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.function.Function; @@ -98,10 +99,21 @@ public class TNTSimulatorListener implements Listener { SimulatorStorage.getSimulatorNames().forEach(s -> { SimulatorStorage.getSimulator(s)._hide(event.getPlayer()); }); + PreviewRecord previewRecord = previewRecordMap.remove(event.getPlayer()); + if (previewRecord != null) { + previewRecord.close(event.getPlayer()); + } } + private Map previewRecordMap = new HashMap<>(); + @EventHandler public void onPlayerSwapHandItems(PlayerSwapHandItemsEvent event) { + PreviewRecord current = previewRecordMap.remove(event.getPlayer()); + if (current != null) { + current.close(event.getPlayer()); + } + if (!ItemUtils.isItem(event.getOffHandItem(), "simulator")) { return; } @@ -111,7 +123,7 @@ public class TNTSimulatorListener implements Listener { if (!permissionCheck(event.getPlayer())) { return; } - + TNTSimulator simulator = SimulatorStorage.getSimulator(event.getOffHandItem()); if (simulator == null) return; Pair>>>> toCalculate = simulator.locations(event.getPlayer()); @@ -121,6 +133,7 @@ public class TNTSimulatorListener implements Listener { return; } previewRecord.add(event.getPlayer()); + previewRecordMap.put(event.getPlayer(), previewRecord); } @EventHandler diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/preview/PreviewRecord.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/preview/PreviewRecord.java index b59db64e..caa621c3 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/preview/PreviewRecord.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/preview/PreviewRecord.java @@ -21,22 +21,29 @@ package de.steamwar.bausystem.features.simulator.preview; import de.steamwar.entity.REntityServer; import de.steamwar.entity.RFallingBlockEntity; +import org.bukkit.Location; import org.bukkit.Material; +import org.bukkit.block.data.BlockData; import org.bukkit.entity.Player; import org.bukkit.util.Vector; -import java.util.ArrayList; import java.util.HashSet; -import java.util.List; import java.util.Set; public class PreviewRecord { + private static final BlockData AIR_BLOCK_DATA = Material.AIR.createBlockData(); + private Set vectorList = new HashSet<>(); + private Set destroyedBlocks = new HashSet<>(); private REntityServer rEntityServer; public void add(double x, double y, double z) { - vectorList.add(new Vector(x, y, z)); + vectorList.add(new Vector(x + 0.49, y, z + 0.49)); + } + + public void addAir(Vector destroyed) { + destroyedBlocks.add(destroyed); } public void add(Player player) { @@ -49,5 +56,19 @@ public class PreviewRecord { }); } rEntityServer.addPlayer(player); + + destroyedBlocks.forEach(vector -> { + player.sendBlockChange(vector.toLocation(player.getWorld()), AIR_BLOCK_DATA); + }); + } + + public void close(Player player) { + rEntityServer.close(); + rEntityServer = null; + + destroyedBlocks.forEach(vector -> { + Location location = vector.toLocation(player.getWorld()); + player.sendBlockChange(location, location.getBlock().getBlockData()); + }); } }