Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Snapshot #74

Merged
merged 2 commits into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
63 changes: 1 addition & 62 deletions src/main/java/com/dre/brewery/Barrel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -69,7 +68,7 @@
public class Barrel extends BarrelBody implements InventoryHolder {

@Getter
public static volatile List<Barrel> barrels = new ArrayList<>();
public static final List<Barrel> 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
Expand Down Expand Up @@ -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() {
Expand Down
38 changes: 16 additions & 22 deletions src/main/java/com/dre/brewery/BarrelBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -41,7 +40,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) {
Expand All @@ -65,17 +64,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;
}
}

Expand All @@ -89,7 +88,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;
Expand Down Expand Up @@ -295,13 +294,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;
}

Expand Down Expand Up @@ -369,21 +369,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;
}

public void save(ConfigurationSection config, String prefix) {
if (signoffset != 0) {
config.set(prefix + ".sign", signoffset);
}
config.set(prefix + ".bounds", bounds.serialize());
}
}
17 changes: 16 additions & 1 deletion src/main/java/com/dre/brewery/utility/BoundingBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down
Loading