add proper container and do some syncy stuff and save
This commit is contained in:
@@ -20,6 +20,7 @@ import net.minecraft.world.phys.shapes.VoxelShape
|
||||
import org.neoflock.neocomputers.NeoComputers
|
||||
import org.neoflock.neocomputers.entity.BlockEntities
|
||||
import org.neoflock.neocomputers.entity.RackEntity
|
||||
import org.neoflock.neocomputers.network.NodeSynchronizer
|
||||
|
||||
class RackBlock : BaseBlock(Properties.of().noOcclusion()), EntityBlock {
|
||||
override fun newBlockEntity(
|
||||
@@ -59,7 +60,10 @@ class RackBlock : BaseBlock(Properties.of().noOcclusion()), EntityBlock {
|
||||
}
|
||||
}
|
||||
|
||||
if (!level.isClientSide) MenuRegistry.openMenu(player as ServerPlayer, ent)
|
||||
if (!level.isClientSide) {
|
||||
MenuRegistry.openMenu(player as ServerPlayer, ent)
|
||||
NodeSynchronizer.registerPlayerScreen(player as ServerPlayer, ent.node)
|
||||
}
|
||||
return InteractionResult.SUCCESS
|
||||
}
|
||||
}
|
||||
@@ -1,19 +1,113 @@
|
||||
package org.neoflock.neocomputers.entity
|
||||
|
||||
import net.minecraft.core.BlockPos
|
||||
import net.minecraft.core.Direction
|
||||
import net.minecraft.core.HolderLookup
|
||||
import net.minecraft.core.NonNullList
|
||||
import net.minecraft.nbt.CompoundTag
|
||||
import net.minecraft.network.FriendlyByteBuf
|
||||
import net.minecraft.network.chat.Component
|
||||
import net.minecraft.server.level.ServerPlayer
|
||||
import net.minecraft.world.ContainerHelper
|
||||
import net.minecraft.world.MenuProvider
|
||||
import net.minecraft.world.entity.player.Inventory
|
||||
import net.minecraft.world.entity.player.Player
|
||||
import net.minecraft.world.inventory.AbstractContainerMenu
|
||||
import net.minecraft.world.item.ItemStack
|
||||
import net.minecraft.world.level.block.entity.BlockEntity
|
||||
import net.minecraft.world.level.block.state.BlockState
|
||||
import org.neoflock.neocomputers.NeoComputers
|
||||
import org.neoflock.neocomputers.block.DeviceBlockEntity
|
||||
import org.neoflock.neocomputers.gui.menu.RackMenu
|
||||
import org.neoflock.neocomputers.network.DeviceNode
|
||||
import org.neoflock.neocomputers.network.Networking
|
||||
import org.neoflock.neocomputers.utils.GenericContainer
|
||||
|
||||
class RackEntity(pos: BlockPos, state: BlockState) : DeviceBlockEntity(BlockEntities.RACK_ENTITY.get(), pos, state), MenuProvider, GenericContainer {
|
||||
val stacks: NonNullList<ItemStack> = NonNullList<ItemStack>.withSize(4, ItemStack.EMPTY)
|
||||
|
||||
var conns = mutableListOf(
|
||||
-1, -1, -1, -1,
|
||||
-1, -1, -1, -1,
|
||||
-1, -1, -1, -1,
|
||||
-1, -1, -1, -1
|
||||
)
|
||||
|
||||
var relayMode = false
|
||||
|
||||
val node: DeviceNode = object : DeviceNode() {
|
||||
override var reachability: Networking.Visibility = Networking.Visibility.NONE
|
||||
|
||||
override fun writeFullStateCommit(buf: FriendlyByteBuf) {
|
||||
super.writeFullStateCommit(buf)
|
||||
buf.writeBoolean(relayMode)
|
||||
|
||||
|
||||
for (conn in conns) {
|
||||
buf.writeInt(conn)
|
||||
}
|
||||
}
|
||||
|
||||
override fun processCommit(buf: FriendlyByteBuf) {
|
||||
super.processCommit(buf)
|
||||
relayMode = buf.readBoolean()
|
||||
|
||||
for (i in 0..15) {
|
||||
conns[i] = buf.readInt()
|
||||
}
|
||||
}
|
||||
|
||||
override fun processScreenInteraction(player: ServerPlayer, buf: FriendlyByteBuf) {
|
||||
super.processScreenInteraction(player, buf)
|
||||
relayMode = buf.readBoolean()
|
||||
|
||||
val slot = buf.readInt()
|
||||
NeoComputers.LOGGER.info(slot.toString())
|
||||
for (i in 0..3) {
|
||||
conns[slot*4+i] = buf.readInt()
|
||||
NeoComputers.LOGGER.info("{} {}", slot*4+i, conns[slot*4+i])
|
||||
}
|
||||
setChanged()
|
||||
}
|
||||
|
||||
override fun encodeScreenData(player: ServerPlayer, buf: FriendlyByteBuf) {
|
||||
super.encodeScreenData(player, buf)
|
||||
buf.writeBoolean(relayMode)
|
||||
|
||||
for (conn in conns) {
|
||||
buf.writeInt(conn)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class RackEntity(pos: BlockPos, state: BlockState) : BlockEntity(BlockEntities.RACK_ENTITY.get(), pos, state), MenuProvider {
|
||||
override fun getDisplayName(): Component? = Component.literal("Rack")
|
||||
|
||||
override fun createMenu(i: Int, inventory: Inventory, player: Player): AbstractContainerMenu {
|
||||
return RackMenu(i, inventory)
|
||||
return RackMenu(i, inventory, this)
|
||||
}
|
||||
|
||||
override fun getDeviceNodes(): List<DeviceNode> = listOf(node)
|
||||
|
||||
override fun getNodeFromSide(directionToRequester: Direction): DeviceNode? = node
|
||||
|
||||
override fun loadAdditional(tag: CompoundTag, registries: HolderLookup.Provider) {
|
||||
super.loadAdditional(tag, registries)
|
||||
ContainerHelper.loadAllItems(tag, getItems(), registries)
|
||||
relayMode = tag.getBoolean("relay")
|
||||
|
||||
val connarray = tag.getIntArray("conns")
|
||||
if (connarray.size == 16) conns = connarray.toMutableList()
|
||||
}
|
||||
|
||||
override fun saveAdditional(tag: CompoundTag, registries: HolderLookup.Provider) {
|
||||
super.saveAdditional(tag, registries)
|
||||
ContainerHelper.saveAllItems(tag, getItems(), registries)
|
||||
tag.putBoolean("relay", relayMode)
|
||||
tag.putIntArray("conns", conns.toIntArray())
|
||||
}
|
||||
|
||||
// override fun getItems(): NonNullList<ItemStack> = items
|
||||
override fun getItems(): NonNullList<ItemStack> = stacks
|
||||
|
||||
override fun stillValid(player: Player): Boolean = true
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import com.mojang.blaze3d.vertex.DefaultVertexFormat
|
||||
import com.mojang.blaze3d.vertex.Tesselator
|
||||
import com.mojang.blaze3d.vertex.VertexConsumer
|
||||
import com.mojang.blaze3d.vertex.VertexFormat
|
||||
import io.netty.buffer.Unpooled
|
||||
import net.minecraft.client.Minecraft
|
||||
import net.minecraft.client.gui.GuiGraphics
|
||||
import net.minecraft.client.gui.components.events.GuiEventListener
|
||||
@@ -14,6 +15,7 @@ import net.minecraft.client.renderer.MultiBufferSource
|
||||
import net.minecraft.client.renderer.RenderStateShard
|
||||
import net.minecraft.client.renderer.RenderType
|
||||
import net.minecraft.client.resources.sounds.SimpleSoundInstance
|
||||
import net.minecraft.network.FriendlyByteBuf
|
||||
import net.minecraft.sounds.SoundEvents
|
||||
import net.minecraft.world.Container
|
||||
import net.minecraft.world.SimpleContainer
|
||||
@@ -29,8 +31,8 @@ class RackSlot(container: Container, slot: Int, x: Int, y: Int) : DynamicSlot(co
|
||||
val DARK_COLOURS = listOf(0xff6a6ab0.toInt(), 0xff60999d.toInt(), 0xffa2a44e.toInt(), 0xffb36660.toInt(), 0xff67a34e.toInt())
|
||||
val LIGHT_COLOURS = listOf(0xffdcdcf0.toInt(), 0xffdcdcf0.toInt(), 0xffececd4.toInt(), 0xfff0dbd9.toInt(), 0xffdbecd4.toInt())
|
||||
|
||||
val secondaries = 2
|
||||
val selected = mutableListOf(-1, -1, -1)
|
||||
val secondaries = 3
|
||||
val selected = mutableListOf(-1, -1, -1, -1)
|
||||
|
||||
override fun draw(graphics: GuiGraphics, mouseX: Int, mouseY: Int) {
|
||||
super.draw(graphics, mouseX, mouseY)
|
||||
@@ -97,6 +99,22 @@ class RackSlot(container: Container, slot: Int, x: Int, y: Int) : DynamicSlot(co
|
||||
}
|
||||
}
|
||||
}
|
||||
fun processStateScreenPacket(buf: FriendlyByteBuf) {
|
||||
selected[0] = buf.readInt()
|
||||
selected[1] = buf.readInt()
|
||||
selected[2] = buf.readInt()
|
||||
selected[3] = buf.readInt()
|
||||
}
|
||||
|
||||
fun encode(buf: FriendlyByteBuf) {
|
||||
// val buf = FriendlyByteBuf(Unpooled.buffer())
|
||||
buf.writeInt(containerSlot)
|
||||
buf.writeInt(selected[0])
|
||||
buf.writeInt(selected[1])
|
||||
buf.writeInt(selected[2])
|
||||
buf.writeInt(selected[3])
|
||||
|
||||
}
|
||||
|
||||
// TODO: replace with graphics.fill (cant be assed atm)
|
||||
fun drawColor(guiGraphics: GuiGraphics, buffer: VertexConsumer, _x:Int, _y: Int, col: Int, width: Int=1, height: Int=1) {
|
||||
|
||||
@@ -1,16 +1,21 @@
|
||||
package org.neoflock.neocomputers.gui.screen
|
||||
|
||||
import io.netty.buffer.Unpooled
|
||||
import net.minecraft.client.gui.GuiGraphics
|
||||
import net.minecraft.client.gui.components.Button
|
||||
import net.minecraft.client.gui.components.SpriteIconButton
|
||||
import net.minecraft.client.gui.components.events.GuiEventListener
|
||||
import net.minecraft.client.gui.narration.NarrationElementOutput
|
||||
import net.minecraft.network.FriendlyByteBuf
|
||||
import net.minecraft.network.chat.Component
|
||||
import net.minecraft.network.chat.MutableComponent
|
||||
import net.minecraft.resources.ResourceLocation
|
||||
import net.minecraft.world.entity.player.Inventory
|
||||
import org.neoflock.neocomputers.NeoComputers
|
||||
import org.neoflock.neocomputers.gui.menu.RackMenu
|
||||
import org.neoflock.neocomputers.gui.menu.RackSlot
|
||||
import org.neoflock.neocomputers.gui.widget.IconTextButton
|
||||
import org.neoflock.neocomputers.network.NodeSynchronizer
|
||||
import org.neoflock.neocomputers.utils.GenericContainerScreen
|
||||
import java.util.function.Supplier
|
||||
|
||||
@@ -28,6 +33,12 @@ class RackScreen(menu: RackMenu, inventory: Inventory, component: Component) : G
|
||||
it.message = Component.literal("Enabled")
|
||||
relay_mode = true
|
||||
}
|
||||
|
||||
val buffer = FriendlyByteBuf(Unpooled.buffer())
|
||||
buffer.writeBoolean(relay_mode)
|
||||
(menu.slots[0] as RackSlot).encode(buffer)
|
||||
NodeSynchronizer.sendScreenInteraction(buffer)
|
||||
NeoComputers.LOGGER.info("sent")
|
||||
}
|
||||
init {
|
||||
this.imageWidth = 175
|
||||
@@ -59,4 +70,37 @@ class RackScreen(menu: RackMenu, inventory: Inventory, component: Component) : G
|
||||
graphics.drawString(font, "Right", x, y+33, 0x404040, false)
|
||||
graphics.drawString(font, "Left", x, y+44, 0x404040, false)
|
||||
}
|
||||
|
||||
override fun processScreenStatePacket(buf: FriendlyByteBuf) {
|
||||
super.processScreenStatePacket(buf)
|
||||
// NeoComputers.LOGGER.info("porcessing screen state packet...")
|
||||
relay_mode = buf.readBoolean()
|
||||
if (relay_mode) relaybtn.message = Component.literal("Enabled")
|
||||
else relaybtn.message = Component.literal("Disabled")
|
||||
|
||||
for (slot in menu.slots) {
|
||||
if (slot is RackSlot) {
|
||||
slot.processStateScreenPacket(buf)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
override fun mouseClicked(mouseX: Double, mouseY: Double, button: Int): Boolean {
|
||||
if (super.mouseClicked(mouseX, mouseY, button)) return true
|
||||
for (slot in menu.slots) {
|
||||
if (slot is RackSlot) {
|
||||
if(slot.mouseClicked(mouseX-imageX, mouseY-imageY, button)) {
|
||||
val buf = FriendlyByteBuf(Unpooled.buffer())
|
||||
buf.writeBoolean(relay_mode)
|
||||
slot.encode(buf)
|
||||
|
||||
NodeSynchronizer.sendScreenInteraction(buf)
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -193,11 +193,6 @@ abstract class GenericContainerScreen<T: GenericContainerMenu>(menu: T, inventor
|
||||
for (widget in widgets) {
|
||||
if (widget.mouseClicked(mouseX-imageX, mouseY-imageY, button)) return true
|
||||
}
|
||||
for (slot in menu.slots) { // TODO: this solution sucks, make this less ass
|
||||
if (slot is GuiEventListener) {
|
||||
slot.mouseClicked(mouseX-imageX, mouseY-imageY, button)
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user