power bullshit

This commit is contained in:
2026-04-11 21:48:45 +02:00
parent f5b5a30299
commit 71b6d3a606
8 changed files with 213 additions and 39 deletions

View File

@@ -6,9 +6,11 @@ import net.minecraft.core.registries.Registries
import net.minecraft.world.level.block.entity.BlockEntityType
import org.neoflock.neocomputers.NeoComputers
import org.neoflock.neocomputers.block.Blocks
import org.neoflock.neocomputers.block.CapacitorEntity
object BlockEntities {
val BLOCKENTITIES: DeferredRegister<BlockEntityType<*>> = DeferredRegister.create(NeoComputers.MODID, Registries.BLOCK_ENTITY_TYPE);
val SCREEN_ENTITY: RegistrySupplier<BlockEntityType<ScreenEntity>> = BLOCKENTITIES.register("screen_entity") { BlockEntityType(::ScreenEntity, mutableSetOf(Blocks.SCREEN_BLOCK.get()))}
val CAPACITOR_ENTITY: RegistrySupplier<BlockEntityType<CapacitorEntity>> = BLOCKENTITIES.register("capacitor_entity") { BlockEntityType(::CapacitorEntity, mutableSetOf(Blocks.CAPACITOR_BLOCK.get()))}
}

View File

@@ -0,0 +1,85 @@
package org.neoflock.neocomputers.entity;
import net.minecraft.core.BlockPos
import net.minecraft.world.level.block.entity.BlockEntity
import net.minecraft.world.level.block.entity.BlockEntityType
import net.minecraft.world.level.block.state.BlockState
import org.neoflock.neocomputers.network.Networking
open class NodeEntity(blockEntityType: BlockEntityType<*>, blockPos: BlockPos, blockState: BlockState) :
BlockEntity(blockEntityType, blockPos, blockState) {
// stuff
open fun getNode(): Networking.Node? = null
open fun getDirectConnections(): List<NodeEntity> {
if(level == null) return listOf();
val offs = listOf(
BlockPos(0, 1, 0),
BlockPos(0, -1, 0),
BlockPos(1, 0, 0),
BlockPos(-1, 0, 0),
BlockPos(0, 0, 1),
BlockPos(0, 0, -1),
)
val entities = mutableListOf<NodeEntity>()
offs.forEach {
val ent = level?.getBlockEntity(blockPos.offset(it.x, it.y, it.z))
if(ent is NodeEntity) {
entities.add(ent)
}
}
return entities
}
// may include itself
fun getReachableNodes(): Set<Networking.Node> {
val visited = mutableSetOf<NodeEntity>()
val working = mutableListOf<NodeEntity>(this)
val nodes = mutableSetOf<Networking.Node>()
while(working.isNotEmpty()) {
val cur = working.removeFirst()
if(cur in visited) continue
visited.add(cur)
val n = cur.getNode()
if(n != null) {
// rely on the defined direct connections of the node
nodes.add(n)
if(n != this.getNode()) continue
}
working.addAll(cur.getDirectConnections());
}
return nodes
}
fun syncReachable() {
val reachable = getReachableNodes().toList()
val node = getNode()
// nothing to sync
if(node == null) return
reachable.filter {
it !in node.connections
}.forEach {
node.connectTo(it)
}
node.connections.filter { it !in reachable }.forEach {
node.disconnectFrom(it)
}
}
override fun setChanged() {
super.setChanged()
syncReachable()
}
override fun setRemoved() {
super.setRemoved()
syncReachable()
val n = getNode()
if(n != null) Networking.removeNode(n);
}
}

View File

@@ -4,9 +4,22 @@ import net.minecraft.core.BlockPos
import net.minecraft.world.level.block.entity.BlockEntity
import net.minecraft.world.level.block.entity.BlockEntityType
import net.minecraft.world.level.block.state.BlockState
import org.neoflock.neocomputers.network.Networking
class ScreenEntity(blockPos: BlockPos, blockState: BlockState) :
BlockEntity(BlockEntities.SCREEN_ENTITY.get(), blockPos, blockState) {
NodeEntity(BlockEntities.SCREEN_ENTITY.get(), blockPos, blockState) {
var energyStored: Double = 0.0
val scrnod = object : Networking.Node() {
override fun getEnergy() = energyStored
override fun setEnergy(energy: Double) {
energyStored = energy
}
override fun maxEnergyCapacity(): Double = 10.0
override fun isConsumer() = true
}
// stuff
override fun getNode() = scrnod
}