relay bugfix and graphics
This commit is contained in:
@@ -14,6 +14,7 @@ import org.neoflock.neocomputers.block.Blocks;
|
|||||||
import org.neoflock.neocomputers.block.CableBlock;
|
import org.neoflock.neocomputers.block.CableBlock;
|
||||||
import org.neoflock.neocomputers.entity.BlockEntities;
|
import org.neoflock.neocomputers.entity.BlockEntities;
|
||||||
import org.neoflock.neocomputers.entity.render.CaseEntityRenderer;
|
import org.neoflock.neocomputers.entity.render.CaseEntityRenderer;
|
||||||
|
import org.neoflock.neocomputers.entity.render.RelayEntityRenderer;
|
||||||
import org.neoflock.neocomputers.entity.render.ScreenEntityRenderer;
|
import org.neoflock.neocomputers.entity.render.ScreenEntityRenderer;
|
||||||
import org.neoflock.neocomputers.item.Items;
|
import org.neoflock.neocomputers.item.Items;
|
||||||
import org.neoflock.neocomputers.platforms.fabric.client.model.ModelLoader;
|
import org.neoflock.neocomputers.platforms.fabric.client.model.ModelLoader;
|
||||||
@@ -24,6 +25,7 @@ public class NeoComputersFabricClient implements ClientModInitializer {
|
|||||||
ModelLoadingPlugin.register(new ModelLoader());
|
ModelLoadingPlugin.register(new ModelLoader());
|
||||||
BlockEntityRenderers.register(BlockEntities.INSTANCE.getSCREEN_ENTITY().get(), ScreenEntityRenderer::new);
|
BlockEntityRenderers.register(BlockEntities.INSTANCE.getSCREEN_ENTITY().get(), ScreenEntityRenderer::new);
|
||||||
BlockEntityRenderers.register(BlockEntities.INSTANCE.getCASE_ENTITY().get(), CaseEntityRenderer::new);
|
BlockEntityRenderers.register(BlockEntities.INSTANCE.getCASE_ENTITY().get(), CaseEntityRenderer::new);
|
||||||
|
BlockEntityRenderers.register(BlockEntities.INSTANCE.getRELAY_ENTITY().get(), RelayEntityRenderer::new);
|
||||||
|
|
||||||
ColorProviderRegistry.BLOCK.register((state, world, pos, index) -> {
|
ColorProviderRegistry.BLOCK.register((state, world, pos, index) -> {
|
||||||
if (index == 0) {
|
if (index == 0) {
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ class RelayEntity(blockPos: BlockPos, blockState: BlockState): SingleDeviceBlock
|
|||||||
fun computeRelayCapacity(): Int = computeRelayBufferSize() + computeRelayQueueSize()
|
fun computeRelayCapacity(): Int = computeRelayBufferSize() + computeRelayQueueSize()
|
||||||
|
|
||||||
val queue = mutableListOf<Networking.ClassicPacket>()
|
val queue = mutableListOf<Networking.ClassicPacket>()
|
||||||
var active = false
|
var activityTickLeft = 0
|
||||||
var ticksUntilQueue = 0
|
var ticksUntilQueue = 0
|
||||||
|
|
||||||
override val deviceNode = object : DeviceNode() {
|
override val deviceNode = object : DeviceNode() {
|
||||||
@@ -69,6 +69,8 @@ class RelayEntity(blockPos: BlockPos, blockState: BlockState): SingleDeviceBlock
|
|||||||
if(message.sender == this) return
|
if(message.sender == this) return
|
||||||
if(message is Networking.ClassicPacket && message.hopCount < 5 && queue.size < computeRelayCapacity()) {
|
if(message is Networking.ClassicPacket && message.hopCount < 5 && queue.size < computeRelayCapacity()) {
|
||||||
queue.addLast(message)
|
queue.addLast(message)
|
||||||
|
activityTickLeft = 20
|
||||||
|
markChanged()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,12 +84,12 @@ class RelayEntity(blockPos: BlockPos, blockState: BlockState): SingleDeviceBlock
|
|||||||
|
|
||||||
override fun writeFullStateCommit(buf: FriendlyByteBuf) {
|
override fun writeFullStateCommit(buf: FriendlyByteBuf) {
|
||||||
super.writeFullStateCommit(buf)
|
super.writeFullStateCommit(buf)
|
||||||
buf.writeBoolean(active)
|
buf.writeVarInt(activityTickLeft)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun processCommit(buf: FriendlyByteBuf) {
|
override fun processCommit(buf: FriendlyByteBuf) {
|
||||||
super.processCommit(buf)
|
super.processCommit(buf)
|
||||||
active = buf.readBoolean()
|
activityTickLeft = buf.readVarInt()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,7 +99,7 @@ class RelayEntity(blockPos: BlockPos, blockState: BlockState): SingleDeviceBlock
|
|||||||
|
|
||||||
for(connection in deviceNode.connections) {
|
for(connection in deviceNode.connections) {
|
||||||
// skip unwanted loopback
|
// skip unwanted loopback
|
||||||
if(connection !in pack.sender.getReachable()) continue
|
if(connection in pack.sender.getReachable()) continue
|
||||||
val hopped = pack.hop(pack.sender)
|
val hopped = pack.hop(pack.sender)
|
||||||
|
|
||||||
if(connection is ConventionalNetworkDevice) {
|
if(connection is ConventionalNetworkDevice) {
|
||||||
@@ -111,6 +113,10 @@ class RelayEntity(blockPos: BlockPos, blockState: BlockState): SingleDeviceBlock
|
|||||||
override fun tickDevice(level: Level) {
|
override fun tickDevice(level: Level) {
|
||||||
super.tickDevice(level)
|
super.tickDevice(level)
|
||||||
if(level !is ServerLevel) return
|
if(level !is ServerLevel) return
|
||||||
|
if(activityTickLeft > 0) {
|
||||||
|
activityTickLeft--
|
||||||
|
deviceNode.markChanged()
|
||||||
|
}
|
||||||
ticksUntilQueue--
|
ticksUntilQueue--
|
||||||
if(ticksUntilQueue <= 0) {
|
if(ticksUntilQueue <= 0) {
|
||||||
ticksUntilQueue = computeRelayInterval()
|
ticksUntilQueue = computeRelayInterval()
|
||||||
@@ -122,7 +128,6 @@ class RelayEntity(blockPos: BlockPos, blockState: BlockState): SingleDeviceBlock
|
|||||||
deviceNode.markChanged()
|
deviceNode.markChanged()
|
||||||
val cap = computeRelayCapacity()
|
val cap = computeRelayCapacity()
|
||||||
while(queue.size > cap) queue.removeLast()
|
while(queue.size > cap) queue.removeLast()
|
||||||
active = queue.isNotEmpty()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getMachineBlockPosition() = blockPos!!
|
override fun getMachineBlockPosition() = blockPos!!
|
||||||
|
|||||||
@@ -110,7 +110,7 @@ class CaseBlockEntity(blockPos: BlockPos, blockState: BlockState): SingleDeviceB
|
|||||||
override fun received(message: Networking.Message) {
|
override fun received(message: Networking.Message) {
|
||||||
super.received(message)
|
super.received(message)
|
||||||
if(message is Networking.ClassicPacket) {
|
if(message is Networking.ClassicPacket) {
|
||||||
NeoComputers.LOGGER.info("machine $address got $message")
|
NeoComputers.LOGGER.info("machine $address got $message from ${message.sender.address}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -177,7 +177,7 @@ class CaseBlockEntity(blockPos: BlockPos, blockState: BlockState): SingleDeviceB
|
|||||||
}
|
}
|
||||||
// Server-side stuff!!
|
// Server-side stuff!!
|
||||||
sendMachineEvent(MachinePowerEvent(this, isOn))
|
sendMachineEvent(MachinePowerEvent(this, isOn))
|
||||||
Networking.emitMessage(deviceNode, Networking.ClassicPacket(deviceNode, deviceNode.address.toString(), "fuck you", 1, listOf(), 0))
|
Networking.emitMessage(deviceNode, Networking.ClassicPacket(deviceNode, deviceNode.address.toString(), null, 1, listOf(), 0))
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun start(): Boolean {
|
override fun start(): Boolean {
|
||||||
@@ -277,11 +277,11 @@ class CaseBlockEntity(blockPos: BlockPos, blockState: BlockState): SingleDeviceB
|
|||||||
|
|
||||||
override fun stillValid(player: Player): Boolean = !this.isRemoved
|
override fun stillValid(player: Player): Boolean = !this.isRemoved
|
||||||
|
|
||||||
override fun loadAdditional(compoundTag: CompoundTag, provider: HolderLookup.Provider) {
|
override fun loadAdditional(tag: CompoundTag, registries: HolderLookup.Provider) {
|
||||||
super.loadAdditional(compoundTag, provider)
|
super.loadAdditional(tag, registries)
|
||||||
deviceNode.energy = min(deviceNode.energyCapacity, compoundTag.getLong("energy"))
|
deviceNode.energy = min(deviceNode.energyCapacity, tag.getLong("energy"))
|
||||||
//isOn = compoundTag.getBoolean("powerOn")
|
//isOn = compoundTag.getBoolean("powerOn")
|
||||||
ContainerHelper.loadAllItems(compoundTag, getItems(), provider)
|
ContainerHelper.loadAllItems(tag, getItems(), registries)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun saveAdditional(compoundTag: CompoundTag, provider: HolderLookup.Provider) {
|
override fun saveAdditional(compoundTag: CompoundTag, provider: HolderLookup.Provider) {
|
||||||
|
|||||||
@@ -0,0 +1,65 @@
|
|||||||
|
package org.neoflock.neocomputers.entity.render
|
||||||
|
|
||||||
|
import com.mojang.blaze3d.vertex.DefaultVertexFormat
|
||||||
|
import com.mojang.blaze3d.vertex.PoseStack
|
||||||
|
import com.mojang.blaze3d.vertex.VertexFormat
|
||||||
|
import com.mojang.math.Axis
|
||||||
|
import net.minecraft.client.renderer.GameRenderer
|
||||||
|
import net.minecraft.client.renderer.MultiBufferSource
|
||||||
|
import net.minecraft.client.renderer.RenderStateShard
|
||||||
|
import net.minecraft.client.renderer.RenderType
|
||||||
|
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer
|
||||||
|
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider
|
||||||
|
import net.minecraft.resources.ResourceLocation
|
||||||
|
import org.neoflock.neocomputers.NeoComputers
|
||||||
|
import org.neoflock.neocomputers.block.RelayEntity
|
||||||
|
import kotlin.math.min
|
||||||
|
|
||||||
|
class RelayEntityRenderer(val context: BlockEntityRendererProvider.Context?): BlockEntityRenderer<RelayEntity> {
|
||||||
|
val RELAY_ON_TEX = ResourceLocation.fromNamespaceAndPath(NeoComputers.MODID, "textures/block/relay_side_on.png")
|
||||||
|
|
||||||
|
val RENDER_TYPE: RenderType =
|
||||||
|
RenderType.create(
|
||||||
|
"nc_screen",
|
||||||
|
DefaultVertexFormat.POSITION_TEX_COLOR,
|
||||||
|
VertexFormat.Mode.QUADS,
|
||||||
|
RenderType.TRANSIENT_BUFFER_SIZE, RenderType.CompositeState.builder().
|
||||||
|
setShaderState(RenderStateShard.ShaderStateShard(GameRenderer::getPositionTexColorShader)).
|
||||||
|
setTextureState(RenderStateShard.TextureStateShard(RELAY_ON_TEX, false, false))
|
||||||
|
.createCompositeState(false))
|
||||||
|
|
||||||
|
override fun render(
|
||||||
|
blockEntity: RelayEntity,
|
||||||
|
partialTick: Float,
|
||||||
|
mat: PoseStack,
|
||||||
|
bufferSource: MultiBufferSource,
|
||||||
|
packedLight: Int,
|
||||||
|
packedOverlay: Int
|
||||||
|
) {
|
||||||
|
//if(blockEntity.activityTickLeft == 0) return
|
||||||
|
|
||||||
|
val alpha = min((blockEntity.activityTickLeft.toFloat() * 255 / 20).toInt(), 255)
|
||||||
|
|
||||||
|
for(i in 0..<4) {
|
||||||
|
mat.pushPose()
|
||||||
|
|
||||||
|
val antiZFight = 0.001F
|
||||||
|
|
||||||
|
mat.rotateAround(Axis.YN.rotationDegrees(90F * i), 0.5F, 0.5F, 0.5F)
|
||||||
|
mat.translate(0F, 0F, 1F + antiZFight)
|
||||||
|
|
||||||
|
val width = 1F
|
||||||
|
val height = 1F
|
||||||
|
val bx = 0F
|
||||||
|
val by = 0F
|
||||||
|
|
||||||
|
val buffer = bufferSource.getBuffer(RENDER_TYPE)
|
||||||
|
buffer.addVertex(mat.last(), bx + width, by, 0f).setUv(1f, 1f).setColor(alpha, alpha, alpha, alpha)
|
||||||
|
buffer.addVertex(mat.last(), bx + width, by + height, 0f).setUv(1f, 0f).setColor(alpha, alpha, alpha, alpha)
|
||||||
|
buffer.addVertex(mat.last(), bx, by + height, 0f).setUv(0f, 0f).setColor(alpha, alpha, alpha, alpha)
|
||||||
|
buffer.addVertex(mat.last(), bx, by, 0f).setUv(0f, 1f).setColor(alpha, alpha, alpha, alpha)
|
||||||
|
|
||||||
|
mat.popPose()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -39,7 +39,8 @@ object Networking {
|
|||||||
|
|
||||||
abstract class Message(val sender: DeviceNode)
|
abstract class Message(val sender: DeviceNode)
|
||||||
|
|
||||||
class ClassicPacket(sender: DeviceNode, val src: String, val dst: String, val port: Int, val data: List<Any>, val hopCount: Int) : Message(sender) {
|
// null dst means broadcast
|
||||||
|
class ClassicPacket(sender: DeviceNode, val src: String, val dst: String?, val port: Int, val data: List<Any>, val hopCount: Int) : Message(sender) {
|
||||||
fun hop(sender: DeviceNode) = ClassicPacket(sender, src, dst, port, data, hopCount + 1);
|
fun hop(sender: DeviceNode) = ClassicPacket(sender, src, dst, port, data, hopCount + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user