Skip to content

Commit

Permalink
refactor: decouple storage state from multistorage
Browse files Browse the repository at this point in the history
We will need it for the portable grid soon, which is not a mulistorage.
  • Loading branch information
raoulvdberge committed Dec 27, 2023
1 parent b372340 commit abff619
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -14,47 +14,27 @@
import javax.annotation.Nullable;

public class MultiStorageInternalStorage<T> implements TrackedStorage<T> {
private static final double NEAR_CAPACITY_THRESHOLD = .75;

private final Storage<T> delegate;
private final StorageChannelType<T> storageChannelType;
@Nullable
private final MultiStorageListener listener;
private MultiStorageStorageState state;
private StorageState state;

public MultiStorageInternalStorage(final Storage<T> delegate,
final StorageChannelType<T> storageChannelType,
@Nullable final MultiStorageListener listener) {
this.delegate = delegate;
this.storageChannelType = storageChannelType;
this.listener = listener;
this.state = computeState();
this.state = StorageState.compute(this);
}

public StorageChannelType<T> getStorageChannelType() {
return storageChannelType;
}

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;
}
}

private void checkStateChanged() {
final MultiStorageStorageState currentState = computeState();
final StorageState currentState = StorageState.compute(this);
if (state != currentState) {
this.state = currentState;
notifyListener();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -169,17 +170,8 @@ 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;
}
return internalStorage.computeState();
public StorageState getState(final int index) {
return StorageState.compute(isActive(), cache[index]);
}

@Override
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -124,7 +125,7 @@ void testInitialState(@InjectNetworkStorageChannel final StorageChannel<String>
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);
}
}

Expand Down Expand Up @@ -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);

Check failure on line 162 in refinedstorage2-network/src/test/java/com/refinedmods/refinedstorage2/api/network/impl/node/multistorage/MultiStorageNetworkNodeTest.java

View workflow job for this annotation

GitHub Actions / JUnit Test Report

MultiStorageNetworkNodeTest.[1] true

org.opentest4j.AssertionFailedError: expected: NEAR_CAPACITY but was: NORMAL
Raw output
org.opentest4j.AssertionFailedError: 
expected: NEAR_CAPACITY
 but was: NORMAL
	at [email protected]/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at [email protected]/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
	at [email protected]/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at [email protected]/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
	at app//com.refinedmods.refinedstorage2.api.network.impl.node.multistorage.MultiStorageNetworkNodeTest.testState(MultiStorageNetworkNodeTest.java:162)
	at [email protected]/java.lang.reflect.Method.invoke(Method.java:568)
	at [email protected]/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
	at [email protected]/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)
	at [email protected]/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:179)
	at [email protected]/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)
	at [email protected]/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
	at [email protected]/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)
	at [email protected]/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)
	at [email protected]/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)
	at [email protected]/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
	at [email protected]/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)
	at [email protected]/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:992)
	at [email protected]/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509)
	at [email protected]/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
	at [email protected]/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
	at [email protected]/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
	at [email protected]/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at [email protected]/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:596)
	at [email protected]/java.util.stream.ReferencePipeline$7$1.accept(ReferencePipeline.java:276)
	at [email protected]/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)
	at [email protected]/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)
	at [email protected]/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)
	at [email protected]/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1625)
	at [email protected]/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509)
	at [email protected]/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
	at [email protected]/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
	at [email protected]/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
	at [email protected]/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at [email protected]/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:596)
	at [email protected]/java.util.stream.ReferencePipeline$7$1.accept(ReferencePipeline.java:276)
	at [email protected]/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1625)
	at [email protected]/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509)
	at [email protected]/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
	at [email protected]/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
	at [email protected]/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
	at [email protected]/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at [email protected]/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:596)
	at [email protected]/java.util.ArrayList.forEach(ArrayList.java:1511)
	at [email protected]/java.util.ArrayList.forEach(ArrayList.java:1511)
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));
}

Expand Down Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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());
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -195,7 +195,7 @@ public List<BakedQuad> load(final DiskDriveStateCacheKey key) {
private List<BakedQuad> 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());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.refinedmods.refinedstorage2.api.storage;

import com.refinedmods.refinedstorage2.api.storage.limited.LimitedStorage;

import javax.annotation.Nullable;

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 <T> StorageState compute(final boolean active, @Nullable final Storage<T> storage) {
if (storage == null) {
return StorageState.NONE;
}
if (!active) {
return StorageState.INACTIVE;
}
return compute(storage);
}

public static <T> StorageState compute(final Storage<T> storage) {
if (storage instanceof LimitedStorage<T> 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;
}
}

0 comments on commit abff619

Please sign in to comment.