fixed pixel duplication because of unloaded chunks

This commit is contained in:
Elias Müller 2024-10-15 21:25:11 +02:00
parent 46782b97c8
commit 744f1dd6e2

View File

@ -6,6 +6,7 @@ import eu.mhsl.minecraft.pixelblocks.PixelBlockItem;
import eu.mhsl.minecraft.pixelblocks.utils.Direction; import eu.mhsl.minecraft.pixelblocks.utils.Direction;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Chunk;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Sound; import org.bukkit.Sound;
import org.bukkit.World; import org.bukkit.World;
@ -150,8 +151,9 @@ public class PixelBlock {
public void exitBlock(@NotNull Player player) { public void exitBlock(@NotNull Player player) {
this.getBlockTaskChain() this.getBlockTaskChain()
.sync(() -> player.teleport(this.lastEntryLocation != null ? this.lastEntryLocation : this.pixelBlockLocation)) .sync(() -> player.teleport(this.lastEntryLocation != null ? this.lastEntryLocation : this.pixelBlockLocation))
.current(() -> Main.logger().info(String.format("%s exited PixelBlock", player.getName())))
.sync(() -> this.pixelData = this.pixelWorld.getPixels(this.facingDirection)) .sync(() -> this.pixelData = this.pixelWorld.getPixels(this.facingDirection))
.current(() -> Main.logger().info(String.format("%s exited PixelBlock", player.getName())))
.delay(1)
.execute(); .execute();
this.scheduleEntityUpdate(); this.scheduleEntityUpdate();
@ -159,9 +161,12 @@ public class PixelBlock {
private void scheduleEntityUpdate() { private void scheduleEntityUpdate() {
this.getBlockTaskChain() this.getBlockTaskChain()
.sync(this::ensureChunksLoaded)
.sync(this::removeEntities) .sync(this::removeEntities)
.delay(1)
.async(() -> Collections.shuffle(this.pixelData)) .async(() -> Collections.shuffle(this.pixelData))
.delay(1) .delay(1)
.sync(this::ensureChunksLoaded)
.syncLast((pixelData) -> { .syncLast((pixelData) -> {
this.pixels.spawn(); this.pixels.spawn();
this.hitbox.spawn(); this.hitbox.spawn();
@ -221,11 +226,21 @@ public class PixelBlock {
} }
private void removeEntities() { private void removeEntities() {
this.ensureChunksLoaded();
this.pixels.destroy(); this.pixels.destroy();
this.placeholder.destroy(); this.placeholder.destroy();
this.hitbox.destroy(); this.hitbox.destroy();
} }
private void ensureChunksLoaded() {
Chunk chunk = this.pixelBlockLocation.getChunk();
if(!chunk.isLoaded() || !chunk.isEntitiesLoaded()) {
Main.logger().info(String.format("Loading chunk '%d, %d' for pixelblock '%s'", chunk.getX(), chunk.getZ(), this.blockUUID));
chunk.load();
chunk.getEntities();
}
}
public @NotNull PixelBlockWorld getPixelWorld() { public @NotNull PixelBlockWorld getPixelWorld() {
return pixelWorld; return pixelWorld;
} }