Split powered component in three objects
This commit is contained in:
parent
b8b4be30e8
commit
474516506a
@ -30,9 +30,7 @@ import ru.dbotthepony.mc.otm.matter.MatterDataKt;
|
|||||||
import ru.dbotthepony.mc.otm.matter.MatterRegistryKt;
|
import ru.dbotthepony.mc.otm.matter.MatterRegistryKt;
|
||||||
import ru.dbotthepony.mc.otm.network.MatteryNetworking;
|
import ru.dbotthepony.mc.otm.network.MatteryNetworking;
|
||||||
import ru.dbotthepony.mc.otm.registry.*;
|
import ru.dbotthepony.mc.otm.registry.*;
|
||||||
import ru.dbotthepony.mc.otm.storage.ItemStackWrapper;
|
import ru.dbotthepony.mc.otm.storage.*;
|
||||||
import ru.dbotthepony.mc.otm.storage.StorageRegistry;
|
|
||||||
import ru.dbotthepony.mc.otm.storage.StorageStackType;
|
|
||||||
|
|
||||||
import javax.annotation.ParametersAreNonnullByDefault;
|
import javax.annotation.ParametersAreNonnullByDefault;
|
||||||
|
|
||||||
|
@ -24,10 +24,7 @@ import ru.dbotthepony.mc.otm.menu.DriveRackMenu
|
|||||||
import ru.dbotthepony.mc.otm.set
|
import ru.dbotthepony.mc.otm.set
|
||||||
import ru.dbotthepony.mc.otm.graph.storage.StorageNetworkGraph
|
import ru.dbotthepony.mc.otm.graph.storage.StorageNetworkGraph
|
||||||
import ru.dbotthepony.mc.otm.registry.MBlockEntities
|
import ru.dbotthepony.mc.otm.registry.MBlockEntities
|
||||||
import ru.dbotthepony.mc.otm.storage.IStorageStack
|
import ru.dbotthepony.mc.otm.storage.*
|
||||||
import ru.dbotthepony.mc.otm.storage.ItemStackWrapper
|
|
||||||
import ru.dbotthepony.mc.otm.storage.PoweredVirtualComponent
|
|
||||||
import ru.dbotthepony.mc.otm.storage.StorageStackType
|
|
||||||
|
|
||||||
class DriveRackBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) :
|
class DriveRackBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) :
|
||||||
MatteryPoweredBlockEntity(MBlockEntities.DRIVE_RACK, p_155229_, p_155230_) {
|
MatteryPoweredBlockEntity(MBlockEntities.DRIVE_RACK, p_155229_, p_155230_) {
|
||||||
|
@ -35,7 +35,7 @@ class DriveViewerMenu @JvmOverloads constructor(
|
|||||||
override fun getNetworkedItemView() = view
|
override fun getNetworkedItemView() = view
|
||||||
|
|
||||||
init {
|
init {
|
||||||
val container = if (tile != null) tile.container else SimpleContainer(1)
|
val container = tile?.container ?: SimpleContainer(1)
|
||||||
|
|
||||||
driveSlot = object : MatterySlot(container, 0) {
|
driveSlot = object : MatterySlot(container, 0) {
|
||||||
override fun mayPlace(stack: ItemStack): Boolean {
|
override fun mayPlace(stack: ItemStack): Boolean {
|
||||||
|
@ -196,3 +196,12 @@ interface IStorageTuple<T : IStorageStack> {
|
|||||||
|
|
||||||
class StorageTuple<T : IStorageStack>(override val id: UUID, override val stack: T) : IStorageTuple<T>
|
class StorageTuple<T : IStorageStack>(override val id: UUID, override val stack: T) : IStorageTuple<T>
|
||||||
interface IStorageComponent<T : IStorageStack> : IStorageView<T>, IStorageConsumer<T>
|
interface IStorageComponent<T : IStorageStack> : IStorageView<T>, IStorageConsumer<T>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Component which (most time) proxy other components (combine their contents into single view)
|
||||||
|
*/
|
||||||
|
interface IVirtualStorageComponent<T : IStorageStack> : IStorageComponent<T>, IStorageListener<T> {
|
||||||
|
fun add(identity: IStorage<T>)
|
||||||
|
fun remove(identity: IStorage<T>)
|
||||||
|
fun contains(identity: IStorage<T>): Boolean
|
||||||
|
}
|
||||||
|
@ -28,7 +28,7 @@ class RemoteTuple<T : IStorageStack>(val obj: T, val remote_id: UUID, val provid
|
|||||||
|
|
||||||
class LocalTuple<T : IStorageStack>(override val stack: T, override val id: UUID, val tuples: ArrayList<RemoteTuple<T>>) : IStorageTuple<T>
|
class LocalTuple<T : IStorageStack>(override val stack: T, override val id: UUID, val tuples: ArrayList<RemoteTuple<T>>) : IStorageTuple<T>
|
||||||
|
|
||||||
open class VirtualComponent<T : IStorageStack>(type: StorageStackType<T>) : IStorageComponent<T>, IStorageListener<T> {
|
open class VirtualComponent<T : IStorageStack>(type: StorageStackType<T>) : IVirtualStorageComponent<T> {
|
||||||
constructor(type: Class<T>) : this(StorageRegistry.get(type))
|
constructor(type: Class<T>) : this(StorageRegistry.get(type))
|
||||||
|
|
||||||
override val storageType: StorageStackType<T> = type
|
override val storageType: StorageStackType<T> = type
|
||||||
@ -55,7 +55,7 @@ open class VirtualComponent<T : IStorageStack>(type: StorageStackType<T>) : ISto
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun add(identity: IStorage<T>) {
|
override fun add(identity: IStorage<T>) {
|
||||||
if (set.add(identity)) {
|
if (set.add(identity)) {
|
||||||
if (identity is IStorageView<T>) {
|
if (identity is IStorageView<T>) {
|
||||||
identity.addListenerAuto(this)
|
identity.addListenerAuto(this)
|
||||||
@ -71,7 +71,7 @@ open class VirtualComponent<T : IStorageStack>(type: StorageStackType<T>) : ISto
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun remove(identity: IStorage<T>) {
|
override fun remove(identity: IStorage<T>) {
|
||||||
if (set.remove(identity)) {
|
if (set.remove(identity)) {
|
||||||
if (identity is IStorageView<T>) {
|
if (identity is IStorageView<T>) {
|
||||||
identity.removeListenerAuto(this)
|
identity.removeListenerAuto(this)
|
||||||
@ -87,7 +87,7 @@ open class VirtualComponent<T : IStorageStack>(type: StorageStackType<T>) : ISto
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun contains(identity: IStorage<T>): Boolean {
|
override fun contains(identity: IStorage<T>): Boolean {
|
||||||
return set.contains(identity)
|
return set.contains(identity)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -245,12 +245,15 @@ open class VirtualComponent<T : IStorageStack>(type: StorageStackType<T>) : ISto
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
open class PoweredVirtualComponent<T : IStorageStack>(type: StorageStackType<T>, val energyProvider: () -> IMatteryEnergyStorage) : VirtualComponent<T>(type) {
|
/**
|
||||||
constructor(type: Class<T>, energyStorage: IMatteryEnergyStorage) : this(StorageRegistry.get(type), {energyStorage})
|
* Adds energy demand to operations over [parent]
|
||||||
constructor(type: StorageStackType<T>, energyStorage: IMatteryEnergyStorage) : this(type, {energyStorage})
|
*/
|
||||||
constructor(other: IStorage<T>, energyStorage: IMatteryEnergyStorage) : this(other.storageType.identity, energyStorage) {
|
open class PoweredComponent<T : IStorageStack>(open val parent: IStorageComponent<T>, val energyProvider: () -> IMatteryEnergyStorage) : IStorageComponent<T> {
|
||||||
add(other)
|
override val storageType: StorageStackType<T>
|
||||||
}
|
get() = parent.storageType
|
||||||
|
|
||||||
|
override fun addListener(listener: IStorageListener<T>) = parent.addListener(listener)
|
||||||
|
override fun removeListener(listener: IStorageListener<T>) = parent.removeListener(listener)
|
||||||
|
|
||||||
@Suppress("unchecked_cast")
|
@Suppress("unchecked_cast")
|
||||||
override fun insertStack(stack: T, simulate: Boolean): T {
|
override fun insertStack(stack: T, simulate: Boolean): T {
|
||||||
@ -263,7 +266,7 @@ open class PoweredVirtualComponent<T : IStorageStack>(type: StorageStackType<T>,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (extracted == required) {
|
if (extracted == required) {
|
||||||
val leftover = super.insertStack(stack, simulate)
|
val leftover = parent.insertStack(stack, simulate)
|
||||||
|
|
||||||
if (leftover.isEmpty) {
|
if (leftover.isEmpty) {
|
||||||
if (!simulate) {
|
if (!simulate) {
|
||||||
@ -290,7 +293,7 @@ open class PoweredVirtualComponent<T : IStorageStack>(type: StorageStackType<T>,
|
|||||||
val newExtracted = energy.extractEnergyInner(newRequired, true)
|
val newExtracted = energy.extractEnergyInner(newRequired, true)
|
||||||
|
|
||||||
if (newExtracted == newRequired) {
|
if (newExtracted == newRequired) {
|
||||||
val leftover = super.insertStack(stack, simulate)
|
val leftover = parent.insertStack(stack, simulate)
|
||||||
|
|
||||||
if (leftover.isEmpty) {
|
if (leftover.isEmpty) {
|
||||||
if (!simulate) {
|
if (!simulate) {
|
||||||
@ -313,6 +316,8 @@ open class PoweredVirtualComponent<T : IStorageStack>(type: StorageStackType<T>,
|
|||||||
return stack
|
return stack
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun getStack(id: UUID) = parent.getStack(id)
|
||||||
|
|
||||||
override fun extractStack(id: UUID, amount: ImpreciseFraction, simulate: Boolean): T {
|
override fun extractStack(id: UUID, amount: ImpreciseFraction, simulate: Boolean): T {
|
||||||
val required = storageType.energyPerOperation * amount
|
val required = storageType.energyPerOperation * amount
|
||||||
val energy = energyProvider.invoke()
|
val energy = energyProvider.invoke()
|
||||||
@ -323,7 +328,7 @@ open class PoweredVirtualComponent<T : IStorageStack>(type: StorageStackType<T>,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (extracted == required) {
|
if (extracted == required) {
|
||||||
val extractedStack = super.extractStack(id, amount, simulate)
|
val extractedStack = parent.extractStack(id, amount, simulate)
|
||||||
|
|
||||||
if (extractedStack.isEmpty) {
|
if (extractedStack.isEmpty) {
|
||||||
return extractedStack
|
return extractedStack
|
||||||
@ -342,7 +347,7 @@ open class PoweredVirtualComponent<T : IStorageStack>(type: StorageStackType<T>,
|
|||||||
|
|
||||||
@Suppress("name_shadowing")
|
@Suppress("name_shadowing")
|
||||||
val amount = required / storageType.energyPerOperation
|
val amount = required / storageType.energyPerOperation
|
||||||
val extractedStack = super.extractStack(id, amount, simulate)
|
val extractedStack = parent.extractStack(id, amount, simulate)
|
||||||
|
|
||||||
if (extractedStack.isEmpty) {
|
if (extractedStack.isEmpty) {
|
||||||
return extractedStack
|
return extractedStack
|
||||||
@ -354,4 +359,24 @@ open class PoweredVirtualComponent<T : IStorageStack>(type: StorageStackType<T>,
|
|||||||
|
|
||||||
return extractedStack
|
return extractedStack
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun getStacks() = parent.getStacks()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds energy demand to virtual component [parent]
|
||||||
|
*/
|
||||||
|
open class PoweredVirtualComponent<T : IStorageStack>(override val parent: IVirtualStorageComponent<T>, energyProvider: () -> IMatteryEnergyStorage)
|
||||||
|
: PoweredComponent<T>(parent, energyProvider), IVirtualStorageComponent<T> {
|
||||||
|
constructor(parent: IVirtualStorageComponent<T>, energy: IMatteryEnergyStorage) : this(parent, { energy })
|
||||||
|
constructor(parent: Class<T>, energy: IMatteryEnergyStorage) : this(VirtualComponent(parent), { energy })
|
||||||
|
constructor(parent: StorageStackType<T>, energy: IMatteryEnergyStorage) : this(VirtualComponent(parent), { energy })
|
||||||
|
|
||||||
|
override fun addStack(stack: T, id: UUID, provider: IStorageView<T>) = parent.addStack(stack, id, provider)
|
||||||
|
override fun changeStack(stack: T, id: UUID, oldCount: ImpreciseFraction) = parent.changeStack(stack, id, oldCount)
|
||||||
|
override fun removeStack(stack: T, id: UUID) = parent.removeStack(stack, id)
|
||||||
|
|
||||||
|
override fun add(identity: IStorage<T>) = parent.add(identity)
|
||||||
|
override fun remove(identity: IStorage<T>) = parent.remove(identity)
|
||||||
|
override fun contains(identity: IStorage<T>) = parent.contains(identity)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user