From 5f34ebf6dfa3a7a73d1a89f66fc56a97fe379b7f Mon Sep 17 00:00:00 2001 From: B1n_ry Date: Fri, 11 Oct 2024 13:54:08 +0200 Subject: [PATCH] Graves will no longer overwrite other graves when dying in exact same places outside the world --- CHANGELOG.md | 4 + .../yigd/components/GraveComponent.java | 74 +++++++++---------- .../yigd/events/YigdServerEventHandler.java | 4 + 3 files changed, 44 insertions(+), 38 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0fad4e2..4ae22fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # You're in Grave Danger 2.0.10 +### Fixes +* Graves will no longer overwrite other graves when dying in exact same places +outside the world + --- # You're in Grave Danger 2.0.9 diff --git a/src/main/java/com/b1n_ry/yigd/components/GraveComponent.java b/src/main/java/com/b1n_ry/yigd/components/GraveComponent.java index 0b820a4..7d25bee 100644 --- a/src/main/java/com/b1n_ry/yigd/components/GraveComponent.java +++ b/src/main/java/com/b1n_ry/yigd/components/GraveComponent.java @@ -201,11 +201,44 @@ public DirectionalPos findGravePos(Direction defaultDirection) { return new DirectionalPos(this.pos, defaultDirection); } + YigdConfig config = YigdConfig.getConfig(); + int y = this.pos.getY(); + int lowerAcceptableY = config.graveConfig.lowestGraveY + this.world.getBottomY(); + if (config.graveConfig.generateGraveInVoid && this.pos.getY() <= lowerAcceptableY) { + y = lowerAcceptableY; + } + int topY = this.world.getTopY() - 1; + if (y > topY) { + y = topY; + } + + int x = this.pos.getX(); + int z = this.pos.getZ(); + if (config.graveConfig.generateOnlyWithinBorder) { + WorldBorder border = this.world.getWorldBorder(); + if (!border.contains(x, z)) { + x = (int) Math.max(x, border.getBoundWest()); + x = (int) Math.min(x, border.getBoundEast()); + + z = (int) Math.max(z, border.getBoundNorth()); + z = (int) Math.min(z, border.getBoundSouth()); + } + } + + this.pos = new BlockPos(x, y, z); + + // Makes sure the grave is not broken/replaced by portal, or the dragon egg + if (this.world.getDimensionKey().equals(DimensionTypes.THE_END)) { + if (Math.abs(this.pos.getX()) + Math.abs(this.pos.getZ()) < 25 && this.world.getBlockState(this.pos.down()).isOf(Blocks.BEDROCK)) + this.pos = this.pos.up(); + } + + DeathInfoManager.INSTANCE.markDirty(); // The "this" object is (at least should be) located inside DeathInfoManager.INSTANCE + DirectionalPos graveyardPos = this.findPosInGraveyard(defaultDirection); if (graveyardPos != null) return graveyardPos; - YigdConfig config = YigdConfig.getConfig(); YigdConfig.GraveConfig.Range generationMaxDistance = config.graveConfig.generationMaxDistance; if (config.graveConfig.tryGenerateOnGround) { @@ -267,50 +300,15 @@ private DirectionalPos findPosInGraveyard(Direction defaultDirection) { /** * Called to place down a grave block. Should only be called from server - * @param attemptedPos Where the grave should try to be placed * @param state Which block should be placed * @return Weather or not the grave was placed */ - public boolean tryPlaceGraveAt(BlockPos attemptedPos, BlockState state) { + public boolean tryPlaceGrave(BlockState state) { if (this.world == null) { Yigd.LOGGER.error("GraveComponent tried to place grave without knowing the ServerWorld"); return false; } - YigdConfig.GraveConfig config = YigdConfig.getConfig().graveConfig; - int y = attemptedPos.getY(); - int lowerAcceptableY = config.lowestGraveY + this.world.getBottomY(); - if (config.generateGraveInVoid && attemptedPos.getY() <= lowerAcceptableY) { - y = lowerAcceptableY; - } - int topY = this.world.getTopY() - 1; - if (y > topY) { - y = topY; - } - - int x = attemptedPos.getX(); - int z = attemptedPos.getZ(); - if (config.generateOnlyWithinBorder) { - WorldBorder border = this.world.getWorldBorder(); - if (!border.contains(x, z)) { - x = (int) Math.max(x, border.getBoundWest()); - x = (int) Math.min(x, border.getBoundEast()); - - z = (int) Math.max(z, border.getBoundNorth()); - z = (int) Math.min(z, border.getBoundSouth()); - } - } - - this.pos = new BlockPos(x, y, z); - - // Makes sure the grave is not broken/replaced by portal, or the dragon egg - if (this.world.getDimensionKey().equals(DimensionTypes.THE_END)) { - if (Math.abs(attemptedPos.getX()) + Math.abs(attemptedPos.getZ()) < 25 && this.world.getBlockState(attemptedPos.down()).isOf(Blocks.BEDROCK)) - this.pos = this.pos.up(); - } - - DeathInfoManager.INSTANCE.markDirty(); // The "this" object is (at least should be) located inside DeathInfoManager.INSTANCE - this.placeBlockUnder(); return this.world.setBlockState(this.pos, state); } @@ -339,7 +337,7 @@ public void placeAndLoad(Direction direction, DeathContext context, BlockPos pos Yigd.END_OF_TICK.add(() -> { BlockState previousState = world.getBlockState(pos); - boolean placed = this.tryPlaceGraveAt(pos, graveBlock); + boolean placed = this.tryPlaceGrave(graveBlock); BlockPos placedPos = this.getPos(); if (!placed) { diff --git a/src/main/java/com/b1n_ry/yigd/events/YigdServerEventHandler.java b/src/main/java/com/b1n_ry/yigd/events/YigdServerEventHandler.java index dcc2e14..68435f3 100644 --- a/src/main/java/com/b1n_ry/yigd/events/YigdServerEventHandler.java +++ b/src/main/java/com/b1n_ry/yigd/events/YigdServerEventHandler.java @@ -204,6 +204,10 @@ public static void registerEventCallbacks() { (grave, currentUnder) -> YigdConfig.getConfig().graveConfig.blockUnderGrave.enabled && currentUnder.isIn(YigdTags.REPLACE_SOFT_WHITELIST)); GraveGenerationEvent.EVENT.register((world, pos, nthTry) -> { + if (world.isOutOfHeightLimit(pos) || ! world.getWorldBorder().contains(pos)) { + return false; + } + BlockState state = world.getBlockState(pos); YigdConfig.GraveConfig config = YigdConfig.getConfig().graveConfig; if (world.getBlockEntity(pos) != null) // Block entities should NOT be replaced by graves