networking bullshit
This commit is contained in:
@@ -4,40 +4,35 @@ import net.minecraft.core.BlockPos
|
||||
import net.minecraft.network.chat.ChatType
|
||||
import net.minecraft.network.chat.OutgoingChatMessage
|
||||
import net.minecraft.network.chat.PlayerChatMessage
|
||||
import net.minecraft.server.level.ServerLevel
|
||||
import net.minecraft.server.level.ServerPlayer
|
||||
import net.minecraft.util.RandomSource
|
||||
import net.minecraft.world.InteractionHand
|
||||
import net.minecraft.world.InteractionResult
|
||||
import net.minecraft.world.entity.player.Player
|
||||
import net.minecraft.world.item.ItemStack
|
||||
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.entity.BlockEntity
|
||||
import net.minecraft.world.level.block.entity.BlockEntityType
|
||||
import net.minecraft.world.level.block.state.BlockState
|
||||
import net.minecraft.world.level.redstone.Orientation
|
||||
import net.minecraft.world.phys.BlockHitResult
|
||||
import org.neoflock.neocomputers.entity.BlockEntities
|
||||
import org.neoflock.neocomputers.entity.NodeEntity
|
||||
import org.neoflock.neocomputers.network.Networking
|
||||
import org.neoflock.neocomputers.network.PowerRole
|
||||
|
||||
class CapacitorEntity(pos: BlockPos, state: BlockState) : NodeEntity(BlockEntities.CAPACITOR_ENTITY.get(), pos, state) {
|
||||
class CapacitorEntity(pos: BlockPos, state: BlockState) : NodeBlockEntity(BlockEntities.CAPACITOR_ENTITY.get(), pos, state) {
|
||||
var amountStored: Double = 0.0
|
||||
val capacity = 20000.0
|
||||
|
||||
val netNode = object : Networking.Node() {
|
||||
override fun isProducer() = true
|
||||
override val node = object : Networking.Node() {
|
||||
override fun getPowerRole() = PowerRole.PRODUCER
|
||||
override fun getEnergy() = amountStored
|
||||
override fun maxEnergyCapacity(): Double = capacity
|
||||
override fun setEnergy(energy: Double) {
|
||||
amountStored = energy
|
||||
}
|
||||
}
|
||||
|
||||
override fun getNode() = netNode
|
||||
}
|
||||
|
||||
class CapacitorBlock : BaseBlock("capacitor"), EntityBlock {
|
||||
class CapacitorBlock : NodeBlock("capacitor") {
|
||||
override fun newBlockEntity(blockPos: BlockPos, blockState: BlockState): BlockEntity? {
|
||||
val cap = CapacitorEntity(blockPos, blockState)
|
||||
cap.initNetworking()
|
||||
@@ -57,7 +52,7 @@ class CapacitorBlock : BaseBlock("capacitor"), EntityBlock {
|
||||
if(ent.isPresent()) {
|
||||
val cap = ent.get()
|
||||
if(sp.isCrouching()) cap.amountStored++
|
||||
val msg = PlayerChatMessage.system("energy: ${cap.amountStored} / ${cap.capacity} (${cap.getReachableNodes().size} reachable, ${cap.getNode().getReachable().size} connected)")
|
||||
val msg = PlayerChatMessage.system("energy: ${cap.amountStored} / ${cap.capacity} (${cap.computeEdges().size} edges, ${cap.node.getReachable().size} connected)")
|
||||
sp.sendChatMessage(OutgoingChatMessage.create(msg), false, ChatType.bind(ChatType.CHAT, player))
|
||||
}
|
||||
}
|
||||
|
||||
123
src/main/kotlin/org/neoflock/neocomputers/block/NodeBlock.kt
Normal file
123
src/main/kotlin/org/neoflock/neocomputers/block/NodeBlock.kt
Normal file
@@ -0,0 +1,123 @@
|
||||
package org.neoflock.neocomputers.block
|
||||
|
||||
import net.minecraft.core.BlockPos
|
||||
|
||||
import net.minecraft.world.entity.LivingEntity
|
||||
import net.minecraft.world.item.ItemStack
|
||||
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.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.BlockState
|
||||
import net.minecraft.world.level.redstone.Orientation
|
||||
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() {
|
||||
Networking.addNode(node)
|
||||
}
|
||||
|
||||
private var stateIsDirty = true
|
||||
|
||||
open fun getNeighbourEntities(): List<BlockEntity> {
|
||||
val subpos = listOf(
|
||||
blockPos.offset(0, 0, 1),
|
||||
blockPos.offset(0, 0, -1),
|
||||
blockPos.offset(0, 1, 0),
|
||||
blockPos.offset(0, -1, 0),
|
||||
blockPos.offset(1, 0, 0),
|
||||
blockPos.offset(-1, 0, 0),
|
||||
)
|
||||
|
||||
return subpos.mapNotNull { pos -> level?.getBlockEntity(pos) }
|
||||
}
|
||||
|
||||
fun computeEdges(): Set<NodeBlockEntity> {
|
||||
val s = mutableSetOf<NodeBlockEntity>()
|
||||
val neighbours = getNeighbourEntities()
|
||||
for(neighbour in neighbours) {
|
||||
if(neighbour is NodeBlockEntity) s.add(neighbour);
|
||||
// TODO: handle cable entities
|
||||
}
|
||||
s.remove(this)
|
||||
return s
|
||||
}
|
||||
|
||||
fun invalidateNodeState() {
|
||||
stateIsDirty = true
|
||||
}
|
||||
|
||||
fun needsSynchronization() = stateIsDirty
|
||||
|
||||
fun ensureSynchronized() {
|
||||
if(!stateIsDirty) return
|
||||
stateIsDirty = false
|
||||
computeEdges().forEach {
|
||||
node.connectTo(it.node)
|
||||
}
|
||||
}
|
||||
|
||||
override fun setChanged() {
|
||||
invalidateNodeState()
|
||||
computeEdges().forEach { it.invalidateNodeState() }
|
||||
super.setChanged()
|
||||
}
|
||||
|
||||
override fun setRemoved() {
|
||||
super.setRemoved()
|
||||
Networking.removeNode(node)
|
||||
}
|
||||
}
|
||||
|
||||
abstract class NodeBlock(name: String): BaseBlock(name), EntityBlock {
|
||||
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 NodeBlockEntity) return;
|
||||
blockEntity.ensureSynchronized()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun setPlacedBy(
|
||||
level: Level,
|
||||
blockPos: BlockPos,
|
||||
blockState: BlockState,
|
||||
livingEntity: LivingEntity?,
|
||||
itemStack: ItemStack
|
||||
) {
|
||||
if(!level.isClientSide) {
|
||||
val ent = level.getBlockEntity(blockPos)
|
||||
if(ent is NodeBlockEntity) {
|
||||
ent.invalidateNodeState()
|
||||
}
|
||||
}
|
||||
super.setPlacedBy(level, blockPos, blockState, livingEntity, itemStack)
|
||||
}
|
||||
|
||||
override fun neighborChanged(
|
||||
blockState: BlockState,
|
||||
level: Level,
|
||||
blockPos: BlockPos,
|
||||
block: Block,
|
||||
orientation: Orientation?,
|
||||
bl: Boolean
|
||||
) {
|
||||
if(!level.isClientSide) {
|
||||
val ent = level.getBlockEntity(blockPos)
|
||||
if(ent is NodeBlockEntity) {
|
||||
ent.invalidateNodeState()
|
||||
}
|
||||
|
||||
}
|
||||
super.neighborChanged(blockState, level, blockPos, block, orientation, bl)
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,7 @@ import org.neoflock.neocomputers.entity.ScreenEntity
|
||||
import org.neoflock.neocomputers.gui.menu.Menus
|
||||
import org.neoflock.neocomputers.network.Networking
|
||||
|
||||
class ScreenBlock() : BaseBlock("screen"), EntityBlock {
|
||||
class ScreenBlock() : NodeBlock("screen") {
|
||||
|
||||
override fun newBlockEntity(blockPos: BlockPos, blockState: BlockState): BlockEntity? {
|
||||
val scr = ScreenEntity(blockPos, blockState)
|
||||
@@ -39,7 +39,7 @@ class ScreenBlock() : BaseBlock("screen"), EntityBlock {
|
||||
): InteractionResult {
|
||||
if(!level.isClientSide) {
|
||||
val screenState = level.getBlockEntity(blockPos, BlockEntities.SCREEN_ENTITY.get()).get()
|
||||
if(!screenState.getNode().consumeEnergy(5.0)) return InteractionResult.SUCCESS;
|
||||
if(!screenState.node.consumeEnergy(5.0)) return InteractionResult.SUCCESS;
|
||||
MenuRegistry.openMenu(player as ServerPlayer, object : MenuProvider {
|
||||
override fun getDisplayName(): Component = Component.literal("SCREEEEEN!")
|
||||
override fun createMenu(i: Int, inventory: Inventory, player: Player): AbstractContainerMenu {
|
||||
|
||||
Reference in New Issue
Block a user