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 idProperty = new NamespacedKey(Main.plugin(), "id");
public static NamespacedKey ownerProperty = new NamespacedKey(Main.plugin(), "owner"); 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 record BlockInfo(UUID id, @Nullable UUID owner) {
public boolean hasOwner() { public boolean hasOwner() {

View File

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