diff --git a/src/main/java/dev/galacticraft/machinelib/api/block/entity/BasicRecipeMachineBlockEntity.java b/src/main/java/dev/galacticraft/machinelib/api/block/entity/BasicRecipeMachineBlockEntity.java index 0564783b..56c25b6c 100644 --- a/src/main/java/dev/galacticraft/machinelib/api/block/entity/BasicRecipeMachineBlockEntity.java +++ b/src/main/java/dev/galacticraft/machinelib/api/block/entity/BasicRecipeMachineBlockEntity.java @@ -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; @@ -46,10 +49,8 @@ public abstract class BasicRecipeMachineBlockEntity inputSlots; + protected final SlottedStorageAccess outputSlots; /** * Constructs a new machine block entity that processes recipes. @@ -98,10 +99,8 @@ protected BasicRecipeMachineBlockEntity(@NotNull MachineType 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(); } @@ -128,7 +127,7 @@ protected BasicRecipeMachineBlockEntity(@NotNull MachineType 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()); } /** @@ -140,7 +139,7 @@ protected void outputStacks(@NotNull RecipeHolder recipe) { @Override protected boolean canOutputStacks(@NotNull RecipeHolder 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()); } /** @@ -150,8 +149,8 @@ protected boolean canOutputStacks(@NotNull RecipeHolder recipe) { */ @Override protected void extractCraftingMaterials(@NotNull RecipeHolder recipe) { - for (int i = 0; i < this.inputSlotsLen; i++) { - this.itemStorage().consumeOne(this.inputSlots + i); + for (ItemResourceSlot slot : this.inputSlots) { + slot.consumeOne(); } } } diff --git a/src/main/java/dev/galacticraft/machinelib/api/block/entity/MachineBlockEntity.java b/src/main/java/dev/galacticraft/machinelib/api/block/entity/MachineBlockEntity.java index 94d37d61..178d7a42 100644 --- a/src/main/java/dev/galacticraft/machinelib/api/block/entity/MachineBlockEntity.java +++ b/src/main/java/dev/galacticraft/machinelib/api/block/entity/MachineBlockEntity.java @@ -98,7 +98,7 @@ /** * A block entity that represents a machine. *

- * This class handles 3 different types of storage and IO configurations: + * This class handles three different types of storage and IO configurations: * {@link MachineEnergyStorage energy}, {@link MachineItemStorage item} and {@link MachineFluidStorage fluid} storage. * * @see MachineBlock @@ -242,19 +242,19 @@ protected MachineBlockEntity(@NotNull MachineType { if (blockEntity != null) { - return ((MachineBlockEntity) blockEntity).getExposedEnergyStorage(state, context); + return ((MachineBlockEntity) blockEntity).getExposedEnergyStorage(BlockFace.from(state, context)); } return null; }, blocks); ItemStorage.SIDED.registerForBlocks((world, pos, state, blockEntity, context) -> { if (blockEntity != null) { - return ((MachineBlockEntity) blockEntity).getExposedItemStorage(state, context); + return ((MachineBlockEntity) blockEntity).getExposedItemStorage(BlockFace.from(state, context)); } return null; }, blocks); FluidStorage.SIDED.registerForBlocks((world, pos, state, blockEntity, context) -> { if (blockEntity != null) { - return ((MachineBlockEntity) blockEntity).getExposedFluidStorage(state, context); + return ((MachineBlockEntity) blockEntity).getExposedFluidStorage(BlockFace.from(state, context)); } return null; }, blocks); @@ -271,7 +271,7 @@ public static void registerComponents(@NotNull Block... blocks) { * The maximum amount of energy that the machine can insert into items in its inventory (per transaction). * * @return The maximum amount of energy that the machine can insert into items in its inventory (per transaction). - * @see #drainPowerToStack(int) + * @see #drainPowerToSlot(int) * @see #getEnergyItemExtractionRate() */ @Contract(pure = true) @@ -283,7 +283,7 @@ public long getEnergyItemInsertionRate() { * The maximum amount of energy that the machine can extract from items in its inventory (per transaction). * * @return The maximum amount of energy that the machine can extract from items in its inventory (per transaction). - * @see #chargeFromStack(int) + * @see #chargeFromSlot(int) * @see #getEnergyItemInsertionRate() */ @Contract(pure = true) @@ -372,7 +372,7 @@ public boolean areDropsDisabled() { * @see RedstoneMode */ public boolean isDisabled() { - return !this.getRedstoneMode().isActive(this.state.isPowered()); + return !this.redstone.isActive(this.state.isPowered()); } /** @@ -468,29 +468,6 @@ public boolean isActive() { return active; } - /** - * {@return a controlled/throttled energy storage to expose to adjacent blocks} - * - * @param direction the direction the adjacent block is in. - * @see #getExposedEnergyStorage(Direction, Direction) - */ - @ApiStatus.Internal - private @Nullable EnergyStorage getExposedEnergyStorage(@NotNull BlockState state, @Nullable Direction direction) { - return this.getExposedEnergyStorage(state.getValue(BlockStateProperties.HORIZONTAL_FACING), direction); - } - - /** - * {@return a controlled/throttled energy storage to expose to adjacent blocks} - * - * @param facing the direction this machine is facing. - * @param direction the direction the adjacent block is in. - * @see #getExposedEnergyStorage(BlockFace) - */ - @ApiStatus.Internal - private @Nullable EnergyStorage getExposedEnergyStorage(@NotNull Direction facing, @Nullable Direction direction) { - return this.getExposedEnergyStorage(BlockFace.toFace(facing, direction)); - } - /** * {@return a controlled/throttled energy storage to expose to adjacent blocks} * @@ -498,31 +475,8 @@ public boolean isActive() { */ @ApiStatus.Internal private @Nullable EnergyStorage getExposedEnergyStorage(@Nullable BlockFace face) { - if (face == null) return this.energyStorage; - return this.getIOConfig().get(face).getExposedEnergyStorage(this.energyStorage); - } - - /** - * {@return a controlled/throttled item storage to expose to adjacent blocks} - * - * @param direction the direction the adjacent block is in. - * @see #getExposedItemStorage(Direction, Direction) - */ - @ApiStatus.Internal - private @Nullable ExposedStorage getExposedItemStorage(@NotNull BlockState state, @Nullable Direction direction) { - return this.getExposedItemStorage(state.getValue(BlockStateProperties.HORIZONTAL_FACING), direction); - } - - /** - * {@return a controlled/throttled item storage to expose to adjacent blocks} - * - * @param facing the direction this machine is facing. - * @param direction the direction the adjacent block is in. - * @see #getExposedItemStorage(BlockFace) - */ - @ApiStatus.Internal - private @Nullable ExposedStorage getExposedItemStorage(@NotNull Direction facing, @Nullable Direction direction) { - return this.getExposedItemStorage(BlockFace.toFace(facing, direction)); + if (face == null) return this.energyStorage.getExposedStorage(ResourceFlow.BOTH); + return this.configuration.get(face).getExposedEnergyStorage(this.energyStorage); } /** @@ -532,40 +486,8 @@ public boolean isActive() { */ @ApiStatus.Internal private @Nullable ExposedStorage getExposedItemStorage(@Nullable BlockFace face) { - if (face == null) return this.createExposedItemStorage(ResourceFlow.BOTH); - return this.getIOConfig().get(face).getExposedItemStorage(this::createExposedItemStorage); - } - - /** - * Creates a new exposed item storage with the specified flow. - * @param flow the flow of the storage. - * @return the new exposed item storage. - */ - protected ExposedStorage createExposedItemStorage(ResourceFlow flow) { - return ExposedStorage.createItem(this.itemStorage(), flow); - } - - /** - * {@return a controlled/throttled fluid storage to expose to adjacent blocks} - * - * @param direction the direction the adjacent block is in. - * @see #getExposedFluidStorage(Direction, Direction) - */ - @ApiStatus.Internal - private @Nullable ExposedStorage getExposedFluidStorage(@NotNull BlockState state, @Nullable Direction direction) { - return this.getExposedFluidStorage(state.getValue(BlockStateProperties.HORIZONTAL_FACING), direction); - } - - /** - * {@return a controlled/throttled fluid storage to expose to adjacent blocks} - * - * @param facing the direction this machine is facing. - * @param direction the direction the adjacent block is in. - * @see #getExposedFluidStorage(BlockFace) - */ - @ApiStatus.Internal - private @Nullable ExposedStorage getExposedFluidStorage(@NotNull Direction facing, @Nullable Direction direction) { - return this.getExposedFluidStorage(BlockFace.toFace(facing, direction)); + if (face == null) return this.itemStorage.createExposedStorage(ResourceFlow.BOTH); + return this.configuration.get(face).getExposedItemStorage(this.itemStorage::createExposedStorage); } /** @@ -575,18 +497,8 @@ protected ExposedStorage createExposedItemStorage(ResourceFlo */ @ApiStatus.Internal private @Nullable ExposedStorage getExposedFluidStorage(@Nullable BlockFace face) { - if (face == null) return this.createExposedFluidStorage(ResourceFlow.BOTH); - return this.getIOConfig().get(face).getExposedFluidStorage(this::createExposedFluidStorage); - } - - /** - * Creates a new exposed fluid storage with the specified flow. - * - * @param flow the flow of the storage. - * @return the new exposed fluid storage. - */ - protected ExposedStorage createExposedFluidStorage(ResourceFlow flow) { - return ExposedStorage.createFluid(this.fluidStorage(), flow); + if (face == null) return this.fluidStorage.createExposedStorage(ResourceFlow.BOTH); + return this.configuration.get(face).getExposedFluidStorage(this.fluidStorage::createExposedStorage); } /** @@ -622,7 +534,7 @@ public void loadAdditional(CompoundTag tag, HolderLookup.Provider lookup) { if (tag.contains(Constant.Nbt.SECURITY, Tag.TAG_COMPOUND)) this.security.readTag(tag.getCompound(Constant.Nbt.SECURITY)); if (tag.contains(Constant.Nbt.REDSTONE_MODE, Tag.TAG_BYTE)) - this.redstone = RedstoneMode.readTag((ByteTag) tag.get(Constant.Nbt.REDSTONE_MODE)); + this.redstone = RedstoneMode.readTag(tag.get(Constant.Nbt.REDSTONE_MODE)); if (tag.contains(Constant.Nbt.STATE, Tag.TAG_COMPOUND)) this.state.readTag(tag.getCompound(Constant.Nbt.STATE)); if (tag.contains(Constant.Nbt.ENERGY_STORAGE, Tag.TAG_LONG)) @@ -650,7 +562,7 @@ protected void trySpreadEnergy(@NotNull ServerLevel level, @NotNull BlockState s } Direction facing = state.getValue(BlockStateProperties.HORIZONTAL_FACING); for (Direction direction : Constant.Cache.DIRECTIONS) { - EnergyStorage storage = this.getExposedEnergyStorage(facing, direction); + EnergyStorage storage = this.getExposedEnergyStorage(BlockFace.from(facing, direction)); if (storage != null && storage.supportsExtraction()) { EnergyStorageUtil.move(storage, this.energyCache.find(direction), Long.MAX_VALUE, null); } @@ -669,7 +581,7 @@ protected void trySpreadFluids(@NotNull ServerLevel level, @NotNull BlockState s } Direction facing = state.getValue(BlockStateProperties.HORIZONTAL_FACING); for (Direction direction : Constant.Cache.DIRECTIONS) { - ExposedStorage storage = this.getExposedFluidStorage(facing, direction); + ExposedStorage storage = this.getExposedFluidStorage(BlockFace.from(facing, direction)); if (storage != null && storage.supportsExtraction()) { StorageHelper.moveAll(storage, this.fluidCache.find(direction), Long.MAX_VALUE, null); //TODO: fluid I/O cap } @@ -679,8 +591,8 @@ protected void trySpreadFluids(@NotNull ServerLevel level, @NotNull BlockState s /** * Pushes items from this machine to adjacent item storages. * - * @param level the level. - * @param state the machine's block state. + * @param level the level + * @param state the machine's block state */ protected void trySpreadItems(@NotNull ServerLevel level, @NotNull BlockState state) { if (this.itemCache == null) { @@ -688,7 +600,7 @@ protected void trySpreadItems(@NotNull ServerLevel level, @NotNull BlockState st } Direction facing = state.getValue(BlockStateProperties.HORIZONTAL_FACING); for (Direction direction : Constant.Cache.DIRECTIONS) { - Storage storage = this.getExposedItemStorage(facing, direction); + Storage storage = this.getExposedItemStorage(BlockFace.from(facing, direction)); if (storage != null && storage.supportsExtraction()) { StorageHelper.moveAll(storage, this.itemCache.find(direction), Long.MAX_VALUE, null); } @@ -700,10 +612,10 @@ protected void trySpreadItems(@NotNull ServerLevel level, @NotNull BlockState st * * @param slot the index of the input slot. */ - protected void chargeFromStack(int slot) { + protected void chargeFromSlot(int slot) { if (this.energyStorage().isFull()) return; - EnergyStorage energyStorage = this.itemStorage.getSlot(slot).find(EnergyStorage.ITEM); + EnergyStorage energyStorage = this.itemStorage.slot(slot).find(EnergyStorage.ITEM); if (energyStorage != null && energyStorage.supportsExtraction()) { EnergyStorageUtil.move(energyStorage, this.energyStorage, this.getEnergyItemExtractionRate(), null); } @@ -714,9 +626,9 @@ protected void chargeFromStack(int slot) { * * @param slot the index of the input slot. */ - protected void drainPowerToStack(int slot) { + protected void drainPowerToSlot(int slot) { if (this.energyStorage().isEmpty()) return; - EnergyStorage energyStorage = this.itemStorage.getSlot(slot).find(EnergyStorage.ITEM); + EnergyStorage energyStorage = this.itemStorage.slot(slot).find(EnergyStorage.ITEM); if (energyStorage != null && energyStorage.supportsInsertion()) { EnergyStorageUtil.move(this.energyStorage, energyStorage, this.getEnergyItemInsertionRate(), null); } @@ -730,10 +642,10 @@ protected void drainPowerToStack(int slot) { * @param tankSlot the index of the tank where the fluid will be moved to * @param fluid the fluid to be extracted and stored */ - protected void takeFluidFromStack(int inputSlot, int tankSlot, @NotNull Fluid fluid) { - FluidResourceSlot tank = this.fluidStorage().getSlot(tankSlot); + protected void takeFluidFromSlot(int inputSlot, int tankSlot, @NotNull Fluid fluid) { + FluidResourceSlot tank = this.fluidStorage().slot(tankSlot); if (tank.isFull()) return; - ItemResourceSlot slot = this.itemStorage.getSlot(inputSlot); + ItemResourceSlot slot = this.itemStorage.slot(inputSlot); Storage storage = slot.find(FluidStorage.ITEM); if (storage != null && storage.supportsExtraction()) { StorageHelper.move(FluidVariant.of(fluid), storage, tank, Integer.MAX_VALUE, null); @@ -741,21 +653,36 @@ protected void takeFluidFromStack(int inputSlot, int tankSlot, @NotNull Fluid fl } /** - * Tries to insert the specified fluid into the item in the input slot - * from the tank of the fluid storage. + * Tries to extract fluids from the item in the input slot that match the slot's filter + * and moves them into the tank of the fluid storage. + * + * @param inputSlot the index of the input slot from which the fluid will be extracted + * @param tankSlot the index of the tank where the fluid will be moved to + */ + protected void takeFluidFromSlot(int inputSlot, int tankSlot) { + FluidResourceSlot tank = this.fluidStorage().slot(tankSlot); + if (tank.isFull()) return; + ItemResourceSlot slot = this.itemStorage.slot(inputSlot); + Storage storage = slot.find(FluidStorage.ITEM); + if (storage != null && storage.supportsExtraction()) { + StorageHelper.move(storage, tank, Integer.MAX_VALUE, null); + } + } + + /** + * Tries to extract fluids from the fluid storage and moves them into the tank of the item in the input slot. * * @param inputSlot the index of the input slot where the fluid will be inserted * @param tankSlot the index of the tank from which the fluid will be extracted - * @param fluid the fluid to be inserted into the item */ - protected void insertFluidToStack(int inputSlot, int tankSlot, @NotNull Fluid fluid) { - FluidResourceSlot tank = this.fluidStorage().getSlot(tankSlot); + protected void drainFluidToSlot(int inputSlot, int tankSlot) { + FluidResourceSlot tank = this.fluidStorage().slot(tankSlot); if (tank.isEmpty()) return; - ItemResourceSlot slot = this.itemStorage.getSlot(inputSlot); + ItemResourceSlot slot = this.itemStorage.slot(inputSlot); Storage storage = slot.find(FluidStorage.ITEM); if (storage != null && storage.supportsInsertion()) { - StorageHelper.move(FluidVariant.of(fluid), tank, storage, Integer.MAX_VALUE, null); + StorageHelper.move(tank, storage, Integer.MAX_VALUE, null); } } diff --git a/src/main/java/dev/galacticraft/machinelib/api/compat/transfer/ExposedSlot.java b/src/main/java/dev/galacticraft/machinelib/api/compat/transfer/ExposedSlot.java index c96e6e55..d33dbdb7 100644 --- a/src/main/java/dev/galacticraft/machinelib/api/compat/transfer/ExposedSlot.java +++ b/src/main/java/dev/galacticraft/machinelib/api/compat/transfer/ExposedSlot.java @@ -22,20 +22,9 @@ 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. @@ -43,54 +32,5 @@ * @param The type of resource stored in the slot. * @param The type of variant for the resource that can be stored in the slot. */ -public interface ExposedSlot> extends ExposedStorage, SingleSlotStorage { - /** - * 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 createItem(@NotNull ResourceSlot 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 createFluid(@NotNull ResourceSlot 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 createFullItem(@NotNull ResourceSlot 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 createFullFluid(@NotNull ResourceSlot slot) { - return new ExposedFullFluidSlotImpl(slot); - } +public interface ExposedSlot> extends Storage, StorageView { } diff --git a/src/main/java/dev/galacticraft/machinelib/api/compat/transfer/ExposedStorage.java b/src/main/java/dev/galacticraft/machinelib/api/compat/transfer/ExposedStorage.java index 8f0dab42..15856b94 100644 --- a/src/main/java/dev/galacticraft/machinelib/api/compat/transfer/ExposedStorage.java +++ b/src/main/java/dev/galacticraft/machinelib/api/compat/transfer/ExposedStorage.java @@ -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. @@ -43,44 +34,4 @@ * @param the type of variant associated with the resource */ public interface ExposedStorage> extends Storage { - /** - * 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 createItem(ResourceStorage> storage, ResourceFlow flow) { - if (storage.size() == 0) return null; - - ResourceSlot[] rawSlots = storage.getSlots(); - ExposedSlot[] 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 createFluid(ResourceStorage> storage, ResourceFlow flow) { - if (storage.size() == 0) return null; - - ResourceSlot[] rawSlots = storage.getSlots(); - ExposedSlot[] 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(); } diff --git a/src/main/java/dev/galacticraft/machinelib/api/gametest/MachineGameTest.java b/src/main/java/dev/galacticraft/machinelib/api/gametest/MachineGameTest.java index c9d5ed13..9ab50ffb 100644 --- a/src/main/java/dev/galacticraft/machinelib/api/gametest/MachineGameTest.java +++ b/src/main/java/dev/galacticraft/machinelib/api/gametest/MachineGameTest.java @@ -101,7 +101,7 @@ protected MachineGameTest(MachineType 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()); @@ -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()) { @@ -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(); @@ -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()) { diff --git a/src/main/java/dev/galacticraft/machinelib/api/gametest/RecipeGameTest.java b/src/main/java/dev/galacticraft/machinelib/api/gametest/RecipeGameTest.java index 1e0c4531..fffc842c 100644 --- a/src/main/java/dev/galacticraft/machinelib/api/gametest/RecipeGameTest.java +++ b/src/main/java/dev/galacticraft/machinelib/api/gametest/RecipeGameTest.java @@ -73,12 +73,15 @@ protected RecipeGameTest(@NotNull MachineType type, List createExposedStorage(@NotNull ResourceFlow flow); + @Contract(pure = true) static @NotNull MachineFluidStorage empty() { return MachineFluidStorageImpl.EMPTY; diff --git a/src/main/java/dev/galacticraft/machinelib/api/storage/MachineItemStorage.java b/src/main/java/dev/galacticraft/machinelib/api/storage/MachineItemStorage.java index bd762622..01537ff7 100644 --- a/src/main/java/dev/galacticraft/machinelib/api/storage/MachineItemStorage.java +++ b/src/main/java/dev/galacticraft/machinelib/api/storage/MachineItemStorage.java @@ -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; @@ -67,6 +70,9 @@ public interface MachineItemStorage extends ResourceStorage createExposedStorage(@NotNull ResourceFlow flow); + // ITEM EXTENSIONS boolean consumeOne(@NotNull Item resource); diff --git a/src/main/java/dev/galacticraft/machinelib/api/storage/ResourceStorage.java b/src/main/java/dev/galacticraft/machinelib/api/storage/ResourceStorage.java index bf5bcd51..601e1137 100644 --- a/src/main/java/dev/galacticraft/machinelib/api/storage/ResourceStorage.java +++ b/src/main/java/dev/galacticraft/machinelib/api/storage/ResourceStorage.java @@ -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). @@ -45,13 +48,5 @@ public interface ResourceStorage> */ 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 createExposedStorage(@NotNull ResourceFlow flow); } diff --git a/src/main/java/dev/galacticraft/machinelib/api/storage/SlottedStorageAccess.java b/src/main/java/dev/galacticraft/machinelib/api/storage/SlottedStorageAccess.java index 22393213..244a6031 100644 --- a/src/main/java/dev/galacticraft/machinelib/api/storage/SlottedStorageAccess.java +++ b/src/main/java/dev/galacticraft/machinelib/api/storage/SlottedStorageAccess.java @@ -22,11 +22,8 @@ package dev.galacticraft.machinelib.api.storage; -import dev.galacticraft.machinelib.api.filter.ResourceFilter; -import dev.galacticraft.machinelib.api.storage.slot.ResourceSlot; -import net.minecraft.core.component.DataComponentPatch; +import dev.galacticraft.machinelib.api.misc.Modifiable; 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). @@ -35,7 +32,7 @@ * @param The type of slot this storage contains. * @see StorageAccess */ -public interface SlottedStorageAccess> extends StorageAccess, Iterable { +public interface SlottedStorageAccess> extends StorageAccess, Modifiable, Iterable { /** * {@return the number of slots in this storage} */ @@ -56,562 +53,17 @@ public interface SlottedStorageAccess subStorage(int... slots); - // START SLOT METHODS - - /** - * {@return the resource stored in the slot at the given index} - * - * @param slot the index of the slot - * @see ResourceSlot#getResource() - */ - @Nullable Resource getResource(int slot); - - /** - * {@return the amount of the resource stored in the slot at the given index} - * - * @param slot the index of the slot - * @see ResourceSlot#getAmount() - */ - long getAmount(int slot); - - /** - * {@return the components of the resource stored in the slot at the given index} - * - * @param slot the index of the slot - * @see ResourceSlot#getComponents() - */ - @Nullable DataComponentPatch getComponents(int slot); - - /** - * {@return the capacity of the slot at the given index} - * - * @param slot the index of the slot - * @see ResourceSlot#getCapacity() - */ - long getCapacity(int slot); - - /** - * {@return the capacity of the slot at the given index for a specific resource} - * - * @param slot the index of the slot - * @param resource the resource to check the capacity for - * @param components the components of the resource to check the capacity for - * @see ResourceSlot#getCapacityFor(Object, DataComponentPatch) - */ - long getCapacityFor(int slot, @NotNull Resource resource, @NotNull DataComponentPatch components); - - /** - * {@return the real capacity of the slot at the given index} - * - * @param slot the index of the slot - * @see ResourceSlot#getRealCapacity() - */ - long getRealCapacity(int slot); - - /** - * {@return the filter for the slot at the given index} - * - * @param slot the index of the slot - * @see ResourceSlot#getFilter() - */ - @NotNull ResourceFilter getFilter(int slot); - - /** - * {@return whether the slot at the given index is empty} - * - * @param slot the index of the slot - * @see ResourceSlot#isEmpty() - */ - boolean isEmpty(int slot); - - /** - * {@return whether the slot at the given index is full} - * - * @param slot the index of the slot - * @see ResourceSlot#isFull() - */ - boolean isFull(int slot); - - /** - * {@return whether the slot at the given index can accept the given resource} - * - * @param slot the index of the slot - * @param resource the resource to check - * @see ResourceSlot#canInsert(Object) - */ - boolean canInsert(int slot, @NotNull Resource resource); - - /** - * {@return whether the slot at the given index can accept the given resource} - * - * @param slot the index of the slot - * @param resource the resource to check - * @param components the components of the resource to check - * @see ResourceSlot#canInsert(Object, DataComponentPatch) - */ - boolean canInsert(int slot, @NotNull Resource resource, @NotNull DataComponentPatch components); - - /** - * {@return whether the slot at the given index can accept the given amount of the given resource} - * - * @param slot the index of the slot - * @param resource the resource to check - * @param amount the amount of the resource to check - * @see ResourceSlot#canInsert(Object, long) - */ - boolean canInsert(int slot, @NotNull Resource resource, long amount); - - /** - * {@return whether the slot at the given index can accept the given amount of the given resource} - * - * @param slot the index of the slot - * @param resource the resource to check - * @param components the components of the resource to check - * @param amount the amount of the resource to check - * @see ResourceSlot#canInsert(Object, DataComponentPatch, long) - */ - boolean canInsert(int slot, @NotNull Resource resource, @NotNull DataComponentPatch components, long amount); - - /** - * {@return the amount of the given resource that the slot at the given index can accept} - * - * @param slot the index of the slot - * @param resource the resource to check - * @param amount the amount of the resource to check - * @see ResourceSlot#tryInsert(Object, DataComponentPatch, long) - */ - long tryInsert(int slot, @NotNull Resource resource, long amount); - - /** - * {@return the amount of the given resource that the slot at the given index can accept} - * - * @param slot the index of the slot - * @param resource the resource to check - * @param components the components of the resource to check - * @param amount the amount of the resource to check - * @see ResourceSlot#tryInsert(Object, DataComponentPatch, long) - */ - long tryInsert(int slot, @NotNull Resource resource, @NotNull DataComponentPatch components, long amount); - /** - * Inserts the given amount of the given resource into the slot at the given index. - * - * @param slot the index of the slot - * @param resource the resource to insert - * @param amount the amount of the resource to insert - * @return the amount of the resource that was inserted - * @see ResourceSlot#insert(Object, DataComponentPatch, long) + * {@return the slots in this storage} + * Do not modify the returned array. */ - long insert(int slot, @NotNull Resource resource, long amount); + Slot[] getSlots(); /** - * Inserts the given amount of the given resource into the slot at the given index. - * - * @param slot the index of the slot - * @param resource the resource to insert - * @param components the components of the resource to insert - * @param amount the amount of the resource to insert - * @return the amount of the resource that was inserted - * @see ResourceSlot#insert(Object, DataComponentPatch, long) - */ - long insert(int slot, @NotNull Resource resource, @NotNull DataComponentPatch components, long amount); - - /** - * {@return whether the slot at the given index contains the given resource} - * - * @param slot the index of the slot - * @param resource the resource to check - * @see ResourceSlot#contains(Object) - */ - boolean contains(int slot, @NotNull Resource resource); - - /** - * {@return whether the slot at the given index contains the given resource} - * - * @param slot the index of the slot - * @param resource the resource to check - * @param components the components of the resource to check - * @see ResourceSlot#contains(Object, DataComponentPatch) - */ - boolean contains(int slot, @NotNull Resource resource, @Nullable DataComponentPatch components); - - /** - * {@return whether the slot at the given index can extract the given amount resources} - * - * @param slot the index of the slot - * @param amount the amount of the resource to check - * @see ResourceSlot#canExtract(Object, long) - */ - boolean canExtract(int slot, long amount); - - /** - * {@return whether the slot at the given index can extract the given amount of the given resource} - * - * @param slot the index of the slot - * @param resource the resource to check - * @param amount the amount of the resource to check - * @see ResourceSlot#canExtract(Object, long) - */ - boolean canExtract(int slot, @NotNull Resource resource, long amount); - - /** - * {@return whether the slot at the given index can extract the given amount of the given resource} - * - * @param slot the index of the slot - * @param resource the resource to check - * @param components the components of the resource to check - * @param amount the amount of the resource to check - * @see ResourceSlot#canExtract(Object, DataComponentPatch, long) - */ - boolean canExtract(int slot, @NotNull Resource resource, @Nullable DataComponentPatch components, long amount); - - /** - * {@return the amount of the given resource that the slot at the given index can extract} - * - * @param slot the index of the slot - * @param amount the amount of the resource to check - * @see ResourceSlot#tryExtract(long) - */ - long tryExtract(int slot, long amount); - - /** - * {@return the amount of the given resource that the slot at the given index can extract} - * - * @param slot the index of the slot - * @param resource the resource to check - * @param amount the amount of the resource to check - * @see ResourceSlot#tryExtract(Object, DataComponentPatch, long) - */ - long tryExtract(int slot, @NotNull Resource resource, long amount); - - /** - * {@return the amount of the given resource that the slot at the given index can extract} - * - * @param slot the index of the slot - * @param resource the resource to check - * @param components the components of the resource to check - * @param amount the amount of the resource to check - * @see ResourceSlot#tryExtract(Object, DataComponentPatch, long) - */ - long tryExtract(int slot, @NotNull Resource resource, @Nullable DataComponentPatch components, long amount); - - /** - * Extracts a single resource from the slot at the given index. - * - * @param slot the index of the slot - * @return the extracted resource, or {@code null} if the slot is empty - * @see ResourceSlot#extractOne() - */ - @Nullable Resource extractOne(int slot); - - /** - * Extracts the given amount of resources from the slot at the given index. - * - * @param slot the index of the slot - * @param resource the resource to extract - * @return whether a resource was extracted - * @see ResourceSlot#extract(long) - */ - boolean extractOne(int slot, @NotNull Resource resource); - - /** - * Extracts the given amount of resources from the slot at the given index. - * - * @param slot the index of the slot - * @param resource the resource to extract - * @param components the components of the resource to extract - * @return whether a resource was extracted - * @see ResourceSlot#extract(long) - */ - boolean extractOne(int slot, @NotNull Resource resource, @Nullable DataComponentPatch components); - - /** - * Extracts the given amount of resources from the slot at the given index. - * - * @param slot the index of the slot - * @param amount the amount of the resource to extract - * @return the amount of resources extracted - * @see ResourceSlot#extract(long) - */ - long extract(int slot, long amount); - - /** - * Extracts the given amount of resources from the slot at the given index. - * - * @param slot the index of the slot - * @param resource the resource to extract - * @param amount the amount of the resource to extract - * @return the amount of resources extracted - * @see ResourceSlot#extract(long) - */ - long extract(int slot, @NotNull Resource resource, long amount); - - /** - * Extracts the given amount of resources from the slot at the given index. - * - * @param slot the index of the slot - * @param resource the resource to extract - * @param components the components of the resource to extract - * @param amount the amount of the resource to extract - * @return the amount of resources extracted - * @see ResourceSlot#extract(long) - */ - long extract(int slot, @NotNull Resource resource, @Nullable DataComponentPatch components, long amount); - - // END SLOT METHODS - - // START RANGE METHODS - - /** - * {@return whether all slots in the given range of slots are empty} - * - * @param start the start of the range (inclusive) - * @param len the length of the range - * @see ResourceSlot#isEmpty() - */ - boolean isEmpty(int start, int len); - - /** - * {@return whether all slots in the given range of slots are full} - * - * @param start the start of the range (inclusive) - * @param len the length of the range - * @see ResourceSlot#isFull() - */ - boolean isFull(int start, int len); - - /** - * {@return whether any of the slots in the given range of slots can accept the given resource} - * - * @param start the start of the range (inclusive) - * @param len the length of the range - * @param resource the resource to check - * @see ResourceSlot#canInsert(Object) - */ - boolean canInsert(int start, int len, @NotNull Resource resource); - - /** - * {@return whether any of the slots in the given range of slots can accept the given resource} - * - * @param start the start of the range (inclusive) - * @param len the length of the range - * @param resource the resource to check - * @param components the components of the resource to check - * @see ResourceSlot#canInsert(Object, DataComponentPatch) - */ - boolean canInsert(int start, int len, @NotNull Resource resource, @NotNull DataComponentPatch components); - - /** - * {@return whether any combination of the slots in the given range of slots can accept the given amount of the given resource} - * - * @param start the start of the range (inclusive) - * @param len the length of the range - * @param resource the resource to check - * @param amount the amount of the resource to check - * @see ResourceSlot#canInsert(Object, long) - */ - boolean canInsert(int start, int len, @NotNull Resource resource, long amount); - - /** - * {@return whether any combination of the slots in the given range of slots can accept the given amount of the given resource} - * - * @param start the start of the range (inclusive) - * @param len the length of the range - * @param resource the resource to check - * @param components the components of the resource to check - * @param amount the amount of the resource to check - * @see ResourceSlot#canInsert(Object, DataComponentPatch, long) - */ - boolean canInsert(int start, int len, @NotNull Resource resource, @NotNull DataComponentPatch components, long amount); - - /** - * {@return the amount of the given resource that the slots in the given range of slots can accept} - * - * @param start the start of the range (inclusive) - * @param len the length of the range - * @param resource the resource to check - * @param amount the amount of the resource to check - * @see ResourceSlot#tryInsert(Object, DataComponentPatch, long) - */ - long tryInsert(int start, int len, @NotNull Resource resource, long amount); - - /** - * {@return the amount of the given resource that the slots in the given range of slots can accept} - * - * @param start the start of the range (inclusive) - * @param len the length of the range - * @param resource the resource to check - * @param components the components of the resource to check - * @param amount the amount of the resource to check - * @see ResourceSlot#tryInsert(Object, DataComponentPatch, long) - */ - long tryInsert(int start, int len, @NotNull Resource resource, @NotNull DataComponentPatch components, long amount); - - /** - * Inserts the given amount of the given resource into the slots in the given range of slots. - * - * @param start the start of the range (inclusive) - * @param len the length of the range - * @param resource the resource to insert - * @param amount the amount of the resource to insert - * @return the amount of the resource that was inserted - * @see ResourceSlot#insert(Object, DataComponentPatch, long) - */ - long insert(int start, int len, @NotNull Resource resource, long amount); - - /** - * Inserts the given amount of the given resource into the slots in the given range of slots. - * - * @param start the start of the range (inclusive) - * @param len the length of the range - * @param resource the resource to insert - * @param components the components of the resource to insert - * @param amount the amount of the resource to insert - * @return the amount of the resource that was inserted - * @see ResourceSlot#insert(Object, DataComponentPatch, long) - */ - long insert(int start, int len, @NotNull Resource resource, @NotNull DataComponentPatch components, long amount); - - /** - * Inserts the given amount of the given resource into the slots in the given range of slots. - * If the resource is already present in the range, the resources are first inserted onto the existing resources. - * - * @param start the start of the range (inclusive) - * @param len the length of the range - * @param resource the resource to insert - * @param amount the amount of the resource to insert - * @return the amount of the resource that was inserted - * @see ResourceSlot#contains(Object) - */ - long insertMatching(int start, int len, @NotNull Resource resource, long amount); - - /** - * Inserts the given amount of the given resource into the slots in the given range of slots. - * If the resource is already present in the range, the resources are first inserted onto the existing resources. - * - * @param start the start of the range (inclusive) - * @param len the length of the range - * @param resource the resource to insert - * @param components the components of the resource to insert - * @param amount the amount of the resource to insert - * @return the amount of the resource that was inserted - * @see ResourceSlot#contains(Object, DataComponentPatch) - */ - long insertMatching(int start, int len, @NotNull Resource resource, @NotNull DataComponentPatch components, long amount); - - /** - * {@return whether any of the slots in the given range of slots contains the given resource} - * - * @param start the start of the range (inclusive) - * @param len the length of the range - * @param resource the resource to check - * @see ResourceSlot#contains(Object) - */ - boolean contains(int start, int len, @NotNull Resource resource); - - /** - * {@return whether any of the slots in the given range of slots contains the given resource} - * - * @param start the start of the range (inclusive) - * @param len the length of the range - * @param resource the resource to check - * @param components the components of the resource to check - * @see ResourceSlot#contains(Object, DataComponentPatch) - */ - boolean contains(int start, int len, @NotNull Resource resource, @Nullable DataComponentPatch components); - - /** - * {@return whether any of the slots in the given range of slots can extract the given amount resources} - * - * @param start the start of the range (inclusive) - * @param len the length of the range - * @param resource the resource to check - * @param amount the amount of the resource to check - * @see ResourceSlot#canExtract(Object, long) - */ - boolean canExtract(int start, int len, @NotNull Resource resource, long amount); - - /** - * {@return whether any of the slots in the given range of slots can extract the given amount of the given resource} - * - * @param start the start of the range (inclusive) - * @param len the length of the range - * @param resource the resource to check - * @param components the components of the resource to check - * @param amount the amount of the resource to check - * @see ResourceSlot#canExtract(Object, DataComponentPatch, long) - */ - boolean canExtract(int start, int len, @NotNull Resource resource, @Nullable DataComponentPatch components, long amount); - - /** - * {@return the amount of the given resource that the slots in the given range of slots can extract} - * - * @param start the start of the range (inclusive) - * @param len the length of the range - * @param resource the resource to check - * @param amount the amount of the resource to check - * @see ResourceSlot#tryExtract(Object, long) - */ - long tryExtract(int start, int len, @NotNull Resource resource, long amount); - - /** - * {@return the amount of the given resource that the slots in the given range of slots can extract} - * - * @param start the start of the range (inclusive) - * @param len the length of the range - * @param resource the resource to check - * @param components the components of the resource to check - * @param amount the amount of the resource to check - * @see ResourceSlot#tryExtract(Object, DataComponentPatch, long) - */ - long tryExtract(int start, int len, @NotNull Resource resource, @Nullable DataComponentPatch components, long amount); - - /** - * Extracts a single resource from the slots in the given range of slots. - * - * @param start the start of the range (inclusive) - * @param len the length of the range - * @param resource the resource to extract - * @return the extracted resource, or {@code null} if no resource was extracted - * @see ResourceSlot#extractOne(Object) - */ - boolean extractOne(int start, int len, @NotNull Resource resource); - - /** - * Extracts a single resource from the slots in the given range of slots. - * - * @param start the start of the range (inclusive) - * @param len the length of the range - * @param resource the resource to extract - * @param components the components of the resource to extract - * @return whether a resource was extracted - * @see ResourceSlot#extractOne(Object, DataComponentPatch) - */ - boolean extractOne(int start, int len, @NotNull Resource resource, @Nullable DataComponentPatch components); - - /** - * Extracts the given amount of resources from the slots in the given range of slots. - * - * @param start the start of the range (inclusive) - * @param len the length of the range - * @param amount the amount of the resource to extract - * @return the amount of resources extracted - * @see ResourceSlot#extract(Object, long) - */ - long extract(int start, int len, @NotNull Resource resource, long amount); - - /** - * Extracts the given amount of resources from the slots in the given range of slots. - * - * @param start the start of the range (inclusive) - * @param len the length of the range - * @param resource the resource to extract - * @param components the components of the resource to extract - * @param amount the amount of the resource to extract - * @return the amount of resources extracted - * @see ResourceSlot#extract(Object, DataComponentPatch, long) + * {@return the slot at the given index} */ - long extract(int start, int len, @NotNull Resource resource, @Nullable DataComponentPatch components, long amount); + @NotNull Slot slot(int slot); - // END RANGE METHODS + @Override + long getModifications(); } diff --git a/src/main/java/dev/galacticraft/machinelib/api/storage/StorageAccess.java b/src/main/java/dev/galacticraft/machinelib/api/storage/StorageAccess.java index bf61eac3..1f863c74 100644 --- a/src/main/java/dev/galacticraft/machinelib/api/storage/StorageAccess.java +++ b/src/main/java/dev/galacticraft/machinelib/api/storage/StorageAccess.java @@ -95,7 +95,7 @@ default boolean canInsert(@NotNull Resource resource, long amount) { * except it assumes an empty component patch. * * @param resource the resource being inserted - * @param amount the amount of the resource being inserted + * @param amount the amount of the resource that can be inserted * @see #tryInsert(Resource, DataComponentPatch, long) */ default long tryInsert(@NotNull Resource resource, long amount) { @@ -108,7 +108,7 @@ default long tryInsert(@NotNull Resource resource, long amount) { * * @param resource the resource being inserted * @param components the components of the resource - * @param amount the amount of the resource being inserted + * @param amount the amount of the resource that can be inserted */ long tryInsert(@NotNull Resource resource, @NotNull DataComponentPatch components, long amount); @@ -208,7 +208,7 @@ default boolean canExtract(@NotNull Resource resource, long amount) { * except it ignores the components of the extracted resources. * * @param resource the resource being extracted - * @param amount the amount of the resource being extracted + * @param amount the amount of the resource that can be extracted */ default long tryExtract(@NotNull Resource resource, long amount) { return this.tryExtract(resource, null, amount); @@ -219,7 +219,7 @@ default long tryExtract(@NotNull Resource resource, long amount) { * * @param resource the resource being extracted * @param components the components of the resource. If {@code null}, the components will be ignored - * @param amount the amount of the resource being extracted + * @param amount the amount of the resource that can be extracted */ long tryExtract(@NotNull Resource resource, @Nullable DataComponentPatch components, long amount); diff --git a/src/main/java/dev/galacticraft/machinelib/api/util/BlockFace.java b/src/main/java/dev/galacticraft/machinelib/api/util/BlockFace.java index f1c794b6..559d05b5 100644 --- a/src/main/java/dev/galacticraft/machinelib/api/util/BlockFace.java +++ b/src/main/java/dev/galacticraft/machinelib/api/util/BlockFace.java @@ -30,6 +30,8 @@ import net.minecraft.network.codec.ByteBufCodecs; import net.minecraft.network.codec.StreamCodec; import net.minecraft.util.StringRepresentable; +import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.level.block.state.properties.BlockStateProperties; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -80,8 +82,8 @@ public enum BlockFace implements StringRepresentable { /** * Constructs a block face. * - * @param name The name of the block face. - * @param side Whether the block face is a side face or not. + * @param name the name of the block face + * @param side whether the block face is a side face or not */ BlockFace(String id, @NotNull MutableComponent name, boolean side) { this.id = id; @@ -89,15 +91,26 @@ public enum BlockFace implements StringRepresentable { this.side = side; } + /** + * Gets the face corresponding to the given direction and rotation (derived from the block state). + * + * @param state the block state to get the rotation from + * @param target the direction to get the face for + * @return the face corresponding to the given direction and rotation + */ + public static @Nullable BlockFace from(@NotNull BlockState state, @Nullable Direction target) { + return from(state.getValue(BlockStateProperties.HORIZONTAL_FACING), target); + } + /** * Gets the face corresponding to the given direction and rotation. * - * @param facing The rotation to get the face for. - * @param target The direction to get the face for. - * @return The face corresponding to the given direction and rotation. + * @param facing the rotation to get the face for + * @param target the direction to get the face for + * @return the face corresponding to the given direction and rotation */ @Contract(pure = true, value = "_, null -> null; _, !null -> !null") - public static @Nullable BlockFace toFace(@NotNull Direction facing, @Nullable Direction target) { //todo: a better way to do this? + public static @Nullable BlockFace from(@NotNull Direction facing, @Nullable Direction target) { //todo: a better way to do this? assert facing != Direction.UP && facing != Direction.DOWN; if (target == null) return null; diff --git a/src/main/java/dev/galacticraft/machinelib/api/util/StorageHelper.java b/src/main/java/dev/galacticraft/machinelib/api/util/StorageHelper.java index 9a2eb9d3..0e5c5399 100644 --- a/src/main/java/dev/galacticraft/machinelib/api/util/StorageHelper.java +++ b/src/main/java/dev/galacticraft/machinelib/api/util/StorageHelper.java @@ -24,6 +24,11 @@ import dev.galacticraft.machinelib.api.filter.ResourceFilter; import dev.galacticraft.machinelib.api.storage.StorageAccess; +import dev.galacticraft.machinelib.api.storage.slot.FluidResourceSlot; +import dev.galacticraft.machinelib.api.storage.slot.ItemResourceSlot; +import dev.galacticraft.machinelib.api.storage.slot.ResourceSlot; +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.StoragePreconditions; import net.fabricmc.fabric.api.transfer.v1.storage.StorageView; @@ -37,30 +42,53 @@ /** * Utility methods for dealing with {@link Storage storages}. */ -@SuppressWarnings("UnstableApiUsage") public final class StorageHelper { @Contract(value = " -> fail", pure = true) private StorageHelper() { throw new UnsupportedOperationException("Utility class cannot be instantiated"); } - public static , S extends Storage> long calculateCapacity(Variant variant, @NotNull S storage, @Nullable TransactionContext context) { - if (variant.isBlank()) return 0; - + public static > long theoreticalCapacity(@NotNull Storage storage) { long capacity = 0; for (StorageView view : storage) { - if (variant.equals(view.getResource())) { - capacity += view.getCapacity(); + capacity += view.getCapacity(); + } + + return capacity; + } + + /** + * Calculates the capacity for a specific variant in a storage. + * @param variant the variant to calculate the capacity for + * @param storage the storage to calculate the capacity in + * @param context the transaction context to use + * @param the type of variant + * + * @return the capacity of the resource in the storage + */ + public static > long capacity(@NotNull Variant variant, @NotNull Storage storage, @Nullable TransactionContext context) { + if (variant.isBlank()) return theoreticalCapacity(storage); + + long capacity = 0; + try (Transaction transaction = Transaction.openNested(context)) { + for (int i = 0; i < 128; i++) { + if (storage.insert(variant, Integer.MAX_VALUE / 2, transaction) <= 0) break; + } + + for (StorageView view : storage) { + if (variant.equals(view.getResource())) { + capacity += view.getCapacity(); + } } } return capacity; } - public static , S extends Storage> long calculateAmount(Variant variant, @NotNull S storage, @Nullable TransactionContext context) { + public static > long calculateAmount(Variant variant, @NotNull Storage storage) { if (variant.isBlank()) return 0; long amount = 0; - for (StorageView view : storage) { + for (StorageView view : storage.nonEmptyViews()) { if (variant.equals(view.getResource())) { amount += view.getAmount(); } @@ -68,19 +96,34 @@ public static , S extends St return amount; } - public static , S extends Storage> long move(Variant variant, @Nullable StorageAccess from, @Nullable S to, long maxAmount, @Nullable TransactionContext context) { + /** + * Moves the specified amount of a resource from one storage to another. + * + * @param variant the variant to move + * @param from the source storage + * @param to the destination storage + * @param maxAmount the maximum amount of resources to move + * @param context the transaction context to use (if {@code null} a new transaction will be created and committed) + * @param the type of resource + * @param the type of variant + * @return the amount of resources moved + */ + public static > long move(@NotNull Variant variant, @Nullable StorageAccess from, @Nullable Storage to, long maxAmount, @Nullable TransactionContext context) { if (from == null || to == null || variant.isBlank() || maxAmount == 0) return 0; StoragePreconditions.notNegative(maxAmount); - long maxExtracted; - try (Transaction test = Transaction.openNested(context)) { - maxExtracted = from.extract(variant.getObject(), variant.getComponents(), maxAmount, test); - } - try (Transaction moveTransaction = Transaction.openNested(context)) { - long accepted = to.insert(variant, maxExtracted, moveTransaction); + // limit movement to the amount that can be extracted + maxAmount = from.tryExtract(variant.getObject(), variant.getComponents(), maxAmount); + + try (Transaction transaction = Transaction.openNested(context)) { + // insert the resource into the target storage + long accepted = to.insert(variant, maxAmount, transaction); - if (from.extract(variant.getObject(), variant.getComponents(), accepted, moveTransaction) == accepted) { - moveTransaction.commit(); + // extract the resource from the source storage + // and check if the amount extracted is equal to the amount inserted + if (from.extract(variant.getObject(), variant.getComponents(), accepted, transaction) == accepted) { + // commit the transaction if the move was successful + transaction.commit(); return accepted; } } @@ -88,20 +131,69 @@ public static , S extends St return 0; } - public static , S extends Storage> long move(Variant variant, @Nullable S from, @Nullable StorageAccess to, long maxAmount, @Nullable TransactionContext context) { + /** + * Moves the specified amount of a resource from one storage to another. + * + * @param variant the variant to move + * @param from the source storage + * @param to the destination storage + * @param maxAmount the maximum amount of resources to move + * @param context the transaction context to use (if {@code null} a new transaction will be created and committed) + * @param the type of resource + * @param the type of variant + * @return the amount of resources moved + */ + public static > long move(Variant variant, @Nullable Storage from, @Nullable StorageAccess to, long maxAmount, @Nullable TransactionContext context) { if (from == null || to == null || variant.isBlank() || maxAmount == 0) return 0; StoragePreconditions.notNegative(maxAmount); - long maxExtracted; + // limit movement to the amount that can be inserted + maxAmount = Math.min(maxAmount, to.tryInsert(variant.getObject(), variant.getComponents(), maxAmount)); + + try (Transaction transaction = Transaction.openNested(context)) { + // extract the resource from the source storage + long extracted = from.extract(variant, maxAmount, transaction); + + // insert the resource into the target storage + // and check if the amount inserted is equal to the amount extracted + if (to.insert(variant.getObject(), variant.getComponents(), extracted, transaction) == extracted) { + // commit the transaction if the move was successful + transaction.commit(); + return extracted; + } + } + + return 0; + } + + /** + * Moves the specified amount of a resource from one storage to another. + * + * @param variant the variant to move + * @param from the source storage + * @param to the destination storage + * @param maxAmount the maximum amount of resources to move + * @param context the transaction context to use (if {@code null} a new transaction will be created and committed) + * @param the type of variant + * @return the amount of resources moved + */ + public static long move(Variant variant, @Nullable Storage from, @Nullable Storage to, long maxAmount, @Nullable TransactionContext context) { + if (from == null || to == null) return 0; + StoragePreconditions.notNegative(maxAmount); + + // limit movement to the amount that can be extracted try (Transaction test = Transaction.openNested(context)) { - maxExtracted = from.extract(variant, maxAmount, test); + maxAmount = from.extract(variant, maxAmount, test); } - try (Transaction moveTransaction = Transaction.openNested(context)) { - long accepted = to.insert(variant.getObject(), variant.getComponents(), maxExtracted, moveTransaction); + try (Transaction transaction = Transaction.openNested(context)) { + // insert the resource into the target storage + long accepted = to.insert(variant, maxAmount, transaction); - if (from.extract(variant, accepted, moveTransaction) == accepted) { - moveTransaction.commit(); + // extract the resource from the source storage + // and check if the amount extracted is equal to the amount inserted + if (from.extract(variant, accepted, transaction) == accepted) { + transaction.commit(); return accepted; } } @@ -109,20 +201,71 @@ public static , S extends St return 0; } - public static > long move(Variant variant, @Nullable S from, @Nullable S to, long maxAmount, @Nullable TransactionContext context) { - if (from == null || to == null) return 0; + /** + * Moves the item(s) contained in the slot into the other storage. + * + * @param from the source storage + * @param to the destination storage + * @param maxAmount the maximum number of items to move + * @param context the transaction context to use (if {@code null} a new transaction will be created and committed) + * @return the amount of resources moved + */ + public static long move(@Nullable ItemResourceSlot from, @Nullable Storage to, long maxAmount, @Nullable TransactionContext context) { + if (from == null || to == null || from.isEmpty() || maxAmount == 0) return 0; StoragePreconditions.notNegative(maxAmount); - long maxExtracted; - try (Transaction extractionTestTransaction = Transaction.openNested(context)) { - maxExtracted = from.extract(variant, maxAmount, extractionTestTransaction); + // limit movement to the amount that can be extracted + maxAmount = from.tryExtract(maxAmount); + if (maxAmount == 0) return 0; + + try (Transaction transaction = Transaction.openNested(context)) { + assert from.getResource() != null; + ItemVariant variant = ItemVariant.of(from.getResource(), from.getComponents()); + + // insert the items into the target storage + long accepted = to.insert(variant, maxAmount, transaction); + + // extract the items from the source storage + // and check if the amount extracted is equal to the amount inserted + if (from.extract(variant.getObject(), variant.getComponents(), accepted, transaction) == accepted) { + // commit the transaction if the move was successful + transaction.commit(); + return accepted; + } } - try (Transaction moveTransaction = Transaction.openNested(context)) { - long accepted = to.insert(variant, maxExtracted, moveTransaction); + return 0; + } + + /** + * Moves the fluid(s) contained in the slot into the other storage. + * + * @param from the source storage + * @param to the destination storage + * @param maxAmount the maximum number of fluids to move + * @param context the transaction context to use (if {@code null} a new transaction will be created and committed) + * @return the amount of resources moved + */ + public static long move(@Nullable FluidResourceSlot from, @Nullable Storage to, long maxAmount, @Nullable TransactionContext context) { + if (from == null || to == null || from.isEmpty() || maxAmount == 0) return 0; + StoragePreconditions.notNegative(maxAmount); - if (from.extract(variant, accepted, moveTransaction) == accepted) { - moveTransaction.commit(); + // limit movement to the amount that can be extracted + maxAmount = from.tryExtract(maxAmount); + if (maxAmount == 0) return 0; + + try (Transaction transaction = Transaction.openNested(context)) { + assert from.getResource() != null; + FluidVariant variant = FluidVariant.of(from.getResource(), from.getComponents()); + + // insert the items into the target storage + long accepted = to.insert(variant, maxAmount, transaction); + + // extract the items from the source storage + // and check if the amount extracted is equal to the amount inserted + if (from.extract(variant.getObject(), variant.getComponents(), accepted, transaction) == accepted) { + // commit the transaction if the move was successful + transaction.commit(); return accepted; } } @@ -130,8 +273,53 @@ public static > long move(Variant variant, @ return 0; } + /** + * Moves all resources (matching the slot filter) from the storage into the slot. + * + * @param from the source storage + * @param to the destination slot + * @param maxAmount the maximum number of resources to move + * @param context the transaction context to use (if {@code null} a new transaction will be created and committed) + * @return the amount of resources moved + */ + public static > long move(@Nullable Storage from, @Nullable ResourceSlot to, long maxAmount, @Nullable TransactionContext context) { + if (from == null || to == null || !from.supportsExtraction() || maxAmount == 0) return 0; + StoragePreconditions.notNegative(maxAmount); + + Variant variant = findCompatibleVariant(to.getFilter(), from, to); + if (variant == null) return 0; + + // limit movement to the amount that can be inserted + maxAmount = Math.min(maxAmount, to.tryInsert(variant.getObject(), variant.getComponents(), maxAmount)); + if (maxAmount == 0) return 0; + + try (Transaction transaction = Transaction.openNested(context)) { + // extract the items into the source storage + long extracted = from.extract(variant, maxAmount, transaction); + + // insert the items from the target storage + // and check if the amount inserted is equal to the amount inserted + if (to.insert(variant.getObject(), variant.getComponents(), extracted, transaction) == extracted) { + // commit the transaction if the move was successful + transaction.commit(); + return extracted; + } + } + + return 0; + } + + /** + * Moves all items (matching the slot filter) from the storage into the slot. + * + * @param from the source storage + * @param to the destination slot + * @param maxAmount the maximum number of items to move + * @param context the transaction context to use (if {@code null} a new transaction will be created and committed) + * @return whether any resources were moved + */ @Contract("_, null, _, _, _ -> false; _, !null, null, _, _ -> false") - public static , S extends Storage> boolean moveAll(@NotNull ResourceFilter filter, @Nullable S from, @Nullable StorageAccess to, long maxAmount, @Nullable TransactionContext context) { + public static > boolean moveAll(@NotNull ResourceFilter filter, @Nullable Storage from, @Nullable StorageAccess to, long maxAmount, @Nullable TransactionContext context) { if (from == null || to == null || maxAmount == 0 || !from.supportsExtraction()) return false; boolean changed = false; StoragePreconditions.notNegative(maxAmount); @@ -161,7 +349,7 @@ public static , S extends St } @Contract("_, null, _, _, _ -> false; _, !null, null, _, _ -> false") - public static , S extends Storage> boolean moveAll(@NotNull ResourceFilter filter, @Nullable S from, @Nullable S to, long maxAmount, @Nullable TransactionContext context) { + public static > boolean moveAll(@NotNull ResourceFilter filter, @Nullable Storage from, @Nullable Storage to, long maxAmount, @Nullable TransactionContext context) { if (from == null || to == null || maxAmount == 0 || !from.supportsExtraction() || !to.supportsInsertion()) return false; boolean changed = false; StoragePreconditions.notNegative(maxAmount); @@ -191,7 +379,7 @@ public static , S extends St } @Contract("null, _, _, _ -> false; !null, null, _, _ -> false") - public static , S extends Storage> boolean moveAll(@Nullable S from, @Nullable S to, long maxAmount, @Nullable TransactionContext context) { + public static > boolean moveAll(@Nullable Storage from, @Nullable Storage to, long maxAmount, @Nullable TransactionContext context) { if (from == null || to == null || maxAmount == 0 || !from.supportsExtraction() || !to.supportsInsertion()) return false; boolean changed = false; StoragePreconditions.notNegative(maxAmount); @@ -218,5 +406,46 @@ public static , S extends St return changed; } - public record StorageContents(long amount, long capacity) {} + /** + * Finds a variant in the storage that is compatible with the given filter. + * @param filter the filter to test the variants against + * @param storage the storage to search in + * @return a variant that is compatible with the filter, or {@code null} if none was found + * @param the type of resource + * @param the type of variant + */ + public static > @Nullable Variant findCompatibleVariant(@NotNull ResourceFilter filter, @NotNull Storage storage) { + for (StorageView view : storage.nonEmptyViews()) { + Variant resource = view.getResource(); + if (filter.test(resource.getObject(), resource.getComponents())) return resource; + } + return null; + } + + /** + * Finds a variant in the storage that is compatible with the given slot. + * If the slot is not empty, the variant must match the slot's resource and components. + * Otherwise, the variant must be compatible with the slot's filter. + * + * @param filter the filter to test the variants against + * @param storage the storage to search in + * @param slot the slot to test the variants against + * @return a variant that is compatible with the filter and slot, or {@code null} if none was found + * @param the type of resource + * @param the type of variant + */ + public static > @Nullable Variant findCompatibleVariant(@NotNull ResourceFilter filter, @NotNull Storage storage, ResourceSlot slot) { + if (!slot.isEmpty()) { + for (StorageView view : storage.nonEmptyViews()) { + Variant resource = view.getResource(); + if (resource.getObject() == slot.getResource() && resource.getComponents().equals(slot.getComponents())) return resource; + } + } else { + for (StorageView view : storage.nonEmptyViews()) { + Variant resource = view.getResource(); + if (filter.test(resource.getObject(), resource.getComponents())) return resource; + } + } + return null; + } } diff --git a/src/main/java/dev/galacticraft/machinelib/client/impl/model/MachineBakedModel.java b/src/main/java/dev/galacticraft/machinelib/client/impl/model/MachineBakedModel.java index 5bb9f15b..a24800d7 100644 --- a/src/main/java/dev/galacticraft/machinelib/client/impl/model/MachineBakedModel.java +++ b/src/main/java/dev/galacticraft/machinelib/client/impl/model/MachineBakedModel.java @@ -117,7 +117,7 @@ public MachineBakedModel(MachineModelRegistry.SpriteProviderFactory factory, Jso } private boolean transform(MachineRenderData renderData, @NotNull BlockState state, @NotNull MutableQuadView quad) { - BlockFace face = BlockFace.toFace(state.getValue(BlockStateProperties.HORIZONTAL_FACING), quad.nominalFace()); + BlockFace face = BlockFace.from(state.getValue(BlockStateProperties.HORIZONTAL_FACING), quad.nominalFace()); assert face != null; IOFace machineFace = renderData == null ? new IOFace() : renderData.getIOConfig().get(face); @@ -130,7 +130,7 @@ private boolean transform(MachineRenderData renderData, @NotNull BlockState stat } private boolean transformItem(IOConfig config, @NotNull MutableQuadView quad) { - BlockFace face = BlockFace.toFace(Direction.NORTH, quad.nominalFace()); + BlockFace face = BlockFace.from(Direction.NORTH, quad.nominalFace()); IOFace IOFace = config.get(face); assert face != null; quad.spriteBake(getSprite(face, diff --git a/src/main/java/dev/galacticraft/machinelib/impl/compat/transfer/ExposedFluidSlotImpl.java b/src/main/java/dev/galacticraft/machinelib/impl/compat/transfer/ExposedFluidSlotImpl.java index 5aae6388..a16d97c2 100644 --- a/src/main/java/dev/galacticraft/machinelib/impl/compat/transfer/ExposedFluidSlotImpl.java +++ b/src/main/java/dev/galacticraft/machinelib/impl/compat/transfer/ExposedFluidSlotImpl.java @@ -23,6 +23,7 @@ package dev.galacticraft.machinelib.impl.compat.transfer; import dev.galacticraft.machinelib.api.storage.slot.ResourceSlot; +import dev.galacticraft.machinelib.api.transfer.ResourceFlow; import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant; import net.minecraft.core.component.DataComponentPatch; import net.minecraft.world.level.material.Fluid; @@ -30,12 +31,12 @@ import org.jetbrains.annotations.Nullable; public class ExposedFluidSlotImpl extends ExposedSlotImpl { - public ExposedFluidSlotImpl(@NotNull ResourceSlot slot, boolean insertion, boolean extraction) { - super(slot, insertion, extraction); + public ExposedFluidSlotImpl(@NotNull ResourceSlot slot, @NotNull ResourceFlow flow) { + super(slot, flow); } @Override - protected @NotNull FluidVariant createVariant(@Nullable Fluid fluid, @Nullable DataComponentPatch components) { + protected @NotNull FluidVariant createVariant(@Nullable Fluid fluid, @NotNull DataComponentPatch components) { return fluid != null ? FluidVariant.of(fluid, components) : FluidVariant.blank(); } } diff --git a/src/main/java/dev/galacticraft/machinelib/impl/compat/transfer/ExposedFullFluidSlotImpl.java b/src/main/java/dev/galacticraft/machinelib/impl/compat/transfer/ExposedFullFluidSlotImpl.java deleted file mode 100644 index bf609d2d..00000000 --- a/src/main/java/dev/galacticraft/machinelib/impl/compat/transfer/ExposedFullFluidSlotImpl.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) 2021-2024 Team Galacticraft - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package dev.galacticraft.machinelib.impl.compat.transfer; - -import dev.galacticraft.machinelib.api.storage.slot.ResourceSlot; -import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant; -import net.minecraft.core.component.DataComponentPatch; -import net.minecraft.world.level.material.Fluid; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -public class ExposedFullFluidSlotImpl extends ExposedFullSlotImpl { - public ExposedFullFluidSlotImpl(@NotNull ResourceSlot slot) { - super(slot); - } - - @Override - protected @NotNull FluidVariant createVariant(@Nullable Fluid fluid, @Nullable DataComponentPatch components) { - return (fluid != null && components != null) ? FluidVariant.of(fluid, components) : FluidVariant.blank(); - } -} diff --git a/src/main/java/dev/galacticraft/machinelib/impl/compat/transfer/ExposedFullItemSlotImpl.java b/src/main/java/dev/galacticraft/machinelib/impl/compat/transfer/ExposedFullItemSlotImpl.java deleted file mode 100644 index f6b67280..00000000 --- a/src/main/java/dev/galacticraft/machinelib/impl/compat/transfer/ExposedFullItemSlotImpl.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) 2021-2024 Team Galacticraft - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package dev.galacticraft.machinelib.impl.compat.transfer; - -import dev.galacticraft.machinelib.api.storage.slot.ResourceSlot; -import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant; -import net.minecraft.core.component.DataComponentPatch; -import net.minecraft.world.item.Item; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -public class ExposedFullItemSlotImpl extends ExposedFullSlotImpl { - public ExposedFullItemSlotImpl(@NotNull ResourceSlot slot) { - super(slot); - } - - @Override - protected @NotNull ItemVariant createVariant(@Nullable Item item, @Nullable DataComponentPatch components) { - return (item != null && components != null) ? ItemVariant.of(item, components) : ItemVariant.blank(); - } -} diff --git a/src/main/java/dev/galacticraft/machinelib/impl/compat/transfer/ExposedFullSlotImpl.java b/src/main/java/dev/galacticraft/machinelib/impl/compat/transfer/ExposedFullSlotImpl.java deleted file mode 100644 index ca57326b..00000000 --- a/src/main/java/dev/galacticraft/machinelib/impl/compat/transfer/ExposedFullSlotImpl.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Copyright (c) 2021-2024 Team Galacticraft - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package dev.galacticraft.machinelib.impl.compat.transfer; - -import dev.galacticraft.machinelib.api.compat.transfer.ExposedSlot; -import dev.galacticraft.machinelib.api.storage.slot.ResourceSlot; -import net.fabricmc.fabric.api.transfer.v1.storage.TransferVariant; -import net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext; -import net.minecraft.core.component.DataComponentPatch; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -public abstract class ExposedFullSlotImpl> implements ExposedSlot { - private final @NotNull ResourceSlot slot; - - public ExposedFullSlotImpl(@NotNull ResourceSlot slot) { - this.slot = slot; - } - - protected abstract @NotNull Variant createVariant(@Nullable Resource resource, @Nullable DataComponentPatch components); - - @Override - public long insert(Variant variant, long maxAmount, TransactionContext transaction) { - return this.slot.insert(variant.getObject(), variant.getComponents(), maxAmount, transaction); - } - - @Override - public long extract(Variant variant, long maxAmount, TransactionContext transaction) { - return this.slot.extract(variant.getObject(), variant.getComponents(), maxAmount, transaction); - } - - @Override - public boolean supportsInsertion() { - return true; - } - - @Override - public boolean supportsExtraction() { - return true; - } - - @Override - public boolean isResourceBlank() { - return this.slot.isEmpty(); - } - - @Override - public Variant getResource() { - return this.createVariant(this.slot.getResource(), this.slot.getComponents()); - } - - @Override - public long getAmount() { - return this.slot.getAmount(); - } - - @Override - public long getCapacity() { - return this.slot.getRealCapacity(); - } - - @Override - public long getVersion() { - return this.slot.getModifications(); - } -} diff --git a/src/main/java/dev/galacticraft/machinelib/impl/compat/transfer/ExposedItemSlotImpl.java b/src/main/java/dev/galacticraft/machinelib/impl/compat/transfer/ExposedItemSlotImpl.java index 38ac321e..dd238913 100644 --- a/src/main/java/dev/galacticraft/machinelib/impl/compat/transfer/ExposedItemSlotImpl.java +++ b/src/main/java/dev/galacticraft/machinelib/impl/compat/transfer/ExposedItemSlotImpl.java @@ -23,6 +23,7 @@ package dev.galacticraft.machinelib.impl.compat.transfer; import dev.galacticraft.machinelib.api.storage.slot.ResourceSlot; +import dev.galacticraft.machinelib.api.transfer.ResourceFlow; import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant; import net.minecraft.core.component.DataComponentPatch; import net.minecraft.world.item.Item; @@ -30,12 +31,12 @@ import org.jetbrains.annotations.Nullable; public class ExposedItemSlotImpl extends ExposedSlotImpl { - public ExposedItemSlotImpl(@NotNull ResourceSlot slot, boolean insertion, boolean extraction) { - super(slot, insertion, extraction); + public ExposedItemSlotImpl(@NotNull ResourceSlot slot, @NotNull ResourceFlow flow) { + super(slot, flow); } @Override - protected @NotNull ItemVariant createVariant(@Nullable Item item, @Nullable DataComponentPatch components) { + protected @NotNull ItemVariant createVariant(@Nullable Item item, @NotNull DataComponentPatch components) { return item != null ? ItemVariant.of(item, components) : ItemVariant.blank(); } } diff --git a/src/main/java/dev/galacticraft/machinelib/impl/compat/transfer/ExposedSlotImpl.java b/src/main/java/dev/galacticraft/machinelib/impl/compat/transfer/ExposedSlotImpl.java index d26a8975..642132f3 100644 --- a/src/main/java/dev/galacticraft/machinelib/impl/compat/transfer/ExposedSlotImpl.java +++ b/src/main/java/dev/galacticraft/machinelib/impl/compat/transfer/ExposedSlotImpl.java @@ -22,37 +22,42 @@ package dev.galacticraft.machinelib.impl.compat.transfer; +import com.google.common.collect.Iterators; import dev.galacticraft.machinelib.api.compat.transfer.ExposedSlot; import dev.galacticraft.machinelib.api.storage.slot.ResourceSlot; +import dev.galacticraft.machinelib.api.transfer.ResourceFlow; +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.transaction.TransactionContext; import net.minecraft.core.component.DataComponentPatch; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.Iterator; + public abstract class ExposedSlotImpl> implements ExposedSlot { private final @NotNull ResourceSlot slot; private final boolean insertion; private final boolean extraction; - public ExposedSlotImpl(@NotNull ResourceSlot slot, boolean insertion, boolean extraction) { + public ExposedSlotImpl(@NotNull ResourceSlot slot, @NotNull ResourceFlow flow) { this.slot = slot; - this.insertion = insertion; - this.extraction = extraction; + this.insertion = slot.inputType().externalInsertion() && flow == ResourceFlow.INPUT || flow == ResourceFlow.BOTH; + this.extraction = slot.inputType().externalExtraction() && flow == ResourceFlow.OUTPUT || flow == ResourceFlow.BOTH; } - protected abstract @NotNull Variant createVariant(@Nullable Resource resource, @Nullable DataComponentPatch components); + protected abstract @NotNull Variant createVariant(@Nullable Resource resource, @NotNull DataComponentPatch components); @Override public long insert(Variant variant, long maxAmount, TransactionContext transaction) { - if (this.insertion) return this.slot.insert(variant.getObject(), variant.getComponents(), maxAmount, transaction); - return 0; + return this.insertion && this.slot.getFilter().test(variant.getObject(), variant.getComponents()) ? + this.slot.insert(variant.getObject(), variant.getComponents(), maxAmount, transaction) + : 0; } @Override public long extract(Variant variant, long maxAmount, TransactionContext transaction) { - if (this.extraction) return this.slot.extract(variant.getObject(), variant.getComponents(), maxAmount, transaction); - return 0; + return this.extraction ? this.slot.extract(variant.getObject(), variant.getComponents(), maxAmount, transaction) : 0; } @Override @@ -75,6 +80,11 @@ public Variant getResource() { return this.createVariant(this.slot.getResource(), this.slot.getComponents()); } + @Override + public @NotNull Iterator> iterator() { + return Iterators.singletonIterator(this); + } + @Override public long getAmount() { return this.slot.getAmount(); diff --git a/src/main/java/dev/galacticraft/machinelib/impl/compat/transfer/ExposedStorageImpl.java b/src/main/java/dev/galacticraft/machinelib/impl/compat/transfer/ExposedStorageImpl.java index 31f578c9..a06ee7c9 100644 --- a/src/main/java/dev/galacticraft/machinelib/impl/compat/transfer/ExposedStorageImpl.java +++ b/src/main/java/dev/galacticraft/machinelib/impl/compat/transfer/ExposedStorageImpl.java @@ -25,7 +25,7 @@ import com.google.common.collect.Iterators; import dev.galacticraft.machinelib.api.compat.transfer.ExposedSlot; import dev.galacticraft.machinelib.api.compat.transfer.ExposedStorage; -import dev.galacticraft.machinelib.api.misc.Modifiable; +import dev.galacticraft.machinelib.api.storage.ResourceStorage; 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.transaction.TransactionContext; @@ -34,18 +34,38 @@ import java.util.Iterator; public class ExposedStorageImpl> implements ExposedStorage { - private final Modifiable modifiable; + private final ResourceStorage storage; private final ExposedSlot[] slots; - public ExposedStorageImpl(Modifiable modifiable, ExposedSlot[] slots) { - this.modifiable = modifiable; + public ExposedStorageImpl(ResourceStorage storage, ExposedSlot[] slots) { + this.storage = storage; this.slots = slots; } + @Override + public boolean supportsInsertion() { + for (ExposedSlot slot : this.slots) { + if (slot.supportsInsertion()) { + return true; + } + } + return false; + } + + @Override + public boolean supportsExtraction() { + for (ExposedSlot slot : this.slots) { + if (slot.supportsExtraction()) { + return true; + } + } + return false; + } + @Override public long insert(Variant variant, long maxAmount, TransactionContext transaction) { long requested = maxAmount; - for (ExposedSlot slot : slots) { + for (ExposedSlot slot : this.slots) { if (maxAmount == 0) return requested; maxAmount -= slot.insert(variant, maxAmount, transaction); } @@ -55,7 +75,7 @@ public long insert(Variant variant, long maxAmount, TransactionContext transacti @Override public long extract(Variant variant, long maxAmount, TransactionContext transaction) { long requested = maxAmount; - for (ExposedSlot slot : slots) { + for (ExposedSlot slot : this.slots) { if (maxAmount == 0) return requested; maxAmount -= slot.extract(variant, maxAmount, transaction); } @@ -69,6 +89,6 @@ public long extract(Variant variant, long maxAmount, TransactionContext transact @Override public long getVersion() { - return this.modifiable.getModifications(); + return this.storage.getModifications(); } } diff --git a/src/main/java/dev/galacticraft/machinelib/impl/compat/transfer/RecipeBiasedExposedStorage.java b/src/main/java/dev/galacticraft/machinelib/impl/compat/transfer/RecipeBiasedExposedStorage.java deleted file mode 100644 index 01f10299..00000000 --- a/src/main/java/dev/galacticraft/machinelib/impl/compat/transfer/RecipeBiasedExposedStorage.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2021-2024 Team Galacticraft - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package dev.galacticraft.machinelib.impl.compat.transfer; - -import dev.galacticraft.machinelib.api.compat.transfer.ExposedSlot; -import dev.galacticraft.machinelib.api.misc.Modifiable; -import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant; -import net.minecraft.world.item.Item; - -public class RecipeBiasedExposedStorage extends ExposedStorageImpl { - public RecipeBiasedExposedStorage(Modifiable modifiable, ExposedSlot[] slots) { - super(modifiable, slots); - } -} diff --git a/src/main/java/dev/galacticraft/machinelib/impl/compat/transfer/UnmodifiableStorageView.java b/src/main/java/dev/galacticraft/machinelib/impl/compat/transfer/UnmodifiableStorageView.java deleted file mode 100644 index 6922d4b9..00000000 --- a/src/main/java/dev/galacticraft/machinelib/impl/compat/transfer/UnmodifiableStorageView.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright (c) 2021-2024 Team Galacticraft - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package dev.galacticraft.machinelib.impl.compat.transfer; - -import net.fabricmc.fabric.api.transfer.v1.storage.StorageView; -import net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext; -import org.jetbrains.annotations.ApiStatus; -import org.jetbrains.annotations.Contract; - -@ApiStatus.Internal -public record UnmodifiableStorageView(StorageView parent) implements StorageView { - @Contract(value = "_, true -> param1; _, false -> new") - public static StorageView maybeCreate(StorageView parent, boolean extract) { - if (extract) return parent; // if the storage is able to extract, then there is no need to limit it - return new UnmodifiableStorageView<>(parent); - } - - @Override - public long extract(T resource, long maxAmount, TransactionContext transaction) { - return 0L; - } - - @Override - public boolean isResourceBlank() { - return this.parent.isResourceBlank(); - } - - @Override - public T getResource() { - return this.parent.getResource(); - } - - @Override - public long getAmount() { - return this.parent.getAmount(); - } - - @Override - public long getCapacity() { - return this.parent.getCapacity(); - } -} diff --git a/src/main/java/dev/galacticraft/machinelib/impl/compat/vanilla/StorageSlot.java b/src/main/java/dev/galacticraft/machinelib/impl/compat/vanilla/StorageSlot.java index 8a212f15..69fd20a4 100644 --- a/src/main/java/dev/galacticraft/machinelib/impl/compat/vanilla/StorageSlot.java +++ b/src/main/java/dev/galacticraft/machinelib/impl/compat/vanilla/StorageSlot.java @@ -26,7 +26,7 @@ import dev.galacticraft.machinelib.api.storage.slot.ItemResourceSlot; import dev.galacticraft.machinelib.api.storage.slot.display.ItemSlotDisplay; import dev.galacticraft.machinelib.api.util.ItemStackUtil; -import dev.galacticraft.machinelib.impl.Utils; +import dev.galacticraft.machinelib.impl.util.Utils; import net.minecraft.core.component.DataComponentPatch; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.Container; diff --git a/src/main/java/dev/galacticraft/machinelib/impl/storage/BaseSlottedStorage.java b/src/main/java/dev/galacticraft/machinelib/impl/storage/BaseSlottedStorage.java index dea5f158..e57f39dc 100644 --- a/src/main/java/dev/galacticraft/machinelib/impl/storage/BaseSlottedStorage.java +++ b/src/main/java/dev/galacticraft/machinelib/impl/storage/BaseSlottedStorage.java @@ -23,7 +23,6 @@ package dev.galacticraft.machinelib.impl.storage; import com.google.common.collect.Iterators; -import dev.galacticraft.machinelib.api.filter.ResourceFilter; import dev.galacticraft.machinelib.api.storage.SlottedStorageAccess; import dev.galacticraft.machinelib.api.storage.slot.ResourceSlot; import net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext; @@ -202,391 +201,27 @@ public SlottedStorageAccess subStorage(int... slots) { } @Override - public @Nullable Resource getResource(int slot) { - return this.slots[slot].getResource(); + public Slot[] getSlots() { + return this.slots; } @Override - public long getAmount(int slot) { - return this.slots[slot].getAmount(); - } - - @Override - public @Nullable DataComponentPatch getComponents(int slot) { - return this.slots[slot].getComponents(); - } - - @Override - public long getCapacity(int slot) { - return this.slots[slot].getCapacity(); - } - - @Override - public long getCapacityFor(int slot, @NotNull Resource resource, @NotNull DataComponentPatch components) { - return this.slots[slot].getCapacityFor(resource, components); - } - - @Override - public long getRealCapacity(int slot) { - return this.slots[slot].getRealCapacity(); - } - - @Override - public @NotNull ResourceFilter getFilter(int slot) { - return this.slots[slot].getFilter(); - } - - @Override - public boolean isEmpty(int slot) { - return this.slots[slot].isEmpty(); - } - - @Override - public boolean isFull(int slot) { - return this.slots[slot].isFull(); - } - - @Override - public boolean canInsert(int slot, @NotNull Resource resource) { - return this.slots[slot].canInsert(resource); - } - - @Override - public boolean canInsert(int slot, @NotNull Resource resource, @NotNull DataComponentPatch components) { - return this.slots[slot].canInsert(resource, components); - } - - @Override - public boolean canInsert(int slot, @NotNull Resource resource, long amount) { - return this.slots[slot].canInsert(resource, amount); - } - - @Override - public boolean canInsert(int slot, @NotNull Resource resource, @NotNull DataComponentPatch components, long amount) { - return this.slots[slot].canInsert(resource, components, amount); - } - - @Override - public long tryInsert(int slot, @NotNull Resource resource, long amount) { - return this.slots[slot].tryInsert(resource, amount); - } - - @Override - public long tryInsert(int slot, @NotNull Resource resource, @NotNull DataComponentPatch components, long amount) { - return this.slots[slot].tryInsert(resource, components, amount); - } - - @Override - public long insert(int slot, @NotNull Resource resource, long amount) { - return this.slots[slot].insert(resource, amount); - } - - @Override - public long insert(int slot, @NotNull Resource resource, @NotNull DataComponentPatch components, long amount) { - return this.slots[slot].insert(resource, components, amount); - } - - @Override - public boolean contains(int slot, @NotNull Resource resource) { - return this.slots[slot].contains(resource); - } - - @Override - public boolean contains(int slot, @NotNull Resource resource, @Nullable DataComponentPatch components) { - return this.slots[slot].contains(resource, components); - } - - @Override - public boolean canExtract(int slot, long amount) { - return this.slots[slot].canExtract(amount); - } - - @Override - public boolean canExtract(int slot, @NotNull Resource resource, long amount) { - return this.slots[slot].canExtract(resource, amount); - } - - @Override - public boolean canExtract(int slot, @NotNull Resource resource, @Nullable DataComponentPatch components, long amount) { - return this.slots[slot].canExtract(resource, components, amount); - } - - @Override - public long tryExtract(int slot, long amount) { - return this.slots[slot].tryExtract(amount); - } - - @Override - public long tryExtract(int slot, @NotNull Resource resource, long amount) { - return this.slots[slot].tryExtract(resource, amount); - } - - @Override - public long tryExtract(int slot, @NotNull Resource resource, @Nullable DataComponentPatch components, long amount) { - return this.slots[slot].tryExtract(resource, components, amount); - } - - @Override - public @Nullable Resource extractOne(int slot) { - return this.slots[slot].extractOne(); - } - - @Override - public boolean extractOne(int slot, @NotNull Resource resource) { - return this.slots[slot].extractOne(resource); - } - - @Override - public boolean extractOne(int slot, @NotNull Resource resource, @Nullable DataComponentPatch components) { - return this.slots[slot].extractOne(resource, components); - } - - @Override - public long extract(int slot, long amount) { - return this.slots[slot].extract(amount); - } - - @Override - public long extract(int slot, @NotNull Resource resource, long amount) { - return this.slots[slot].extract(resource, amount); - } - - @Override - public long extract(int slot, @NotNull Resource resource, @Nullable DataComponentPatch components, long amount) { - return this.slots[slot].extract(resource, components, amount); - } - - @Override - public boolean isEmpty(int start, int len) { - for (int i = start; i < start + len; i++) { - Slot slot = this.slots[i]; - if (!slot.isEmpty()) return false; - } - return true; - } - - @Override - public boolean isFull(int start, int len) { - for (int i = start; i < start + len; i++) { - Slot slot = this.slots[i]; - if (!slot.isFull()) return false; - } - return true; - } - - @Override - public boolean canInsert(int start, int len, @NotNull Resource resource) { - for (int i = start; i < start + len; i++) { - Slot slot = this.slots[i]; - if (slot.canInsert(resource)) return true; - } - return false; - } - - @Override - public boolean canInsert(int start, int len, @NotNull Resource resource, @NotNull DataComponentPatch components) { - for (int i = start; i < start + len; i++) { - Slot slot = this.slots[i]; - if (slot.canInsert(resource, components)) return true; - } - return false; - } - - @Override - public boolean canInsert(int start, int len, @NotNull Resource resource, long amount) { - long inserted = 0; - for (int i = start; i < start + len; i++) { - Slot slot = this.slots[i]; - inserted += slot.tryInsert(resource, amount - inserted); - if (inserted == amount) return true; - } - return inserted == amount; - } - - @Override - public boolean canInsert(int start, int len, @NotNull Resource resource, @NotNull DataComponentPatch components, long amount) { - long inserted = 0; - for (int i = start; i < start + len; i++) { - Slot slot = this.slots[i]; - inserted += slot.tryInsert(resource, components, amount - inserted); - if (inserted == amount) return true; - } - return inserted == amount; - } - - @Override - public long tryInsert(int start, int len, @NotNull Resource resource, long amount) { - long inserted = 0; - for (int i = start; i < start + len; i++) { - Slot slot = this.slots[i]; - inserted += slot.tryInsert(resource, amount - inserted); - if (inserted == amount) break; - } - return inserted; - } - - @Override - public long tryInsert(int start, int len, @NotNull Resource resource, @NotNull DataComponentPatch components, long amount) { - long inserted = 0; - for (int i = start; i < start + len; i++) { - Slot slot = this.slots[i]; - inserted += slot.tryInsert(resource, components, amount - inserted); - if (inserted == amount) break; - } - return inserted; - } - - @Override - public long insert(int start, int len, @NotNull Resource resource, long amount) { - long inserted = 0; - for (int i = start; i < start + len; i++) { - Slot slot = this.slots[i]; - inserted += slot.insert(resource, amount - inserted); - if (inserted == amount) break; - } - return inserted; - } - - @Override - public long insert(int start, int len, @NotNull Resource resource, @NotNull DataComponentPatch components, long amount) { - long inserted = 0; - for (int i = start; i < start + len; i++) { - Slot slot = this.slots[i]; - inserted += slot.insert(resource, components, amount - inserted); - if (inserted == amount) break; - } - return inserted; - } - - @Override - public long insertMatching(int start, int len, @NotNull Resource resource, long amount) { - long inserted = 0; - for (int i = start; i < start + len; i++) { - Slot slot = this.slots[i]; - if (slot.contains(resource)) { - inserted += slot.insert(resource, amount - inserted); - if (inserted == amount) return inserted; - } - } - - return this.insert(start, len, resource, amount - inserted) + inserted; - } - - @Override - public long insertMatching(int start, int len, @NotNull Resource resource, @NotNull DataComponentPatch components, long amount) { - long inserted = 0; - for (int i = start; i < start + len; i++) { - Slot slot = this.slots[i]; - if (slot.contains(resource, components)) { - inserted += slot.insert(resource, components, amount - inserted); - if (inserted == amount) return inserted; - } - } - - return this.insert(start, len, resource, components, amount - inserted) + inserted; - } - - @Override - public boolean contains(int start, int len, @NotNull Resource resource) { - for (int i = start; i < start + len; i++) { - Slot slot = this.slots[i]; - if (slot.contains(resource)) return true; - } - return false; - } - - @Override - public boolean contains(int start, int len, @NotNull Resource resource, @Nullable DataComponentPatch components) { - for (int i = start; i < start + len; i++) { - Slot slot = this.slots[i]; - if (slot.contains(resource, components)) return true; - } - return false; - } - - @Override - public boolean canExtract(int start, int len, @NotNull Resource resource, long amount) { - for (int i = start; i < start + len; i++) { - Slot slot = this.slots[i]; - if (slot.canExtract(resource, amount)) return true; - } - return false; - } - - @Override - public boolean canExtract(int start, int len, @NotNull Resource resource, @Nullable DataComponentPatch components, long amount) { - for (int i = start; i < start + len; i++) { - Slot slot = this.slots[i]; - if (slot.canExtract(resource, components, amount)) return true; - } - return false; - } - - @Override - public long tryExtract(int start, int len, @NotNull Resource resource, long amount) { - long extracted = 0; - for (int i = start; i < start + len; i++) { - Slot slot = this.slots[i]; - extracted += slot.tryExtract(resource, amount - extracted); - if (extracted == amount) break; - } - return extracted; - } - - @Override - public boolean extractOne(int start, int len, @NotNull Resource resource) { - for (int i = start; i < start + len; i++) { - Slot slot = this.slots[i]; - if (slot.extractOne(resource)) return true; - } - return false; - } - - @Override - public boolean extractOne(int start, int len, @NotNull Resource resource, @Nullable DataComponentPatch components) { - for (int i = start; i < start + len; i++) { - Slot slot = this.slots[i]; - if (slot.extractOne(resource, components)) return true; - } - return false; - } - - @Override - public long tryExtract(int start, int len, @NotNull Resource resource, @Nullable DataComponentPatch components, long amount) { - long extracted = 0; - for (int i = start; i < start + len; i++) { - Slot slot = this.slots[i]; - extracted += slot.tryExtract(resource, components, amount - extracted); - if (extracted == amount) break; - } - return extracted; + public @NotNull Slot slot(int slot) { + return this.slots[slot]; } + @NotNull @Override - public long extract(int start, int len, @NotNull Resource resource, long amount) { - long extracted = 0; - for (int i = start; i < start + len; i++) { - Slot slot = this.slots[i]; - extracted += slot.extract(resource, amount - extracted); - if (extracted == amount) break; - } - return extracted; + public Iterator iterator() { + return Iterators.forArray(this.slots); } @Override - public long extract(int start, int len, @NotNull Resource resource, @Nullable DataComponentPatch components, long amount) { - long extracted = 0; - for (int i = start; i < start + len; i++) { - Slot slot = this.slots[i]; - extracted += slot.extract(resource, components, amount - extracted); - if (extracted == amount) break; + public long getModifications() { + long modifications = 0; + for (Slot slot : this.slots) { + modifications += slot.getModifications(); } - return extracted; - } - - @NotNull - @Override - public Iterator iterator() { - return Iterators.forArray(this.slots); + return modifications; } } diff --git a/src/main/java/dev/galacticraft/machinelib/impl/storage/MachineFluidStorageImpl.java b/src/main/java/dev/galacticraft/machinelib/impl/storage/MachineFluidStorageImpl.java index 6d344ca2..96611226 100644 --- a/src/main/java/dev/galacticraft/machinelib/impl/storage/MachineFluidStorageImpl.java +++ b/src/main/java/dev/galacticraft/machinelib/impl/storage/MachineFluidStorageImpl.java @@ -22,10 +22,16 @@ package dev.galacticraft.machinelib.impl.storage; +import dev.galacticraft.machinelib.api.compat.transfer.ExposedStorage; import dev.galacticraft.machinelib.api.storage.MachineFluidStorage; import dev.galacticraft.machinelib.api.storage.slot.FluidResourceSlot; +import dev.galacticraft.machinelib.api.transfer.ResourceFlow; +import dev.galacticraft.machinelib.impl.compat.transfer.ExposedFluidSlotImpl; +import dev.galacticraft.machinelib.impl.compat.transfer.ExposedStorageImpl; +import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant; import net.minecraft.world.level.material.Fluid; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; public class MachineFluidStorageImpl extends ResourceStorageImpl implements MachineFluidStorage { public static final MachineFluidStorageImpl EMPTY = new MachineFluidStorageImpl(new FluidResourceSlot[0]); @@ -33,4 +39,13 @@ public class MachineFluidStorageImpl extends ResourceStorageImpl createExposedStorage(@NotNull ResourceFlow flow) { + ExposedFluidSlotImpl[] slots = new ExposedFluidSlotImpl[this.size()]; + for (int i = 0; i < slots.length; i++) { + slots[i] = new ExposedFluidSlotImpl(this.getSlots()[i], flow); + } + return new ExposedStorageImpl<>(this, slots); + } } diff --git a/src/main/java/dev/galacticraft/machinelib/impl/storage/MachineItemStorageImpl.java b/src/main/java/dev/galacticraft/machinelib/impl/storage/MachineItemStorageImpl.java index 9df31fbc..b9d6b258 100644 --- a/src/main/java/dev/galacticraft/machinelib/impl/storage/MachineItemStorageImpl.java +++ b/src/main/java/dev/galacticraft/machinelib/impl/storage/MachineItemStorageImpl.java @@ -22,10 +22,15 @@ package dev.galacticraft.machinelib.impl.storage; +import dev.galacticraft.machinelib.api.compat.transfer.ExposedStorage; import dev.galacticraft.machinelib.api.storage.MachineItemStorage; import dev.galacticraft.machinelib.api.storage.slot.ItemResourceSlot; +import dev.galacticraft.machinelib.api.transfer.ResourceFlow; import dev.galacticraft.machinelib.api.util.ItemStackUtil; -import dev.galacticraft.machinelib.impl.Utils; +import dev.galacticraft.machinelib.impl.compat.transfer.ExposedItemSlotImpl; +import dev.galacticraft.machinelib.impl.compat.transfer.ExposedStorageImpl; +import dev.galacticraft.machinelib.impl.util.Utils; +import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant; import net.minecraft.core.component.DataComponentPatch; import net.minecraft.world.Container; import net.minecraft.world.entity.player.Player; @@ -48,7 +53,7 @@ public int getContainerSize() { @Override public @NotNull ItemStack getItem(int i) { - return ItemStackUtil.create(this.getSlot(i)); + return ItemStackUtil.create(this.slot(i)); } @Override @@ -94,6 +99,15 @@ public void clearContent() { Utils.breakpointMe("attempted to clear items in a vanilla compat container!"); } + @Override + public @Nullable ExposedStorage createExposedStorage(@NotNull ResourceFlow flow) { + ExposedItemSlotImpl[] slots = new ExposedItemSlotImpl[this.size()]; + for (int i = 0; i < slots.length; i++) { + slots[i] = new ExposedItemSlotImpl(this.slot(i), flow); + } + return new ExposedStorageImpl<>(this, slots); + } + @Override public boolean consumeOne(@NotNull Item resource) { for (ItemResourceSlot slot : this.slots) { diff --git a/src/main/java/dev/galacticraft/machinelib/impl/storage/ResourceStorageImpl.java b/src/main/java/dev/galacticraft/machinelib/impl/storage/ResourceStorageImpl.java index 6dc6eaf9..f4ee3926 100644 --- a/src/main/java/dev/galacticraft/machinelib/impl/storage/ResourceStorageImpl.java +++ b/src/main/java/dev/galacticraft/machinelib/impl/storage/ResourceStorageImpl.java @@ -22,7 +22,6 @@ package dev.galacticraft.machinelib.impl.storage; -import com.google.common.collect.Iterators; import dev.galacticraft.machinelib.api.storage.ResourceStorage; import dev.galacticraft.machinelib.api.storage.slot.ResourceSlot; import it.unimi.dsi.fastutil.longs.LongArrayList; @@ -33,8 +32,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.Iterator; - public abstract class ResourceStorageImpl> extends BaseSlottedStorage implements ResourceStorage, TransactionContext.CloseCallback { private long modifications = 1; private final LongList transactions = new LongArrayList(); @@ -52,17 +49,6 @@ public void setListener(Runnable listener) { this.listener = listener; } - @Override - public @NotNull Slot getSlot(int slot) { - return this.slots[slot]; - } - - @NotNull - @Override - public Iterator iterator() { - return Iterators.forArray(this.slots); - } - @Override public long getModifications() { return this.modifications; @@ -111,11 +97,6 @@ public void onClose(TransactionContext transaction, TransactionContext.Result re } } - @Override - public Slot[] getSlots() { - return this.slots; - } - @Override public @NotNull ListTag createTag() { ListTag tag = new ListTag(); @@ -163,10 +144,17 @@ public void writeDeltaPacket(@NotNull RegistryFriendlyByteBuf buf, long @NotNull n++; } } - buf.writeByte(n); + buf.writeVarInt(n); + + // If all slots have changed, write a full packet instead (removes index overhead) + if (n == this.size()) { + this.writePacket(buf); + return; + } + for (int i = 0; i < this.slots.length; i++) { if (this.slots[i].getModifications() != previous[i]) { - buf.writeByte(i); + buf.writeVarInt(i); this.slots[i].writePacket(buf); } } @@ -174,9 +162,15 @@ public void writeDeltaPacket(@NotNull RegistryFriendlyByteBuf buf, long @NotNull @Override public void readDeltaPacket(@NotNull RegistryFriendlyByteBuf buf) { - int n = buf.readByte(); + int n = buf.readVarInt(); + + if (n == this.size()) { + this.readPacket(buf); + return; + } + for (int i = 0; i < n; i++) { - this.slots[buf.readByte()].readPacket(buf); + this.slots[buf.readVarInt()].readPacket(buf); } } diff --git a/src/main/java/dev/galacticraft/machinelib/impl/storage/slot/ItemResourceSlotImpl.java b/src/main/java/dev/galacticraft/machinelib/impl/storage/slot/ItemResourceSlotImpl.java index 4123e398..48b9fcee 100644 --- a/src/main/java/dev/galacticraft/machinelib/impl/storage/slot/ItemResourceSlotImpl.java +++ b/src/main/java/dev/galacticraft/machinelib/impl/storage/slot/ItemResourceSlotImpl.java @@ -27,6 +27,7 @@ import dev.galacticraft.machinelib.api.storage.slot.display.ItemSlotDisplay; import dev.galacticraft.machinelib.api.transfer.InputType; import dev.galacticraft.machinelib.api.util.ItemStackUtil; +import dev.galacticraft.machinelib.impl.util.Utils; import net.fabricmc.fabric.api.lookup.v1.item.ItemApiLookup; import net.fabricmc.fabric.api.transfer.v1.context.ContainerItemContext; import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant; @@ -52,10 +53,10 @@ public class ItemResourceSlotImpl extends ResourceSlotImpl implements Item private final @Nullable ItemSlotDisplay display; private long cachedExpiry = -1; + private @Nullable Set recipes = null; private @Nullable SingleSlotStorage cachedStorage = null; private @Nullable ItemApiLookup cachedLookup = null; private @Nullable Object cachedApi = null; - private @Nullable Set recipes; public ItemResourceSlotImpl(@NotNull InputType inputType, @Nullable ItemSlotDisplay display, @NotNull ResourceFilter externalFilter, int capacity) { super(inputType, externalFilter, capacity); @@ -142,11 +143,20 @@ private void insertRemainder(@NotNull Item resource, @NotNull DataComponentPatch @Override public @NotNull CompoundTag createTag() { CompoundTag tag = new CompoundTag(); + + // If the slot is empty, return an empty tag if (this.isEmpty()) return tag; + tag.putString(RESOURCE_KEY, BuiltInRegistries.ITEM.getKey(this.resource).toString()); tag.putInt(AMOUNT_KEY, (int) this.amount); - if (!this.components.isEmpty()) tag.put(COMPONENTS_KEY, DataComponentPatch.CODEC.encodeStart(NbtOps.INSTANCE, this.components).getOrThrow()); - if (this.recipes != null) { + + // Only write the components if we have components + if (!this.components.isEmpty()) { + tag.put(COMPONENTS_KEY, DataComponentPatch.CODEC.encodeStart(NbtOps.INSTANCE, this.components).getOrThrow()); + } + + // Only write the recipes if we have recipes + if (this.inputType() == InputType.RECIPE_OUTPUT && this.recipes != null) { ListTag recipeTag = new ListTag(); for (ResourceLocation entry : this.recipes) { recipeTag.add(StringTag.valueOf(entry.toString())); @@ -160,29 +170,32 @@ private void insertRemainder(@NotNull Item resource, @NotNull DataComponentPatch public void readTag(@NotNull CompoundTag tag) { if (tag.isEmpty()) { this.setEmpty(); - } else { - this.set(BuiltInRegistries.ITEM.get(new ResourceLocation(tag.getString(RESOURCE_KEY))), tag.contains(COMPONENTS_KEY) ? DataComponentPatch.CODEC.parse(NbtOps.INSTANCE, tag.get(COMPONENTS_KEY)).getOrThrow() : DataComponentPatch.EMPTY, tag.getInt(AMOUNT_KEY)); - if (this.inputType() == InputType.RECIPE_OUTPUT && tag.contains(RECIPES_KEY, Tag.TAG_COMPOUND)) { - ListTag list = tag.getList(RECIPES_KEY, Tag.TAG_STRING); - if (!list.isEmpty()) { - if (this.recipes == null) { - this.recipes = new HashSet<>(); - } else { - this.recipes.clear(); - } - for (int i = 0; i < list.size(); i++) { - this.recipes.add(new ResourceLocation(list.getString(i))); - } + return; + } + + this.set( + BuiltInRegistries.ITEM.get(new ResourceLocation(tag.getString(RESOURCE_KEY))), + tag.contains(COMPONENTS_KEY) ? DataComponentPatch.CODEC.parse(NbtOps.INSTANCE, tag.get(COMPONENTS_KEY)).getOrThrow() : DataComponentPatch.EMPTY, + tag.getInt(AMOUNT_KEY) + ); + + if (this.inputType() == InputType.RECIPE_OUTPUT && tag.contains(RECIPES_KEY, Tag.TAG_COMPOUND)) { + ListTag list = tag.getList(RECIPES_KEY, Tag.TAG_STRING); + if (!list.isEmpty()) { + this.recipes = new HashSet<>(list.size()); + for (int i = 0; i < list.size(); i++) { + this.recipes.add(new ResourceLocation(list.getString(i))); } } } + } @Override public void writePacket(@NotNull RegistryFriendlyByteBuf buf) { if (this.amount > 0) { buf.writeInt((int) this.amount); - buf.writeUtf(BuiltInRegistries.ITEM.getKey(this.resource).toString()); + buf.writeUtf(Utils.getShortId(BuiltInRegistries.ITEM.getKey(this.resource))); DataComponentPatch.STREAM_CODEC.encode(buf, this.components); } else { buf.writeInt(0); @@ -205,8 +218,8 @@ public void readPacket(@NotNull RegistryFriendlyByteBuf buf) { public @Nullable A find(ItemApiLookup lookup) { if (this.cachedExpiry != this.getModifications() || this.cachedLookup != lookup) { this.cachedExpiry = this.getModifications(); - this.cachedApi = ItemResourceSlot.super.find(lookup); this.cachedLookup = lookup; + this.cachedApi = ItemResourceSlot.super.find(lookup); } return (A) this.cachedApi; } @@ -214,42 +227,7 @@ public void readPacket(@NotNull RegistryFriendlyByteBuf buf) { @Override public SingleSlotStorage getMainSlot() { if (this.cachedStorage == null) { - this.cachedStorage = new SingleSlotStorage<>() { - @Override - public long insert(ItemVariant resource, long maxAmount, TransactionContext transaction) { - return ItemResourceSlotImpl.this.insert(resource.getItem(), resource.getComponents(), maxAmount, transaction); - } - - @Override - public long extract(ItemVariant resource, long maxAmount, TransactionContext transaction) { - return ItemResourceSlotImpl.this.extract(resource.getItem(), resource.getComponents(), maxAmount, transaction); - } - - @Override - public boolean isResourceBlank() { - return ItemResourceSlotImpl.this.isEmpty(); - } - - @Override - public ItemVariant getResource() { - return ItemResourceSlotImpl.this.isEmpty() ? ItemVariant.blank() : ItemVariant.of(Objects.requireNonNull(ItemResourceSlotImpl.this.resource), ItemResourceSlotImpl.this.components); - } - - @Override - public long getAmount() { - return ItemResourceSlotImpl.this.getAmount(); - } - - @Override - public long getCapacity() { - return ItemResourceSlotImpl.this.getRealCapacity(); - } - - @Override - public long getVersion() { - return ItemResourceSlotImpl.this.getModifications(); - } - }; + this.cachedStorage = new InnerSingleSlotStorage(); } return this.cachedStorage; } @@ -298,7 +276,7 @@ public List> getAdditionalSlots() { @Override public boolean isSane() { - return super.isSane() && this.resource != Items.AIR; + return super.isSane() && this.resource != Items.AIR && this.amount <= Integer.MAX_VALUE; } @Override @@ -319,4 +297,46 @@ public void recipeCrafted(@NotNull ResourceLocation id) { } this.recipes.add(id); } + + /** + * A {@link SingleSlotStorage} implementation for this slot. + * Used for fabric-api compatibility. + * ItemResourceSlotImpl can't implement {@link SingleSlotStorage} directly due to conflicting methods. + */ + private class InnerSingleSlotStorage implements SingleSlotStorage { + @Override + public long insert(ItemVariant resource, long maxAmount, TransactionContext transaction) { + return ItemResourceSlotImpl.this.insert(resource.getItem(), resource.getComponents(), maxAmount, transaction); + } + + @Override + public long extract(ItemVariant resource, long maxAmount, TransactionContext transaction) { + return ItemResourceSlotImpl.this.extract(resource.getItem(), resource.getComponents(), maxAmount, transaction); + } + + @Override + public boolean isResourceBlank() { + return ItemResourceSlotImpl.this.isEmpty(); + } + + @Override + public ItemVariant getResource() { + return ItemResourceSlotImpl.this.isEmpty() ? ItemVariant.blank() : ItemVariant.of(Objects.requireNonNull(ItemResourceSlotImpl.this.resource), ItemResourceSlotImpl.this.components); + } + + @Override + public long getAmount() { + return ItemResourceSlotImpl.this.getAmount(); + } + + @Override + public long getCapacity() { + return ItemResourceSlotImpl.this.getRealCapacity(); + } + + @Override + public long getVersion() { + return ItemResourceSlotImpl.this.getModifications(); + } + } } diff --git a/src/main/java/dev/galacticraft/machinelib/impl/Utils.java b/src/main/java/dev/galacticraft/machinelib/impl/util/Utils.java similarity index 74% rename from src/main/java/dev/galacticraft/machinelib/impl/Utils.java rename to src/main/java/dev/galacticraft/machinelib/impl/util/Utils.java index 3ff06c71..8b175dba 100644 --- a/src/main/java/dev/galacticraft/machinelib/impl/Utils.java +++ b/src/main/java/dev/galacticraft/machinelib/impl/util/Utils.java @@ -20,8 +20,10 @@ * SOFTWARE. */ -package dev.galacticraft.machinelib.impl; +package dev.galacticraft.machinelib.impl.util; +import dev.galacticraft.machinelib.impl.MachineLib; +import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.Item; import net.minecraft.world.item.Items; import org.jetbrains.annotations.Contract; @@ -37,6 +39,16 @@ public static boolean itemsEqual(@Nullable Item a, @NotNull Item b) { return a == b || (a == null && b == Items.AIR); } + /** + * {@return the short string representation of the given id} + * Should only be used to serialize packets and other short-lived data. + * If the namespace matches "minecraft", only the path is returned. + * @param id the id to shorten + */ + public static String getShortId(ResourceLocation id) { + return id.getNamespace().equals("minecraft") ? id.getPath() : id.toString(); + } + public static void breakpointMe(String s) { MachineLib.LOGGER.error(s); } diff --git a/src/test/java/dev/galacticraft/machinelib/impl/storage/MachineEnergyStorageImplTest.java b/src/test/java/dev/galacticraft/machinelib/impl/storage/MachineEnergyStorageImplTest.java index 95374a86..7f51408b 100644 --- a/src/test/java/dev/galacticraft/machinelib/impl/storage/MachineEnergyStorageImplTest.java +++ b/src/test/java/dev/galacticraft/machinelib/impl/storage/MachineEnergyStorageImplTest.java @@ -24,7 +24,7 @@ import dev.galacticraft.machinelib.api.storage.MachineEnergyStorage; import dev.galacticraft.machinelib.test.MinecraftTest; -import dev.galacticraft.machinelib.test.Utils; +import dev.galacticraft.machinelib.test.util.Utils; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import net.fabricmc.fabric.api.transfer.v1.transaction.Transaction; diff --git a/src/test/java/dev/galacticraft/machinelib/impl/storage/slot/ItemResourceSlotImplTest.java b/src/test/java/dev/galacticraft/machinelib/impl/storage/slot/ItemResourceSlotImplTest.java index 1e4ca94c..6c182d3e 100644 --- a/src/test/java/dev/galacticraft/machinelib/impl/storage/slot/ItemResourceSlotImplTest.java +++ b/src/test/java/dev/galacticraft/machinelib/impl/storage/slot/ItemResourceSlotImplTest.java @@ -24,7 +24,7 @@ import dev.galacticraft.machinelib.api.filter.ResourceFilters; import dev.galacticraft.machinelib.api.transfer.InputType; -import dev.galacticraft.machinelib.test.Utils; +import dev.galacticraft.machinelib.test.util.Utils; import net.minecraft.core.component.DataComponentPatch; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.Item; diff --git a/src/test/java/dev/galacticraft/machinelib/impl/storage/slot/ResourceSlotImplTest.java b/src/test/java/dev/galacticraft/machinelib/impl/storage/slot/ResourceSlotImplTest.java index 65f52630..d4cc05d8 100644 --- a/src/test/java/dev/galacticraft/machinelib/impl/storage/slot/ResourceSlotImplTest.java +++ b/src/test/java/dev/galacticraft/machinelib/impl/storage/slot/ResourceSlotImplTest.java @@ -24,7 +24,7 @@ import dev.galacticraft.machinelib.api.storage.slot.ResourceSlot; import dev.galacticraft.machinelib.test.MinecraftTest; -import dev.galacticraft.machinelib.test.Utils; +import dev.galacticraft.machinelib.test.util.Utils; import net.fabricmc.fabric.api.transfer.v1.transaction.Transaction; import net.minecraft.core.component.DataComponentPatch; import net.minecraft.nbt.CompoundTag; diff --git a/src/test/java/dev/galacticraft/machinelib/test/misc/ConfigTest.java b/src/test/java/dev/galacticraft/machinelib/test/misc/ConfigTest.java index 619b7d74..43691b98 100644 --- a/src/test/java/dev/galacticraft/machinelib/test/misc/ConfigTest.java +++ b/src/test/java/dev/galacticraft/machinelib/test/misc/ConfigTest.java @@ -25,6 +25,7 @@ import dev.galacticraft.machinelib.api.config.Config; import dev.galacticraft.machinelib.test.MinecraftTest; import net.fabricmc.loader.api.FabricLoader; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -43,6 +44,11 @@ public void setup() { this.config = Config.loadFrom(FILE); } + @AfterEach + public void cleanup() { + FILE.delete(); + } + @Test public void saveOnOpen() { assertTrue(FILE.exists()); diff --git a/src/test/java/dev/galacticraft/machinelib/test/storage/extraction/FluidStorageExtractionTest.java b/src/test/java/dev/galacticraft/machinelib/test/storage/extraction/FluidStorageExtractionTest.java index 4fe35bcf..22296dfe 100644 --- a/src/test/java/dev/galacticraft/machinelib/test/storage/extraction/FluidStorageExtractionTest.java +++ b/src/test/java/dev/galacticraft/machinelib/test/storage/extraction/FluidStorageExtractionTest.java @@ -29,7 +29,7 @@ import dev.galacticraft.machinelib.api.transfer.InputType; import dev.galacticraft.machinelib.impl.storage.slot.ResourceSlotImpl; import dev.galacticraft.machinelib.test.MinecraftTest; -import dev.galacticraft.machinelib.test.Utils; +import dev.galacticraft.machinelib.test.util.Utils; import net.fabricmc.fabric.api.transfer.v1.fluid.FluidConstants; import net.minecraft.core.component.DataComponentPatch; import net.minecraft.world.level.material.Fluids; @@ -50,7 +50,7 @@ public void setup() { @AfterEach public void verify() { - assertTrue(((ResourceSlotImpl) this.storage.getSlot(0)).isSane()); + assertTrue(((ResourceSlotImpl) this.storage.slot(0)).isSane()); } @Nested @@ -76,7 +76,7 @@ public void empty() { @Test public void incorrectType() { - storage.getSlot(0).set(Fluids.LAVA, FluidConstants.BUCKET); + storage.slot(0).set(Fluids.LAVA, FluidConstants.BUCKET); assertFalse(storage.canExtract(Fluids.WATER, FluidConstants.BUCKET)); assertEquals(0, storage.tryExtract(Fluids.WATER, FluidConstants.BUCKET)); @@ -88,7 +88,7 @@ public void incorrectType() { @Test public void extractionTag() { - storage.getSlot(0).set(Fluids.WATER, FluidConstants.BUCKET); + storage.slot(0).set(Fluids.WATER, FluidConstants.BUCKET); assertFalse(storage.canExtract(Fluids.WATER, Utils.generateComponents(), FluidConstants.BUCKET)); assertEquals(0, storage.tryExtract(Fluids.WATER, Utils.generateComponents(), FluidConstants.BUCKET)); @@ -100,7 +100,7 @@ public void extractionTag() { @Test public void containedTag() { - storage.getSlot(0).set(Fluids.WATER, Utils.generateComponents(), FluidConstants.BUCKET); + storage.slot(0).set(Fluids.WATER, Utils.generateComponents(), FluidConstants.BUCKET); assertFalse(storage.canExtract(Fluids.WATER, DataComponentPatch.EMPTY, FluidConstants.BUCKET)); assertEquals(0, storage.tryExtract(Fluids.WATER, DataComponentPatch.EMPTY, FluidConstants.BUCKET)); @@ -112,7 +112,7 @@ public void containedTag() { @Test public void mismatchedTag() { - storage.getSlot(0).set(Fluids.WATER, Utils.generateComponents(), FluidConstants.BUCKET); + storage.slot(0).set(Fluids.WATER, Utils.generateComponents(), FluidConstants.BUCKET); assertFalse(storage.canExtract(Fluids.WATER, Utils.generateComponents(), FluidConstants.BUCKET)); assertEquals(0, storage.tryExtract(Fluids.WATER, Utils.generateComponents(), FluidConstants.BUCKET)); @@ -127,7 +127,7 @@ public void mismatchedTag() { final class ExtractionSuccessTests { @Test public void exact_typed() { - storage.getSlot(0).set(Fluids.WATER, FluidConstants.BUCKET); + storage.slot(0).set(Fluids.WATER, FluidConstants.BUCKET); assertTrue(storage.canExtract(Fluids.WATER, FluidConstants.BUCKET)); assertEquals(FluidConstants.BUCKET, storage.tryExtract(Fluids.WATER, FluidConstants.BUCKET)); @@ -139,7 +139,7 @@ public void exact_typed() { @Test public void exact_typed_nbt() { DataComponentPatch components = Utils.generateComponents(); - storage.getSlot(0).set(Fluids.WATER, components, FluidConstants.BUCKET); + storage.slot(0).set(Fluids.WATER, components, FluidConstants.BUCKET); assertTrue(storage.canExtract(Fluids.WATER, components, FluidConstants.BUCKET)); assertEquals(FluidConstants.BUCKET, storage.tryExtract(Fluids.WATER, components, FluidConstants.BUCKET)); @@ -150,7 +150,7 @@ public void exact_typed_nbt() { @Test public void exact_typed_emptyNbt() { - storage.getSlot(0).set(Fluids.WATER, FluidConstants.BUCKET); + storage.slot(0).set(Fluids.WATER, FluidConstants.BUCKET); assertTrue(storage.canExtract(Fluids.WATER, DataComponentPatch.EMPTY, FluidConstants.BUCKET)); assertEquals(FluidConstants.BUCKET, storage.tryExtract(Fluids.WATER, DataComponentPatch.EMPTY, FluidConstants.BUCKET)); @@ -161,41 +161,41 @@ public void exact_typed_emptyNbt() { @Test public void excess_typed() { - storage.getSlot(0).set(Fluids.WATER, FluidConstants.BUCKET); + storage.slot(0).set(Fluids.WATER, FluidConstants.BUCKET); assertTrue(storage.canExtract(Fluids.WATER, FluidConstants.BOTTLE)); assertEquals(FluidConstants.BOTTLE, storage.tryExtract(Fluids.WATER, FluidConstants.BOTTLE)); assertEquals(FluidConstants.BOTTLE, storage.extract(Fluids.WATER, FluidConstants.BOTTLE)); - assertEquals(storage.getAmount(0), FluidConstants.BUCKET - FluidConstants.BOTTLE); + assertEquals(storage.slot(0).getAmount(), FluidConstants.BUCKET - FluidConstants.BOTTLE); } @Test public void excess_typed_nbt() { DataComponentPatch components = Utils.generateComponents(); - storage.getSlot(0).set(Fluids.WATER, components, FluidConstants.BUCKET); + storage.slot(0).set(Fluids.WATER, components, FluidConstants.BUCKET); assertTrue(storage.canExtract(Fluids.WATER, components, FluidConstants.BOTTLE)); assertEquals(FluidConstants.BOTTLE, storage.tryExtract(Fluids.WATER, components, FluidConstants.BOTTLE)); assertEquals(FluidConstants.BOTTLE, storage.extract(Fluids.WATER, components, FluidConstants.BOTTLE)); - assertEquals(storage.getAmount(0), FluidConstants.BUCKET - FluidConstants.BOTTLE); + assertEquals(storage.slot(0).getAmount(), FluidConstants.BUCKET - FluidConstants.BOTTLE); } @Test public void excess_typed_emptyNbt() { - storage.getSlot(0).set(Fluids.WATER, FluidConstants.BUCKET); + storage.slot(0).set(Fluids.WATER, FluidConstants.BUCKET); assertTrue(storage.canExtract(Fluids.WATER, DataComponentPatch.EMPTY, FluidConstants.BOTTLE)); assertEquals(FluidConstants.BOTTLE, storage.tryExtract(Fluids.WATER, DataComponentPatch.EMPTY, FluidConstants.BOTTLE)); assertEquals(FluidConstants.BOTTLE, storage.extract(Fluids.WATER, DataComponentPatch.EMPTY, FluidConstants.BOTTLE)); - assertEquals(storage.getAmount(0), FluidConstants.BUCKET - FluidConstants.BOTTLE); + assertEquals(storage.slot(0).getAmount(), FluidConstants.BUCKET - FluidConstants.BOTTLE); } @Test public void insufficient_typed() { - storage.getSlot(0).set(Fluids.WATER, FluidConstants.BOTTLE); + storage.slot(0).set(Fluids.WATER, FluidConstants.BOTTLE); assertEquals(FluidConstants.BOTTLE, storage.tryExtract(Fluids.WATER, FluidConstants.BUCKET)); assertEquals(FluidConstants.BOTTLE, storage.extract(Fluids.WATER, FluidConstants.BUCKET)); @@ -206,7 +206,7 @@ public void insufficient_typed() { @Test public void insufficient_typed_nbt() { DataComponentPatch components = Utils.generateComponents(); - storage.getSlot(0).set(Fluids.WATER, components, FluidConstants.BOTTLE); + storage.slot(0).set(Fluids.WATER, components, FluidConstants.BOTTLE); assertEquals(FluidConstants.BOTTLE, storage.tryExtract(Fluids.WATER, components, FluidConstants.BUCKET)); assertEquals(FluidConstants.BOTTLE, storage.extract(Fluids.WATER, components, FluidConstants.BUCKET)); @@ -216,7 +216,7 @@ public void insufficient_typed_nbt() { @Test public void insufficient_typed_emptyNbt() { - storage.getSlot(0).set(Fluids.WATER, FluidConstants.BOTTLE); + storage.slot(0).set(Fluids.WATER, FluidConstants.BOTTLE); assertEquals(FluidConstants.BOTTLE, storage.tryExtract(Fluids.WATER, DataComponentPatch.EMPTY, FluidConstants.BUCKET)); assertEquals(FluidConstants.BOTTLE, storage.extract(Fluids.WATER, DataComponentPatch.EMPTY, FluidConstants.BUCKET)); diff --git a/src/test/java/dev/galacticraft/machinelib/test/storage/extraction/ItemStorageExtractionTests.java b/src/test/java/dev/galacticraft/machinelib/test/storage/extraction/ItemStorageExtractionTests.java index 81adf1ce..e8359a11 100644 --- a/src/test/java/dev/galacticraft/machinelib/test/storage/extraction/ItemStorageExtractionTests.java +++ b/src/test/java/dev/galacticraft/machinelib/test/storage/extraction/ItemStorageExtractionTests.java @@ -29,7 +29,7 @@ import dev.galacticraft.machinelib.api.transfer.InputType; import dev.galacticraft.machinelib.impl.storage.slot.ResourceSlotImpl; import dev.galacticraft.machinelib.test.MinecraftTest; -import dev.galacticraft.machinelib.test.Utils; +import dev.galacticraft.machinelib.test.util.Utils; import net.minecraft.core.component.DataComponentPatch; import net.minecraft.world.item.Items; import org.junit.jupiter.api.AfterEach; @@ -40,83 +40,83 @@ import static org.junit.jupiter.api.Assertions.*; public class ItemStorageExtractionTests implements MinecraftTest { - protected MachineItemStorage group; + protected MachineItemStorage storage; @BeforeEach public void setup() { - this.group = MachineItemStorage.create(ItemResourceSlot.create(InputType.STORAGE, ItemSlotDisplay.create(0, 0), ResourceFilters.any())); + this.storage = MachineItemStorage.create(ItemResourceSlot.create(InputType.STORAGE, ItemSlotDisplay.create(0, 0), ResourceFilters.any())); } @AfterEach public void verify() { - assertTrue(((ResourceSlotImpl) this.group.getSlot(0)).isSane()); + assertTrue(((ResourceSlotImpl) this.storage.slot(0)).isSane()); } @Nested final class ExtractionFailureTests { @Test public void empty() { - assertTrue(group.isEmpty()); + assertTrue(storage.isEmpty()); - assertFalse(group.canExtract(Items.GOLD_INGOT, 1)); - assertEquals(0, group.tryExtract(Items.GOLD_INGOT, 1)); - assertEquals(0, group.extract(Items.GOLD_INGOT, 1)); + assertFalse(storage.canExtract(Items.GOLD_INGOT, 1)); + assertEquals(0, storage.tryExtract(Items.GOLD_INGOT, 1)); + assertEquals(0, storage.extract(Items.GOLD_INGOT, 1)); - assertFalse(group.canExtract(Items.GOLD_INGOT, DataComponentPatch.EMPTY, 1)); - assertEquals(0, group.tryExtract(Items.GOLD_INGOT, DataComponentPatch.EMPTY, 1)); - assertEquals(0, group.extract(Items.GOLD_INGOT, DataComponentPatch.EMPTY, 1)); + assertFalse(storage.canExtract(Items.GOLD_INGOT, DataComponentPatch.EMPTY, 1)); + assertEquals(0, storage.tryExtract(Items.GOLD_INGOT, DataComponentPatch.EMPTY, 1)); + assertEquals(0, storage.extract(Items.GOLD_INGOT, DataComponentPatch.EMPTY, 1)); - assertFalse(group.canExtract(Items.GOLD_INGOT, Utils.generateComponents(), 1)); - assertEquals(0, group.tryExtract(Items.GOLD_INGOT, Utils.generateComponents(), 1)); - assertEquals(0, group.extract(Items.GOLD_INGOT, Utils.generateComponents(), 1)); + assertFalse(storage.canExtract(Items.GOLD_INGOT, Utils.generateComponents(), 1)); + assertEquals(0, storage.tryExtract(Items.GOLD_INGOT, Utils.generateComponents(), 1)); + assertEquals(0, storage.extract(Items.GOLD_INGOT, Utils.generateComponents(), 1)); } @Test public void incorrectType() { - group.getSlot(0).set(Items.IRON_INGOT, 1); + storage.slot(0).set(Items.IRON_INGOT, 1); - assertFalse(group.canExtract(Items.GOLD_INGOT, 1)); - assertEquals(0, group.tryExtract(Items.GOLD_INGOT, 1)); - assertEquals(0, group.extract(Items.GOLD_INGOT, 1)); - assertFalse(group.extractOne(Items.GOLD_INGOT)); + assertFalse(storage.canExtract(Items.GOLD_INGOT, 1)); + assertEquals(0, storage.tryExtract(Items.GOLD_INGOT, 1)); + assertEquals(0, storage.extract(Items.GOLD_INGOT, 1)); + assertFalse(storage.extractOne(Items.GOLD_INGOT)); - assertFalse(group.isEmpty()); + assertFalse(storage.isEmpty()); } @Test public void extractionTag() { - group.getSlot(0).set(Items.GOLD_INGOT, 1); + storage.slot(0).set(Items.GOLD_INGOT, 1); - assertFalse(group.canExtract(Items.GOLD_INGOT, Utils.generateComponents(), 1)); - assertEquals(0, group.tryExtract(Items.GOLD_INGOT, Utils.generateComponents(), 1)); - assertEquals(0, group.extract(Items.GOLD_INGOT, Utils.generateComponents(), 1)); - assertFalse(group.extractOne(Items.GOLD_INGOT, Utils.generateComponents())); + assertFalse(storage.canExtract(Items.GOLD_INGOT, Utils.generateComponents(), 1)); + assertEquals(0, storage.tryExtract(Items.GOLD_INGOT, Utils.generateComponents(), 1)); + assertEquals(0, storage.extract(Items.GOLD_INGOT, Utils.generateComponents(), 1)); + assertFalse(storage.extractOne(Items.GOLD_INGOT, Utils.generateComponents())); - assertFalse(group.isEmpty()); + assertFalse(storage.isEmpty()); } @Test public void containedTag() { - group.getSlot(0).set(Items.GOLD_INGOT, Utils.generateComponents(), 1); + storage.slot(0).set(Items.GOLD_INGOT, Utils.generateComponents(), 1); - assertFalse(group.canExtract(Items.GOLD_INGOT, DataComponentPatch.EMPTY, 1)); - assertEquals(0, group.tryExtract(Items.GOLD_INGOT, DataComponentPatch.EMPTY, 1)); - assertEquals(0, group.extract(Items.GOLD_INGOT, DataComponentPatch.EMPTY, 1)); - assertFalse(group.extractOne(Items.GOLD_INGOT, DataComponentPatch.EMPTY)); + assertFalse(storage.canExtract(Items.GOLD_INGOT, DataComponentPatch.EMPTY, 1)); + assertEquals(0, storage.tryExtract(Items.GOLD_INGOT, DataComponentPatch.EMPTY, 1)); + assertEquals(0, storage.extract(Items.GOLD_INGOT, DataComponentPatch.EMPTY, 1)); + assertFalse(storage.extractOne(Items.GOLD_INGOT, DataComponentPatch.EMPTY)); - assertFalse(group.isEmpty()); + assertFalse(storage.isEmpty()); } @Test public void mismatchedTag() { - group.getSlot(0).set(Items.GOLD_INGOT, Utils.generateComponents(), 1); + storage.slot(0).set(Items.GOLD_INGOT, Utils.generateComponents(), 1); - assertFalse(group.canExtract(Items.GOLD_INGOT, Utils.generateComponents(), 1)); - assertEquals(0, group.tryExtract(Items.GOLD_INGOT, Utils.generateComponents(), 1)); - assertEquals(0, group.extract(Items.GOLD_INGOT, Utils.generateComponents(), 1)); - assertFalse(group.extractOne(Items.GOLD_INGOT, Utils.generateComponents())); + assertFalse(storage.canExtract(Items.GOLD_INGOT, Utils.generateComponents(), 1)); + assertEquals(0, storage.tryExtract(Items.GOLD_INGOT, Utils.generateComponents(), 1)); + assertEquals(0, storage.extract(Items.GOLD_INGOT, Utils.generateComponents(), 1)); + assertFalse(storage.extractOne(Items.GOLD_INGOT, Utils.generateComponents())); - assertFalse(group.isEmpty()); + assertFalse(storage.isEmpty()); } } @@ -124,101 +124,101 @@ public void mismatchedTag() { final class ExtractionSuccessTests { @Test public void exact_typed() { - group.getSlot(0).set(Items.GOLD_INGOT, 1); + storage.slot(0).set(Items.GOLD_INGOT, 1); - assertTrue(group.canExtract(Items.GOLD_INGOT, 1)); - assertEquals(1, group.tryExtract(Items.GOLD_INGOT, 1)); - assertEquals(1, group.extract(Items.GOLD_INGOT, 1)); + assertTrue(storage.canExtract(Items.GOLD_INGOT, 1)); + assertEquals(1, storage.tryExtract(Items.GOLD_INGOT, 1)); + assertEquals(1, storage.extract(Items.GOLD_INGOT, 1)); - assertTrue(group.isEmpty()); + assertTrue(storage.isEmpty()); } @Test public void exact_typed_nbt() { DataComponentPatch components = Utils.generateComponents(); - group.getSlot(0).set(Items.GOLD_INGOT, components, 1); + storage.slot(0).set(Items.GOLD_INGOT, components, 1); - assertTrue(group.canExtract(Items.GOLD_INGOT, components, 1)); - assertEquals(1, group.tryExtract(Items.GOLD_INGOT, components, 1)); - assertEquals(1, group.extract(Items.GOLD_INGOT, components, 1)); + assertTrue(storage.canExtract(Items.GOLD_INGOT, components, 1)); + assertEquals(1, storage.tryExtract(Items.GOLD_INGOT, components, 1)); + assertEquals(1, storage.extract(Items.GOLD_INGOT, components, 1)); - assertTrue(group.isEmpty()); + assertTrue(storage.isEmpty()); } @Test public void exact_typed_emptyNbt() { - group.getSlot(0).set(Items.GOLD_INGOT, 1); + storage.slot(0).set(Items.GOLD_INGOT, 1); - assertTrue(group.canExtract(Items.GOLD_INGOT, DataComponentPatch.EMPTY, 1)); - assertEquals(1, group.tryExtract(Items.GOLD_INGOT, DataComponentPatch.EMPTY, 1)); - assertEquals(1, group.extract(Items.GOLD_INGOT, DataComponentPatch.EMPTY, 1)); + assertTrue(storage.canExtract(Items.GOLD_INGOT, DataComponentPatch.EMPTY, 1)); + assertEquals(1, storage.tryExtract(Items.GOLD_INGOT, DataComponentPatch.EMPTY, 1)); + assertEquals(1, storage.extract(Items.GOLD_INGOT, DataComponentPatch.EMPTY, 1)); - assertTrue(group.isEmpty()); + assertTrue(storage.isEmpty()); } @Test public void excess_typed() { - group.getSlot(0).set(Items.GOLD_INGOT, 48); + storage.slot(0).set(Items.GOLD_INGOT, 48); - assertTrue(group.canExtract(Items.GOLD_INGOT, 16)); - assertEquals(16, group.tryExtract(Items.GOLD_INGOT, 16)); - assertEquals(16, group.extract(Items.GOLD_INGOT, 16)); + assertTrue(storage.canExtract(Items.GOLD_INGOT, 16)); + assertEquals(16, storage.tryExtract(Items.GOLD_INGOT, 16)); + assertEquals(16, storage.extract(Items.GOLD_INGOT, 16)); - assertEquals(group.getAmount(0), 48 - 16); + assertEquals(storage.slot(0).getAmount(), 48 - 16); } @Test public void excess_typed_nbt() { DataComponentPatch components = Utils.generateComponents(); - group.getSlot(0).set(Items.GOLD_INGOT, components, 48); + storage.slot(0).set(Items.GOLD_INGOT, components, 48); - assertTrue(group.canExtract(Items.GOLD_INGOT, components, 16)); - assertEquals(16, group.tryExtract(Items.GOLD_INGOT, components, 16)); - assertEquals(16, group.extract(Items.GOLD_INGOT, components, 16)); + assertTrue(storage.canExtract(Items.GOLD_INGOT, components, 16)); + assertEquals(16, storage.tryExtract(Items.GOLD_INGOT, components, 16)); + assertEquals(16, storage.extract(Items.GOLD_INGOT, components, 16)); - assertEquals(group.getAmount(0), 48 - 16); + assertEquals(storage.slot(0).getAmount(), 48 - 16); } @Test public void excess_typed_emptyNbt() { - group.getSlot(0).set(Items.GOLD_INGOT, 48); + storage.slot(0).set(Items.GOLD_INGOT, 48); - assertTrue(group.canExtract(Items.GOLD_INGOT, DataComponentPatch.EMPTY, 16)); - assertEquals(16, group.tryExtract(Items.GOLD_INGOT, DataComponentPatch.EMPTY, 16)); - assertEquals(16, group.extract(Items.GOLD_INGOT, DataComponentPatch.EMPTY, 16)); + assertTrue(storage.canExtract(Items.GOLD_INGOT, DataComponentPatch.EMPTY, 16)); + assertEquals(16, storage.tryExtract(Items.GOLD_INGOT, DataComponentPatch.EMPTY, 16)); + assertEquals(16, storage.extract(Items.GOLD_INGOT, DataComponentPatch.EMPTY, 16)); - assertEquals(group.getAmount(0), 48 - 16); + assertEquals(storage.slot(0).getAmount(), 48 - 16); } @Test public void insufficient_typed() { - group.getSlot(0).set(Items.GOLD_INGOT, 16); + storage.slot(0).set(Items.GOLD_INGOT, 16); - assertEquals(16, group.tryExtract(Items.GOLD_INGOT, 64)); - assertEquals(16, group.extract(Items.GOLD_INGOT, 64)); + assertEquals(16, storage.tryExtract(Items.GOLD_INGOT, 64)); + assertEquals(16, storage.extract(Items.GOLD_INGOT, 64)); - assertTrue(group.isEmpty()); + assertTrue(storage.isEmpty()); } @Test public void insufficient_typed_nbt() { DataComponentPatch components = Utils.generateComponents(); - group.getSlot(0).set(Items.GOLD_INGOT, components, 16); + storage.slot(0).set(Items.GOLD_INGOT, components, 16); - assertEquals(16, group.tryExtract(Items.GOLD_INGOT, components, 64)); - assertEquals(16, group.extract(Items.GOLD_INGOT, components, 64)); + assertEquals(16, storage.tryExtract(Items.GOLD_INGOT, components, 64)); + assertEquals(16, storage.extract(Items.GOLD_INGOT, components, 64)); - assertTrue(group.isEmpty()); + assertTrue(storage.isEmpty()); } @Test public void insufficient_typed_emptyNbt() { - group.getSlot(0).set(Items.GOLD_INGOT, 16); + storage.slot(0).set(Items.GOLD_INGOT, 16); - assertEquals(16, group.tryExtract(Items.GOLD_INGOT, DataComponentPatch.EMPTY, 64)); - assertEquals(16, group.extract(Items.GOLD_INGOT, DataComponentPatch.EMPTY, 64)); + assertEquals(16, storage.tryExtract(Items.GOLD_INGOT, DataComponentPatch.EMPTY, 64)); + assertEquals(16, storage.extract(Items.GOLD_INGOT, DataComponentPatch.EMPTY, 64)); - assertTrue(group.isEmpty()); + assertTrue(storage.isEmpty()); } } } diff --git a/src/test/java/dev/galacticraft/machinelib/test/storage/insertion/FluidStorageInsertionTests.java b/src/test/java/dev/galacticraft/machinelib/test/storage/insertion/FluidStorageInsertionTests.java index f4445658..cc42d177 100644 --- a/src/test/java/dev/galacticraft/machinelib/test/storage/insertion/FluidStorageInsertionTests.java +++ b/src/test/java/dev/galacticraft/machinelib/test/storage/insertion/FluidStorageInsertionTests.java @@ -29,7 +29,7 @@ import dev.galacticraft.machinelib.api.transfer.InputType; import dev.galacticraft.machinelib.impl.storage.slot.ResourceSlotImpl; import dev.galacticraft.machinelib.test.MinecraftTest; -import dev.galacticraft.machinelib.test.Utils; +import dev.galacticraft.machinelib.test.util.Utils; import net.fabricmc.fabric.api.transfer.v1.fluid.FluidConstants; import net.minecraft.core.component.DataComponentPatch; import net.minecraft.world.level.material.Fluids; @@ -42,71 +42,71 @@ public class FluidStorageInsertionTests implements MinecraftTest { private static final long CAPACITY = FluidConstants.BUCKET * 16; - protected MachineFluidStorage group; + protected MachineFluidStorage storage; @BeforeEach public void setup() { - this.group = MachineFluidStorage.create(FluidResourceSlot.create(InputType.STORAGE, TankDisplay.create(0, 0), CAPACITY, ResourceFilters.any())); + this.storage = MachineFluidStorage.create(FluidResourceSlot.create(InputType.STORAGE, TankDisplay.create(0, 0), CAPACITY, ResourceFilters.any())); } @AfterEach public void verify() { - assertTrue(((ResourceSlotImpl) this.group.getSlot(0)).isSane()); + assertTrue(((ResourceSlotImpl) this.storage.slot(0)).isSane()); } @Nested final class InsertionFailureTests { @Test public void full() { - group.getSlot(0).set(Fluids.WATER, CAPACITY); + storage.slot(0).set(Fluids.WATER, CAPACITY); - assertFalse(group.canInsert(Fluids.WATER, FluidConstants.BUCKET)); - assertEquals(0, group.tryInsert(Fluids.WATER, FluidConstants.BUCKET)); - assertEquals(0, group.insert(Fluids.WATER, FluidConstants.BUCKET)); + assertFalse(storage.canInsert(Fluids.WATER, FluidConstants.BUCKET)); + assertEquals(0, storage.tryInsert(Fluids.WATER, FluidConstants.BUCKET)); + assertEquals(0, storage.insert(Fluids.WATER, FluidConstants.BUCKET)); } @Test public void incorrectType() { - group.getSlot(0).set(Fluids.WATER, FluidConstants.BUCKET); + storage.slot(0).set(Fluids.WATER, FluidConstants.BUCKET); - assertFalse(group.canInsert(Fluids.LAVA, FluidConstants.BUCKET)); - assertEquals(0, group.tryInsert(Fluids.LAVA, FluidConstants.BUCKET)); - assertEquals(0, group.insert(Fluids.LAVA, FluidConstants.BUCKET)); + assertFalse(storage.canInsert(Fluids.LAVA, FluidConstants.BUCKET)); + assertEquals(0, storage.tryInsert(Fluids.LAVA, FluidConstants.BUCKET)); + assertEquals(0, storage.insert(Fluids.LAVA, FluidConstants.BUCKET)); - assertEquals(group.getAmount(0), FluidConstants.BUCKET); + assertEquals(storage.slot(0).getAmount(), FluidConstants.BUCKET); } @Test public void insertTag() { - group.getSlot(0).set(Fluids.WATER, FluidConstants.BUCKET); + storage.slot(0).set(Fluids.WATER, FluidConstants.BUCKET); - assertFalse(group.canInsert(Fluids.WATER, Utils.generateComponents(), FluidConstants.BUCKET)); - assertEquals(0, group.tryInsert(Fluids.WATER, Utils.generateComponents(), FluidConstants.BUCKET)); - assertEquals(0, group.insert(Fluids.WATER, Utils.generateComponents(), FluidConstants.BUCKET)); + assertFalse(storage.canInsert(Fluids.WATER, Utils.generateComponents(), FluidConstants.BUCKET)); + assertEquals(0, storage.tryInsert(Fluids.WATER, Utils.generateComponents(), FluidConstants.BUCKET)); + assertEquals(0, storage.insert(Fluids.WATER, Utils.generateComponents(), FluidConstants.BUCKET)); - assertEquals(group.getAmount(0), FluidConstants.BUCKET); + assertEquals(storage.slot(0).getAmount(), FluidConstants.BUCKET); } @Test public void containedTag() { - group.getSlot(0).set(Fluids.WATER, Utils.generateComponents(), FluidConstants.BUCKET); + storage.slot(0).set(Fluids.WATER, Utils.generateComponents(), FluidConstants.BUCKET); - assertFalse(group.canInsert(Fluids.WATER, FluidConstants.BUCKET)); - assertEquals(0, group.tryInsert(Fluids.WATER, FluidConstants.BUCKET)); - assertEquals(0, group.insert(Fluids.WATER, FluidConstants.BUCKET)); + assertFalse(storage.canInsert(Fluids.WATER, FluidConstants.BUCKET)); + assertEquals(0, storage.tryInsert(Fluids.WATER, FluidConstants.BUCKET)); + assertEquals(0, storage.insert(Fluids.WATER, FluidConstants.BUCKET)); - assertEquals(group.getAmount(0), FluidConstants.BUCKET); + assertEquals(storage.slot(0).getAmount(), FluidConstants.BUCKET); } @Test public void mismatchedTag() { - group.getSlot(0).set(Fluids.WATER, Utils.generateComponents(), FluidConstants.BUCKET); + storage.slot(0).set(Fluids.WATER, Utils.generateComponents(), FluidConstants.BUCKET); - assertFalse(group.canInsert(Fluids.WATER, Utils.generateComponents(), FluidConstants.BUCKET)); - assertEquals(0, group.tryInsert(Fluids.WATER, Utils.generateComponents(), FluidConstants.BUCKET)); - assertEquals(0, group.insert(Fluids.WATER, Utils.generateComponents(), FluidConstants.BUCKET)); + assertFalse(storage.canInsert(Fluids.WATER, Utils.generateComponents(), FluidConstants.BUCKET)); + assertEquals(0, storage.tryInsert(Fluids.WATER, Utils.generateComponents(), FluidConstants.BUCKET)); + assertEquals(0, storage.insert(Fluids.WATER, Utils.generateComponents(), FluidConstants.BUCKET)); - assertEquals(group.getAmount(0), FluidConstants.BUCKET); + assertEquals(storage.slot(0).getAmount(), FluidConstants.BUCKET); } } @@ -114,68 +114,68 @@ public void mismatchedTag() { final class InsertionSuccessTests { @Test public void empty() { - assertTrue(group.canInsert(Fluids.WATER, FluidConstants.BUCKET)); - assertEquals(FluidConstants.BUCKET, group.tryInsert(Fluids.WATER, FluidConstants.BUCKET)); - assertEquals(FluidConstants.BUCKET, group.insert(Fluids.WATER, FluidConstants.BUCKET)); + assertTrue(storage.canInsert(Fluids.WATER, FluidConstants.BUCKET)); + assertEquals(FluidConstants.BUCKET, storage.tryInsert(Fluids.WATER, FluidConstants.BUCKET)); + assertEquals(FluidConstants.BUCKET, storage.insert(Fluids.WATER, FluidConstants.BUCKET)); - assertEquals(FluidConstants.BUCKET, group.getAmount(0)); + assertEquals(FluidConstants.BUCKET, storage.slot(0).getAmount()); } @Test public void toCapacity() { - assertTrue(group.canInsert(Fluids.WATER, CAPACITY)); - assertEquals(CAPACITY, group.tryInsert(Fluids.WATER, CAPACITY)); - assertEquals(CAPACITY, group.insert(Fluids.WATER, CAPACITY)); + assertTrue(storage.canInsert(Fluids.WATER, CAPACITY)); + assertEquals(CAPACITY, storage.tryInsert(Fluids.WATER, CAPACITY)); + assertEquals(CAPACITY, storage.insert(Fluids.WATER, CAPACITY)); - assertEquals(CAPACITY, group.getAmount(0)); + assertEquals(CAPACITY, storage.slot(0).getAmount()); } @Test public void overCapacity() { - assertEquals(CAPACITY, group.tryInsert(Fluids.WATER, CAPACITY + FluidConstants.BOTTLE)); - assertEquals(CAPACITY, group.insert(Fluids.WATER, CAPACITY + FluidConstants.BOTTLE)); + assertEquals(CAPACITY, storage.tryInsert(Fluids.WATER, CAPACITY + FluidConstants.BOTTLE)); + assertEquals(CAPACITY, storage.insert(Fluids.WATER, CAPACITY + FluidConstants.BOTTLE)); - assertEquals(CAPACITY, group.getAmount(0)); + assertEquals(CAPACITY, storage.slot(0).getAmount()); } @Test public void preFill() { - group.getSlot(0).set(Fluids.WATER, FluidConstants.BUCKET); - assertTrue(group.canInsert(Fluids.WATER, FluidConstants.BUCKET * 6)); - assertEquals(FluidConstants.BUCKET * 6, group.tryInsert(Fluids.WATER, FluidConstants.BUCKET * 6)); - assertEquals(FluidConstants.BUCKET * 6, group.insert(Fluids.WATER, FluidConstants.BUCKET * 6)); + storage.slot(0).set(Fluids.WATER, FluidConstants.BUCKET); + assertTrue(storage.canInsert(Fluids.WATER, FluidConstants.BUCKET * 6)); + assertEquals(FluidConstants.BUCKET * 6, storage.tryInsert(Fluids.WATER, FluidConstants.BUCKET * 6)); + assertEquals(FluidConstants.BUCKET * 6, storage.insert(Fluids.WATER, FluidConstants.BUCKET * 6)); - assertEquals(FluidConstants.BUCKET * 7, group.getAmount(0)); + assertEquals(FluidConstants.BUCKET * 7, storage.slot(0).getAmount()); } @Test public void preFill_tag() { DataComponentPatch components = Utils.generateComponents(); - group.getSlot(0).set(Fluids.WATER, components, FluidConstants.BUCKET); - assertTrue(group.canInsert(Fluids.WATER, components, FluidConstants.BUCKET * 6)); - assertEquals(FluidConstants.BUCKET * 6, group.tryInsert(Fluids.WATER, components, FluidConstants.BUCKET * 6)); - assertEquals(FluidConstants.BUCKET * 6, group.insert(Fluids.WATER, components, FluidConstants.BUCKET * 6)); + storage.slot(0).set(Fluids.WATER, components, FluidConstants.BUCKET); + assertTrue(storage.canInsert(Fluids.WATER, components, FluidConstants.BUCKET * 6)); + assertEquals(FluidConstants.BUCKET * 6, storage.tryInsert(Fluids.WATER, components, FluidConstants.BUCKET * 6)); + assertEquals(FluidConstants.BUCKET * 6, storage.insert(Fluids.WATER, components, FluidConstants.BUCKET * 6)); - assertEquals(FluidConstants.BUCKET * 7, group.getAmount(0)); + assertEquals(FluidConstants.BUCKET * 7, storage.slot(0).getAmount()); } @Test public void preFill_overCapacity() { - group.getSlot(0).set(Fluids.WATER, FluidConstants.BUCKET * 12); - assertEquals(FluidConstants.BUCKET * 4, group.tryInsert(Fluids.WATER, FluidConstants.BUCKET * 7)); - assertEquals(FluidConstants.BUCKET * 4, group.insert(Fluids.WATER, FluidConstants.BUCKET * 7)); + storage.slot(0).set(Fluids.WATER, FluidConstants.BUCKET * 12); + assertEquals(FluidConstants.BUCKET * 4, storage.tryInsert(Fluids.WATER, FluidConstants.BUCKET * 7)); + assertEquals(FluidConstants.BUCKET * 4, storage.insert(Fluids.WATER, FluidConstants.BUCKET * 7)); - assertEquals(CAPACITY, group.getAmount(0)); + assertEquals(CAPACITY, storage.slot(0).getAmount()); } @Test public void preFill_overCapacity_tag() { DataComponentPatch components = Utils.generateComponents(); - group.getSlot(0).set(Fluids.WATER, components, FluidConstants.BUCKET * 12); - assertEquals(FluidConstants.BUCKET * 4, group.tryInsert(Fluids.WATER, components, FluidConstants.BUCKET * 7)); - assertEquals(FluidConstants.BUCKET * 4, group.insert(Fluids.WATER, components, FluidConstants.BUCKET * 7)); + storage.slot(0).set(Fluids.WATER, components, FluidConstants.BUCKET * 12); + assertEquals(FluidConstants.BUCKET * 4, storage.tryInsert(Fluids.WATER, components, FluidConstants.BUCKET * 7)); + assertEquals(FluidConstants.BUCKET * 4, storage.insert(Fluids.WATER, components, FluidConstants.BUCKET * 7)); - assertEquals(CAPACITY, group.getAmount(0)); + assertEquals(CAPACITY, storage.slot(0).getAmount()); } } } diff --git a/src/test/java/dev/galacticraft/machinelib/test/storage/insertion/ItemStorageInsertionTests.java b/src/test/java/dev/galacticraft/machinelib/test/storage/insertion/ItemStorageInsertionTests.java index 0155f66c..fcdb3bce 100644 --- a/src/test/java/dev/galacticraft/machinelib/test/storage/insertion/ItemStorageInsertionTests.java +++ b/src/test/java/dev/galacticraft/machinelib/test/storage/insertion/ItemStorageInsertionTests.java @@ -29,7 +29,7 @@ import dev.galacticraft.machinelib.api.transfer.InputType; import dev.galacticraft.machinelib.impl.storage.slot.ResourceSlotImpl; import dev.galacticraft.machinelib.test.MinecraftTest; -import dev.galacticraft.machinelib.test.Utils; +import dev.galacticraft.machinelib.test.util.Utils; import net.minecraft.core.component.DataComponentPatch; import net.minecraft.world.item.Items; import org.junit.jupiter.api.AfterEach; @@ -50,14 +50,14 @@ public void setup() { @AfterEach public void verify() { - assertTrue(((ResourceSlotImpl) this.storage.getSlot(0)).isSane()); + assertTrue(((ResourceSlotImpl) this.storage.slot(0)).isSane()); } @Nested final class InsertionFailureTests { @Test public void full() { - storage.getSlot(0).set(Items.GOLD_INGOT, CAPACITY); + storage.slot(0).set(Items.GOLD_INGOT, CAPACITY); assertFalse(storage.canInsert(Items.GOLD_INGOT, 16)); assertEquals(0, storage.tryInsert(Items.GOLD_INGOT, 16)); @@ -66,46 +66,46 @@ public void full() { @Test public void incorrectType() { - storage.getSlot(0).set(Items.IRON_INGOT, 16); + storage.slot(0).set(Items.IRON_INGOT, 16); assertFalse(storage.canInsert(Items.GOLD_INGOT, 16)); assertEquals(0, storage.tryInsert(Items.GOLD_INGOT, 16)); assertEquals(0, storage.insert(Items.GOLD_INGOT, 16)); - assertEquals(storage.getAmount(0), 16); + assertEquals(storage.slot(0).getAmount(), 16); } @Test public void insertTag() { - storage.getSlot(0).set(Items.GOLD_INGOT, 16); + storage.slot(0).set(Items.GOLD_INGOT, 16); assertFalse(storage.canInsert(Items.GOLD_INGOT, Utils.generateComponents(), 16)); assertEquals(0, storage.tryInsert(Items.GOLD_INGOT, Utils.generateComponents(), 16)); assertEquals(0, storage.insert(Items.GOLD_INGOT, Utils.generateComponents(), 16)); - assertEquals(storage.getAmount(0), 16); + assertEquals(storage.slot(0).getAmount(), 16); } @Test public void containedTag() { - storage.getSlot(0).set(Items.GOLD_INGOT, Utils.generateComponents(), 16); + storage.slot(0).set(Items.GOLD_INGOT, Utils.generateComponents(), 16); assertFalse(storage.canInsert(Items.GOLD_INGOT, 16)); assertEquals(0, storage.tryInsert(Items.GOLD_INGOT, 16)); assertEquals(0, storage.insert(Items.GOLD_INGOT, 16)); - assertEquals(storage.getAmount(0), 16); + assertEquals(storage.slot(0).getAmount(), 16); } @Test public void mismatchedTag() { - storage.getSlot(0).set(Items.GOLD_INGOT, Utils.generateComponents(), 16); + storage.slot(0).set(Items.GOLD_INGOT, Utils.generateComponents(), 16); assertFalse(storage.canInsert(Items.GOLD_INGOT, Utils.generateComponents(), 16)); assertEquals(0, storage.tryInsert(Items.GOLD_INGOT, Utils.generateComponents(), 16)); assertEquals(0, storage.insert(Items.GOLD_INGOT, Utils.generateComponents(), 16)); - assertEquals(storage.getAmount(0), 16); + assertEquals(storage.slot(0).getAmount(), 16); } } @@ -117,7 +117,7 @@ public void empty() { assertEquals(16, storage.tryInsert(Items.GOLD_INGOT, 16)); assertEquals(16, storage.insert(Items.GOLD_INGOT, 16)); - assertEquals(16, storage.getAmount(0)); + assertEquals(16, storage.slot(0).getAmount()); } @Test @@ -126,7 +126,7 @@ public void toCapacity() { assertEquals(CAPACITY, storage.tryInsert(Items.GOLD_INGOT, CAPACITY)); assertEquals(CAPACITY, storage.insert(Items.GOLD_INGOT, CAPACITY)); - assertEquals(CAPACITY, storage.getAmount(0)); + assertEquals(CAPACITY, storage.slot(0).getAmount()); } @Test @@ -134,47 +134,47 @@ public void overCapacity() { assertEquals(CAPACITY, storage.tryInsert(Items.GOLD_INGOT, CAPACITY + 8)); assertEquals(CAPACITY, storage.insert(Items.GOLD_INGOT, CAPACITY + 8)); - assertEquals(CAPACITY, storage.getAmount(0)); + assertEquals(CAPACITY, storage.slot(0).getAmount()); } @Test public void preFill() { - storage.getSlot(0).set(Items.GOLD_INGOT, 16); + storage.slot(0).set(Items.GOLD_INGOT, 16); assertTrue(storage.canInsert(Items.GOLD_INGOT, 48)); assertEquals(48, storage.tryInsert(Items.GOLD_INGOT, 48)); assertEquals(48, storage.insert(Items.GOLD_INGOT, 48)); - assertEquals(16 + 48, storage.getAmount(0)); + assertEquals(16 + 48, storage.slot(0).getAmount()); } @Test public void preFill_tag() { DataComponentPatch components = Utils.generateComponents(); - storage.getSlot(0).set(Items.GOLD_INGOT, components, 16); + storage.slot(0).set(Items.GOLD_INGOT, components, 16); assertTrue(storage.canInsert(Items.GOLD_INGOT, components, 48)); assertEquals(48, storage.tryInsert(Items.GOLD_INGOT, components, 48)); assertEquals(48, storage.insert(Items.GOLD_INGOT, components, 48)); - assertEquals(16 + 48, storage.getAmount(0)); + assertEquals(16 + 48, storage.slot(0).getAmount()); } @Test public void preFill_overCapacity() { - storage.getSlot(0).set(Items.GOLD_INGOT, 50); + storage.slot(0).set(Items.GOLD_INGOT, 50); assertEquals(14, storage.tryInsert(Items.GOLD_INGOT, 16)); assertEquals(14, storage.insert(Items.GOLD_INGOT, 16)); - assertEquals(CAPACITY, storage.getAmount(0)); + assertEquals(CAPACITY, storage.slot(0).getAmount()); } @Test public void preFill_overCapacity_tag() { DataComponentPatch components = Utils.generateComponents(); - storage.getSlot(0).set(Items.GOLD_INGOT, components, 50); + storage.slot(0).set(Items.GOLD_INGOT, components, 50); assertEquals(14, storage.tryInsert(Items.GOLD_INGOT, components, 16)); assertEquals(14, storage.insert(Items.GOLD_INGOT, components, 16)); - assertEquals(CAPACITY, storage.getAmount(0)); + assertEquals(CAPACITY, storage.slot(0).getAmount()); } } } diff --git a/src/test/java/dev/galacticraft/machinelib/test/Utils.java b/src/test/java/dev/galacticraft/machinelib/test/util/Utils.java similarity index 96% rename from src/test/java/dev/galacticraft/machinelib/test/Utils.java rename to src/test/java/dev/galacticraft/machinelib/test/util/Utils.java index cc01e5f5..9a5ac72c 100644 --- a/src/test/java/dev/galacticraft/machinelib/test/Utils.java +++ b/src/test/java/dev/galacticraft/machinelib/test/util/Utils.java @@ -20,9 +20,8 @@ * SOFTWARE. */ -package dev.galacticraft.machinelib.test; +package dev.galacticraft.machinelib.test.util; -import dev.galacticraft.machinelib.test.util.StaticRegistryAccess; import io.netty.buffer.Unpooled; import net.minecraft.core.component.DataComponentPatch; import net.minecraft.core.component.DataComponents; diff --git a/src/testmod/java/dev/galacticraft/machinelib/testmod/block/entity/GeneratorBlockEntity.java b/src/testmod/java/dev/galacticraft/machinelib/testmod/block/entity/GeneratorBlockEntity.java index ab12a838..8b5f3249 100644 --- a/src/testmod/java/dev/galacticraft/machinelib/testmod/block/entity/GeneratorBlockEntity.java +++ b/src/testmod/java/dev/galacticraft/machinelib/testmod/block/entity/GeneratorBlockEntity.java @@ -53,14 +53,14 @@ public class GeneratorBlockEntity extends MachineBlockEntity { public GeneratorBlockEntity(@NotNull BlockPos pos, BlockState state) { super(TestModMachineTypes.GENERATOR, pos, state); - this.fuelInput = this.itemStorage().getSlot(FUEL_SLOT); + this.fuelInput = this.itemStorage().slot(FUEL_SLOT); } @Override protected void tickConstant(@NotNull ServerLevel level, @NotNull BlockPos pos, @NotNull BlockState state, @NotNull ProfilerFiller profiler) { super.tickConstant(level, pos, state, profiler); profiler.push("power_drain"); - this.drainPowerToStack(BATTERY_SLOT); + this.drainPowerToSlot(BATTERY_SLOT); profiler.pop(); this.trySpreadEnergy(level, state); } diff --git a/src/testmod/java/dev/galacticraft/machinelib/testmod/block/entity/MelterBlockEntity.java b/src/testmod/java/dev/galacticraft/machinelib/testmod/block/entity/MelterBlockEntity.java index 39dc1a3c..609c13db 100644 --- a/src/testmod/java/dev/galacticraft/machinelib/testmod/block/entity/MelterBlockEntity.java +++ b/src/testmod/java/dev/galacticraft/machinelib/testmod/block/entity/MelterBlockEntity.java @@ -61,16 +61,16 @@ public class MelterBlockEntity extends MachineBlockEntity { public MelterBlockEntity(@NotNull BlockPos pos, BlockState state) { super(TestModMachineTypes.MELTER, pos, state); - this.itemInput = this.itemStorage().getSlot(INPUT_SLOT); - this.fluidOutput = this.fluidStorage().getSlot(LAVA_TANK); + this.itemInput = this.itemStorage().slot(INPUT_SLOT); + this.fluidOutput = this.fluidStorage().slot(LAVA_TANK); } @Override protected void tickConstant(@NotNull ServerLevel level, @NotNull BlockPos pos, @NotNull BlockState state, @NotNull ProfilerFiller profiler) { super.tickConstant(level, pos, state, profiler); profiler.push("charge_stack"); - this.chargeFromStack(BATTERY_SLOT); - this.insertFluidToStack(LAVA_OUTPUT_SLOT, LAVA_TANK, Fluids.LAVA); + this.chargeFromSlot(BATTERY_SLOT); + this.drainFluidToSlot(LAVA_OUTPUT_SLOT, LAVA_TANK); profiler.pop(); this.trySpreadFluids(level, state); } diff --git a/src/testmod/java/dev/galacticraft/machinelib/testmod/block/entity/MixerBlockEntity.java b/src/testmod/java/dev/galacticraft/machinelib/testmod/block/entity/MixerBlockEntity.java index c03e522e..3f5b0655 100644 --- a/src/testmod/java/dev/galacticraft/machinelib/testmod/block/entity/MixerBlockEntity.java +++ b/src/testmod/java/dev/galacticraft/machinelib/testmod/block/entity/MixerBlockEntity.java @@ -66,21 +66,21 @@ public MixerBlockEntity(@NotNull BlockPos pos, BlockState state) { protected void tickConstant(@NotNull ServerLevel level, @NotNull BlockPos pos, @NotNull BlockState state, @NotNull ProfilerFiller profiler) { super.tickConstant(level, pos, state, profiler); profiler.push("charge_stack"); - this.chargeFromStack(BATTERY_SLOT); - this.takeFluidFromStack(WATER_INPUT_SLOT, WATER_TANK, Fluids.WATER); - this.takeFluidFromStack(LAVA_INPUT_SLOT, LAVA_TANK, Fluids.LAVA); + this.chargeFromSlot(BATTERY_SLOT); + this.takeFluidFromSlot(WATER_INPUT_SLOT, WATER_TANK, Fluids.WATER); + this.takeFluidFromSlot(LAVA_INPUT_SLOT, LAVA_TANK, Fluids.LAVA); profiler.pop(); } @Override protected @NotNull MachineStatus tick(@NotNull ServerLevel level, @NotNull BlockPos pos, @NotNull BlockState state, @NotNull ProfilerFiller profiler) { - ItemResourceSlot output = this.itemStorage().getSlot(OUTPUT_SLOT); + ItemResourceSlot output = this.itemStorage().slot(OUTPUT_SLOT); if (!output.canInsert(Items.OBSIDIAN)) { this.progress = 0; return MachineStatuses.OUTPUT_FULL; } - FluidResourceSlot water = this.fluidStorage().getSlot(WATER_TANK); - FluidResourceSlot lava = this.fluidStorage().getSlot(LAVA_TANK); + FluidResourceSlot water = this.fluidStorage().slot(WATER_TANK); + FluidResourceSlot lava = this.fluidStorage().slot(LAVA_TANK); if (water.tryExtract(Fluids.WATER, FLUID_REQUIRED) != FLUID_REQUIRED || lava.tryExtract(Fluids.LAVA, FLUID_REQUIRED) != FLUID_REQUIRED) { this.progress = 0; return MachineStatuses.IDLE; diff --git a/src/testmod/java/dev/galacticraft/machinelib/testmod/gametest/GeneratorTests.java b/src/testmod/java/dev/galacticraft/machinelib/testmod/gametest/GeneratorTests.java index 99b7c8ce..8a1c8ee9 100644 --- a/src/testmod/java/dev/galacticraft/machinelib/testmod/gametest/GeneratorTests.java +++ b/src/testmod/java/dev/galacticraft/machinelib/testmod/gametest/GeneratorTests.java @@ -44,7 +44,7 @@ public GeneratorTests() { @MachineTest(workTime = 10) public Runnable generatePower(GeneratorBlockEntity machine) { - machine.itemStorage().getSlot(GeneratorBlockEntity.FUEL_SLOT).set(Items.COAL, 1); + machine.itemStorage().slot(GeneratorBlockEntity.FUEL_SLOT).set(Items.COAL, 1); return () -> { if (machine.energyStorage().getAmount() != GeneratorBlockEntity.GENERATION_RATE * 10) { throw new GameTestAssertException(String.format("Failed to generate power (%s / %s)!", machine.energyStorage().getAmount(), GeneratorBlockEntity.GENERATION_RATE * 10));