PhysicalConnectivity/src/main/kotlin/net/shadowfacts/phycon/network/block/extractor/ExtractorBlockEntity.kt

101 lines
3.1 KiB
Kotlin
Raw Normal View History

2021-02-15 01:01:33 +00:00
package net.shadowfacts.phycon.network.block.extractor
import alexiil.mc.lib.attributes.SearchOptions
import alexiil.mc.lib.attributes.item.GroupedItemInv
import alexiil.mc.lib.attributes.item.ItemAttributes
2021-02-24 03:05:05 +00:00
import net.minecraft.block.BlockState
import net.minecraft.nbt.CompoundTag
2021-02-15 01:01:33 +00:00
import net.minecraft.util.math.Direction
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
2021-02-24 03:05:05 +00:00
import net.shadowfacts.phycon.network.component.ActivationController
2021-02-15 01:01:33 +00:00
import net.shadowfacts.phycon.network.packet.CapacityPacket
import net.shadowfacts.phycon.network.packet.CheckCapacityPacket
import net.shadowfacts.phycon.network.packet.ItemStackPacket
2021-02-24 03:05:05 +00:00
import net.shadowfacts.phycon.network.packet.RemoteActivationPacket
import net.shadowfacts.phycon.util.ActivationMode
2021-02-15 01:01:33 +00:00
/**
* @author shadowfacts
*/
2021-02-24 03:05:05 +00:00
class ExtractorBlockEntity: DeviceBlockEntity(PhyBlockEntities.EXTRACTOR),
ActivationController.ActivatableDevice {
companion object {
val SLEEP_TIME = 40L
}
2021-02-15 01:01:33 +00:00
private val facing: Direction
get() = cachedState[ExtractorBlock.FACING]
private var inventory: GroupedItemInv? = null
private var shouldExtract = false
2021-02-24 03:05:05 +00:00
override val controller = ActivationController(SLEEP_TIME, this)
2021-02-15 01:01:33 +00:00
fun updateInventory() {
val offsetPos = pos.offset(facing)
val option = SearchOptions.inDirection(facing)
inventory = ItemAttributes.GROUPED_INV.getFirstOrNull(world, offsetPos, option)
}
private fun getInventory(): GroupedItemInv? {
if (inventory == null) updateInventory()
return inventory
}
override fun handle(packet: Packet) {
2021-02-24 03:05:05 +00:00
when (packet) {
is CapacityPacket -> handleCapacity(packet)
is RemoteActivationPacket -> controller.handleRemoteActivation(packet)
}
}
private fun handleCapacity(packet: CapacityPacket) {
if (shouldExtract && packet.capacity > 0) {
2021-02-15 01:01:33 +00:00
getInventory()?.also { inv ->
shouldExtract = false
val extracted = inv.extract(packet.stack, packet.capacity)
sendPacket(ItemStackPacket(extracted, ipAddress, packet.stackReceiver.ipAddress))
}
}
}
override fun tick() {
super.tick()
2021-02-24 03:05:05 +00:00
if (!world!!.isClient) {
controller.tick()
2021-02-15 01:01:33 +00:00
}
}
2021-02-24 03:05:05 +00:00
override fun activate(): Boolean {
val inventory = getInventory() ?: return false
val stack = inventory.storedStacks.firstOrNull() ?: return false
shouldExtract = true
sendPacket(CheckCapacityPacket(stack, ipAddress, IPAddress.BROADCAST))
return true
}
override fun toTag(tag: CompoundTag): CompoundTag {
tag.putString("ActivationMode", controller.activationMode.name)
return super.toTag(tag)
}
override fun fromTag(state: BlockState, tag: CompoundTag) {
super.fromTag(state, tag)
controller.activationMode = ActivationMode.valueOf(tag.getString("ActivationMode"))
}
override fun toClientTag(tag: CompoundTag): CompoundTag {
tag.putString("ActivationMode", controller.activationMode.name)
return super.toClientTag(tag)
}
override fun fromClientTag(tag: CompoundTag) {
super.fromClientTag(tag)
controller.activationMode = ActivationMode.valueOf(tag.getString("ActivationMode"))
}
}