fixed race condition

This commit is contained in:
2024-12-18 23:34:45 +01:00
parent 8bb2d382b9
commit d3c5a6c6b0
2 changed files with 13 additions and 9 deletions

View File

@@ -22,7 +22,7 @@ import java.util.Objects;
import java.util.UUID;
public class PixelBlock {
private boolean exists = true;
private boolean isAccessible = false;
private PixelBlockWorld pixelWorld;
private final Location pixelBlockLocation;
@@ -83,6 +83,8 @@ public class PixelBlock {
this.placeholder = new PixelBlockPlaceholder(this);
this.hitbox = new PixelBlockHitbox(this);
this.getBlockTaskChain().sync(() -> this.isAccessible = true).execute();
Main.logger().info(String.format("Loaded existing pixelblock '%s'", this.blockUUID));
} catch(Exception e) {
Main.logger().info(String.format("Failed initializing existing pixelblock '%s': %s", this.blockUUID, e.getMessage()));
@@ -122,6 +124,8 @@ public class PixelBlock {
Main.pixelBlocks.add(this);
})
.execute();
this.getBlockTaskChain().sync(() -> this.isAccessible = true).execute();
}
public <T> TaskChain<T> getBlockTaskChain() {
@@ -134,13 +138,12 @@ public class PixelBlock {
return;
}
this.lastEntryLocation = player.getLocation();
getBlockTaskChain()
.async(() -> {
this.lastEntryLocation = player.getLocation();
Main.database().savePixelBlock(this);
})
.async(() -> Main.database().savePixelBlock(this))
.sync(() -> {
if(!exists) return;
if(!this.isAccessible) return;
player.teleport(this.pixelWorld.getSpawnLocation());
})
.current(() -> Main.logger()
@@ -193,7 +196,7 @@ public class PixelBlock {
// }
public void destroy(Player destroyedBy) {
if(!this.exists) return;
if(!this.isAccessible) return;
if(Main.configuration().onlyBreakableByOwner() && !destroyedBy.getUniqueId().equals(ownerUUID)) {
destroyedBy.sendMessage("Dieser Pixelblock gehört nicht dir!");
return;
@@ -205,7 +208,7 @@ public class PixelBlock {
});
Main.logger().info(String.format("Destroying PixelBlock '%s' at %s", this.blockUUID, pixelBlockLocation));
this.exists = false;
this.isAccessible = false;
this.pixelWorld.getEntitiesInWorld().stream()
.filter(entity -> entity instanceof Item)