-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: decouple storage state from multistorage
We will need it for the portable grid soon, which is not a mulistorage.
- Loading branch information
1 parent
b372340
commit abff619
Showing
10 changed files
with
92 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 0 additions & 9 deletions
9
...inedmods/refinedstorage2/api/network/impl/node/multistorage/MultiStorageStorageState.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<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); | ||
} | ||
} | ||
|
||
|
@@ -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 GitHub Actions / JUnit Test ReportMultiStorageNetworkNodeTest.[1] true
Raw output
|
||
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); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...java/com/refinedmods/refinedstorage2/platform/common/storage/diskdrive/DiskDriveDisk.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...2-storage-api/src/main/java/com/refinedmods/refinedstorage2/api/storage/StorageState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |