PhysicalConnectivity/src/main/kotlin/net/shadowfacts/phycon/networking/C2SConfigureInserterAmount.kt

46 lines
1.6 KiB
Kotlin
Raw Normal View History

2021-02-28 22:56:25 +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.inserter.InserterBlockEntity
/**
* @author shadowfacts
*/
object C2SConfigureInserterAmount: ServerReceiver {
override val CHANNEL = Identifier(PhysicalConnectivity.MODID, "configure_inserter_amount")
operator fun invoke(be: InserterBlockEntity): Packet<*> {
val buf = PacketByteBufs.create()
buf.writeIdentifier(be.world!!.registryKey.value)
buf.writeBlockPos(be.pos)
buf.writeVarInt(be.amountToExtract)
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 amount = buf.readVarInt()
server.execute {
val key = RegistryKey.of(Registry.DIMENSION, dimID)
val world = server.getWorld(key) ?: return@execute
val be = world.getBlockEntity(pos) as? InserterBlockEntity ?: return@execute
be.amountToExtract = amount
be.markDirty()
}
}
}