Skip to content

Commit

Permalink
fix chunk corruption when unloading a level with a lit kerosene lamp
Browse files Browse the repository at this point in the history
  • Loading branch information
desht committed Nov 25, 2023
1 parent 43a4a36 commit bc2fb16
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
import me.desht.pneumaticcraft.common.core.ModItems;
import me.desht.pneumaticcraft.common.item.ICustomTooltipName;
import me.desht.pneumaticcraft.common.util.DirectionUtil;
import me.desht.pneumaticcraft.common.util.PneumaticCraftUtils;
import me.desht.pneumaticcraft.common.util.VoxelShapeUtils;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
Expand Down Expand Up @@ -87,6 +89,14 @@ public BlockState updateShape(BlockState stateIn, Direction facing, BlockState f
return stateIn.setValue(CONNECTED, getConnectedDirection(worldIn, currentPos));
}

@Override
public void onRemove(BlockState state, Level world, BlockPos pos, BlockState newState, boolean isMoving) {
PneumaticCraftUtils.getTileEntityAt(world, pos, KeroseneLampBlockEntity.class)
.ifPresent(KeroseneLampBlockEntity::removeLights);

super.onRemove(state, world, pos, newState, isMoving);
}

private Direction getConnectedDirection(LevelAccessor world, BlockPos pos) {
Direction connectedDir = Direction.DOWN;
for (Direction d : DirectionUtil.VALUES) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ public AbstractPneumaticCraftBlockEntity(BlockEntityType type, BlockPos pos, Blo
}

@Nonnull
public Level nonNullLevel()
{
public Level nonNullLevel() {
// use in methods where we know the level is not null, e.g. tickers, renderers...
return Objects.requireNonNull(super.getLevel());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public class KeroseneLampBlockEntity extends AbstractTickingBlockEntity implemen
@GuiSynced
private int fuel;
private int checkingX, checkingY, checkingZ;
private int rangeSq;

@DescSynced
@GuiSynced
Expand All @@ -129,7 +130,6 @@ public boolean isItemValid(int slot, ItemStack itemStack) {
};
private final LazyOptional<IItemHandler> inventoryCap = LazyOptional.of(() -> inventory);


public KeroseneLampBlockEntity(BlockPos pos, BlockState state) {
super(ModBlockEntities.KEROSENE_LAMP.get(), pos, state);
}
Expand Down Expand Up @@ -190,7 +190,7 @@ private void recalculateFuelQuality() {

private void useFuel() {
if (fuelQuality == 0) return; // tank is empty or a non-burnable liquid in the tank
fuel -= range * range * 3;// * range;
fuel -= rangeSq * 3;
while (fuel <= 0 && !tank.drain(1, IFluidHandler.FluidAction.EXECUTE).isEmpty()) {
fuel += fuelQuality;
}
Expand All @@ -205,11 +205,11 @@ public void clearRemoved() {
checkingZ = getBlockPos().getZ();
}

@Override
public void setRemoved() {
super.setRemoved();
public void removeLights() {
// called from KeroseneLampBlock#onRemove()
// note: this should *not* be done in setRemoved(), since that's also called on level unload!
for (BlockPos pos : managingLights) {
if (isLampLight(pos)) {
if (nonNullLevel().isLoaded(pos) && isLampLight(pos)) {
nonNullLevel().removeBlock(pos, false);
}
}
Expand Down Expand Up @@ -270,13 +270,14 @@ private void updateRange(int targetRange) {
if (nonNullLevel().isLoaded(pos)) {
if (!isLampLight(pos)) {
iterator.remove();
} else if (PneumaticCraftUtils.distBetween(pos, getBlockPos()) > range) {
} else if (PneumaticCraftUtils.distBetweenSq(pos, getBlockPos()) > rangeSq) {
nonNullLevel().removeBlock(pos, false);
iterator.remove();
}
}
}
}
rangeSq = range * range;
boolean wasOn = isOn;
isOn = range > 0;
if (isOn != wasOn) {
Expand All @@ -293,13 +294,15 @@ private boolean passesRaytraceTest(BlockPos pos, BlockPos lampPos) {
}

private void tryAddLight(BlockPos pos) {
if (nonNullLevel().isLoaded(pos) && !ConfigHelper.common().advanced.disableKeroseneLampFakeAirBlock.get() && PneumaticCraftUtils.distBetween(pos, getBlockPos()) <= range) {
if (nonNullLevel().isEmptyBlock(pos) && !isLampLight(pos)) {
if (passesRaytraceTest(pos, getBlockPos())) {
nonNullLevel().setBlockAndUpdate(pos, ModBlocks.KEROSENE_LAMP_LIGHT.get().defaultBlockState());
managingLights.add(pos);
}
}
if (!ConfigHelper.common().advanced.disableKeroseneLampFakeAirBlock.get()
&& nonNullLevel().isLoaded(pos)
&& PneumaticCraftUtils.distBetweenSq(pos, getBlockPos()) <= rangeSq
&& nonNullLevel().isEmptyBlock(pos)
&& !isLampLight(pos)
&& passesRaytraceTest(pos, getBlockPos()))
{
nonNullLevel().setBlockAndUpdate(pos, ModBlocks.KEROSENE_LAMP_LIGHT.get().defaultBlockState());
managingLights.add(pos);
}
}

Expand Down Expand Up @@ -335,6 +338,7 @@ public void load(CompoundTag tag) {
recalculateFuelQuality();
targetRange = tag.getByte("targetRange");
range = tag.getByte("range");
rangeSq = range * range;
inventory.deserializeNBT(tag.getCompound("Items"));
}

Expand Down

0 comments on commit bc2fb16

Please sign in to comment.