fixes and made solar power also nodes
This commit is contained in:
@@ -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 <T : BlockEntity> getTicker(
|
||||
level: Level,
|
||||
blockState: BlockState,
|
||||
blockEntityType: BlockEntityType<T>
|
||||
): BlockEntityTicker<T> {
|
||||
return object : BlockEntityTicker<T> {
|
||||
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 <T : BlockEntity> getTicker(
|
||||
level: Level,
|
||||
blockState: BlockState,
|
||||
blockEntityType: BlockEntityType<T>
|
||||
): BlockEntityTicker<T> {
|
||||
return object : BlockEntityTicker<T> {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<T> {
|
||||
override fun tick(level: Level, blockPos: BlockPos, blockState: BlockState, blockEntity: T) {
|
||||
if(blockEntity !is NodeBlockEntity) return;
|
||||
blockEntity.ensureSynchronized()
|
||||
blockEntity.tickNode()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user