Skip to content

Commit

Permalink
Fix machine extender initial state and API forward
Browse files Browse the repository at this point in the history
  • Loading branch information
shartte committed Jan 29, 2024
1 parent 74d7aef commit 6acfb71
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
import dev.technici4n.moderndynamics.MdBlock;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.item.context.BlockPlaceContext;
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.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.material.MapColor;
import org.jetbrains.annotations.Nullable;

public class MachineExtenderBlock extends MdBlock {
public static final BooleanProperty TOP = BooleanProperty.create("top");
Expand All @@ -53,6 +55,18 @@ public BlockState updateShape(BlockState state, Direction direction, BlockState
return super.updateShape(state, direction, neighborState, level, currentPos, neighborPos);
}

@Nullable
@Override
public BlockState getStateForPlacement(BlockPlaceContext context) {
var state = super.getStateForPlacement(context);
if (state != null) {
var pos = context.getClickedPos();
var level = context.getLevel();
state = state.setValue(TOP, !level.getBlockState(pos.above()).is(this));
}
return state;
}

@Override
public void neighborChanged(BlockState state, Level level, BlockPos pos, Block block, BlockPos fromPos, boolean isMoving) {
if (fromPos.equals(pos.below())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,26 @@
import net.neoforged.neoforge.capabilities.BlockCapability;
import net.neoforged.neoforge.capabilities.BlockCapabilityCache;
import net.neoforged.neoforge.capabilities.RegisterCapabilitiesEvent;
import org.jetbrains.annotations.Nullable;

public class MachineExtenderBlockEntity extends MdBlockEntity {
private static int registeredApis = 0;

private static int getDirectionIndex(@Nullable Direction direction) {
return direction == null ? 0 : (1 + direction.ordinal());
}

private static int getCacheIndex(int apiId, @Nullable Direction direction) {
return apiId * 7 + getDirectionIndex(direction);
}

@SuppressWarnings("unchecked")
public static <A> void forwardApi(RegisterCapabilitiesEvent evt, BlockEntityType<MachineExtenderBlockEntity> bet,
BlockCapability<A, Direction> lookup) {
int apiId = registeredApis++;

evt.registerBlockEntity(lookup, bet, (sideExtender, direction) -> {
var cacheIndex = apiId * 6 + direction.ordinal();
var cacheIndex = getCacheIndex(apiId, direction);
if (sideExtender.inApiQuery[cacheIndex]) {
return null;
}
Expand All @@ -65,8 +74,8 @@ public static <A> void forwardApi(RegisterCapabilitiesEvent evt, BlockEntityType
});
}

private final boolean[] inApiQuery = new boolean[registeredApis * Direction.values().length];
private final BlockCapabilityCache[] apiCaches = new BlockCapabilityCache[registeredApis * Direction.values().length];
private final boolean[] inApiQuery = new boolean[registeredApis * (1 + Direction.values().length)];
private final BlockCapabilityCache[] apiCaches = new BlockCapabilityCache[registeredApis * (1 + Direction.values().length)];
boolean inNeighborUpdate = false;

public MachineExtenderBlockEntity(BlockEntityType<?> bet, BlockPos pos, BlockState state) {
Expand Down

0 comments on commit 6acfb71

Please sign in to comment.