Skip to content

Commit

Permalink
refactor: remove ResourceType system and reuse PlatformStorageChannel…
Browse files Browse the repository at this point in the history
…Type

We'll need to do this to make filtering behavior consistent with grid behavior.
  • Loading branch information
raoulvdberge committed Jan 30, 2023
1 parent 0a5926a commit 05b9ec7
Show file tree
Hide file tree
Showing 70 changed files with 673 additions and 1,073 deletions.
8 changes: 5 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

- Ported to Minecraft 1.19.3.
- The regular Grid now shows fluids as well.
- You can insert fluids in the Grid by right-clicking a fluid container item in the Grid slots.
- You can no longer insert fluids straight from the player slots, you have to insert the fluid while holding the
fluid container item.
- You can insert fluids in the Grid by right-clicking a fluid container in the Grid slots.
- You no longer have to explicitly select a resource type for the filter configuration slots. You can set a fluid
by right-clicking a fluid container in the filter slots.
- You can no longer insert fluids into the Grid or filter slots straight from the player inventory slots, you have to
insert the fluid while holding the fluid container.

### Removed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.refinedmods.refinedstorage2.platform.api.network.node.externalstorage.PlatformExternalStorageProviderFactory;
import com.refinedmods.refinedstorage2.platform.api.network.node.importer.ImporterTransferStrategyFactory;
import com.refinedmods.refinedstorage2.platform.api.resource.ItemResource;
import com.refinedmods.refinedstorage2.platform.api.resource.filter.ResourceType;
import com.refinedmods.refinedstorage2.platform.api.resource.filter.FilteredResourceFactory;
import com.refinedmods.refinedstorage2.platform.api.storage.StorageRepository;
import com.refinedmods.refinedstorage2.platform.api.storage.channel.PlatformStorageChannelType;
import com.refinedmods.refinedstorage2.platform.api.storage.type.StorageType;
Expand Down Expand Up @@ -61,8 +61,6 @@ <T> Set<PlatformExternalStorageProviderFactory> getExternalStorageProviderFactor

MutableComponent createTranslation(String category, String value, Object... args);

OrderedRegistry<ResourceLocation, ResourceType> getResourceTypeRegistry();

ComponentMapFactory<NetworkComponent, Network> getNetworkComponentMapFactory();

OrderedRegistry<ResourceLocation, GridSynchronizer> getGridSynchronizerRegistry();
Expand Down Expand Up @@ -93,4 +91,8 @@ GridScrollingStrategy createGridScrollingStrategy(AbstractContainerMenu containe
GridServiceFactory gridServiceFactory);

void addGridScrollingStrategyFactory(GridScrollingStrategyFactory scrollingStrategyFactory);

void addFilteredResourceFactory(FilteredResourceFactory factory);

FilteredResourceFactory getFilteredResourceFactory();
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.refinedmods.refinedstorage2.platform.api.network.node.externalstorage.PlatformExternalStorageProviderFactory;
import com.refinedmods.refinedstorage2.platform.api.network.node.importer.ImporterTransferStrategyFactory;
import com.refinedmods.refinedstorage2.platform.api.resource.ItemResource;
import com.refinedmods.refinedstorage2.platform.api.resource.filter.ResourceType;
import com.refinedmods.refinedstorage2.platform.api.resource.filter.FilteredResourceFactory;
import com.refinedmods.refinedstorage2.platform.api.storage.StorageRepository;
import com.refinedmods.refinedstorage2.platform.api.storage.channel.PlatformStorageChannelType;
import com.refinedmods.refinedstorage2.platform.api.storage.type.StorageType;
Expand Down Expand Up @@ -95,11 +95,6 @@ public MutableComponent createTranslation(final String category, final String va
return ensureLoaded().createTranslation(category, value, args);
}

@Override
public OrderedRegistry<ResourceLocation, ResourceType> getResourceTypeRegistry() {
return ensureLoaded().getResourceTypeRegistry();
}

