Skip to content

Commit

Permalink
feat: revamp exposed storage system
Browse files Browse the repository at this point in the history
remove indexed slotted storage methods
  • Loading branch information
marcus8448 committed Jul 5, 2024
1 parent dabc5c2 commit a37561c
Show file tree
Hide file tree
Showing 44 changed files with 793 additions and 1,807 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@

import dev.galacticraft.machinelib.api.machine.MachineType;
import dev.galacticraft.machinelib.api.menu.RecipeMachineMenu;
import dev.galacticraft.machinelib.api.storage.SlottedStorageAccess;
import dev.galacticraft.machinelib.api.storage.slot.ItemResourceSlot;
import net.minecraft.core.BlockPos;
import net.minecraft.world.Container;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.RecipeHolder;
Expand All @@ -46,10 +49,8 @@ public abstract class BasicRecipeMachineBlockEntity<C extends Container, R exten
*/
protected final @NotNull C craftingInv;

protected final int inputSlots;
protected final int inputSlotsLen;
protected final int outputSlots;
protected final int outputSlotsLen;
protected final SlottedStorageAccess<Item, ItemResourceSlot> inputSlots;
protected final SlottedStorageAccess<Item, ItemResourceSlot> outputSlots;

/**
* Constructs a new machine block entity that processes recipes.
Expand Down Expand Up @@ -98,10 +99,8 @@ protected BasicRecipeMachineBlockEntity(@NotNull MachineType<? extends BasicReci
@NotNull BlockPos pos, BlockState state, @NotNull RecipeType<R> recipeType, int inputSlots, int inputSlotsLen, int outputSlots, int outputSlotsLen) {
super(type, pos, state, recipeType);

this.inputSlots = inputSlots;
this.inputSlotsLen = inputSlotsLen;
this.outputSlots = outputSlots;
this.outputSlotsLen = outputSlotsLen;
this.inputSlots = this.itemStorage().subStorage(inputSlots, inputSlotsLen);
this.outputSlots = this.itemStorage().subStorage(outputSlots, outputSlotsLen);

this.craftingInv = this.createCraftingInv();
}
Expand All @@ -128,7 +127,7 @@ protected BasicRecipeMachineBlockEntity(@NotNull MachineType<? extends BasicReci
@Override
protected void outputStacks(@NotNull RecipeHolder<R> recipe) {
ItemStack assembled = recipe.value().assemble(this.craftingInv(), this.level.registryAccess());
this.itemStorage().insertMatching(this.outputSlots, this.outputSlotsLen, assembled.getItem(), assembled.getComponentsPatch(), assembled.getCount());
this.outputSlots.insertMatching(assembled.getItem(), assembled.getComponentsPatch(), assembled.getCount());
}

/**
Expand All @@ -140,7 +139,7 @@ protected void outputStacks(@NotNull RecipeHolder<R> recipe) {
@Override
protected boolean canOutputStacks(@NotNull RecipeHolder<R> recipe) {
ItemStack assembled = recipe.value().assemble(this.craftingInv(), this.level.registryAccess());
return this.itemStorage().canInsert(this.outputSlots, this.outputSlotsLen, assembled.getItem(), assembled.getComponentsPatch(), assembled.getCount());
return this.inputSlots.canInsert(assembled.getItem(), assembled.getComponentsPatch(), assembled.getCount());
}

/**
Expand All @@ -150,8 +149,8 @@ protected boolean canOutputStacks(@NotNull RecipeHolder<R> recipe) {
*/
@Override
protected void extractCraftingMaterials(@NotNull RecipeHolder<R> recipe) {
for (int i = 0; i < this.inputSlotsLen; i++) {
this.itemStorage().consumeOne(this.inputSlots + i);
for (ItemResourceSlot slot : this.inputSlots) {
slot.consumeOne();
}
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -22,75 +22,15 @@

package dev.galacticraft.machinelib.api.compat.transfer;

import dev.galacticraft.machinelib.api.storage.slot.ResourceSlot;
import dev.galacticraft.machinelib.api.transfer.ResourceFlow;
import dev.galacticraft.machinelib.impl.compat.transfer.ExposedFluidSlotImpl;
import dev.galacticraft.machinelib.impl.compat.transfer.ExposedFullFluidSlotImpl;
import dev.galacticraft.machinelib.impl.compat.transfer.ExposedFullItemSlotImpl;
import dev.galacticraft.machinelib.impl.compat.transfer.ExposedItemSlotImpl;
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant;
import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant;
import net.fabricmc.fabric.api.transfer.v1.storage.Storage;
import net.fabricmc.fabric.api.transfer.v1.storage.StorageView;
import net.fabricmc.fabric.api.transfer.v1.storage.TransferVariant;
import net.fabricmc.fabric.api.transfer.v1.storage.base.SingleSlotStorage;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.material.Fluid;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

/**
* Represents a slot exposed to adjacent blocks or items.
*
* @param <Resource> The type of resource stored in the slot.
* @param <Variant> The type of variant for the resource that can be stored in the slot.
*/
public interface ExposedSlot<Resource, Variant extends TransferVariant<Resource>> extends ExposedStorage<Resource, Variant>, SingleSlotStorage<Variant> {
/**
* Creates an exposed item slot.
*
* @param slot The backing resource slot.
* @param flow The flow restrictions on the slot.
* @return An exposed item slot.
*/
@Contract("_, _ -> new")
static @NotNull ExposedSlot<Item, ItemVariant> createItem(@NotNull ResourceSlot<Item> slot, @NotNull ResourceFlow flow) {
if (flow == ResourceFlow.BOTH) return createFullItem(slot);
return new ExposedItemSlotImpl(slot, flow.canFlowIn(ResourceFlow.INPUT), flow.canFlowIn(ResourceFlow.OUTPUT));
}

/**
* Creates an exposed fluid slot.
*
* @param slot The backing resource slot.
* @param flow The flow restrictions on the slot.
* @return An exposed fluid slot.
*/
@Contract("_, _ -> new")
static @NotNull ExposedSlot<Fluid, FluidVariant> createFluid(@NotNull ResourceSlot<Fluid> slot, @NotNull ResourceFlow flow) {
if (flow == ResourceFlow.BOTH) return createFullFluid(slot);
return new ExposedFluidSlotImpl(slot, flow.canFlowIn(ResourceFlow.INPUT), flow.canFlowIn(ResourceFlow.OUTPUT));
}

/**
* Creates a fully exposed item slot.
* This slot has no additional I/O restrictions.
*
* @param slot The backing resource slot.
* @return A fully exposed item slot.
*/
@Contract("_ -> new")
static @NotNull ExposedSlot<Item, ItemVariant> createFullItem(@NotNull ResourceSlot<Item> slot) {
return new ExposedFullItemSlotImpl(slot);
}

/**
* Creates a fully exposed fluid slot.
* This slot has no additional I/O restrictions.
*
* @param slot The backing resource slot.
* @return A fully exposed fluid slot.
*/
@Contract("_ -> new")
static @NotNull ExposedSlot<Fluid, FluidVariant> createFullFluid(@NotNull ResourceSlot<Fluid> slot) {
return new ExposedFullFluidSlotImpl(slot);
}
public interface ExposedSlot<Resource, Variant extends TransferVariant<Resource>> extends Storage<Variant>, StorageView<Variant> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,8 @@
package dev.galacticraft.machinelib.api.compat.transfer;

import dev.galacticraft.machinelib.api.storage.ResourceStorage;
import dev.galacticraft.machinelib.api.storage.slot.ResourceSlot;
import dev.galacticraft.machinelib.api.transfer.ResourceFlow;
import dev.galacticraft.machinelib.impl.compat.transfer.ExposedStorageImpl;
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant;
import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant;
import net.fabricmc.fabric.api.transfer.v1.storage.Storage;
import net.fabricmc.fabric.api.transfer.v1.storage.TransferVariant;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.material.Fluid;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;

/**
* Represents a resource storage exposed to adjacent blocks or items.
Expand All @@ -43,44 +34,4 @@
* @param <Variant> the type of variant associated with the resource
*/
public interface ExposedStorage<Resource, Variant extends TransferVariant<Resource>> extends Storage<Variant> {
/**
* Creates an exposed item storage.
*
* @param storage the backing resource storage
* @param flow the flow restrictions on the storage
* @return an exposed item storage
*/
@Contract("_, _ -> new")
static @Nullable ExposedStorage<Item, ItemVariant> createItem(ResourceStorage<Item, ? extends ResourceSlot<Item>> storage, ResourceFlow flow) {
if (storage.size() == 0) return null;

ResourceSlot<Item>[] rawSlots = storage.getSlots();
ExposedSlot<Item, ItemVariant>[] slots = new ExposedSlot[rawSlots.length];
for (int i = 0; i < rawSlots.length; i++) {
slots[i] = ExposedSlot.createItem(rawSlots[i], flow);
}
return new ExposedStorageImpl<>(storage, slots);
}

/**
* Creates an exposed fluid storage.
*
* @param storage the backing resource storage
* @param flow the flow restrictions on the storage
* @return an exposed fluid storage
*/
@Contract("_, _ -> new")
static @Nullable ExposedStorage<Fluid, FluidVariant> createFluid(ResourceStorage<Fluid, ? extends ResourceSlot<Fluid>> storage, ResourceFlow flow) {
if (storage.size() == 0) return null;

ResourceSlot<Fluid>[] rawSlots = storage.getSlots();
ExposedSlot<Fluid, FluidVariant>[] slots = new ExposedSlot[rawSlots.length];
for (int i = 0; i < rawSlots.length; i++) {
slots[i] = ExposedSlot.createFluid(rawSlots[i], flow);
}
return new ExposedStorageImpl<>(storage, slots);
}

@Override
long getVersion();
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ protected MachineGameTest(MachineType<Machine, ?> type) {
public TestFunction createChargeFromEnergyItemTest(int slot, Item energyProvider) {
return this.createTest("chargeFromItem", STRUCTURE_3x3, 2, 1, helper -> {
Machine machine = this.createMachine(helper);
machine.itemStorage().getSlot(slot).set(energyProvider, 1);
machine.itemStorage().slot(slot).set(energyProvider, 1);
helper.runAfterDelay(1, () -> {
if (machine.energyStorage().isEmpty()) {
helper.fail("Machine did not charge from the stack!", machine.getBlockPos());
Expand All @@ -117,7 +117,7 @@ public TestFunction createDrainToEnergyItemTest(int slot, Item energyConsumer) {
Machine machine = this.createMachine(helper);

machine.energyStorage().setEnergy(machine.energyStorage().getCapacity());
machine.itemStorage().getSlot(slot).set(energyConsumer, 1);
machine.itemStorage().slot(slot).set(energyConsumer, 1);

helper.runAfterDelay(1, () -> {
if (machine.energyStorage().isFull()) {
Expand All @@ -132,9 +132,9 @@ public TestFunction createDrainToEnergyItemTest(int slot, Item energyConsumer) {
public TestFunction createTakeFromFluidItemTest(int slot, Item fluidProvider, int tank) {
return this.createTest("takeFluidFromItem", STRUCTURE_3x3, 2, 1, helper -> {
Machine machine = this.createMachine(helper);
machine.itemStorage().getSlot(slot).set(fluidProvider, 1);
machine.itemStorage().slot(slot).set(fluidProvider, 1);
helper.runAfterDelay(1, () -> {
if (machine.fluidStorage().getSlot(tank).isEmpty()) {
if (machine.fluidStorage().slot(tank).isEmpty()) {
helper.fail("Machine did not take fluid from the stack!", machine.getBlockPos());
} else {
helper.succeed();
Expand All @@ -147,8 +147,8 @@ public TestFunction createDrainFluidIntoItemTest(int slot, Fluid fluid, int tank
return this.createTest("drainFluidIntoItem", STRUCTURE_3x3, 2, 1, helper -> {
Machine machine = this.createMachine(helper);

machine.fluidStorage().getSlot(tank).set(fluid, FluidConstants.BUCKET);
machine.itemStorage().getSlot(slot).set(Items.BUCKET, 1);
machine.fluidStorage().slot(tank).set(fluid, FluidConstants.BUCKET);
machine.itemStorage().slot(slot).set(Items.BUCKET, 1);

helper.runAfterDelay(1, () -> {
if (machine.energyStorage().isFull()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,15 @@ protected RecipeGameTest(@NotNull MachineType<Machine, ?> type, List<IngredientS
}

protected boolean anyRecipeCrafted(@NotNull MachineItemStorage storage) {
return !storage.isEmpty(this.outputSlotsStart, this.outputSlotsLength);
for (int i = 0; i < this.outputSlotsLength; i++) {
if (!storage.slot(this.outputSlotsStart + i).isEmpty()) return true;
}
return false;
}

protected void fillOutputSlots(@NotNull MachineItemStorage storage) {
for (int i = 0; i < this.outputSlotsLength; i++) {
storage.getSlot(this.outputSlotsStart + i).set(Items.BARRIER, 1);
storage.slot(this.outputSlotsStart + i).set(Items.BARRIER, 1);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
public interface Modifiable {
/**
* {@return the number of times this object has been modified}
* Note that the number may decrease should a transaction be aborted. This could cause numbers to be reused.
*/
long getModifications();
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@

package dev.galacticraft.machinelib.api.storage;

import dev.galacticraft.machinelib.api.compat.transfer.ExposedStorage;
import dev.galacticraft.machinelib.api.storage.slot.FluidResourceSlot;
import dev.galacticraft.machinelib.api.transfer.ResourceFlow;
import dev.galacticraft.machinelib.impl.storage.MachineFluidStorageImpl;
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant;
import net.minecraft.world.level.material.Fluid;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.function.Supplier;

Expand All @@ -51,6 +55,9 @@ public interface MachineFluidStorage extends ResourceStorage<Fluid, FluidResourc
};
}

@Override
@Nullable ExposedStorage<Fluid, FluidVariant> createExposedStorage(@NotNull ResourceFlow flow);

@Contract(pure = true)
static @NotNull MachineFluidStorage empty() {
return MachineFluidStorageImpl.EMPTY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@

package dev.galacticraft.machinelib.api.storage;

import dev.galacticraft.machinelib.api.compat.transfer.ExposedStorage;
import dev.galacticraft.machinelib.api.storage.slot.ItemResourceSlot;
import dev.galacticraft.machinelib.api.transfer.InputType;
import dev.galacticraft.machinelib.api.transfer.ResourceFlow;
import dev.galacticraft.machinelib.impl.storage.MachineItemStorageImpl;
import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant;
import net.minecraft.core.component.DataComponentPatch;
import net.minecraft.world.Container;
import net.minecraft.world.item.Item;
Expand Down Expand Up @@ -67,6 +70,9 @@ public interface MachineItemStorage extends ResourceStorage<Item, ItemResourceSl
return MachineItemStorageImpl.EMPTY;
}

@Override
@Nullable ExposedStorage<Item, ItemVariant> createExposedStorage(@NotNull ResourceFlow flow);

// ITEM EXTENSIONS

boolean consumeOne(@NotNull Item resource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@

package dev.galacticraft.machinelib.api.storage;

import dev.galacticraft.machinelib.api.compat.transfer.ExposedStorage;
import dev.galacticraft.machinelib.api.misc.DeltaPacketSerializable;
import dev.galacticraft.machinelib.api.misc.MutableModifiable;
import dev.galacticraft.machinelib.api.misc.PacketSerializable;
import dev.galacticraft.machinelib.api.misc.Serializable;
import dev.galacticraft.machinelib.api.storage.slot.ResourceSlot;
import dev.galacticraft.machinelib.api.transfer.ResourceFlow;
import net.minecraft.nbt.ListTag;
import net.minecraft.network.RegistryFriendlyByteBuf;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* A storage that can store multiple of multiple instances of one type of resource (e.g., 10 sticks and 3 snowballs).
Expand All @@ -45,13 +48,5 @@ public interface ResourceStorage<Resource, Slot extends ResourceSlot<Resource>>
*/
void setListener(Runnable listener);

/**
* {@return the slots in this storage}
*/
Slot[] getSlots();

/**
* {@return the slot at the given index}
*/
@NotNull Slot getSlot(int slot);
@Nullable ExposedStorage<Resource, ?> createExposedStorage(@NotNull ResourceFlow flow);
}
Loading

0 comments on commit a37561c

Please sign in to comment.