2021-02-16 03:51:33 +00:00
|
|
|
package net.shadowfacts.phycon.networking
|
|
|
|
|
|
|
|
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking
|
|
|
|
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs
|
|
|
|
import net.fabricmc.fabric.api.networking.v1.PacketSender
|
|
|
|
import net.minecraft.item.ItemStack
|
|
|
|
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
|
2021-03-27 14:22:18 +00:00
|
|
|
import net.shadowfacts.phycon.block.terminal.AbstractTerminalBlockEntity
|
2021-03-23 02:01:33 +00:00
|
|
|
import net.shadowfacts.phycon.util.readItemStackWithoutCount
|
|
|
|
import net.shadowfacts.phycon.util.writeItemStackWithoutCount
|
2021-02-16 03:51:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author shadowfacts
|
|
|
|
*/
|
|
|
|
object C2STerminalRequestItem: ServerReceiver {
|
|
|
|
|
|
|
|
override val CHANNEL = Identifier(PhysicalConnectivity.MODID, "terminal_request_item")
|
|
|
|
|
2021-03-27 14:22:18 +00:00
|
|
|
operator fun invoke(terminal: AbstractTerminalBlockEntity, stack: ItemStack, amount: Int): Packet<*> {
|
2021-02-16 03:51:33 +00:00
|
|
|
val buf = PacketByteBufs.create()
|
|
|
|
buf.writeIdentifier(terminal.world!!.registryKey.value)
|
|
|
|
buf.writeBlockPos(terminal.pos)
|
2021-02-18 04:18:15 +00:00
|
|
|
|
2021-03-23 02:01:33 +00:00
|
|
|
// Don't send the count of the fake stack when sending over the network because
|
2021-02-18 04:18:15 +00:00
|
|
|
// PacketByteBuf serializes the stack count as a byte. Otherwise counts > 127 will result in
|
|
|
|
// an overflow and be negative on the receiving side.
|
2021-03-23 02:01:33 +00:00
|
|
|
buf.writeItemStackWithoutCount(stack)
|
2021-02-18 04:18:15 +00:00
|
|
|
|
2021-02-16 03:51:33 +00:00
|
|
|
buf.writeVarInt(amount)
|
|
|
|
return ClientPlayNetworking.createC2SPacket(CHANNEL, buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun receive(server: MinecraftServer, player: ServerPlayerEntity, handler: ServerPlayNetworkHandler, buf: PacketByteBuf, responseSender: PacketSender) {
|
|
|
|
val dimID = buf.readIdentifier()
|
|
|
|
val pos = buf.readBlockPos()
|
2021-03-23 02:01:33 +00:00
|
|
|
val stack = buf.readItemStackWithoutCount()
|
2021-02-16 03:51:33 +00:00
|
|
|
val amount = buf.readVarInt()
|
|
|
|
|
|
|
|
server.execute {
|
2021-12-22 23:59:51 +00:00
|
|
|
val key = RegistryKey.of(Registry.WORLD_KEY, dimID)
|
2021-02-16 03:51:33 +00:00
|
|
|
val world = server.getWorld(key) ?: return@execute
|
2021-03-27 14:22:18 +00:00
|
|
|
val terminal = world.getBlockEntity(pos) as? AbstractTerminalBlockEntity ?: return@execute
|
2021-02-16 03:51:33 +00:00
|
|
|
terminal.requestItem(stack, amount)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-28 18:48:39 +00:00
|
|
|
}
|