Skip to content

Commit

Permalink
Fix deadlock when getting light level of cable
Browse files Browse the repository at this point in the history
This fixes an incompatibility with the Moonrise mod.

Closes #1415
  • Loading branch information
rubensworks committed Nov 2, 2024
1 parent 5f1cd5e commit eebbc79
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -387,10 +387,10 @@ public boolean hasDynamicShape() {
@Override
public int getLightBlock(BlockState blockState, BlockGetter world, BlockPos pos) {
if (world instanceof Level level) {
if (CableHelpers.isLightTransparent(level, pos, null)) {
if (CableHelpers.isLightTransparent(level, pos, null, blockState)) {
return 0;
}
return CableHelpers.getFacade(level, pos)
return CableHelpers.getFacade(level, pos, blockState)
.map(facade -> facade.getLightBlock(world, pos))
.orElse(0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,18 @@ public static Optional<BlockState> getFacade(ILevelExtension world, BlockPos pos
.flatMap(facadeable -> Optional.ofNullable(facadeable.getFacade()));
}

/**
* Get the target's facade
* @param world The world.
* @param pos The position.
* @param blockState The block state.
* @return The optional facade.
*/
public static Optional<BlockState> getFacade(ILevelExtension world, BlockPos pos, BlockState blockState) {
return Optional.ofNullable(world.getCapability(Capabilities.Facadeable.BLOCK, pos, blockState, null, null))
.flatMap(facadeable -> Optional.ofNullable(facadeable.getFacade()));
}

public static boolean isLightTransparent(Level world, BlockPos pos, @Nullable Direction side) {
return PartHelpers.getPartContainer(world, pos, side)
.map(partContainer -> {
Expand All @@ -409,6 +421,20 @@ public static boolean isLightTransparent(Level world, BlockPos pos, @Nullable Di
.orElse(false);
}

public static boolean isLightTransparent(Level world, BlockPos pos, @Nullable Direction side, BlockState blockState) {
return PartHelpers.getPartContainer(world, pos, side, blockState)
.map(partContainer -> {
for (Map.Entry<Direction, IPartType<?, ?>> entry : partContainer.getParts().entrySet()) {
IPartType part = entry.getValue();
if (part.forceLightTransparency(partContainer.getPartState(entry.getKey()))) {
return true;
}
}
return false;
})
.orElse(false);
}

/**
* Get the sides the cable is currently connected to.
* @param cable A cable.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import net.neoforged.neoforge.common.extensions.ILevelExtension;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.tuple.Triple;
Expand Down Expand Up @@ -63,6 +64,18 @@ public static Optional<IPartContainer> getPartContainer(ILevelExtension world, B
return BlockEntityHelpers.getCapability(world, pos, side, Capabilities.PartContainer.BLOCK);
}

/**
* Get the part container capability at the given position.
* @param world The world.
* @param pos The position.
* @param side The side.
* @param blockState The block state.
* @return The optional part container capability.
*/
public static Optional<IPartContainer> getPartContainer(ILevelExtension world, BlockPos pos, @Nullable Direction side, BlockState blockState) {
return Optional.ofNullable(world.getCapability(Capabilities.PartContainer.BLOCK, pos, blockState, null, side));
}

/**
* Get the part container capability at the given position.
* @param dimPos The dimensional position.
Expand Down

0 comments on commit eebbc79

Please sign in to comment.