From ab9d1baa361cba1b0da5fcb6f055e31c3b3e0010 Mon Sep 17 00:00:00 2001 From: IonutParau Date: Tue, 14 Apr 2026 00:40:41 +0200 Subject: [PATCH] fixes and made solar power also nodes --- .../neoflock/neocomputers/block/Generators.kt | 48 +++---------------- .../neoflock/neocomputers/block/NodeBlock.kt | 7 +-- .../entity/CombustionGeneratorBlockEntity.kt | 5 +- .../entity/SolarGeneratorBlockEntity.kt | 36 ++++++++++---- 4 files changed, 40 insertions(+), 56 deletions(-) diff --git a/src/main/kotlin/org/neoflock/neocomputers/block/Generators.kt b/src/main/kotlin/org/neoflock/neocomputers/block/Generators.kt index 1be25c6..38eec00 100644 --- a/src/main/kotlin/org/neoflock/neocomputers/block/Generators.kt +++ b/src/main/kotlin/org/neoflock/neocomputers/block/Generators.kt @@ -1,6 +1,5 @@ package org.neoflock.neocomputers.block -import net.minecraft.client.resources.sounds.Sound import net.minecraft.core.BlockPos import net.minecraft.network.chat.ChatType import net.minecraft.network.chat.OutgoingChatMessage @@ -9,56 +8,21 @@ import net.minecraft.server.level.ServerPlayer import net.minecraft.world.InteractionResult import net.minecraft.world.entity.player.Player import net.minecraft.world.level.Level -import net.minecraft.world.level.block.Block import net.minecraft.world.level.block.EntityBlock -import net.minecraft.world.level.block.FurnaceBlock import net.minecraft.world.level.block.entity.BlockEntity -import net.minecraft.world.level.block.entity.BlockEntityTicker -import net.minecraft.world.level.block.entity.BlockEntityType -import net.minecraft.world.level.block.state.BlockBehaviour import net.minecraft.world.level.block.state.BlockState import net.minecraft.world.phys.BlockHitResult import org.neoflock.neocomputers.entity.BlockEntities import org.neoflock.neocomputers.entity.SolarGeneratorBlockEntity import org.neoflock.neocomputers.entity.CombustionGeneratorBlockEntity -class SolarGeneratorBlock : BaseBlock(), EntityBlock { - override fun newBlockEntity(blockPos: BlockPos, blockState: BlockState): BlockEntity { - return SolarGeneratorBlockEntity(blockPos, blockState) - } - - override fun getTicker( - level: Level, - blockState: BlockState, - blockEntityType: BlockEntityType - ): BlockEntityTicker { - return object : BlockEntityTicker { - override fun tick(level: Level, blockPos: BlockPos, blockState: BlockState, blockEntity: T) { - if(blockEntity !is SolarGeneratorBlockEntity) return; - blockEntity.giveSolarPower(); - } - } - } +class SolarGeneratorBlock : NodeBlock(), EntityBlock { + override fun newBlockEntity(blockPos: BlockPos, blockState: BlockState): BlockEntity = SolarGeneratorBlockEntity(blockPos, blockState).initNetworking() } // TODO: make it glow when burning -class CombustionGeneratorBlock : Block(BlockBehaviour.Properties.of()), EntityBlock { - override fun newBlockEntity(blockPos: BlockPos, blockState: BlockState): BlockEntity { - return CombustionGeneratorBlockEntity(blockPos, blockState) - } - - override fun getTicker( - level: Level, - blockState: BlockState, - blockEntityType: BlockEntityType - ): BlockEntityTicker { - return object : BlockEntityTicker { - override fun tick(level: Level, blockPos: BlockPos, blockState: BlockState, blockEntity: T) { - if(blockEntity !is CombustionGeneratorBlockEntity) return; - blockEntity.burnFuelForEnergy(); - } - } - } +class CombustionGeneratorBlock : NodeBlock(), EntityBlock { + override fun newBlockEntity(blockPos: BlockPos, blockState: BlockState): BlockEntity = CombustionGeneratorBlockEntity(blockPos, blockState).initNetworking() override fun useWithoutItem( blockState: BlockState, @@ -66,7 +30,7 @@ class CombustionGeneratorBlock : Block(BlockBehaviour.Properties.of()), EntityBl blockPos: BlockPos, player: Player, blockHitResult: BlockHitResult - ): InteractionResult? { + ): InteractionResult { if(!level.isClientSide()) { val sp = player as ServerPlayer val ent = level.getBlockEntity(blockPos, BlockEntities.COMBUSTGEN_ENTITY.get()) @@ -77,6 +41,6 @@ class CombustionGeneratorBlock : Block(BlockBehaviour.Properties.of()), EntityBl sp.sendChatMessage(OutgoingChatMessage.create(msg), false, ChatType.bind(ChatType.CHAT, player)) } } - return InteractionResult.SUCCESS; + return InteractionResult.SUCCESS } } diff --git a/src/main/kotlin/org/neoflock/neocomputers/block/NodeBlock.kt b/src/main/kotlin/org/neoflock/neocomputers/block/NodeBlock.kt index 8acbcec..4b6d8a0 100644 --- a/src/main/kotlin/org/neoflock/neocomputers/block/NodeBlock.kt +++ b/src/main/kotlin/org/neoflock/neocomputers/block/NodeBlock.kt @@ -16,8 +16,9 @@ import org.neoflock.neocomputers.network.Networking abstract class NodeBlockEntity(blockEntityType: BlockEntityType<*>, blockPos: BlockPos, blockState: BlockState) : BlockEntity(blockEntityType, blockPos, blockState) { abstract val node: Networking.Node - fun initNetworking() { + fun initNetworking(): NodeBlockEntity { Networking.addNode(node) + return this } private var stateIsDirty = true @@ -52,7 +53,7 @@ abstract class NodeBlockEntity(blockEntityType: BlockEntityType<*>, blockPos: Bl fun needsSynchronization() = stateIsDirty - fun ensureSynchronized() { + open fun tickNode() { if(!stateIsDirty) return stateIsDirty = false computeEdges().forEach { @@ -81,7 +82,7 @@ abstract class NodeBlock: BaseBlock(), EntityBlock { return object : BlockEntityTicker { override fun tick(level: Level, blockPos: BlockPos, blockState: BlockState, blockEntity: T) { if(blockEntity !is NodeBlockEntity) return; - blockEntity.ensureSynchronized() + blockEntity.tickNode() } } } diff --git a/src/main/kotlin/org/neoflock/neocomputers/entity/CombustionGeneratorBlockEntity.kt b/src/main/kotlin/org/neoflock/neocomputers/entity/CombustionGeneratorBlockEntity.kt index 1c80f59..2dbb334 100644 --- a/src/main/kotlin/org/neoflock/neocomputers/entity/CombustionGeneratorBlockEntity.kt +++ b/src/main/kotlin/org/neoflock/neocomputers/entity/CombustionGeneratorBlockEntity.kt @@ -16,7 +16,7 @@ class CombustionGeneratorBlockEntity(blockPos: BlockPos, blockState: BlockState) val energyPerTick: Long = 50 var energy: Long = 0 - val maxEnergy: Long = 50000 + val maxEnergy: Long = 100000 var burningTimeRemaining: Int = 0 override val node = object : Networking.Node() { @@ -48,7 +48,8 @@ class CombustionGeneratorBlockEntity(blockPos: BlockPos, blockState: BlockState) return !this.isRemoved } - fun burnFuelForEnergy() { + override fun tickNode() { + super.tickNode() // TODO: give us a block state tag for active // keep combusting and shi diff --git a/src/main/kotlin/org/neoflock/neocomputers/entity/SolarGeneratorBlockEntity.kt b/src/main/kotlin/org/neoflock/neocomputers/entity/SolarGeneratorBlockEntity.kt index c2408e6..3f1d9e9 100644 --- a/src/main/kotlin/org/neoflock/neocomputers/entity/SolarGeneratorBlockEntity.kt +++ b/src/main/kotlin/org/neoflock/neocomputers/entity/SolarGeneratorBlockEntity.kt @@ -1,20 +1,38 @@ 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.block.NodeBlockEntity +import org.neoflock.neocomputers.network.Networking +import org.neoflock.neocomputers.network.PowerRole +import kotlin.math.min -class SolarGeneratorBlockEntity(blockPos: BlockPos, blockState: BlockState) : BlockEntity(BlockEntities.SOLARGEN_ENTITY.get(), blockPos, blockState) { +class SolarGeneratorBlockEntity(blockPos: BlockPos, blockState: BlockState) : NodeBlockEntity(BlockEntities.SOLARGEN_ENTITY.get(), blockPos, blockState) { val energyPerTick: Long = 50 + var energyStored: Long = 0 + val capacity: Long = 50000 - fun giveSolarPower() { - if(level?.isDay == true) { - val below = level?.getBlockEntity(blockPos.below()) - if(below is NodeBlockEntity) { - below.node.giveEnergy(energyPerTick) - } + override val node = object : Networking.Node() { + override fun getPowerRole(): PowerRole = PowerRole.GENERATOR + override fun getEnergy(): Long = energyStored + override fun getEnergyCapacity(): Long = capacity + override fun giveEnergy(amount: Long): Long { + val taken = min(amount, capacity - energyStored) + energyStored += taken + return taken + } + override fun withdrawEnergy(amount: Long): Long { + val taken = min(amount, energyStored) + energyStored -= taken + return taken + } + } + + override fun tickNode() { + super.tickNode() + val l = level ?: return + if(l.isDay) { + node.giveEnergy(energyPerTick) } } } \ No newline at end of file