Skip to content

Commit

Permalink
feat: storage block api
Browse files Browse the repository at this point in the history
This introduces a client API so we can use client classes safely.
  • Loading branch information
raoulvdberge committed Dec 7, 2024
1 parent cf40335 commit 94072dc
Show file tree
Hide file tree
Showing 66 changed files with 886 additions and 671 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@
import com.refinedmods.refinedstorage.api.network.energy.EnergyStorage;
import com.refinedmods.refinedstorage.api.network.node.NetworkNode;
import com.refinedmods.refinedstorage.api.network.security.SecurityPolicy;
import com.refinedmods.refinedstorage.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage.api.resource.ResourceKey;
import com.refinedmods.refinedstorage.common.api.constructordestructor.ConstructorStrategyFactory;
import com.refinedmods.refinedstorage.common.api.constructordestructor.DestructorStrategyFactory;
import com.refinedmods.refinedstorage.common.api.exporter.ExporterTransferStrategyFactory;
import com.refinedmods.refinedstorage.common.api.grid.Grid;
import com.refinedmods.refinedstorage.common.api.grid.GridInsertionHint;
import com.refinedmods.refinedstorage.common.api.grid.GridInsertionHints;
import com.refinedmods.refinedstorage.common.api.grid.GridSynchronizer;
import com.refinedmods.refinedstorage.common.api.grid.strategy.GridExtractionStrategy;
import com.refinedmods.refinedstorage.common.api.grid.strategy.GridExtractionStrategyFactory;
Expand All @@ -24,21 +20,23 @@
import com.refinedmods.refinedstorage.common.api.grid.strategy.GridScrollingStrategyFactory;
import com.refinedmods.refinedstorage.common.api.importer.ImporterTransferStrategyFactory;
import com.refinedmods.refinedstorage.common.api.security.PlatformPermission;
import com.refinedmods.refinedstorage.common.api.storage.StorageBlockData;
import com.refinedmods.refinedstorage.common.api.storage.StorageBlockProvider;
import com.refinedmods.refinedstorage.common.api.storage.StorageContainerItemHelper;
import com.refinedmods.refinedstorage.common.api.storage.StorageRepository;
import com.refinedmods.refinedstorage.common.api.storage.StorageType;
import com.refinedmods.refinedstorage.common.api.storage.externalstorage.PlatformExternalStorageProviderFactory;
import com.refinedmods.refinedstorage.common.api.storagemonitor.StorageMonitorExtractionStrategy;
import com.refinedmods.refinedstorage.common.api.storagemonitor.StorageMonitorInsertionStrategy;
import com.refinedmods.refinedstorage.common.api.support.energy.EnergyItemHelper;
import com.refinedmods.refinedstorage.common.api.support.network.AbstractNetworkNodeContainerBlockEntity;
import com.refinedmods.refinedstorage.common.api.support.network.InWorldNetworkNodeContainer;
import com.refinedmods.refinedstorage.common.api.support.network.NetworkNodeContainerProvider;
import com.refinedmods.refinedstorage.common.api.support.network.item.NetworkItemHelper;
import com.refinedmods.refinedstorage.common.api.support.registry.PlatformRegistry;
import com.refinedmods.refinedstorage.common.api.support.resource.RecipeModIngredientConverter;
import com.refinedmods.refinedstorage.common.api.support.resource.ResourceContainerInsertStrategy;
import com.refinedmods.refinedstorage.common.api.support.resource.ResourceFactory;
import com.refinedmods.refinedstorage.common.api.support.resource.ResourceRendering;
import com.refinedmods.refinedstorage.common.api.support.resource.ResourceType;
import com.refinedmods.refinedstorage.common.api.support.slotreference.SlotReference;
import com.refinedmods.refinedstorage.common.api.support.slotreference.SlotReferenceFactory;
Expand All @@ -47,21 +45,24 @@
import com.refinedmods.refinedstorage.common.api.wirelesstransmitter.WirelessTransmitterRangeModifier;

import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import javax.annotation.Nullable;

import net.minecraft.core.BlockPos;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
Expand Down Expand Up @@ -133,10 +134,6 @@ GridInsertionStrategy createGridInsertionStrategy(AbstractContainerMenu containe

Collection<ResourceContainerInsertStrategy> getResourceContainerInsertStrategies();

void addAlternativeGridInsertionHint(GridInsertionHint hint);

GridInsertionHints getGridInsertionHints();

GridExtractionStrategy createGridExtractionStrategy(AbstractContainerMenu containerMenu,
ServerPlayer player,
Grid grid);
Expand All @@ -161,10 +158,6 @@ GridScrollingStrategy createGridScrollingStrategy(AbstractContainerMenu containe

Set<ResourceFactory> getAlternativeResourceFactories();

<T extends ResourceKey> void registerResourceRendering(Class<T> resourceClass, ResourceRendering rendering);

<T extends ResourceKey> ResourceRendering getResourceRendering(Class<T> resourceClass);

void addIngredientConverter(RecipeModIngredientConverter converter);

RecipeModIngredientConverter getIngredientConverter();
Expand Down Expand Up @@ -207,7 +200,19 @@ EnergyStorage asBlockItemEnergyStorage(

Optional<Pattern> getPattern(ItemStack stack, Level level);

void openAutocraftingPreview(List<ResourceAmount> requests, @Nullable Object parentScreen);

ResourceLocation getCreativeModeTabId();

AbstractNetworkNodeContainerBlockEntity<?> createStorageBlockEntity(BlockPos pos,
BlockState state,
StorageBlockProvider provider);

Block createStorageBlock(Block.Properties properties, StorageBlockProvider provider);

AbstractContainerMenu createStorageBlockContainerMenu(int syncId,
Player player,
StorageBlockData data,
ResourceFactory resourceFactory,
MenuType<?> menuType);

StreamCodec<RegistryFriendlyByteBuf, StorageBlockData> getStorageBlockDataStreamCodec();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@
import com.refinedmods.refinedstorage.api.network.energy.EnergyStorage;
import com.refinedmods.refinedstorage.api.network.node.NetworkNode;
import com.refinedmods.refinedstorage.api.network.security.SecurityPolicy;
import com.refinedmods.refinedstorage.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage.api.resource.ResourceKey;
import com.refinedmods.refinedstorage.common.api.constructordestructor.ConstructorStrategyFactory;
import com.refinedmods.refinedstorage.common.api.constructordestructor.DestructorStrategyFactory;
import com.refinedmods.refinedstorage.common.api.exporter.ExporterTransferStrategyFactory;
import com.refinedmods.refinedstorage.common.api.grid.Grid;
import com.refinedmods.refinedstorage.common.api.grid.GridInsertionHint;
import com.refinedmods.refinedstorage.common.api.grid.GridInsertionHints;
import com.refinedmods.refinedstorage.common.api.grid.GridSynchronizer;
import com.refinedmods.refinedstorage.common.api.grid.strategy.GridExtractionStrategy;
import com.refinedmods.refinedstorage.common.api.grid.strategy.GridExtractionStrategyFactory;
Expand All @@ -24,21 +20,23 @@
import com.refinedmods.refinedstorage.common.api.grid.strategy.GridScrollingStrategyFactory;
import com.refinedmods.refinedstorage.common.api.importer.ImporterTransferStrategyFactory;
import com.refinedmods.refinedstorage.common.api.security.PlatformPermission;
import com.refinedmods.refinedstorage.common.api.storage.StorageBlockData;
import com.refinedmods.refinedstorage.common.api.storage.StorageBlockProvider;
import com.refinedmods.refinedstorage.common.api.storage.StorageContainerItemHelper;
import com.refinedmods.refinedstorage.common.api.storage.StorageRepository;
import com.refinedmods.refinedstorage.common.api.storage.StorageType;
import com.refinedmods.refinedstorage.common.api.storage.externalstorage.PlatformExternalStorageProviderFactory;
import com.refinedmods.refinedstorage.common.api.storagemonitor.StorageMonitorExtractionStrategy;
import com.refinedmods.refinedstorage.common.api.storagemonitor.StorageMonitorInsertionStrategy;
import com.refinedmods.refinedstorage.common.api.support.energy.EnergyItemHelper;
import com.refinedmods.refinedstorage.common.api.support.network.AbstractNetworkNodeContainerBlockEntity;
import com.refinedmods.refinedstorage.common.api.support.network.InWorldNetworkNodeContainer;
import com.refinedmods.refinedstorage.common.api.support.network.NetworkNodeContainerProvider;
import com.refinedmods.refinedstorage.common.api.support.network.item.NetworkItemHelper;
import com.refinedmods.refinedstorage.common.api.support.registry.PlatformRegistry;
import com.refinedmods.refinedstorage.common.api.support.resource.RecipeModIngredientConverter;
import com.refinedmods.refinedstorage.common.api.support.resource.ResourceContainerInsertStrategy;
import com.refinedmods.refinedstorage.common.api.support.resource.ResourceFactory;
import com.refinedmods.refinedstorage.common.api.support.resource.ResourceRendering;
import com.refinedmods.refinedstorage.common.api.support.resource.ResourceType;
import com.refinedmods.refinedstorage.common.api.support.slotreference.SlotReference;
import com.refinedmods.refinedstorage.common.api.support.slotreference.SlotReferenceFactory;
Expand All @@ -47,21 +45,24 @@
import com.refinedmods.refinedstorage.common.api.wirelesstransmitter.WirelessTransmitterRangeModifier;

import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import javax.annotation.Nullable;

import net.minecraft.core.BlockPos;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
Expand Down Expand Up @@ -229,16 +230,6 @@ public Collection<ResourceContainerInsertStrategy> getResourceContainerInsertStr
return ensureLoaded().getResourceContainerInsertStrategies();
}

@Override
public void addAlternativeGridInsertionHint(final GridInsertionHint hint) {
ensureLoaded().addAlternativeGridInsertionHint(hint);
}

@Override
public GridInsertionHints getGridInsertionHints() {
return ensureLoaded().getGridInsertionHints();
}

@Override
public GridExtractionStrategy createGridExtractionStrategy(final AbstractContainerMenu containerMenu,
final ServerPlayer player,
Expand Down Expand Up @@ -293,17 +284,6 @@ public Set<ResourceFactory> getAlternativeResourceFactories() {
return ensureLoaded().getAlternativeResourceFactories();
}

@Override
public <T extends ResourceKey> void registerResourceRendering(final Class<T> resourceClass,
final ResourceRendering rendering) {
ensureLoaded().registerResourceRendering(resourceClass, rendering);
}

@Override
public <T extends ResourceKey> ResourceRendering getResourceRendering(final Class<T> resourceClass) {
return ensureLoaded().getResourceRendering(resourceClass);
}

@Override
public void addIngredientConverter(final RecipeModIngredientConverter converter) {
ensureLoaded().addIngredientConverter(converter);
Expand Down Expand Up @@ -405,13 +385,34 @@ public Optional<Pattern> getPattern(final ItemStack stack, final Level level) {
}

@Override
public void openAutocraftingPreview(final List<ResourceAmount> requests, @Nullable final Object parentScreen) {
ensureLoaded().openAutocraftingPreview(requests, parentScreen);
public ResourceLocation getCreativeModeTabId() {
return ensureLoaded().getCreativeModeTabId();
}

@Override
public ResourceLocation getCreativeModeTabId() {
return ensureLoaded().getCreativeModeTabId();
public AbstractNetworkNodeContainerBlockEntity<?> createStorageBlockEntity(final BlockPos pos,
final BlockState state,
final StorageBlockProvider provider) {
return ensureLoaded().createStorageBlockEntity(pos, state, provider);
}

@Override
public Block createStorageBlock(final Block.Properties properties, final StorageBlockProvider provider) {
return ensureLoaded().createStorageBlock(properties, provider);
}

@Override
public AbstractContainerMenu createStorageBlockContainerMenu(final int syncId,
final Player player,
final StorageBlockData data,
final ResourceFactory resourceFactory,
final MenuType<?> menuType) {
return ensureLoaded().createStorageBlockContainerMenu(syncId, player, data, resourceFactory, menuType);
}

@Override
public StreamCodec<RegistryFriendlyByteBuf, StorageBlockData> getStorageBlockDataStreamCodec() {
return ensureLoaded().getStorageBlockDataStreamCodec();
}

private RefinedStorageApi ensureLoaded() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.refinedmods.refinedstorage.common.api;

import com.refinedmods.refinedstorage.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage.api.resource.ResourceKey;
import com.refinedmods.refinedstorage.common.api.grid.GridInsertionHint;
import com.refinedmods.refinedstorage.common.api.grid.GridInsertionHints;
import com.refinedmods.refinedstorage.common.api.support.resource.ResourceRendering;

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

import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.Item;
import org.apiguardian.api.API;

@API(status = API.Status.STABLE, since = "2.0.0-milestone.4.11")
public interface RefinedStorageClientApi {
RefinedStorageClientApi INSTANCE = new RefinedStorageClientApiProxy();

AbstractContainerScreen<AbstractContainerMenu> createStorageBlockScreen(
AbstractContainerMenu menu,
Inventory inventory,
Component title,
Class<? extends ResourceKey> resourceClass
);

void openAutocraftingPreview(List<ResourceAmount> requests, @Nullable Screen parentScreen);

<T extends ResourceKey> void registerResourceRendering(Class<T> resourceClass, ResourceRendering rendering);

<T extends ResourceKey> ResourceRendering getResourceRendering(Class<T> resourceClass);

void addAlternativeGridInsertionHint(GridInsertionHint hint);

GridInsertionHints getGridInsertionHints();

void registerDiskModel(Item item, ResourceLocation model);

Set<ResourceLocation> getDiskModels();

Map<Item, ResourceLocation> getDiskModelsByItem();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.refinedmods.refinedstorage.common.api;

import com.refinedmods.refinedstorage.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage.api.resource.ResourceKey;
import com.refinedmods.refinedstorage.common.api.grid.GridInsertionHint;
import com.refinedmods.refinedstorage.common.api.grid.GridInsertionHints;
import com.refinedmods.refinedstorage.common.api.support.resource.ResourceRendering;

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

import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.Item;

public class RefinedStorageClientApiProxy implements RefinedStorageClientApi {
@Nullable
private RefinedStorageClientApi delegate;

public void setDelegate(final RefinedStorageClientApi delegate) {
if (this.delegate != null) {
throw new IllegalStateException("Client API already injected");
}
this.delegate = delegate;
}

@Override
public AbstractContainerScreen<AbstractContainerMenu> createStorageBlockScreen(
final AbstractContainerMenu menu,
final Inventory inventory,
final Component title,
final Class<? extends ResourceKey> resourceClass
) {
return ensureLoaded().createStorageBlockScreen(menu, inventory, title, resourceClass);
}

@Override
public void openAutocraftingPreview(final List<ResourceAmount> requests, @Nullable final Screen parentScreen) {
ensureLoaded().openAutocraftingPreview(requests, parentScreen);
}

@Override
public <T extends ResourceKey> void registerResourceRendering(final Class<T> resourceClass,
final ResourceRendering rendering) {
ensureLoaded().registerResourceRendering(resourceClass, rendering);
}

@Override
public <T extends ResourceKey> ResourceRendering getResourceRendering(final Class<T> resourceClass) {
return ensureLoaded().getResourceRendering(resourceClass);
}

@Override
public void addAlternativeGridInsertionHint(final GridInsertionHint hint) {
ensureLoaded().addAlternativeGridInsertionHint(hint);
}

@Override
public GridInsertionHints getGridInsertionHints() {
return ensureLoaded().getGridInsertionHints();
}

@Override
public void registerDiskModel(final Item item, final ResourceLocation model) {
ensureLoaded().registerDiskModel(item, model);
}

@Override
public Set<ResourceLocation> getDiskModels() {
return ensureLoaded().getDiskModels();
}

@Override
public Map<Item, ResourceLocation> getDiskModelsByItem() {
return ensureLoaded().getDiskModelsByItem();
}

private RefinedStorageClientApi ensureLoaded() {
if (delegate == null) {
throw new IllegalStateException("API not loaded yet");
}
return delegate;
}
}
Loading

0 comments on commit 94072dc

Please sign in to comment.