2021-03-28 17:50:11 +00:00
|
|
|
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 {
|
2021-12-22 23:59:51 +00:00
|
|
|
val key = RegistryKey.of(Registry.WORLD_KEY, dimID)
|
2021-03-28 17:50:11 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|