Make sure we can handle null-dimensionTypes (fixes #288)

This commit is contained in:
Frank 2021-10-18 20:14:49 +02:00
parent da8813ba06
commit effe958017

View file

@ -1,6 +1,10 @@
package ru.betterend.util; package ru.betterend.util;
import java.util.Set;
import java.util.stream.IntStream;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.BlockPos.MutableBlockPos; import net.minecraft.core.BlockPos.MutableBlockPos;
import net.minecraft.core.Direction; import net.minecraft.core.Direction;
@ -19,25 +23,22 @@ import ru.betterend.blocks.BlueVineBlock;
import ru.betterend.blocks.basis.FurBlock; import ru.betterend.blocks.basis.FurBlock;
import ru.betterend.registry.EndBlocks; import ru.betterend.registry.EndBlocks;
import java.util.Set;
import java.util.stream.IntStream;
public class BlockFixer { public class BlockFixer {
private static final BlockState AIR = Blocks.AIR.defaultBlockState(); private static final BlockState AIR = Blocks.AIR.defaultBlockState();
private static final BlockState WATER = Blocks.WATER.defaultBlockState(); private static final BlockState WATER = Blocks.WATER.defaultBlockState();
public static void fixBlocks(LevelAccessor world, BlockPos start, BlockPos end) { public static void fixBlocks(LevelAccessor level, BlockPos start, BlockPos end) {
Registry<DimensionType> registry = world.registryAccess().registryOrThrow(Registry.DIMENSION_TYPE_REGISTRY); final Registry<DimensionType> registry = level.registryAccess().registryOrThrow(Registry.DIMENSION_TYPE_REGISTRY);
ResourceLocation dimKey = registry.getKey(world.dimensionType()); final ResourceLocation dimKey = registry.getKey(level.dimensionType());
if (dimKey.getNamespace().equals("world_blender")) { if (dimKey!=null && "world_blender".equals(dimKey.getNamespace())) {
return; return;
} }
final Set<BlockPos> doubleCheck = Sets.newConcurrentHashSet(); final Set<BlockPos> doubleCheck = Sets.newConcurrentHashSet();
final int dx = end.getX() - start.getX() + 1; final int dx = end.getX() - start.getX() + 1;
final int dz = end.getZ() - start.getZ() + 1; final int dz = end.getZ() - start.getZ() + 1;
final int count = dx * dz; final int count = dx * dz;
final int minY = Math.max(start.getY(), world.getMinBuildHeight()); final int minY = Math.max(start.getY(), level.getMinBuildHeight());
final int maxY = Math.min(end.getY(), world.getMaxBuildHeight()); final int maxY = Math.min(end.getY(), level.getMaxBuildHeight());
IntStream.range(0, count).parallel().forEach(index -> { IntStream.range(0, count).parallel().forEach(index -> {
MutableBlockPos POS = new MutableBlockPos(); MutableBlockPos POS = new MutableBlockPos();
POS.setX((index % dx) + start.getX()); POS.setX((index % dx) + start.getX());
@ -45,75 +46,75 @@ public class BlockFixer {
BlockState state; BlockState state;
for (int y = minY; y <= maxY; y++) { for (int y = minY; y <= maxY; y++) {
POS.setY(y); POS.setY(y);
state = world.getBlockState(POS); state = level.getBlockState(POS);
if (state.getBlock() instanceof FurBlock) { if (state.getBlock() instanceof FurBlock) {
doubleCheck.add(POS.immutable()); doubleCheck.add(POS.immutable());
} }
// Liquids // Liquids
else if (!state.getFluidState().isEmpty()) { else if (!state.getFluidState().isEmpty()) {
if (!state.canSurvive(world, POS)) { if (!state.canSurvive(level, POS)) {
setWithoutUpdate(world, POS, WATER); setWithoutUpdate(level, POS, WATER);
POS.setY(POS.getY() - 1); POS.setY(POS.getY() - 1);
state = world.getBlockState(POS); state = level.getBlockState(POS);
while (!state.canSurvive(world, POS)) { while (!state.canSurvive(level, POS)) {
state = state.getFluidState().isEmpty() ? AIR : WATER; state = state.getFluidState().isEmpty() ? AIR : WATER;
setWithoutUpdate(world, POS, state); setWithoutUpdate(level, POS, state);
POS.setY(POS.getY() - 1); POS.setY(POS.getY() - 1);
state = world.getBlockState(POS); state = level.getBlockState(POS);
} }
} }
POS.setY(y - 1); POS.setY(y - 1);
if (world.isEmptyBlock(POS)) { if (level.isEmptyBlock(POS)) {
POS.setY(y); POS.setY(y);
while (!world.getFluidState(POS).isEmpty()) { while (!level.getFluidState(POS).isEmpty()) {
setWithoutUpdate(world, POS, AIR); setWithoutUpdate(level, POS, AIR);
POS.setY(POS.getY() + 1); POS.setY(POS.getY() + 1);
} }
continue; continue;
} }
for (Direction dir : BlocksHelper.HORIZONTAL) { for (Direction dir : BlocksHelper.HORIZONTAL) {
if (world.isEmptyBlock(POS.relative(dir))) { if (level.isEmptyBlock(POS.relative(dir))) {
world.getLiquidTicks().scheduleTick(POS, state.getFluidState().getType(), 0); level.getLiquidTicks().scheduleTick(POS, state.getFluidState().getType(), 0);
break; break;
} }
} }
} }
else if (state.is(EndBlocks.SMARAGDANT_CRYSTAL)) { else if (state.is(EndBlocks.SMARAGDANT_CRYSTAL)) {
POS.setY(POS.getY() - 1); POS.setY(POS.getY() - 1);
if (world.isEmptyBlock(POS)) { if (level.isEmptyBlock(POS)) {
POS.setY(POS.getY() + 1); POS.setY(POS.getY() + 1);
while (state.is(EndBlocks.SMARAGDANT_CRYSTAL)) { while (state.is(EndBlocks.SMARAGDANT_CRYSTAL)) {
setWithoutUpdate(world, POS, AIR); setWithoutUpdate(level, POS, AIR);
POS.setY(POS.getY() + 1); POS.setY(POS.getY() + 1);
state = world.getBlockState(POS); state = level.getBlockState(POS);
} }
} }
} }
else if (state.getBlock() instanceof StalactiteBlock) { else if (state.getBlock() instanceof StalactiteBlock) {
if (!state.canSurvive(world, POS)) { if (!state.canSurvive(level, POS)) {
if (world.getBlockState(POS.above()).getBlock() instanceof StalactiteBlock) { if (level.getBlockState(POS.above()).getBlock() instanceof StalactiteBlock) {
while (state.getBlock() instanceof StalactiteBlock) { while (state.getBlock() instanceof StalactiteBlock) {
setWithoutUpdate(world, POS, AIR); setWithoutUpdate(level, POS, AIR);
POS.setY(POS.getY() + 1); POS.setY(POS.getY() + 1);
state = world.getBlockState(POS); state = level.getBlockState(POS);
} }
} }
else { else {
while (state.getBlock() instanceof StalactiteBlock) { while (state.getBlock() instanceof StalactiteBlock) {
setWithoutUpdate(world, POS, AIR); setWithoutUpdate(level, POS, AIR);
POS.setY(POS.getY() - 1); POS.setY(POS.getY() - 1);
state = world.getBlockState(POS); state = level.getBlockState(POS);
} }
} }
} }
} }
else if (state.is(EndBlocks.CAVE_PUMPKIN)) { else if (state.is(EndBlocks.CAVE_PUMPKIN)) {
if (!world.getBlockState(POS.above()).is(EndBlocks.CAVE_PUMPKIN_SEED)) { if (!level.getBlockState(POS.above()).is(EndBlocks.CAVE_PUMPKIN_SEED)) {
setWithoutUpdate(world, POS, AIR); setWithoutUpdate(level, POS, AIR);
} }
} }
else if (!state.canSurvive(world, POS)) { else if (!state.canSurvive(level, POS)) {
// Chorus // Chorus
if (state.is(Blocks.CHORUS_PLANT)) { if (state.is(Blocks.CHORUS_PLANT)) {
Set<BlockPos> ends = Sets.newHashSet(); Set<BlockPos> ends = Sets.newHashSet();
@ -122,21 +123,21 @@ public class BlockFixer {
for (int i = 0; i < 64 && !ends.isEmpty(); i++) { for (int i = 0; i < 64 && !ends.isEmpty(); i++) {
ends.forEach((pos) -> { ends.forEach((pos) -> {
setWithoutUpdate(world, pos, AIR); setWithoutUpdate(level, pos, AIR);
for (Direction dir : BlocksHelper.HORIZONTAL) { for (Direction dir : BlocksHelper.HORIZONTAL) {
BlockPos p = pos.relative(dir); BlockPos p = pos.relative(dir);
BlockState st = world.getBlockState(p); BlockState st = level.getBlockState(p);
if ((st.is(Blocks.CHORUS_PLANT) || st.is(Blocks.CHORUS_FLOWER)) && !st.canSurvive( if ((st.is(Blocks.CHORUS_PLANT) || st.is(Blocks.CHORUS_FLOWER)) && !st.canSurvive(
world, level,
p p
)) { )) {
add.add(p); add.add(p);
} }
} }
BlockPos p = pos.above(); BlockPos p = pos.above();
BlockState st = world.getBlockState(p); BlockState st = level.getBlockState(p);
if ((st.is(Blocks.CHORUS_PLANT) || st.is(Blocks.CHORUS_FLOWER)) && !st.canSurvive( if ((st.is(Blocks.CHORUS_PLANT) || st.is(Blocks.CHORUS_FLOWER)) && !st.canSurvive(
world, level,
p p
)) { )) {
add.add(p); add.add(p);
@ -149,8 +150,8 @@ public class BlockFixer {
} }
// Vines // Vines
else if (state.getBlock() instanceof BaseVineBlock) { else if (state.getBlock() instanceof BaseVineBlock) {
while (world.getBlockState(POS).getBlock() instanceof BaseVineBlock) { while (level.getBlockState(POS).getBlock() instanceof BaseVineBlock) {
setWithoutUpdate(world, POS, AIR); setWithoutUpdate(level, POS, AIR);
POS.setY(POS.getY() - 1); POS.setY(POS.getY() - 1);
} }
} }
@ -159,30 +160,30 @@ public class BlockFixer {
BlockState falling = state; BlockState falling = state;
POS.setY(POS.getY() - 1); POS.setY(POS.getY() - 1);
state = world.getBlockState(POS); state = level.getBlockState(POS);
int ray = BlocksHelper.downRayRep(world, POS.immutable(), 64); int ray = BlocksHelper.downRayRep(level, POS.immutable(), 64);
if (ray > 32) { if (ray > 32) {
setWithoutUpdate(world, POS, Blocks.END_STONE.defaultBlockState()); setWithoutUpdate(level, POS, Blocks.END_STONE.defaultBlockState());
if (world.getRandom().nextBoolean()) { if (level.getRandom().nextBoolean()) {
POS.setY(POS.getY() - 1); POS.setY(POS.getY() - 1);
state = world.getBlockState(POS); state = level.getBlockState(POS);
setWithoutUpdate(world, POS, Blocks.END_STONE.defaultBlockState()); setWithoutUpdate(level, POS, Blocks.END_STONE.defaultBlockState());
} }
} }
else { else {
POS.setY(y); POS.setY(y);
BlockState replacement = AIR; BlockState replacement = AIR;
for (Direction dir : BlocksHelper.HORIZONTAL) { for (Direction dir : BlocksHelper.HORIZONTAL) {
state = world.getBlockState(POS.relative(dir)); state = level.getBlockState(POS.relative(dir));
if (!state.getFluidState().isEmpty()) { if (!state.getFluidState().isEmpty()) {
replacement = state; replacement = state;
break; break;
} }
} }
setWithoutUpdate(world, POS, replacement); setWithoutUpdate(level, POS, replacement);
POS.setY(y - ray); POS.setY(y - ray);
setWithoutUpdate(world, POS, falling); setWithoutUpdate(level, POS, falling);
} }
} }
// Blocks without support // Blocks without support
@ -191,20 +192,20 @@ public class BlockFixer {
if (state.getBlock() instanceof BlueVineBlock) { if (state.getBlock() instanceof BlueVineBlock) {
while (state.is(EndBlocks.BLUE_VINE) || state.is(EndBlocks.BLUE_VINE_LANTERN) || state.is( while (state.is(EndBlocks.BLUE_VINE) || state.is(EndBlocks.BLUE_VINE_LANTERN) || state.is(
EndBlocks.BLUE_VINE_FUR)) { EndBlocks.BLUE_VINE_FUR)) {
setWithoutUpdate(world, POS, AIR); setWithoutUpdate(level, POS, AIR);
POS.setY(POS.getY() + 1); POS.setY(POS.getY() + 1);
state = world.getBlockState(POS); state = level.getBlockState(POS);
} }
} }
// Double plants // Double plants
if (state.getBlock() instanceof BaseDoublePlantBlock) { if (state.getBlock() instanceof BaseDoublePlantBlock) {
setWithoutUpdate(world, POS, AIR); setWithoutUpdate(level, POS, AIR);
POS.setY(POS.getY() + 1); POS.setY(POS.getY() + 1);
setWithoutUpdate(world, POS, AIR); setWithoutUpdate(level, POS, AIR);
} }
// Other blocks // Other blocks
else { else {
setWithoutUpdate(world, POS, getAirOrFluid(state)); setWithoutUpdate(level, POS, getAirOrFluid(state));
} }
} }
} }
@ -212,8 +213,8 @@ public class BlockFixer {
}); });
doubleCheck.forEach((pos) -> { doubleCheck.forEach((pos) -> {
if (!world.getBlockState(pos).canSurvive(world, pos)) { if (!level.getBlockState(pos).canSurvive(level, pos)) {
setWithoutUpdate(world, pos, AIR); setWithoutUpdate(level, pos, AIR);
} }
}); });
} }