fixed race condition

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

View File

@ -26,7 +26,8 @@ public class PixelBlockItem {
public static NamespacedKey idProperty = new NamespacedKey(Main.plugin(), "id");
public static NamespacedKey ownerProperty = new NamespacedKey(Main.plugin(), "owner");
public static final String itemTexture = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvYzE5NGU5ZTc3NTdkMDZkNmY1ZTViZTg0NTQ4YTdjYjUyMTczZDY4Y2NmODAyZDIxMTI3NWQzMWNkYmEwYTA2ZSJ9fX0=";
public static final String itemTexture = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQ" +
"ubmV0L3RleHR1cmUvYzE5NGU5ZTc3NTdkMDZkNmY1ZTViZTg0NTQ4YTdjYjUyMTczZDY4Y2NmODAyZDIxMTI3NWQzMWNkYmEwYTA2ZSJ9fX0=";
public record BlockInfo(UUID id, @Nullable UUID owner) {
public boolean hasOwner() {

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)