Add a redstone prototype for making blocks that use redstone
This commit is contained in:
parent
840fa0a157
commit
a747846585
2 changed files with 61 additions and 1 deletions
|
@ -53,7 +53,7 @@ mod_name=Zontreck's Library Mod
|
|||
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
|
||||
mod_license=GPLv3
|
||||
# The mod version. See https://semver.org/
|
||||
mod_version=1201.13.042524.0527
|
||||
mod_version=1201.13.042624.0634
|
||||
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
|
||||
# This should match the base package used for the mod sources.
|
||||
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html
|
||||
|
|
|
@ -0,0 +1,60 @@
|
|||
package dev.zontreck.libzontreck.blocks;
|
||||
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.core.Direction;
|
||||
import net.minecraft.world.item.context.BlockPlaceContext;
|
||||
import net.minecraft.world.level.BlockGetter;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.LevelReader;
|
||||
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 org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public abstract class RedstoneBlock extends RotatableBlock
|
||||
{
|
||||
public static final BooleanProperty INPUT_POWER = BooleanProperty.create("inputpower");
|
||||
private List<Direction> sides;
|
||||
|
||||
protected RedstoneBlock(Properties pProperties, Direction... validSides) {
|
||||
super(pProperties);
|
||||
sides=List.of(validSides);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlockState getStateForPlacement(BlockPlaceContext pContext) {
|
||||
return super.getStateForPlacement(pContext).setValue(INPUT_POWER, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> pBuilder) {
|
||||
super.createBlockStateDefinition(pBuilder);
|
||||
pBuilder.add(INPUT_POWER);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canConnectRedstone(BlockState state, BlockGetter level, BlockPos pos, @Nullable Direction direction) {
|
||||
if(sides.contains(direction)) return true;
|
||||
else return false;
|
||||
}
|
||||
|
||||
private boolean redstoneIsActivated(LevelReader level, BlockPos pos)
|
||||
{
|
||||
if(level.hasNeighborSignal(pos))
|
||||
return true;
|
||||
else return false;
|
||||
}
|
||||
|
||||
protected abstract void onRedstone(LevelReader level, BlockPos pos, boolean on);
|
||||
|
||||
@Override
|
||||
public void onNeighborChange(BlockState state, LevelReader level, BlockPos pos, BlockPos neighbor) {
|
||||
|
||||
onRedstone(level, pos, redstoneIsActivated(level, pos));
|
||||
}
|
||||
|
||||
|
||||
}
|
Reference in a new issue