Skip to content

Commit

Permalink
Merge pull request #426 from refinedmods/refactor/GH-88/storage
Browse files Browse the repository at this point in the history
Storage API improvements
  • Loading branch information
raoulvdberge authored Aug 24, 2023
2 parents ad42d6a + 81f983b commit 382ee1e
Show file tree
Hide file tree
Showing 19 changed files with 172 additions and 195 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,16 @@ GridScrollingStrategy createGridScrollingStrategy(AbstractContainerMenu containe

ResourceFactory<ItemResource> getItemResourceFactory();

PlatformStorageChannelType<ItemResource> getItemStorageChannelType();

StorageType<ItemResource> getItemStorageType();

ResourceFactory<FluidResource> getFluidResourceFactory();

PlatformStorageChannelType<FluidResource> getFluidStorageChannelType();

StorageType<FluidResource> getFluidStorageType();

Set<ResourceFactory<?>> getAlternativeResourceFactories();

<T> void registerResourceRendering(Class<T> resourceClass, ResourceRendering<T> rendering);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,31 @@ public ResourceFactory<ItemResource> getItemResourceFactory() {
return ensureLoaded().getItemResourceFactory();
}

@Override
public PlatformStorageChannelType<ItemResource> getItemStorageChannelType() {
return ensureLoaded().getItemStorageChannelType();
}

@Override
public StorageType<ItemResource> getItemStorageType() {
return ensureLoaded().getItemStorageType();
}

@Override
public ResourceFactory<FluidResource> getFluidResourceFactory() {
return ensureLoaded().getFluidResourceFactory();
}

@Override
public PlatformStorageChannelType<FluidResource> getFluidStorageChannelType() {
return ensureLoaded().getFluidStorageChannelType();
}

@Override
public StorageType<FluidResource> getFluidStorageType() {
return ensureLoaded().getFluidStorageType();
}

@Override
public Set<ResourceFactory<?>> getAlternativeResourceFactories() {
return ensureLoaded().getAlternativeResourceFactories();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

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

import javax.annotation.Nullable;

import net.minecraft.nbt.CompoundTag;
import org.apiguardian.api.API;

@API(status = API.Status.STABLE, since = "2.0.0-milestone.1.4")
public interface StorageType<T> {
Storage<T> create(@Nullable Long capacity, Runnable listener);

Storage<T> fromTag(CompoundTag tag, Runnable listener);

CompoundTag toTag(Storage<T> storage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import com.refinedmods.refinedstorage2.platform.common.internal.storage.channel.StorageChannelTypes;
import com.refinedmods.refinedstorage2.platform.common.internal.storage.type.FluidStorageType;
import com.refinedmods.refinedstorage2.platform.common.internal.storage.type.ItemStorageType;
import com.refinedmods.refinedstorage2.platform.common.internal.storage.type.StorageTypes;
import com.refinedmods.refinedstorage2.platform.common.internal.upgrade.UpgradeDestinations;
import com.refinedmods.refinedstorage2.platform.common.item.FluidStorageDiskItem;
import com.refinedmods.refinedstorage2.platform.common.item.FortuneUpgradeItem;
Expand Down Expand Up @@ -150,7 +151,7 @@ protected final void initializePlatformApi() {
private void registerAdditionalStorageTypes() {
PlatformApi.INSTANCE.getStorageTypeRegistry().register(
createIdentifier(FLUID_REGISTRY_KEY),
FluidStorageType.INSTANCE
StorageTypes.FLUID
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
import com.refinedmods.refinedstorage2.platform.common.internal.storage.ClientStorageRepository;
import com.refinedmods.refinedstorage2.platform.common.internal.storage.StorageRepositoryImpl;
import com.refinedmods.refinedstorage2.platform.common.internal.storage.channel.StorageChannelTypes;
import com.refinedmods.refinedstorage2.platform.common.internal.storage.type.ItemStorageType;
import com.refinedmods.refinedstorage2.platform.common.internal.storage.type.StorageTypes;
import com.refinedmods.refinedstorage2.platform.common.internal.upgrade.UpgradeRegistryImpl;
import com.refinedmods.refinedstorage2.platform.common.screen.grid.hint.GridInsertionHintsImpl;
import com.refinedmods.refinedstorage2.platform.common.screen.grid.hint.ItemGridInsertionHint;
Expand Down Expand Up @@ -86,7 +86,7 @@ public class PlatformApiImpl implements PlatformApi {
private final NetworkBuilder networkBuilder =
new NetworkBuilderImpl(new NetworkFactory(networkComponentMapFactory));
private final PlatformRegistry<StorageType<?>> storageTypeRegistry =
new PlatformRegistryImpl<>(createIdentifier(ITEM_REGISTRY_KEY), ItemStorageType.INSTANCE);
new PlatformRegistryImpl<>(createIdentifier(ITEM_REGISTRY_KEY), StorageTypes.ITEM);
private final PlatformRegistry<PlatformStorageChannelType<?>> storageChannelTypeRegistry =
new PlatformRegistryImpl<>(createIdentifier(ITEM_REGISTRY_KEY), StorageChannelTypes.ITEM);
private final PlatformRegistry<GridSynchronizer> gridSynchronizerRegistry =
Expand Down Expand Up @@ -319,11 +319,31 @@ public ResourceFactory<ItemResource> getItemResourceFactory() {
return itemResourceFactory;
}

@Override
public PlatformStorageChannelType<ItemResource> getItemStorageChannelType() {
return StorageChannelTypes.ITEM;
}

@Override
public StorageType<ItemResource> getItemStorageType() {
return StorageTypes.ITEM;
}

@Override
public ResourceFactory<FluidResource> getFluidResourceFactory() {
return fluidResourceFactory;
}

@Override
public PlatformStorageChannelType<FluidResource> getFluidStorageChannelType() {
return StorageChannelTypes.FLUID;
}

@Override
public StorageType<FluidResource> getFluidStorageType() {
return StorageTypes.FLUID;
}

@Override
public Set<ResourceFactory<?>> getAlternativeResourceFactories() {
return resourceFactories;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.refinedmods.refinedstorage2.platform.common.block.entity.FilterWithFuzzyMode;
import com.refinedmods.refinedstorage2.platform.common.block.entity.StorageConfigurationContainerImpl;
import com.refinedmods.refinedstorage2.platform.common.internal.resource.ResourceContainerImpl;
import com.refinedmods.refinedstorage2.platform.common.internal.storage.PlatformStorage;
import com.refinedmods.refinedstorage2.platform.common.menu.ExtendedMenuProvider;

import java.util.Objects;
Expand Down Expand Up @@ -61,7 +60,7 @@ protected AbstractStorageBlockBlockEntity(final BlockEntityType<?> type,
getNode().setNormalizer(filter.createNormalizer());
}

protected abstract PlatformStorage<T> createStorage(Runnable listener);
protected abstract Storage<T> createStorage(Runnable listener);

@Override
@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
package com.refinedmods.refinedstorage2.platform.common.block.entity.storage;

import com.refinedmods.refinedstorage2.api.network.impl.node.storage.StorageNetworkNode;
import com.refinedmods.refinedstorage2.api.storage.InMemoryStorageImpl;
import com.refinedmods.refinedstorage2.api.storage.limited.LimitedStorageImpl;
import com.refinedmods.refinedstorage2.api.storage.tracked.InMemoryTrackedStorageRepository;
import com.refinedmods.refinedstorage2.api.storage.tracked.TrackedStorageImpl;
import com.refinedmods.refinedstorage2.api.storage.tracked.TrackedStorageRepository;
import com.refinedmods.refinedstorage2.api.storage.Storage;
import com.refinedmods.refinedstorage2.platform.api.PlatformApi;
import com.refinedmods.refinedstorage2.platform.api.resource.FluidResource;
import com.refinedmods.refinedstorage2.platform.common.Platform;
import com.refinedmods.refinedstorage2.platform.common.containermenu.storage.block.FluidStorageBlockContainerMenu;
import com.refinedmods.refinedstorage2.platform.common.content.BlockEntities;
import com.refinedmods.refinedstorage2.platform.common.internal.storage.LimitedPlatformStorage;
import com.refinedmods.refinedstorage2.platform.common.internal.storage.PlatformStorage;
import com.refinedmods.refinedstorage2.platform.common.internal.storage.channel.StorageChannelTypes;
import com.refinedmods.refinedstorage2.platform.common.internal.storage.type.FluidStorageType;
import com.refinedmods.refinedstorage2.platform.common.internal.storage.type.StorageTypes;

import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
Expand Down Expand Up @@ -58,21 +53,8 @@ private static long getEnergyUsage(final FluidStorageType.Variant variant) {
}

@Override
protected PlatformStorage<FluidResource> createStorage(final Runnable listener) {
final TrackedStorageRepository<FluidResource> trackingRepository = new InMemoryTrackedStorageRepository<>();
if (!variant.hasCapacity()) {
final TrackedStorageImpl<FluidResource> delegate = new TrackedStorageImpl<>(
new InMemoryStorageImpl<>(),
trackingRepository,
System::currentTimeMillis
);
return new PlatformStorage<>(delegate, FluidStorageType.INSTANCE, trackingRepository, listener);
}
final LimitedStorageImpl<FluidResource> delegate = new LimitedStorageImpl<>(
new TrackedStorageImpl<>(new InMemoryStorageImpl<>(), trackingRepository, System::currentTimeMillis),
variant.getCapacityInBuckets() * Platform.INSTANCE.getBucketAmount()
);
return new LimitedPlatformStorage<>(delegate, FluidStorageType.INSTANCE, trackingRepository, listener);
protected Storage<FluidResource> createStorage(final Runnable listener) {
return StorageTypes.FLUID.create(variant.getCapacity(), listener);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
package com.refinedmods.refinedstorage2.platform.common.block.entity.storage;

import com.refinedmods.refinedstorage2.api.network.impl.node.storage.StorageNetworkNode;
import com.refinedmods.refinedstorage2.api.storage.InMemoryStorageImpl;
import com.refinedmods.refinedstorage2.api.storage.limited.LimitedStorageImpl;
import com.refinedmods.refinedstorage2.api.storage.tracked.InMemoryTrackedStorageRepository;
import com.refinedmods.refinedstorage2.api.storage.tracked.TrackedStorageImpl;
import com.refinedmods.refinedstorage2.api.storage.tracked.TrackedStorageRepository;
import com.refinedmods.refinedstorage2.api.storage.Storage;
import com.refinedmods.refinedstorage2.platform.api.PlatformApi;
import com.refinedmods.refinedstorage2.platform.api.resource.ItemResource;
import com.refinedmods.refinedstorage2.platform.common.Platform;
import com.refinedmods.refinedstorage2.platform.common.containermenu.storage.block.ItemStorageBlockContainerMenu;
import com.refinedmods.refinedstorage2.platform.common.content.BlockEntities;
import com.refinedmods.refinedstorage2.platform.common.internal.storage.LimitedPlatformStorage;
import com.refinedmods.refinedstorage2.platform.common.internal.storage.PlatformStorage;
import com.refinedmods.refinedstorage2.platform.common.internal.storage.channel.StorageChannelTypes;
import com.refinedmods.refinedstorage2.platform.common.internal.storage.type.ItemStorageType;
import com.refinedmods.refinedstorage2.platform.common.internal.storage.type.StorageTypes;

import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
Expand Down Expand Up @@ -54,31 +49,8 @@ private static long getEnergyUsage(final ItemStorageType.Variant variant) {
}

@Override
protected PlatformStorage<ItemResource> createStorage(final Runnable listener) {
final TrackedStorageRepository<ItemResource> trackingRepository = new InMemoryTrackedStorageRepository<>();
if (!variant.hasCapacity()) {
final TrackedStorageImpl<ItemResource> delegate = new TrackedStorageImpl<>(
new InMemoryStorageImpl<>(),
trackingRepository,
System::currentTimeMillis
);
return new PlatformStorage<>(
delegate,
ItemStorageType.INSTANCE,
trackingRepository,
listener
);
}
final LimitedStorageImpl<ItemResource> delegate = new LimitedStorageImpl<>(
new TrackedStorageImpl<>(new InMemoryStorageImpl<>(), trackingRepository, System::currentTimeMillis),
variant.getCapacity()
);
return new LimitedPlatformStorage<>(
delegate,
ItemStorageType.INSTANCE,
trackingRepository,
listener
);
protected Storage<ItemResource> createStorage(final Runnable listener) {
return StorageTypes.ITEM.create(variant.getCapacity(), listener);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,62 +12,69 @@
import com.refinedmods.refinedstorage2.platform.api.resource.FluidResource;
import com.refinedmods.refinedstorage2.platform.api.storage.PlayerActor;
import com.refinedmods.refinedstorage2.platform.api.storage.type.StorageType;
import com.refinedmods.refinedstorage2.platform.common.Platform;
import com.refinedmods.refinedstorage2.platform.common.internal.storage.LimitedPlatformStorage;
import com.refinedmods.refinedstorage2.platform.common.internal.storage.PlatformStorage;

import javax.annotation.Nullable;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;

public class FluidStorageType implements StorageType<FluidResource> {
public static final FluidStorageType INSTANCE = new FluidStorageType();

private static final String TAG_CAPACITY = "cap";
private static final String TAG_STACKS = "stacks";
private static final String TAG_CHANGED_BY = "cb";
private static final String TAG_CHANGED_AT = "ca";

private FluidStorageType() {
FluidStorageType() {
}

@Override
public Storage<FluidResource> create(@Nullable final Long capacity, final Runnable listener) {
return innerCreate(capacity, listener);
}

@Override
public Storage<FluidResource> fromTag(final CompoundTag tag, final Runnable listener) {
final PlatformStorage<FluidResource> storage = createStorage(tag, listener);
final PlatformStorage<FluidResource> storage = innerCreate(
tag.contains(TAG_CAPACITY) ? tag.getLong(TAG_CAPACITY) : null,
listener
);
final ListTag stacks = tag.getList(TAG_STACKS, Tag.TAG_COMPOUND);
for (final Tag stackTag : stacks) {
FluidResource
.fromTagWithAmount((CompoundTag) stackTag)
.ifPresent(resourceAmount -> storage.load(
resourceAmount.getResource(),
resourceAmount.getAmount(),
((CompoundTag) stackTag).getString(TAG_CHANGED_BY),
((CompoundTag) stackTag).getLong(TAG_CHANGED_AT)
));
FluidResource.fromTagWithAmount((CompoundTag) stackTag).ifPresent(resourceAmount -> storage.load(
resourceAmount.getResource(),
resourceAmount.getAmount(),
((CompoundTag) stackTag).getString(TAG_CHANGED_BY),
((CompoundTag) stackTag).getLong(TAG_CHANGED_AT)
));
}
return storage;
}

private PlatformStorage<FluidResource> createStorage(final CompoundTag tag, final Runnable listener) {
private PlatformStorage<FluidResource> innerCreate(@Nullable final Long capacity, final Runnable listener) {
final TrackedStorageRepository<FluidResource> trackingRepository = new InMemoryTrackedStorageRepository<>();
if (tag.contains(TAG_CAPACITY)) {
if (capacity != null) {
final LimitedStorageImpl<FluidResource> delegate = new LimitedStorageImpl<>(
new TrackedStorageImpl<>(
new InMemoryStorageImpl<>(),
trackingRepository,
System::currentTimeMillis
),
tag.getLong(TAG_CAPACITY)
capacity
);
return new LimitedPlatformStorage<>(
delegate,
FluidStorageType.INSTANCE,
StorageTypes.FLUID,
trackingRepository,
listener
);
}
return new PlatformStorage<>(
new TrackedStorageImpl<>(new InMemoryStorageImpl<>(), trackingRepository, System::currentTimeMillis),
FluidStorageType.INSTANCE,
StorageTypes.FLUID,
trackingRepository,
listener
);
Expand Down Expand Up @@ -102,16 +109,17 @@ private CompoundTag toTag(final Storage<FluidResource> storage,
}

public enum Variant {
SIXTY_FOUR_B("64b", 64),
TWO_HUNDRED_FIFTY_SIX_B("256b", 256),
THOUSAND_TWENTY_FOUR_B("1024b", 1024),
FOUR_THOUSAND_NINETY_SIX_B("4096b", 4096),
CREATIVE("creative", 0);
SIXTY_FOUR_B("64b", 64L),
TWO_HUNDRED_FIFTY_SIX_B("256b", 256L),
THOUSAND_TWENTY_FOUR_B("1024b", 1024L),
FOUR_THOUSAND_NINETY_SIX_B("4096b", 4096L),
CREATIVE("creative", null);

private final String name;
private final long capacityInBuckets;
@Nullable
private final Long capacityInBuckets;

Variant(final String name, final long capacityInBuckets) {
Variant(final String name, @Nullable final Long capacityInBuckets) {
this.name = name;
this.capacityInBuckets = capacityInBuckets;
}
Expand All @@ -120,12 +128,16 @@ public String getName() {
return name;
}

public long getCapacityInBuckets() {
return capacityInBuckets;
@Nullable
public Long getCapacity() {
if (capacityInBuckets == null) {
return null;
}
return capacityInBuckets * Platform.INSTANCE.getBucketAmount();
}

public boolean hasCapacity() {
return capacityInBuckets > 0;
return capacityInBuckets != null;
}
}
}
Loading

0 comments on commit 382ee1e

Please sign in to comment.