From a7d202b1fe3109aaaca025472487a9c3554f8f8f Mon Sep 17 00:00:00 2001 From: raoulvdberge Date: Wed, 27 Dec 2023 21:17:00 +0100 Subject: [PATCH] refactor: decouple storage state from multistorage We will need it for the portable grid soon, which is not a mulistorage. --- .../MultiStorageInternalStorage.java | 32 ++++--------- .../multistorage/MultiStorageNetworkNode.java | 19 ++++---- .../MultiStorageStorageState.java | 9 ---- .../MultiStorageNetworkNodeTest.java | 25 +++++----- .../AbstractDiskDriveBlockEntity.java | 10 ++-- .../AbstractDiskDriveBlockEntityRenderer.java | 6 +-- .../storage/diskdrive/DiskDriveDisk.java | 4 +- .../diskdrive/DiskDriveBakedModel.java | 4 +- .../diskdrive/DiskDriveBakedModel.java | 4 +- .../api/storage/StorageState.java | 48 +++++++++++++++++++ 10 files changed, 90 insertions(+), 71 deletions(-) delete mode 100644 refinedstorage2-network/src/main/java/com/refinedmods/refinedstorage2/api/network/impl/node/multistorage/MultiStorageStorageState.java create mode 100644 refinedstorage2-storage-api/src/main/java/com/refinedmods/refinedstorage2/api/storage/StorageState.java diff --git a/refinedstorage2-network/src/main/java/com/refinedmods/refinedstorage2/api/network/impl/node/multistorage/MultiStorageInternalStorage.java b/refinedstorage2-network/src/main/java/com/refinedmods/refinedstorage2/api/network/impl/node/multistorage/MultiStorageInternalStorage.java index c6e5a8e26..86d268879 100644 --- a/refinedstorage2-network/src/main/java/com/refinedmods/refinedstorage2/api/network/impl/node/multistorage/MultiStorageInternalStorage.java +++ b/refinedstorage2-network/src/main/java/com/refinedmods/refinedstorage2/api/network/impl/node/multistorage/MultiStorageInternalStorage.java @@ -4,8 +4,8 @@ import com.refinedmods.refinedstorage2.api.resource.ResourceAmount; import com.refinedmods.refinedstorage2.api.storage.Actor; import com.refinedmods.refinedstorage2.api.storage.Storage; +import com.refinedmods.refinedstorage2.api.storage.StorageState; import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannelType; -import com.refinedmods.refinedstorage2.api.storage.limited.LimitedStorage; import com.refinedmods.refinedstorage2.api.storage.tracked.TrackedResource; import com.refinedmods.refinedstorage2.api.storage.tracked.TrackedStorage; @@ -14,13 +14,11 @@ import javax.annotation.Nullable; public class MultiStorageInternalStorage implements TrackedStorage { - private static final double NEAR_CAPACITY_THRESHOLD = .75; - private final Storage delegate; private final StorageChannelType storageChannelType; @Nullable private final MultiStorageListener listener; - private MultiStorageStorageState state; + private StorageState state; public MultiStorageInternalStorage(final Storage delegate, final StorageChannelType storageChannelType, @@ -28,33 +26,19 @@ public MultiStorageInternalStorage(final Storage delegate, this.delegate = delegate; this.storageChannelType = storageChannelType; this.listener = listener; - this.state = computeState(); + this.state = getState(); } - public StorageChannelType getStorageChannelType() { - return storageChannelType; + StorageState getState() { + return StorageState.compute(delegate); } - public MultiStorageStorageState computeState() { - if (delegate instanceof LimitedStorage limitedStorage) { - return computeState(limitedStorage.getCapacity()); - } - return MultiStorageStorageState.NORMAL; - } - - private MultiStorageStorageState computeState(final long capacity) { - final double fullness = (double) delegate.getStored() / capacity; - if (fullness >= 1D) { - return MultiStorageStorageState.FULL; - } else if (fullness >= NEAR_CAPACITY_THRESHOLD) { - return MultiStorageStorageState.NEAR_CAPACITY; - } else { - return MultiStorageStorageState.NORMAL; - } + public StorageChannelType getStorageChannelType() { + return storageChannelType; } private void checkStateChanged() { - final MultiStorageStorageState currentState = computeState(); + final StorageState currentState = getState(); if (state != currentState) { this.state = currentState; notifyListener(); diff --git a/refinedstorage2-network/src/main/java/com/refinedmods/refinedstorage2/api/network/impl/node/multistorage/MultiStorageNetworkNode.java b/refinedstorage2-network/src/main/java/com/refinedmods/refinedstorage2/api/network/impl/node/multistorage/MultiStorageNetworkNode.java index fd767b6bf..2b333126e 100644 --- a/refinedstorage2-network/src/main/java/com/refinedmods/refinedstorage2/api/network/impl/node/multistorage/MultiStorageNetworkNode.java +++ b/refinedstorage2-network/src/main/java/com/refinedmods/refinedstorage2/api/network/impl/node/multistorage/MultiStorageNetworkNode.java @@ -3,6 +3,7 @@ import com.refinedmods.refinedstorage2.api.network.component.StorageProvider; import com.refinedmods.refinedstorage2.api.network.node.AbstractStorageNetworkNode; import com.refinedmods.refinedstorage2.api.storage.Storage; +import com.refinedmods.refinedstorage2.api.storage.StorageState; import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannelType; import java.util.ArrayList; @@ -169,17 +170,15 @@ public int getSize() { return cache.length; } - public MultiStorageStorageState getState(final int index) { - return computeState(cache[index]); - } - - private MultiStorageStorageState computeState(@Nullable final MultiStorageInternalStorage internalStorage) { - if (internalStorage == null) { - return MultiStorageStorageState.NONE; - } else if (!isActive()) { - return MultiStorageStorageState.INACTIVE; + public StorageState getState(final int index) { + final var storage = cache[index]; + if (storage == null) { + return StorageState.NONE; + } + if (!isActive()) { + return StorageState.INACTIVE; } - return internalStorage.computeState(); + return storage.getState(); } @Override diff --git a/refinedstorage2-network/src/main/java/com/refinedmods/refinedstorage2/api/network/impl/node/multistorage/MultiStorageStorageState.java b/refinedstorage2-network/src/main/java/com/refinedmods/refinedstorage2/api/network/impl/node/multistorage/MultiStorageStorageState.java deleted file mode 100644 index 2eb2532c1..000000000 --- a/refinedstorage2-network/src/main/java/com/refinedmods/refinedstorage2/api/network/impl/node/multistorage/MultiStorageStorageState.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.refinedmods.refinedstorage2.api.network.impl.node.multistorage; - -public enum MultiStorageStorageState { - NONE, - INACTIVE, - NORMAL, - NEAR_CAPACITY, - FULL -} diff --git a/refinedstorage2-network/src/test/java/com/refinedmods/refinedstorage2/api/network/impl/node/multistorage/MultiStorageNetworkNodeTest.java b/refinedstorage2-network/src/test/java/com/refinedmods/refinedstorage2/api/network/impl/node/multistorage/MultiStorageNetworkNodeTest.java index 188cfafef..9001b2608 100644 --- a/refinedstorage2-network/src/test/java/com/refinedmods/refinedstorage2/api/network/impl/node/multistorage/MultiStorageNetworkNodeTest.java +++ b/refinedstorage2-network/src/test/java/com/refinedmods/refinedstorage2/api/network/impl/node/multistorage/MultiStorageNetworkNodeTest.java @@ -8,6 +8,7 @@ import com.refinedmods.refinedstorage2.api.storage.EmptyActor; import com.refinedmods.refinedstorage2.api.storage.InMemoryStorageImpl; import com.refinedmods.refinedstorage2.api.storage.Storage; +import com.refinedmods.refinedstorage2.api.storage.StorageState; import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannel; import com.refinedmods.refinedstorage2.api.storage.limited.LimitedStorageImpl; import com.refinedmods.refinedstorage2.api.storage.tracked.TrackedStorageImpl; @@ -124,7 +125,7 @@ void testInitialState(@InjectNetworkStorageChannel final StorageChannel assertThat(networkStorage.getStored()).isZero(); assertThat(sut.getSize()).isEqualTo(9); for (int i = 0; i < 9; ++i) { - assertThat(sut.getState(i)).isEqualTo(MultiStorageStorageState.NONE); + assertThat(sut.getState(i)).isEqualTo(StorageState.NONE); } } @@ -153,18 +154,14 @@ void testState(final boolean active) { sut.setActive(active); // Assert - assertThat(sut.getState(0)).isEqualTo(MultiStorageStorageState.NONE); - assertThat(sut.getState(1)).isEqualTo(MultiStorageStorageState.NONE); - assertThat(sut.getState(2)).isEqualTo( - active ? MultiStorageStorageState.NORMAL : MultiStorageStorageState.INACTIVE); - assertThat(sut.getState(3)).isEqualTo( - active ? MultiStorageStorageState.NORMAL : MultiStorageStorageState.INACTIVE); - assertThat(sut.getState(4)).isEqualTo(MultiStorageStorageState.NONE); - assertThat(sut.getState(5)).isEqualTo( - active ? MultiStorageStorageState.NEAR_CAPACITY : MultiStorageStorageState.INACTIVE); - assertThat(sut.getState(6)).isEqualTo(MultiStorageStorageState.NONE); - assertThat(sut.getState(7)).isEqualTo( - active ? MultiStorageStorageState.FULL : MultiStorageStorageState.INACTIVE); + assertThat(sut.getState(0)).isEqualTo(StorageState.NONE); + assertThat(sut.getState(1)).isEqualTo(StorageState.NONE); + assertThat(sut.getState(2)).isEqualTo(active ? StorageState.NORMAL : StorageState.INACTIVE); + assertThat(sut.getState(3)).isEqualTo(active ? StorageState.NORMAL : StorageState.INACTIVE); + assertThat(sut.getState(4)).isEqualTo(StorageState.NONE); + assertThat(sut.getState(5)).isEqualTo(active ? StorageState.NEAR_CAPACITY : StorageState.INACTIVE); + assertThat(sut.getState(6)).isEqualTo(StorageState.NONE); + assertThat(sut.getState(7)).isEqualTo(active ? StorageState.FULL : StorageState.INACTIVE); assertThat(sut.getEnergyUsage()).isEqualTo(BASE_USAGE + (USAGE_PER_STORAGE * 4)); } @@ -246,7 +243,7 @@ void shouldNotDetectStorageChangeInInvalidIndex() { // Assert assertThat(sut.getSize()).isEqualTo(9); for (int i = 0; i < 9; ++i) { - assertThat(sut.getState(i)).isEqualTo(MultiStorageStorageState.NONE); + assertThat(sut.getState(i)).isEqualTo(StorageState.NONE); } } diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/diskdrive/AbstractDiskDriveBlockEntity.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/diskdrive/AbstractDiskDriveBlockEntity.java index b5b8ecdd3..a950a395c 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/diskdrive/AbstractDiskDriveBlockEntity.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/diskdrive/AbstractDiskDriveBlockEntity.java @@ -2,7 +2,7 @@ import com.refinedmods.refinedstorage2.api.network.impl.node.multistorage.MultiStorageListener; import com.refinedmods.refinedstorage2.api.network.impl.node.multistorage.MultiStorageNetworkNode; -import com.refinedmods.refinedstorage2.api.network.impl.node.multistorage.MultiStorageStorageState; +import com.refinedmods.refinedstorage2.api.storage.StorageState; import com.refinedmods.refinedstorage2.platform.api.PlatformApi; import com.refinedmods.refinedstorage2.platform.common.Platform; import com.refinedmods.refinedstorage2.platform.common.content.BlockEntities; @@ -220,16 +220,16 @@ private void fromClientTag(final CompoundTag tag) { final CompoundTag diskTag = disksList.getCompound(i); disks[i] = BuiltInRegistries.ITEM.getHolder(diskTag.getInt(TAG_DISK_ITEM_ID)) .map(item -> new DiskDriveDisk(item.value(), getState(diskTag))) - .orElse(new DiskDriveDisk(null, MultiStorageStorageState.NONE)); + .orElse(new DiskDriveDisk(null, StorageState.NONE)); } onDriveStateUpdated(); } - private MultiStorageStorageState getState(final CompoundTag tag) { + private StorageState getState(final CompoundTag tag) { final int stateOrdinal = tag.getByte(TAG_DISK_STATE); - final MultiStorageStorageState[] values = MultiStorageStorageState.values(); + final StorageState[] values = StorageState.values(); if (stateOrdinal < 0 || stateOrdinal >= values.length) { - return MultiStorageStorageState.NONE; + return StorageState.NONE; } return values[stateOrdinal]; } diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/diskdrive/AbstractDiskDriveBlockEntityRenderer.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/diskdrive/AbstractDiskDriveBlockEntityRenderer.java index d639de307..e6c490d96 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/diskdrive/AbstractDiskDriveBlockEntityRenderer.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/diskdrive/AbstractDiskDriveBlockEntityRenderer.java @@ -1,6 +1,6 @@ package com.refinedmods.refinedstorage2.platform.common.storage.diskdrive; -import com.refinedmods.refinedstorage2.api.network.impl.node.multistorage.MultiStorageStorageState; +import com.refinedmods.refinedstorage2.api.storage.StorageState; import com.refinedmods.refinedstorage2.platform.common.support.direction.BiDirection; import com.refinedmods.refinedstorage2.platform.common.support.render.CubeBuilder; @@ -94,7 +94,7 @@ private void renderDisk(final PoseStack poseStack, final int y, final int x, final DiskDriveDisk disk) { - if (disk.state() == MultiStorageStorageState.NONE) { + if (disk.state() == StorageState.NONE) { return; } final int color = getColor(disk.state()); @@ -119,7 +119,7 @@ private void renderDisk(final PoseStack poseStack, ); } - private int getColor(final MultiStorageStorageState state) { + private int getColor(final StorageState state) { return switch (state) { case NONE -> 0; case INACTIVE -> 0x323232; diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/diskdrive/DiskDriveDisk.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/diskdrive/DiskDriveDisk.java index 1e7759a20..905d8816e 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/diskdrive/DiskDriveDisk.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/diskdrive/DiskDriveDisk.java @@ -1,10 +1,10 @@ package com.refinedmods.refinedstorage2.platform.common.storage.diskdrive; -import com.refinedmods.refinedstorage2.api.network.impl.node.multistorage.MultiStorageStorageState; +import com.refinedmods.refinedstorage2.api.storage.StorageState; import javax.annotation.Nullable; import net.minecraft.world.item.Item; -public record DiskDriveDisk(@Nullable Item item, MultiStorageStorageState state) { +public record DiskDriveDisk(@Nullable Item item, StorageState state) { } diff --git a/refinedstorage2-platform-fabric/src/main/java/com/refinedmods/refinedstorage2/platform/fabric/storage/diskdrive/DiskDriveBakedModel.java b/refinedstorage2-platform-fabric/src/main/java/com/refinedmods/refinedstorage2/platform/fabric/storage/diskdrive/DiskDriveBakedModel.java index b6d307d31..2d759cc12 100644 --- a/refinedstorage2-platform-fabric/src/main/java/com/refinedmods/refinedstorage2/platform/fabric/storage/diskdrive/DiskDriveBakedModel.java +++ b/refinedstorage2-platform-fabric/src/main/java/com/refinedmods/refinedstorage2/platform/fabric/storage/diskdrive/DiskDriveBakedModel.java @@ -1,6 +1,6 @@ package com.refinedmods.refinedstorage2.platform.fabric.storage.diskdrive; -import com.refinedmods.refinedstorage2.api.network.impl.node.multistorage.MultiStorageStorageState; +import com.refinedmods.refinedstorage2.api.storage.StorageState; import com.refinedmods.refinedstorage2.platform.common.storage.diskdrive.AbstractDiskDriveBlockEntity; import com.refinedmods.refinedstorage2.platform.common.storage.diskdrive.DiskDriveBlock; import com.refinedmods.refinedstorage2.platform.common.storage.diskdrive.DiskDriveDisk; @@ -126,7 +126,7 @@ private void emitDiskQuads(final BlockAndTintGetter blockView, final RenderContext context, final DiskDriveDisk disk, final int index) { - if (disk.state() == MultiStorageStorageState.NONE) { + if (disk.state() == StorageState.NONE) { return; } final BakedModel model = diskModels.get(disk.item()); diff --git a/refinedstorage2-platform-forge/src/main/java/com/refinedmods/refinedstorage2/platform/forge/storage/diskdrive/DiskDriveBakedModel.java b/refinedstorage2-platform-forge/src/main/java/com/refinedmods/refinedstorage2/platform/forge/storage/diskdrive/DiskDriveBakedModel.java index 8f9b5d84e..8b6cf62aa 100644 --- a/refinedstorage2-platform-forge/src/main/java/com/refinedmods/refinedstorage2/platform/forge/storage/diskdrive/DiskDriveBakedModel.java +++ b/refinedstorage2-platform-forge/src/main/java/com/refinedmods/refinedstorage2/platform/forge/storage/diskdrive/DiskDriveBakedModel.java @@ -1,6 +1,6 @@ package com.refinedmods.refinedstorage2.platform.forge.storage.diskdrive; -import com.refinedmods.refinedstorage2.api.network.impl.node.multistorage.MultiStorageStorageState; +import com.refinedmods.refinedstorage2.api.storage.StorageState; import com.refinedmods.refinedstorage2.platform.common.storage.diskdrive.AbstractDiskDriveBlockEntity; import com.refinedmods.refinedstorage2.platform.common.storage.diskdrive.DiskDriveBlock; import com.refinedmods.refinedstorage2.platform.common.storage.diskdrive.DiskDriveDisk; @@ -195,7 +195,7 @@ public List load(final DiskDriveStateCacheKey key) { private List getDiskQuads(final DiskDriveStateCacheKey key, final DiskDriveDisk disk, final int index) { - if (disk.state() == MultiStorageStorageState.NONE) { + if (disk.state() == StorageState.NONE) { return Collections.emptyList(); } final var diskModelBakery = diskModelBakeries.get(disk.item()); diff --git a/refinedstorage2-storage-api/src/main/java/com/refinedmods/refinedstorage2/api/storage/StorageState.java b/refinedstorage2-storage-api/src/main/java/com/refinedmods/refinedstorage2/api/storage/StorageState.java new file mode 100644 index 000000000..e0fb7627b --- /dev/null +++ b/refinedstorage2-storage-api/src/main/java/com/refinedmods/refinedstorage2/api/storage/StorageState.java @@ -0,0 +1,48 @@ +package com.refinedmods.refinedstorage2.api.storage; + +import com.refinedmods.refinedstorage2.api.storage.limited.LimitedStorage; + +import org.apiguardian.api.API; + +@API(status = API.Status.STABLE, since = "2.0.0-milestone.3.3") +public enum StorageState { + /** + * There is no storage in the container. + */ + NONE, + /** + * There is a storage present in the container, but the container is inactive. + */ + INACTIVE, + /** + * The storage is active and has enough capacity to store more resources. + */ + NORMAL, + /** + * The storage is active and has less than 25% capacity left. + */ + NEAR_CAPACITY, + /** + * The storage is active and full. + */ + FULL; + + private static final double NEAR_CAPACITY_THRESHOLD = .75; + + public static StorageState compute(final Storage storage) { + if (storage instanceof LimitedStorage limitedStorage) { + return compute(limitedStorage.getCapacity(), storage.getStored()); + } + return StorageState.NORMAL; + } + + private static StorageState compute(final long capacity, final long stored) { + final double fullness = stored / (double) capacity; + if (fullness >= 1D) { + return FULL; + } else if (fullness >= NEAR_CAPACITY_THRESHOLD) { + return NEAR_CAPACITY; + } + return NORMAL; + } +}