fix VirtualLocation#getTopPos crash when querying empty column

This commit is contained in:
CreepyCre 2021-10-29 14:50:03 +02:00
parent 8c84d83a64
commit a21073f95d
4 changed files with 14 additions and 15 deletions

View file

@ -36,10 +36,10 @@ public class DimensionalEraserItem extends Item {
if (hit != null && hit.getType() == HitResult.Type.ENTITY) {
if(((EntityHitResult) hit).getEntity() instanceof ServerPlayerEntity) {
BlockPos teleportPos = ((EntityHitResult) hit).getEntity().getBlockPos();
while(ModDimensions.LIMBO_DIMENSION.getBlockState(VirtualLocation.getTopPos(ModDimensions.LIMBO_DIMENSION, teleportPos)).getBlock() == ModBlocks.ETERNAL_FLUID) {
while(ModDimensions.LIMBO_DIMENSION.getBlockState(VirtualLocation.getTopPos(ModDimensions.LIMBO_DIMENSION, teleportPos.getX(), teleportPos.getZ())).getBlock() == ModBlocks.ETERNAL_FLUID) {
teleportPos = teleportPos.add(1, 0, 1);
}
TeleportUtil.teleport(((EntityHitResult) hit).getEntity(), ModDimensions.LIMBO_DIMENSION, teleportPos.add(0, 255-((EntityHitResult) hit).getEntity().getBlockY(), 0), entityEulerAngle(((EntityHitResult) hit).getEntity()), ((EntityHitResult) hit).getEntity().getVelocity());
TeleportUtil.teleport(((EntityHitResult) hit).getEntity(), ModDimensions.LIMBO_DIMENSION, teleportPos.withY(255), entityEulerAngle(((EntityHitResult) hit).getEntity()), ((EntityHitResult) hit).getEntity().getVelocity());
}
((EntityHitResult) hit).getEntity().remove(Entity.RemovalReason.KILLED);

View file

@ -100,7 +100,7 @@ public class EscapeTarget extends VirtualTarget implements EntityTarget { // TOD
entity = TeleportUtil.teleport(entity, location.getWorld(), location.getBlockPos(), relativeAngle, relativeVelocity);
entity.fallDistance = 0;
Random random = new Random();
BlockPos.iterateOutwards(location.pos.add(0, -4, 0), 3, 2, 3).forEach((pos1 -> {
BlockPos.iterateOutwards(location.pos.add(0, -3, 0), 3, 2, 3).forEach((pos1 -> {
if (random.nextFloat() < (1 / ((float) location.pos.getSquaredDistance(pos1))) * DimensionalDoorsInitializer.getConfig().getLimboConfig().limboBlocksCorruptingOverworldAmount) {
Block block = location.getWorld().getBlockState(pos1).getBlock();
if (UnravelUtil.unravelBlocksMap.containsKey(block))

View file

@ -24,10 +24,10 @@ public class LimboTarget extends VirtualTarget implements EntityTarget {
@Override
public boolean receiveEntity(Entity entity, Vec3d relativePos, EulerAngle relativeAngle, Vec3d relativeVelocity) {
BlockPos teleportPos = entity.getBlockPos();
while(ModDimensions.LIMBO_DIMENSION.getBlockState(VirtualLocation.getTopPos(ModDimensions.LIMBO_DIMENSION, teleportPos)).getBlock() == ModBlocks.ETERNAL_FLUID) {
while(ModDimensions.LIMBO_DIMENSION.getBlockState(VirtualLocation.getTopPos(ModDimensions.LIMBO_DIMENSION, teleportPos.getX(), teleportPos.getZ())).getBlock() == ModBlocks.ETERNAL_FLUID) {
teleportPos = teleportPos.add(1, 0, 1);
}
TeleportUtil.teleport(entity, ModDimensions.LIMBO_DIMENSION, teleportPos.add(0, 255-entity.getBlockY(), 0), relativeAngle, relativeVelocity);
TeleportUtil.teleport(entity, ModDimensions.LIMBO_DIMENSION, teleportPos.withY(255), relativeAngle, relativeVelocity);
return true;
}

View file

@ -3,6 +3,9 @@ package org.dimdev.dimdoors.world.pocket;
import com.google.common.base.MoreObjects;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.util.math.ChunkSectionPos;
import net.minecraft.world.chunk.Chunk;
import org.dimdev.dimdoors.DimensionalDoorsInitializer;
import org.dimdev.dimdoors.world.level.registry.DimensionalRegistry;
import org.dimdev.dimdoors.api.util.Location;
@ -90,18 +93,14 @@ public class VirtualLocation {
int newX = (int) (this.x + spread * 2 * (Math.random() - 0.5));
int newZ = (int) (this.z + spread * 2 * (Math.random() - 0.5));
//BlockPos pos = world.getTopPosition(Heightmap.Type.WORLD_SURFACE, new BlockPos(newX, 1, newZ));
BlockPos pos = getTopPos(world, new BlockPos(newX, 255, newZ));
BlockPos pos = getTopPos(world, newX, newZ).up();
return new Location(world, pos);
}
public static BlockPos getTopPos(World world, BlockPos pos) {
while(world.getBlockState(pos).isAir()) {
pos = pos.down();
}
while(world.getBlockState(pos).isSolidBlock(world, pos)) {
pos = pos.up();
}
pos = pos.up(2);
return pos;
public static BlockPos getTopPos(World world, int x, int z) {
int topHeight = world.getChunk(ChunkSectionPos.getSectionCoord(x), ChunkSectionPos.getSectionCoord(z)) // guarantees WorldChunk
.sampleHeightmap(Heightmap.Type.MOTION_BLOCKING_NO_LEAVES, x, z);
return new BlockPos(x, topHeight, z);
}
public RegistryKey<World> getWorld() {