127 lines
3.9 KiB
Kotlin
127 lines
3.9 KiB
Kotlin
|
package net.shadowfacts.phycon.network.block.miner
|
||
|
|
||
|
import alexiil.mc.lib.attributes.item.GroupedItemInvView
|
||
|
import alexiil.mc.lib.attributes.item.ItemStackUtil
|
||
|
import alexiil.mc.lib.attributes.item.filter.ItemFilter
|
||
|
import net.minecraft.block.Block
|
||
|
import net.minecraft.block.BlockState
|
||
|
import net.minecraft.item.ItemStack
|
||
|
import net.minecraft.server.world.ServerWorld
|
||
|
import net.minecraft.util.math.BlockPos
|
||
|
import net.minecraft.util.math.Direction
|
||
|
import net.minecraft.world.World
|
||
|
import net.shadowfacts.phycon.api.packet.Packet
|
||
|
import net.shadowfacts.phycon.api.util.IPAddress
|
||
|
import net.shadowfacts.phycon.init.PhyBlockEntities
|
||
|
import net.shadowfacts.phycon.network.DeviceBlockEntity
|
||
|
import net.shadowfacts.phycon.network.component.NetworkStackProvider
|
||
|
import net.shadowfacts.phycon.network.packet.*
|
||
|
import kotlin.math.min
|
||
|
|
||
|
/**
|
||
|
* @author shadowfacts
|
||
|
*/
|
||
|
class MinerBlockEntity: DeviceBlockEntity(PhyBlockEntities.MINER),
|
||
|
NetworkStackProvider {
|
||
|
|
||
|
private val facing: Direction
|
||
|
get() = cachedState[MinerBlock.FACING]
|
||
|
|
||
|
private val invProxy = MinerInvProxy(this)
|
||
|
|
||
|
override fun handle(packet: Packet) {
|
||
|
when (packet) {
|
||
|
is RequestInventoryPacket -> handleRequestInventory(packet)
|
||
|
is LocateStackPacket -> handleLocateStack(packet)
|
||
|
is ExtractStackPacket -> handleExtractStack(packet)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private fun handleRequestInventory(packet: RequestInventoryPacket) {
|
||
|
sendPacket(ReadInventoryPacket(invProxy, ipAddress, packet.source))
|
||
|
}
|
||
|
|
||
|
private fun handleLocateStack(packet: LocateStackPacket) {
|
||
|
val amount = invProxy.getAmount(packet.stack)
|
||
|
if (amount > 0) {
|
||
|
sendPacket(StackLocationPacket(packet.stack, amount, this, ipAddress, packet.source))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private fun handleExtractStack(packet: ExtractStackPacket) {
|
||
|
// always recalculate immediately before breaking
|
||
|
val drops = invProxy.getDrops(true)
|
||
|
if (invProxy.getAmount(packet.stack) > 0) {
|
||
|
world!!.breakBlock(pos.offset(facing), false)
|
||
|
|
||
|
// send the requested amount back to the requester
|
||
|
var remaining = packet.amount
|
||
|
for (droppedStack in drops) {
|
||
|
if (remaining <= 0) {
|
||
|
break
|
||
|
}
|
||
|
if (!ItemStackUtil.areEqualIgnoreAmounts(droppedStack, packet.stack)) {
|
||
|
continue
|
||
|
}
|
||
|
val copy = droppedStack.copy()
|
||
|
|
||
|
val toDecr = min(droppedStack.count, remaining)
|
||
|
droppedStack.decrement(toDecr)
|
||
|
remaining -= toDecr
|
||
|
|
||
|
// todo: should this try to combine stacks and send as few packets as possible?
|
||
|
copy.count = toDecr
|
||
|
sendPacket(ItemStackPacket(copy, ipAddress, packet.source))
|
||
|
}
|
||
|
|
||
|
// dump any remaining drops into the network
|
||
|
for (droppedStack in drops) {
|
||
|
if (droppedStack.isEmpty) continue
|
||
|
sendPacket(ItemStackPacket(droppedStack, ipAddress, IPAddress.BROADCAST))
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class MinerInvProxy(val miner: MinerBlockEntity): GroupedItemInvView {
|
||
|
private var cachedState: BlockState? = null
|
||
|
private var cachedDrops: List<ItemStack>? = null
|
||
|
|
||
|
private val world: World
|
||
|
get() = miner.world!!
|
||
|
private val pos: BlockPos
|
||
|
get() = miner.pos!!
|
||
|
private val facing: Direction
|
||
|
get() = miner.facing
|
||
|
|
||
|
fun getDrops(recalculate: Boolean = false): List<ItemStack> {
|
||
|
val targetPos = pos.offset(facing)
|
||
|
val realState = world.getBlockState(targetPos)
|
||
|
// todo: does BlockState.equals actually work?
|
||
|
if (cachedDrops == null || realState != cachedState || recalculate) {
|
||
|
cachedState = realState
|
||
|
val be = if (realState.block.hasBlockEntity()) world.getBlockEntity(targetPos) else null
|
||
|
cachedDrops = Block.getDroppedStacks(realState, world as ServerWorld, targetPos, be)
|
||
|
}
|
||
|
return cachedDrops!!
|
||
|
}
|
||
|
|
||
|
override fun getStoredStacks(): Set<ItemStack> {
|
||
|
return getDrops().toSet()
|
||
|
}
|
||
|
|
||
|
override fun getTotalCapacity(): Int {
|
||
|
return Int.MAX_VALUE
|
||
|
}
|
||
|
|
||
|
override fun getStatistics(filter: ItemFilter): GroupedItemInvView.ItemInvStatistic {
|
||
|
var totalCount = 0
|
||
|
for (s in getDrops()) {
|
||
|
if (filter.matches(s)) {
|
||
|
totalCount += s.count
|
||
|
}
|
||
|
}
|
||
|
return GroupedItemInvView.ItemInvStatistic(filter, totalCount, 0, Int.MAX_VALUE)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|