Fix #2579: expose inner storage unit stack to transfer API (#2738)

* Fix #2579: expose inner storage unit stack to transfer API

* Maintain == ItemStack.EMPTY
This commit is contained in:
Technici4n 2022-01-17 23:39:57 +01:00 committed by GitHub
parent 2bceaa46c2
commit 86850033e3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 65 additions and 1 deletions

View file

@ -24,6 +24,11 @@
package techreborn.blockentity.storage.item;
import net.fabricmc.fabric.api.transfer.v1.item.InventoryStorage;
import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant;
import net.fabricmc.fabric.api.transfer.v1.item.base.SingleStackStorage;
import net.fabricmc.fabric.api.transfer.v1.storage.Storage;
import net.fabricmc.fabric.api.transfer.v1.storage.base.CombinedStorage;
import net.minecraft.block.BlockState;
import net.minecraft.client.resource.language.I18n;
import net.minecraft.entity.player.PlayerEntity;
@ -51,6 +56,7 @@ import techreborn.init.TRBlockEntities;
import techreborn.init.TRContent;
import java.util.List;
import java.util.Objects;
public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity implements InventoryProvider, IToolDrop, IListInfoProvider, BuiltScreenHandlerProvider {
@ -66,6 +72,8 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity implement
private int serverCapacity = -1;
private ItemStack storeItemStack;
// Fabric transfer API support for the internal stack (one per direction);
private final SingleStackStorage[] internalStoreStorage = new SingleStackStorage[6];
private TRContent.StorageUnit type;
@ -494,4 +502,57 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity implement
public void setStoredStackFromNBT(NbtCompound tag) {
storeItemStack = ItemStack.fromNbt(tag);
}
private Storage<ItemVariant> getInternalStoreStorage(Direction side) {
Objects.requireNonNull(side);
if (internalStoreStorage[side.getId()] == null) {
internalStoreStorage[side.getId()] = new SingleStackStorage() {
@Override
protected ItemStack getStack() {
return storeItemStack;
}
@Override
protected void setStack(ItemStack stack) {
if (stack.isEmpty()) {
// Ensure we maintain reference equality to EMPTY
storeItemStack = ItemStack.EMPTY;
} else {
storeItemStack = stack;
}
}
@Override
protected int getCapacity(ItemVariant itemVariant) {
// subtract capacity of output slot (super capacity is the default capacity)
return maxCapacity - super.getCapacity(itemVariant);
}
@Override
protected boolean canInsert(ItemVariant itemVariant) {
// Check insertion with the same rules as the input slot
return StorageUnitBaseBlockEntity.this.canInsert(INPUT_SLOT, itemVariant.toStack(), side);
}
@Override
protected boolean canExtract(ItemVariant itemVariant) {
// Check extraction with the same rules as the output slot
return StorageUnitBaseBlockEntity.this.canExtract(OUTPUT_SLOT, itemVariant.toStack(), side);
}
@Override
protected void onFinalCommit() {
inventory.setHashChanged();
}
};
}
return internalStoreStorage[side.getId()];
}
public Storage<ItemVariant> getExposedStorage(Direction side) {
return new CombinedStorage<>(List.of(
getInternalStoreStorage(side),
InventoryStorage.of(this, side)
));
}
}

View file

@ -25,6 +25,7 @@
package techreborn.events;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.fabricmc.fabric.api.transfer.v1.item.ItemStorage;
import net.minecraft.block.*;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.item.Item;
@ -36,6 +37,7 @@ import reborncore.common.powerSystem.RcEnergyTier;
import team.reborn.energy.api.EnergyStorage;
import techreborn.TechReborn;
import techreborn.blockentity.cable.CableBlockEntity;
import techreborn.blockentity.storage.item.StorageUnitBaseBlockEntity;
import techreborn.blocks.misc.*;
import techreborn.config.TechRebornConfig;
import techreborn.init.*;
@ -242,6 +244,7 @@ public class ModRegistry {
}
private static void registerApis() {
EnergyStorage.SIDED.registerForBlockEntities((be, direction) -> ((CableBlockEntity) be).getSideEnergyStorage(direction), TRBlockEntities.CABLE);
EnergyStorage.SIDED.registerForBlockEntity(CableBlockEntity::getSideEnergyStorage, TRBlockEntities.CABLE);
ItemStorage.SIDED.registerForBlockEntity(StorageUnitBaseBlockEntity::getExposedStorage, TRBlockEntities.STORAGE_UNIT);
}
}