redstone I/O

This commit is contained in:
2026-04-18 19:43:35 +02:00
parent 7d4ee8593c
commit df86086d10
14 changed files with 176 additions and 7 deletions

View File

@@ -31,6 +31,7 @@ object Blocks {
val CAPACITOR_BLOCK3: RegistrySupplier<Block> = BaseBlock.register("capacitor3") { CapacitorBlock(3) }
val SOLARGEN_BLOCK: RegistrySupplier<Block> = BaseBlock.register("solargen") { SolarGeneratorBlock() }
val COMBUSTGEN_BLOCK: RegistrySupplier<Block> = BaseBlock.register("combustgen") { CombustionGeneratorBlock() }
val REDSTONEIO_BLOCK: RegistrySupplier<Block> = BaseBlock.register("redio") { RedstoneIOBlock() }
fun registerBlockItems() {
BLOCKS.forEach(Consumer { sup: RegistrySupplier<Block> ->

View File

@@ -190,20 +190,22 @@ abstract class NodeBlock(properties: Properties = Properties.of()): BaseBlock(pr
}
}
override fun setPlacedBy(
override fun onPlace(
blockState: BlockState,
level: Level,
blockPos: BlockPos,
blockState: BlockState,
livingEntity: LivingEntity?,
itemStack: ItemStack
blockState2: BlockState,
bl: Boolean
) {
super.onPlace(blockState, level, blockPos, blockState2, bl)
if(!level.isClientSide) {
val ent = level.getBlockEntity(blockPos)
if(ent is NodeBlockEntity) {
ent.invalidateNodeState()
ent.computeEdges().forEach { it.invalidateNodeState() }
}
level.updateNeighborsAt(blockPos, this)
}
super.setPlacedBy(level, blockPos, blockState, livingEntity, itemStack)
}
override fun neighborChanged(
@@ -214,6 +216,7 @@ abstract class NodeBlock(properties: Properties = Properties.of()): BaseBlock(pr
blockPos2: BlockPos,
bl: Boolean
) {
super.neighborChanged(blockState, level, blockPos, block, blockPos2, bl)
if(!level.isClientSide) {
val ent = level.getBlockEntity(blockPos)
if(ent is NodeBlockEntity) {
@@ -221,6 +224,5 @@ abstract class NodeBlock(properties: Properties = Properties.of()): BaseBlock(pr
}
}
super.neighborChanged(blockState, level, blockPos, block, blockPos2, bl)
}
}

View File

@@ -0,0 +1,138 @@
package org.neoflock.neocomputers.block
import net.minecraft.core.BlockPos
import net.minecraft.core.Direction
import net.minecraft.world.InteractionResult
import net.minecraft.world.entity.player.Player
import net.minecraft.world.level.BlockGetter
import net.minecraft.world.level.Level
import net.minecraft.world.level.block.Block
import net.minecraft.world.level.block.RedStoneWireBlock
import net.minecraft.world.level.block.RedstoneTorchBlock
import net.minecraft.world.level.block.entity.BlockEntity
import net.minecraft.world.level.block.state.BlockState
import net.minecraft.world.phys.BlockHitResult
import org.neoflock.neocomputers.NeoComputers
import org.neoflock.neocomputers.entity.BlockEntities
import org.neoflock.neocomputers.network.Networking
fun dirToIdx(direction: Direction) = Direction.entries.indexOf(direction)
class RedstoneIOEntity(blockPos: BlockPos, blockState: BlockState): NodeBlockEntity(BlockEntities.REDSTONEIO_ENTITY.get(), blockPos, blockState) {
val redstoneIn = Array<Int>(Direction.entries.size) {0}
val redstoneOut = Array<Int>(Direction.entries.size) {0}
// TODO: have redstone I/O node for component and shi
override val node = object : Networking.Node() {
}
fun refetch(dir: Direction) {
val src = blockPos.offset(dir.stepX, dir.stepY, dir.stepZ)
val cur = level?.getSignal(src, dir) ?: 0
val idx = dirToIdx(dir)
if(redstoneIn[idx] != cur) {
onRedstoneSignalChanged(dir, redstoneIn[idx], cur)
}
redstoneIn[idx] = cur
}
fun refetchAll() {
Direction.entries.forEach { refetch(it) }
}
fun onRedstoneSignalChanged(dir: Direction, oldValue: Int, newValue: Int) {
Networking.emitMessage(node, Networking.ComputerUncheckedSignal(node, "redstone_changed", arrayOf(node.address.toString(), dirToIdx(dir), oldValue, newValue)))
NeoComputers.LOGGER.info("redstone in direction ${dir.name} changed from $oldValue to $newValue")
}
}
class RedstoneIOBlock(): NodeBlock(Properties.of().isRedstoneConductor { state, getter, pos -> true }) {
override fun newBlockEntity(blockPos: BlockPos, blockState: BlockState): BlockEntity = RedstoneIOEntity(blockPos, blockState)
fun getRedstoneIO(level: BlockGetter, blockPos: BlockPos): RedstoneIOEntity? {
val ent = level.getBlockEntity(blockPos)
if(ent is RedstoneIOEntity) return ent
return null
}
override fun isSignalSource(blockState: BlockState): Boolean {
return true
}
override fun getSignal(
blockState: BlockState,
blockGetter: BlockGetter,
blockPos: BlockPos,
direction: Direction
): Int {
val redstoneIO = getRedstoneIO(blockGetter, blockPos)
if(redstoneIO != null) {
return redstoneIO.redstoneOut[dirToIdx(direction.opposite)]
}
return super.getSignal(blockState, blockGetter, blockPos, direction)
}
override fun onPlace(
blockState: BlockState,
level: Level,
blockPos: BlockPos,
blockState2: BlockState,
bl: Boolean
) {
if(!level.isClientSide) {
level.updateNeighborsAt(blockPos, this)
getRedstoneIO(level, blockPos)?.refetchAll()
}
super.onPlace(blockState, level, blockPos, blockState2, bl)
}
override fun onRemove(
blockState: BlockState,
level: Level,
blockPos: BlockPos,
blockState2: BlockState,
bl: Boolean
) {
if(!level.isClientSide) {
level.updateNeighborsAt(blockPos, this)
}
super.onRemove(blockState, level, blockPos, blockState2, bl)
}
override fun neighborChanged(
blockState: BlockState,
level: Level,
blockPos: BlockPos,
block: Block,
blockPos2: BlockPos,
bl: Boolean
) {
super.neighborChanged(blockState, level, blockPos, block, blockPos2, bl)
if(!level.isClientSide) {
val dir = Direction.getNearest(blockPos2.center.subtract(blockPos.center))
getRedstoneIO(level, blockPos)?.refetch(dir)
}
}
override fun useWithoutItem(
blockState: BlockState,
level: Level,
blockPos: BlockPos,
player: Player,
blockHitResult: BlockHitResult
): InteractionResult? {
if(!level.isClientSide) {
val redio = getRedstoneIO(level, blockPos)
val dir = blockHitResult.direction
if (redio != null) {
val idx = dirToIdx(dir)
redio.redstoneOut[idx]++
redio.redstoneOut[idx] %= 16
NeoComputers.LOGGER.info("outputting redstone level ${redio.redstoneOut[idx]} on ${dir.name}")
}
level.updateNeighborsAt(blockPos, this)
}
return super.useWithoutItem(blockState, level, blockPos, player, blockHitResult)
}
}

View File

@@ -70,6 +70,11 @@ object BlockEntities {
::CombustionGeneratorBlockEntity, mutableSetOf(Blocks.COMBUSTGEN_BLOCK.get()), BullshitFix()
)
}
val REDSTONEIO_ENTITY: RegistrySupplier<BlockEntityType<CombustionGeneratorBlockEntity>> = BLOCKENTITIES.register("redio") {
BlockEntityType(
::CombustionGeneratorBlockEntity, mutableSetOf(Blocks.REDSTONEIO_BLOCK.get()), BullshitFix()
)
}
fun registerPowerBlocks() {
PowerManager.registerPowerBlockEntity(CAPACITOR_ENTITY.get())

View File

@@ -0,0 +1,7 @@
{
"variants": {
"": {
"model": "neocomputers:block/redio"
}
}
}

View File

@@ -1,5 +1,6 @@
{
"neocomputers.confirm": "Confirm",
"neocomputers.cancel": "Cancel",
"block.neocomputers.combustgen": "Combustion Generator"
"block.neocomputers.combustgen": "Combustion Generator",
"block.neocomputers.redio": "Redstone I/O"
}

View File

@@ -0,0 +1,12 @@
{
"parent": "minecraft:block/cube",
"textures": {
"up": "neocomputers:block/redio_top",
"down": "neocomputers:block/redio_bottom",
"north": "neocomputers:block/redio_north",
"south": "neocomputers:block/redio_south",
"west": "neocomputers:block/redio_west",
"east": "neocomputers:block/redio_east",
"particle": "minecraft:block/redstone_block"
}
}

View File

@@ -0,0 +1,3 @@
{
"parent": "neocomputers:block/redio"
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 415 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 552 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 534 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 539 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 331 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 546 B