From c6921fbf58cb2db4d5cdcff44a2a5b47ca9e8d79 Mon Sep 17 00:00:00 2001 From: Flemmli97 Date: Tue, 29 Jun 2021 17:28:48 +0200 Subject: [PATCH] fix #64 init of non existent claim homes caused by updating to 1.5+ loading chunks leading to OOM --- .../io/github/flemmli97/flan/claim/Claim.java | 7 ++++++- .../flan/player/PlayerClaimData.java | 20 ++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/common/src/main/java/io/github/flemmli97/flan/claim/Claim.java b/common/src/main/java/io/github/flemmli97/flan/claim/Claim.java index a1cb333..35ceb67 100644 --- a/common/src/main/java/io/github/flemmli97/flan/claim/Claim.java +++ b/common/src/main/java/io/github/flemmli97/flan/claim/Claim.java @@ -111,6 +111,11 @@ public class Claim implements IPermissionContainer { return new BlockPos(center.getX(), y + 1, center.getZ()); } + private BlockPos getDefaultCenterPos() { + BlockPos center = new BlockPos(this.minX + (this.maxX - this.minX) * 0.5, 0, this.minZ + (this.maxZ - this.minZ) * 0.5); + return new BlockPos(center.getX(), 255, center.getZ()); + } + public void setClaimID(UUID uuid) { this.claimID = uuid; this.setDirty(true); @@ -524,7 +529,7 @@ public class Claim implements IPermissionContainer { this.minY = pos.get(4).getAsInt(); JsonArray home = ConfigHandler.arryFromJson(obj, "Home"); if (home.size() != 3) - this.homePos = this.getInitCenterPos(); + this.homePos = this.getDefaultCenterPos(); else { this.homePos = new BlockPos(home.get(0).getAsInt(), home.get(1).getAsInt(), home.get(2).getAsInt()); } diff --git a/common/src/main/java/io/github/flemmli97/flan/player/PlayerClaimData.java b/common/src/main/java/io/github/flemmli97/flan/player/PlayerClaimData.java index 3ed50e9..dde1bf7 100644 --- a/common/src/main/java/io/github/flemmli97/flan/player/PlayerClaimData.java +++ b/common/src/main/java/io/github/flemmli97/flan/player/PlayerClaimData.java @@ -21,7 +21,11 @@ import net.minecraft.server.world.ServerWorld; import net.minecraft.util.Formatting; import net.minecraft.util.WorldSavePath; import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Box; import net.minecraft.util.math.Vec3d; +import net.minecraft.util.shape.VoxelShape; +import net.minecraft.world.Heightmap; +import net.minecraft.world.chunk.ChunkStatus; import java.io.BufferedReader; import java.io.File; @@ -271,7 +275,21 @@ public class PlayerClaimData implements IPlayerData { if (--this.trappedTick >= 0) { if (this.trappedTick == 0) { if (this.tpPos != null) { - this.player.teleport(this.tpPos.getX(), this.tpPos.getY(), this.tpPos.getZ()); + BlockPos.Mutable tpTo = this.tpPos.mutableCopy(); + Vec3d offset = new Vec3d(this.tpPos.getX() + 0.5, this.tpPos.getY() + 0.01, this.tpPos.getZ() + 0.5).subtract(this.player.getPos()); + int yHighest = this.player.world.getChunk(this.tpPos.getX() >> 4, this.tpPos.getZ() >> 4, ChunkStatus.HEIGHTMAPS).sampleHeightmap(Heightmap.Type.MOTION_BLOCKING, this.tpPos.getX() & 15, this.tpPos.getZ() & 15); + Box box = this.player.getBoundingBox().offset(offset); + if (tpTo.getY() < yHighest) { + while (tpTo.getY() < yHighest) { + if (this.player.world.getBlockCollisions(this.player, box, (state, pos) -> true).allMatch(VoxelShape::isEmpty)) + break; + tpTo.set(tpTo.getX(), tpTo.getY() + 1, tpTo.getZ()); + box = box.offset(0, 1, 0); + } + tpTo.set(tpTo.getX(), tpTo.getY() + 1, tpTo.getZ()); + } else + tpTo.set(tpTo.getX(), yHighest, tpTo.getZ()); + this.player.teleport(tpTo.getX() + 0.5, tpTo.getY(), tpTo.getZ() + 0.5); this.tpPos = null; } else { Vec3d tp = TeleportUtils.getTeleportPos(this.player, this.player.getPos(), ClaimStorage.get(this.player.getServerWorld()),