@Override
public ComponentMapFactory<NetworkComponent, Network> getNetworkComponentMapFactory() {
return ensureLoaded().getNetworkComponentMapFactory();
Expand Down Expand Up @@ -175,6 +170,16 @@ public void addGridScrollingStrategyFactory(final GridScrollingStrategyFactory s
ensureLoaded().addGridScrollingStrategyFactory(scrollingStrategyFactory);
}

@Override
public void addFilteredResourceFactory(final FilteredResourceFactory factory) {
ensureLoaded().addFilteredResourceFactory(factory);
}

@Override
public FilteredResourceFactory getFilteredResourceFactory() {
return ensureLoaded().getFilteredResourceFactory();
}

private PlatformApi ensureLoaded() {
if (delegate == null) {
throw new IllegalStateException("Platform API not loaded yet");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void onExtract(GridExtractMode extractMode,
void onScroll(GridScrollMode scrollMode,
GridScrollingStrategy scrollingStrategy);

void render(PoseStack poseStack, int slotX, int slotY);
void render(PoseStack poseStack, int x, int y);

String getDisplayedAmount();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
package com.refinedmods.refinedstorage2.platform.api.resource.filter;

import com.refinedmods.refinedstorage2.platform.api.storage.channel.PlatformStorageChannelType;

import java.util.List;
import javax.annotation.Nullable;

import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.player.Player;
import org.apiguardian.api.API;

@API(status = API.Status.STABLE, since = "2.0.0-milestone.2.0")
public interface FilteredResource {
void writeToPacket(FriendlyByteBuf buf);

CompoundTag toTag();

public interface FilteredResource<T> {
void render(PoseStack poseStack, int x, int y, int z);

Object getValue();
T getValue();

long getAmount();
FilteredResource<T> withAmount(long newAmount);

FilteredResource withAmount(long newAmount);
long getAmount();

long getMaxAmount();

String getFormattedAmount();
String getDisplayedAmount();

ResourceType getType();
List<Component> getTooltip();

List<Component> getTooltipLines(@Nullable Player player);
PlatformStorageChannelType<T> getStorageChannelType();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.refinedmods.refinedstorage2.platform.api.resource.filter;

import java.util.Optional;

import net.minecraft.world.item.ItemStack;
import org.apiguardian.api.API;

@API(status = API.Status.STABLE, since = "2.0.0-milestone.2.6")
public interface FilteredResourceFactory {
Optional<FilteredResource<?>> create(ItemStack stack, boolean tryAlternatives);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannelType;
import com.refinedmods.refinedstorage2.api.storage.tracked.TrackedResource;

import java.util.Optional;
import java.util.function.BiConsumer;

import net.minecraft.nbt.CompoundTag;
Expand Down Expand Up @@ -47,8 +46,6 @@ public CompoundTag toTag(final T resource, final TrackedResource trackedResource
return tag;
}

protected abstract CompoundTag toTag(T resource);

@Override
public void fromTag(final CompoundTag tag, final BiConsumer<T, TrackedResource> acceptor) {
fromTag(tag).ifPresent(resource -> {
Expand All @@ -58,8 +55,6 @@ public void fromTag(final CompoundTag tag, final BiConsumer<T, TrackedResource>
});
}

protected abstract Optional<T> fromTag(CompoundTag tag);

@Override
public StorageChannel<T> create() {
return delegate.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.refinedmods.refinedstorage2.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannelType;
import com.refinedmods.refinedstorage2.api.storage.tracked.TrackedResource;
import com.refinedmods.refinedstorage2.platform.api.resource.filter.FilteredResource;

import java.util.Optional;
import java.util.function.BiConsumer;
Expand All @@ -16,16 +17,22 @@

@API(status = API.Status.STABLE, since = "2.0.0-milestone.2.5")
public interface PlatformStorageChannelType<T> extends StorageChannelType<T> {
CompoundTag toTag(T resource);

CompoundTag toTag(T resource, TrackedResource trackedResource);

void fromTag(CompoundTag tag, BiConsumer<T, TrackedResource> acceptor);

Optional<T> fromTag(CompoundTag tag);

void toBuffer(T resource, FriendlyByteBuf buf);

T fromBuffer(FriendlyByteBuf buf);

Optional<GridResource> toGridResource(ResourceAmount<?> resourceAmount);

Optional<FilteredResource<T>> toFilteredResource(ResourceAmount<?> resourceAmount);

boolean isGridResourceBelonging(GridResource gridResource);

MutableComponent getTitle();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.refinedmods.refinedstorage2.api.network.impl.component.StorageNetworkComponentImpl;
import com.refinedmods.refinedstorage2.platform.api.PlatformApi;
import com.refinedmods.refinedstorage2.platform.api.PlatformApiProxy;
import com.refinedmods.refinedstorage2.platform.common.internal.resource.filter.fluid.FluidResourceType;
import com.refinedmods.refinedstorage2.platform.common.internal.resource.filter.fluid.FluidFilteredResourceFactory;
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.upgrade.UpgradeDestinations;
Expand Down Expand Up @@ -43,6 +43,10 @@ protected void registerAdditionalStorageChannelTypes() {
);
}

protected void registerAdditionalFilteredResourceFactories() {
PlatformApi.INSTANCE.addFilteredResourceFactory(new FluidFilteredResourceFactory());
}

protected void registerNetworkComponents() {
PlatformApi.INSTANCE.getNetworkComponentMapFactory().addFactory(
EnergyNetworkComponent.class,
Expand All @@ -58,13 +62,6 @@ protected void registerNetworkComponents() {
);
}

protected void registerAdditionalResourceTypes() {
PlatformApi.INSTANCE.getResourceTypeRegistry().register(
createIdentifier(FLUID_REGISTRY_KEY),
FluidResourceType.INSTANCE
);
}

protected void addApplicableUpgrades(final Supplier<Item> speedUpgrade,
final Supplier<Item> stackUpgrade) {
PlatformApi.INSTANCE.getUpgradeRegistry().addApplicableUpgrade(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import com.refinedmods.refinedstorage2.platform.api.network.node.externalstorage.PlatformExternalStorageProviderFactory;
import com.refinedmods.refinedstorage2.platform.api.network.node.importer.ImporterTransferStrategyFactory;
import com.refinedmods.refinedstorage2.platform.api.resource.ItemResource;
import com.refinedmods.refinedstorage2.platform.api.resource.filter.ResourceType;
import com.refinedmods.refinedstorage2.platform.api.resource.filter.FilteredResourceFactory;
import com.refinedmods.refinedstorage2.platform.api.storage.StorageRepository;
import com.refinedmods.refinedstorage2.platform.api.storage.channel.PlatformStorageChannelType;
import com.refinedmods.refinedstorage2.platform.api.storage.type.StorageType;
Expand All @@ -38,7 +38,8 @@
import com.refinedmods.refinedstorage2.platform.common.internal.grid.PlatformGridServiceFactoryImpl;
import com.refinedmods.refinedstorage2.platform.common.internal.item.StorageContainerHelperImpl;
import com.refinedmods.refinedstorage2.platform.common.internal.network.LevelConnectionProvider;
import com.refinedmods.refinedstorage2.platform.common.internal.resource.filter.item.ItemResourceType;
import com.refinedmods.refinedstorage2.platform.common.internal.resource.filter.CompositeFilteredResourceFactory;
import com.refinedmods.refinedstorage2.platform.common.internal.resource.filter.item.ItemFilteredResourceFactory;
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;
Expand Down Expand Up @@ -72,8 +73,6 @@ public class PlatformApiImpl implements PlatformApi {

private final StorageRepository clientStorageRepository =
new ClientStorageRepository(Platform.INSTANCE.getClientToServerCommunications()::sendStorageInfoRequest);
private final OrderedRegistry<ResourceLocation, ResourceType> resourceTypeRegistry =
new OrderedRegistryImpl<>(createIdentifier(ITEM_REGISTRY_KEY), ItemResourceType.INSTANCE);
private final ComponentMapFactory<NetworkComponent, Network> networkComponentMapFactory =
new ComponentMapFactory<>();
private final NetworkBuilder networkBuilder =
Expand All @@ -97,6 +96,9 @@ public class PlatformApiImpl implements PlatformApi {
private final List<GridInsertionStrategyFactory> gridInsertionStrategyFactories = new ArrayList<>();
private final List<GridExtractionStrategyFactory> gridExtractionStrategyFactories = new ArrayList<>();
private final List<GridScrollingStrategyFactory> gridScrollingStrategyFactories = new ArrayList<>();
private final CompositeFilteredResourceFactory filteredResourceFactory = new CompositeFilteredResourceFactory(
new ItemFilteredResourceFactory()
);

@Override
public OrderedRegistry<ResourceLocation, StorageType<?>> getStorageTypeRegistry() {
Expand Down Expand Up @@ -173,11 +175,6 @@ public MutableComponent createTranslation(final String category, final String va
return IdentifierUtil.createTranslation(category, value, args);
}

@Override
public OrderedRegistry<ResourceLocation, ResourceType> getResourceTypeRegistry() {
return resourceTypeRegistry;
}

@Override
public ComponentMapFactory<NetworkComponent, Network> getNetworkComponentMapFactory() {
return networkComponentMapFactory;
Expand Down Expand Up @@ -281,4 +278,14 @@ public GridScrollingStrategy createGridScrollingStrategy(final AbstractContainer
public void addGridScrollingStrategyFactory(final GridScrollingStrategyFactory scrollingStrategyFactory) {
gridScrollingStrategyFactories.add(scrollingStrategyFactory);
}

@Override
public void addFilteredResourceFactory(final FilteredResourceFactory factory) {
filteredResourceFactory.addAlternativeFactory(factory);
}

@Override
public FilteredResourceFactory getFilteredResourceFactory() {
return filteredResourceFactory;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.refinedmods.refinedstorage2.platform.common.block.entity;

import com.refinedmods.refinedstorage2.platform.api.PlatformApi;
import com.refinedmods.refinedstorage2.platform.api.resource.FuzzyModeNormalizer;
import com.refinedmods.refinedstorage2.platform.api.resource.filter.ResourceType;
import com.refinedmods.refinedstorage2.platform.api.storage.channel.PlatformStorageChannelType;
import com.refinedmods.refinedstorage2.platform.common.internal.resource.filter.FilteredResourceFilterContainer;
import com.refinedmods.refinedstorage2.platform.common.internal.resource.filter.ResourceFilterContainer;

Expand All @@ -24,32 +23,30 @@ public final class FilterWithFuzzyMode {

private boolean fuzzyMode;

public FilterWithFuzzyMode(final ResourceType resourceType,
public FilterWithFuzzyMode(final PlatformStorageChannelType<?> storageChannelType,
final Runnable listener,
final Consumer<Set<Object>> templatesAcceptor,
final Consumer<List<Object>> orderedTemplatesAcceptor) {
this.filterContainer = new FilteredResourceFilterContainer(
PlatformApi.INSTANCE.getResourceTypeRegistry(),
9,
this::filterContainerChanged,
resourceType
storageChannelType
);
this.listener = listener;
this.templatesAcceptor = templatesAcceptor;
this.orderedTemplatesAcceptor = orderedTemplatesAcceptor;
}

public FilterWithFuzzyMode(final ResourceType resourceType,
public FilterWithFuzzyMode(final PlatformStorageChannelType<?> storageChannelType,
final Runnable listener,
final Consumer<Set<Object>> templatesAcceptor,
final Consumer<List<Object>> orderedTemplatesAcceptor,
final int size,
final long maxAmount) {
this.filterContainer = new FilteredResourceFilterContainer(
PlatformApi.INSTANCE.getResourceTypeRegistry(),
size,
this::filterContainerChanged,
resourceType,
storageChannelType,
maxAmount
);
this.listener = listener;
Expand All @@ -60,11 +57,7 @@ public FilterWithFuzzyMode(final ResourceType resourceType,
public FilterWithFuzzyMode(final Runnable listener,
final Consumer<Set<Object>> templatesAcceptor,
final Consumer<List<Object>> orderedTemplatesAcceptor) {
this.filterContainer = new ResourceFilterContainer(
PlatformApi.INSTANCE.getResourceTypeRegistry(),
9,
this::filterContainerChanged
);
this.filterContainer = new ResourceFilterContainer(9, this::filterContainerChanged);
this.listener = listener;
this.templatesAcceptor = templatesAcceptor;
this.orderedTemplatesAcceptor = orderedTemplatesAcceptor;
Expand All @@ -91,7 +84,7 @@ public void setFuzzyMode(final boolean fuzzyMode) {

public void load(final CompoundTag tag) {
if (tag.contains(TAG_RESOURCE_FILTER)) {
filterContainer.load(tag.getCompound(TAG_RESOURCE_FILTER));
filterContainer.fromTag(tag.getCompound(TAG_RESOURCE_FILTER));
}
if (tag.contains(TAG_FUZZY_MODE)) {
fuzzyMode = tag.getBoolean(TAG_FUZZY_MODE);
Expand Down
Loading

0 comments on commit 05b9ec7

Please sign in to comment.