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 c406843
Show file tree
Hide file tree
Showing 29 changed files with 109 additions and 92 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.refinedmods.refinedstorage2.api.network.impl.node;

import com.refinedmods.refinedstorage2.api.storage.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 <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;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.refinedmods.refinedstorage2.api.network.impl.node.multistorage;

import com.refinedmods.refinedstorage2.api.core.Action;
import com.refinedmods.refinedstorage2.api.network.impl.node.StorageState;
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.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,31 @@
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 = getState();
}

public StorageChannelType<T> 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<T> getStorageChannelType() {
return storageChannelType;
}

private void checkStateChanged() {
final MultiStorageStorageState currentState = computeState();
final StorageState currentState = getState();
if (state != currentState) {
this.state = currentState;
notifyListener();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.refinedmods.refinedstorage2.api.network.impl.node.multistorage;

import com.refinedmods.refinedstorage2.api.network.component.StorageProvider;
import com.refinedmods.refinedstorage2.api.network.impl.node.StorageState;
import com.refinedmods.refinedstorage2.api.network.node.AbstractStorageNetworkNode;
import com.refinedmods.refinedstorage2.api.storage.Storage;
import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannelType;
Expand Down Expand Up @@ -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
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.refinedmods.refinedstorage2.api.core.Action;
import com.refinedmods.refinedstorage2.api.core.filter.FilterMode;
import com.refinedmods.refinedstorage2.api.network.Network;
import com.refinedmods.refinedstorage2.api.network.impl.node.StorageState;
import com.refinedmods.refinedstorage2.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage2.api.storage.AccessMode;
import com.refinedmods.refinedstorage2.api.storage.EmptyActor;
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);
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
@@ -1,7 +1,6 @@
package com.refinedmods.refinedstorage2.platform.api.storage;

import com.refinedmods.refinedstorage2.api.storage.Storage;
import com.refinedmods.refinedstorage2.api.storage.StorageInfo;
import com.refinedmods.refinedstorage2.api.storage.TypedStorage;
import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannelType;
import com.refinedmods.refinedstorage2.platform.api.PlatformApi;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.refinedmods.refinedstorage2.platform.api.storage;

import com.refinedmods.refinedstorage2.api.storage.StorageInfo;
import com.refinedmods.refinedstorage2.api.storage.TypedStorage;

import java.util.Optional;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.refinedmods.refinedstorage2.platform.api.storage;

import com.refinedmods.refinedstorage2.api.storage.Storage;
import com.refinedmods.refinedstorage2.api.storage.StorageInfo;

import java.util.List;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.refinedmods.refinedstorage2.api.storage;
package com.refinedmods.refinedstorage2.platform.api.storage;

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

import org.apiguardian.api.API;
Expand All @@ -8,10 +9,10 @@
public record StorageInfo(long stored, long capacity) {
public static final StorageInfo UNKNOWN = new StorageInfo(0, 0);

public static StorageInfo of(final Storage<?> storage) {
public static <T> StorageInfo of(final Storage<T> storage) {
return new StorageInfo(
storage.getStored(),
storage instanceof LimitedStorage<?> limitedStorage ? limitedStorage.getCapacity() : 0L
storage instanceof LimitedStorage<T> limitedStorage ? limitedStorage.getCapacity() : 0L
);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.refinedmods.refinedstorage2.platform.api.storage;

import com.refinedmods.refinedstorage2.api.storage.Storage;
import com.refinedmods.refinedstorage2.api.storage.StorageInfo;

import java.util.Optional;
import java.util.UUID;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.refinedmods.refinedstorage2.api.core.CoreValidations;
import com.refinedmods.refinedstorage2.api.storage.Storage;
import com.refinedmods.refinedstorage2.api.storage.StorageInfo;
import com.refinedmods.refinedstorage2.platform.api.storage.StorageInfo;
import com.refinedmods.refinedstorage2.platform.api.storage.StorageRepository;

import java.util.HashMap;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.refinedmods.refinedstorage2.platform.common.storage;

import com.refinedmods.refinedstorage2.api.storage.Storage;
import com.refinedmods.refinedstorage2.api.storage.StorageInfo;
import com.refinedmods.refinedstorage2.platform.api.PlatformApi;
import com.refinedmods.refinedstorage2.platform.api.storage.ItemTransferableStorageBlockEntity;
import com.refinedmods.refinedstorage2.platform.api.storage.StorageContainerItemHelper;
import com.refinedmods.refinedstorage2.platform.api.storage.StorageInfo;
import com.refinedmods.refinedstorage2.platform.api.storage.StorageRepository;

import java.util.Collections;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.refinedmods.refinedstorage2.api.core.CoreValidations;
import com.refinedmods.refinedstorage2.api.storage.Storage;
import com.refinedmods.refinedstorage2.api.storage.StorageInfo;
import com.refinedmods.refinedstorage2.platform.api.storage.SerializableStorage;
import com.refinedmods.refinedstorage2.platform.api.storage.StorageInfo;
import com.refinedmods.refinedstorage2.platform.api.storage.StorageRepository;
import com.refinedmods.refinedstorage2.platform.api.storage.StorageType;
import com.refinedmods.refinedstorage2.platform.api.support.registry.PlatformRegistry;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.refinedmods.refinedstorage2.platform.common.storage.diskdrive;

import com.refinedmods.refinedstorage2.api.network.impl.node.StorageState;
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.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.network.impl.node.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,8 +1,8 @@
package com.refinedmods.refinedstorage2.platform.common.storage.diskdrive;

import com.refinedmods.refinedstorage2.api.storage.StorageInfo;
import com.refinedmods.refinedstorage2.platform.api.PlatformApi;
import com.refinedmods.refinedstorage2.platform.api.storage.StorageContainerItem;
import com.refinedmods.refinedstorage2.platform.api.storage.StorageInfo;
import com.refinedmods.refinedstorage2.platform.api.support.resource.ResourceContainer;
import com.refinedmods.refinedstorage2.platform.common.content.Menus;
import com.refinedmods.refinedstorage2.platform.common.storage.AbstractStorageContainerMenu;
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.network.impl.node.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.common.storage.diskdrive;

import com.refinedmods.refinedstorage2.api.storage.StorageInfo;
import com.refinedmods.refinedstorage2.platform.api.storage.StorageInfo;

import java.util.Optional;

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.storage.StorageInfo;
import com.refinedmods.refinedstorage2.platform.api.storage.StorageInfo;

import java.util.Optional;

Expand Down
Loading

0 comments on commit c406843

Please sign in to comment.