From d6a92f9505d5563a77b23322ba999d5f6d6d6d73 Mon Sep 17 00:00:00 2001 From: jonah Date: Wed, 25 Dec 2024 09:40:33 -0500 Subject: [PATCH 1/2] boundingboxes are forever --- build.gradle.kts | 2 +- src/main/java/com/dre/brewery/BarrelBody.java | 30 ++++++++++--------- .../com/dre/brewery/utility/BoundingBox.java | 17 ++++++++++- 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 9bc74d1d..944ddab5 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -35,7 +35,7 @@ val langVersion = 17 val encoding = "UTF-8" group = "com.dre.brewery" -version = "3.4.5" +version = "3.4.6-SNAPSHOT" repositories { mavenCentral() diff --git a/src/main/java/com/dre/brewery/BarrelBody.java b/src/main/java/com/dre/brewery/BarrelBody.java index 0224472a..92cec477 100644 --- a/src/main/java/com/dre/brewery/BarrelBody.java +++ b/src/main/java/com/dre/brewery/BarrelBody.java @@ -41,7 +41,7 @@ public abstract class BarrelBody { protected final Block spigot; - protected BoundingBox bounds; + protected final BoundingBox bounds; protected byte signoffset; public BarrelBody(Block spigot, byte signoffset) { @@ -65,17 +65,17 @@ public BarrelBody(Block spigot, byte signoffset) { public BarrelBody(Block spigot, byte signoffset, BoundingBox bounds) { this.spigot = spigot; this.signoffset = signoffset; - - if (boundsSeemBad(bounds)) { + this.bounds = bounds; + if (this.bounds == null || this.bounds.isBad()) { // If loading from old data, or block locations are missing, or other error, regenerate BoundingBox // This will only be done in those extreme cases. - if (!Bukkit.isPrimaryThread()) { - BreweryPlugin.getScheduler().runTask(spigot.getLocation(), this::regenerateBounds); - } else { + + // Barrels can be loaded async! + if (Bukkit.isPrimaryThread()) { this.regenerateBounds(); + } else { + BreweryPlugin.getScheduler().runTask(spigot.getLocation(), this::regenerateBounds); } - } else { - this.bounds = bounds; } } @@ -89,7 +89,7 @@ public void destroySign() { } /** - * Quick check if the bounds are valid or seem corrupt + * */ public static boolean boundsSeemBad(BoundingBox bounds) { if (bounds == null) return true; @@ -295,13 +295,14 @@ public Block checkSBarrel() { x = startX; y++; } - bounds = new BoundingBox( + bounds.resize( spigot.getX() + startX, spigot.getY(), spigot.getZ() + startZ, spigot.getX() + endX, spigot.getY() + 1, - spigot.getZ() + endZ); + spigot.getZ() + endZ + ); return null; } @@ -369,14 +370,15 @@ public Block checkLBarrel() { x = startX; y++; } - bounds = new BoundingBox( + + bounds.resize( spigot.getX() + startX, spigot.getY(), spigot.getZ() + startZ, spigot.getX() + endX, spigot.getY() + 2, - spigot.getZ() + endZ); - + spigot.getZ() + endZ + ); return null; } diff --git a/src/main/java/com/dre/brewery/utility/BoundingBox.java b/src/main/java/com/dre/brewery/utility/BoundingBox.java index 90a9ba92..1c3c39dd 100644 --- a/src/main/java/com/dre/brewery/utility/BoundingBox.java +++ b/src/main/java/com/dre/brewery/utility/BoundingBox.java @@ -27,7 +27,7 @@ public class BoundingBox { - private final int x1, y1, z1, x2, y2, z2; + private int x1, y1, z1, x2, y2, z2; public BoundingBox(int x1, int y1, int z1, int x2, int y2, int z2) { this.x1 = Math.min(x1, x2); @@ -54,6 +54,21 @@ public long area() { return ((long) (x2 - x1 + 1)) * ((long) (y2 - y1 + 1)) * ((long) (z2 - z1 + 1)); } + // Quick check if the bounds are valid or seem corrupt + public boolean isBad() { + long area = this.area(); + return area > 64 || area < 4; + } + + public void resize(int x1, int y1, int z1, int x2, int y2, int z2) { + this.x1 = Math.min(x1, x2); + this.y1 = Math.min(y1, y2); + this.z1 = Math.min(z1, z2); + this.x2 = Math.max(x2, x1); + this.y2 = Math.max(y2, y1); + this.z2 = Math.max(z2, z1); + } + public String serialize() { return x1 + "," + y1 + "," + z1 + "," + x2 + "," + y2 + "," + z2; } From 157e8822df843515c5a84a9210c61389e5e75934 Mon Sep 17 00:00:00 2001 From: jonah Date: Wed, 25 Dec 2024 11:02:27 -0500 Subject: [PATCH 2/2] not sure why this was volatile --- src/main/java/com/dre/brewery/Barrel.java | 63 +------------------ src/main/java/com/dre/brewery/BarrelBody.java | 8 --- 2 files changed, 1 insertion(+), 70 deletions(-) diff --git a/src/main/java/com/dre/brewery/Barrel.java b/src/main/java/com/dre/brewery/Barrel.java index c9c25aaa..3b3e6e5a 100644 --- a/src/main/java/com/dre/brewery/Barrel.java +++ b/src/main/java/com/dre/brewery/Barrel.java @@ -44,7 +44,6 @@ import org.bukkit.SoundCategory; import org.bukkit.World; import org.bukkit.block.Block; -import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.HumanEntity; import org.bukkit.entity.Player; import org.bukkit.event.player.PlayerInteractEvent; @@ -69,7 +68,7 @@ public class Barrel extends BarrelBody implements InventoryHolder { @Getter - public static volatile List barrels = new ArrayList<>(); + public static final List barrels = new ArrayList<>(); private static final Config config = ConfigManager.getConfig(Config.class); private static final Lang lang = ConfigManager.getConfig(Lang.class); private static int check = 0; // Which Barrel was last checked @@ -512,66 +511,6 @@ public static void unloadWorlds() { barrels.removeIf(barrel -> !worlds.contains(barrel.spigot.getWorld())); } - /** - * Saves all data - */ - public static void save(ConfigurationSection config, ConfigurationSection oldData) { - BUtil.createWorldSections(config); - - if (!barrels.isEmpty()) { - int id = 0; - for (Barrel barrel : barrels) { - - String worldName = barrel.spigot.getWorld().getName(); - String prefix; - - if (worldName.startsWith("DXL_")) { - prefix = BUtil.getDxlName(worldName) + "." + id; - } else { - prefix = barrel.spigot.getWorld().getUID() + "." + id; - } - - // block: x/y/z - config.set(prefix + ".spigot", barrel.spigot.getX() + "/" + barrel.spigot.getY() + "/" + barrel.spigot.getZ()); - - // save the body data into the section as well - barrel.save(config, prefix); - - if (barrel.inventory != null) { - int slot = 0; - ItemStack item; - ConfigurationSection invConfig = null; - while (slot < barrel.inventory.getSize()) { - item = barrel.inventory.getItem(slot); - if (item != null) { - if (invConfig == null) { - if (barrel.time != 0) { - config.set(prefix + ".time", barrel.time); - } - invConfig = config.createSection(prefix + ".inv"); - } - // ItemStacks are configurationSerializeable, makes them - // really easy to save - invConfig.set(slot + "", item); - } - - slot++; - } - } - - id++; - } - } - // also save barrels that are not loaded - if (oldData != null) { - for (String uuid : oldData.getKeys(false)) { - if (!config.contains(uuid)) { - config.set(uuid, oldData.get(uuid)); - } - } - } - } - public static class BarrelCheck extends UniversalRunnable { @Override public void run() { diff --git a/src/main/java/com/dre/brewery/BarrelBody.java b/src/main/java/com/dre/brewery/BarrelBody.java index 92cec477..f98038e6 100644 --- a/src/main/java/com/dre/brewery/BarrelBody.java +++ b/src/main/java/com/dre/brewery/BarrelBody.java @@ -29,7 +29,6 @@ import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.block.Block; -import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.Player; import org.jetbrains.annotations.Nullable; @@ -381,11 +380,4 @@ public Block checkLBarrel() { ); return null; } - - public void save(ConfigurationSection config, String prefix) { - if (signoffset != 0) { - config.set(prefix + ".sign", signoffset); - } - config.set(prefix + ".bounds", bounds.serialize()); - } }