package net.shadowfacts.phycon.networking import net.fabricmc.fabric.api.networking.v1.PacketByteBufs import net.fabricmc.fabric.api.networking.v1.PacketSender import net.minecraft.network.Packet import net.minecraft.network.PacketByteBuf import net.minecraft.server.MinecraftServer import net.minecraft.server.network.ServerPlayNetworkHandler import net.minecraft.server.network.ServerPlayerEntity import net.minecraft.util.Identifier import net.minecraft.util.registry.Registry import net.minecraft.util.registry.RegistryKey import net.shadowfacts.phycon.PhysicalConnectivity import net.shadowfacts.phycon.block.terminal.CraftingTerminalBlockEntity import net.shadowfacts.phycon.block.terminal.CraftingTerminalScreenHandler /** * @author shadowfacts */ object C2STerminalCraftingButton: ServerReceiver { override val CHANNEL = Identifier(PhysicalConnectivity.MODID, "terminal_crafting_button") enum class Action { CLEAR_GRID, REQUEST_ONE_MORE, REQUEST_MAX_MORE, } operator fun invoke(terminal: CraftingTerminalBlockEntity, action: Action): Packet<*> { val buf = PacketByteBufs.create() buf.writeIdentifier(terminal.world!!.registryKey.value) buf.writeBlockPos(terminal.pos) buf.writeByte(action.ordinal) return createPacket(buf) } override fun receive(server: MinecraftServer, player: ServerPlayerEntity, handler: ServerPlayNetworkHandler, buf: PacketByteBuf, responseSender: PacketSender) { val dimID = buf.readIdentifier() val pos = buf.readBlockPos() val action = Action.values()[buf.readByte().toInt()] server.execute { val key = RegistryKey.of(Registry.WORLD_KEY, dimID) val screenHandler = player.currentScreenHandler as? CraftingTerminalScreenHandler ?: return@execute if (screenHandler.terminal.pos != pos || screenHandler.terminal.world!!.registryKey != key) return@execute when (action) { Action.CLEAR_GRID -> screenHandler.clearCraftingGrid() Action.REQUEST_ONE_MORE -> screenHandler.requestMoreCraftingIngredients(1) Action.REQUEST_MAX_MORE -> screenHandler.requestMoreCraftingIngredients(64) } } } }