From 7bb541a191d608828bb9a94efd3a8a4ceb673834 Mon Sep 17 00:00:00 2001 From: IThundxr Date: Wed, 7 Aug 2024 16:53:57 -0400 Subject: [PATCH] 0.5.1g porting part IV --- .../thresholdSwitch/StorageDrawers.java | 46 +++++++++---------- .../ThresholdSwitchCompat.java | 4 +- .../ThresholdSwitchBlockEntity.java | 39 ++++++++-------- 3 files changed, 44 insertions(+), 45 deletions(-) diff --git a/src/main/java/com/simibubi/create/compat/thresholdSwitch/StorageDrawers.java b/src/main/java/com/simibubi/create/compat/thresholdSwitch/StorageDrawers.java index a5055e017c..40b98ef47c 100644 --- a/src/main/java/com/simibubi/create/compat/thresholdSwitch/StorageDrawers.java +++ b/src/main/java/com/simibubi/create/compat/thresholdSwitch/StorageDrawers.java @@ -1,25 +1,25 @@ package com.simibubi.create.compat.thresholdSwitch; -import com.simibubi.create.compat.Mods; - -import net.minecraft.world.level.block.entity.BlockEntity; -import net.minecraftforge.items.IItemHandler; - -public class StorageDrawers implements ThresholdSwitchCompat { - - @Override - public boolean isFromThisMod(BlockEntity blockEntity) { - return blockEntity != null && Mods.STORAGEDRAWERS.id() - .equals(blockEntity.getType() - .getRegistryName() - .getNamespace()); - } - - @Override - public long getSpaceInSlot(IItemHandler inv, int slot) { - if (slot == 0) - return 0; - - return inv.getSlotLimit(slot); - } -} +//import com.simibubi.create.compat.Mods; +// +//import net.minecraft.world.level.block.entity.BlockEntity; +//import net.minecraftforge.items.IItemHandler; +// +//public class StorageDrawers implements ThresholdSwitchCompat { +// +// @Override +// public boolean isFromThisMod(BlockEntity blockEntity) { +// return blockEntity != null && Mods.STORAGEDRAWERS.id() +// .equals(blockEntity.getType() +// .getRegistryName() +// .getNamespace()); +// } +// +// @Override +// public long getSpaceInSlot(IItemHandler inv, int slot) { +// if (slot == 0) +// return 0; +// +// return inv.getSlotLimit(slot); +// } +//} diff --git a/src/main/java/com/simibubi/create/compat/thresholdSwitch/ThresholdSwitchCompat.java b/src/main/java/com/simibubi/create/compat/thresholdSwitch/ThresholdSwitchCompat.java index 522f56a086..8f60a86df4 100644 --- a/src/main/java/com/simibubi/create/compat/thresholdSwitch/ThresholdSwitchCompat.java +++ b/src/main/java/com/simibubi/create/compat/thresholdSwitch/ThresholdSwitchCompat.java @@ -1,13 +1,13 @@ package com.simibubi.create.compat.thresholdSwitch; 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.minecraft.world.level.block.entity.BlockEntity; public interface ThresholdSwitchCompat { boolean isFromThisMod(BlockEntity blockEntity); - long getSpaceInSlot(Storage inv, int slot); + long getSpaceInSlot(StorageView inv); } diff --git a/src/main/java/com/simibubi/create/content/redstone/thresholdSwitch/ThresholdSwitchBlockEntity.java b/src/main/java/com/simibubi/create/content/redstone/thresholdSwitch/ThresholdSwitchBlockEntity.java index 35d2c7d387..24dcf09fd8 100644 --- a/src/main/java/com/simibubi/create/content/redstone/thresholdSwitch/ThresholdSwitchBlockEntity.java +++ b/src/main/java/com/simibubi/create/content/redstone/thresholdSwitch/ThresholdSwitchBlockEntity.java @@ -2,7 +2,6 @@ import java.util.List; -import com.simibubi.create.compat.thresholdSwitch.StorageDrawers; import com.simibubi.create.compat.thresholdSwitch.ThresholdSwitchCompat; import com.simibubi.create.content.redstone.DirectedDirectionalBlock; import com.simibubi.create.content.redstone.FilteredDetectorFilterSlot; @@ -47,10 +46,11 @@ public class ThresholdSwitchBlockEntity extends SmartBlockEntity { private TankManipulationBehaviour observedTank; private VersionedInventoryTrackerBehaviour invVersionTracker; + // fabric: not needed, view.getCapacity is pretty much correct for the most part private static final List COMPAT = List.of( //new FunctionalStorage(), //new SophisticatedStorage(), - new StorageDrawers() + //new StorageDrawers() ); public ThresholdSwitchBlockEntity(BlockEntityType type, BlockPos pos, BlockState state) { @@ -128,24 +128,23 @@ public void updateCurrentLevel() { } else { invVersionTracker.awaitNewVersion(inv); - for (int slot = 0; slot < inv.getSlots(); slot++) { - ItemStack stackInSlot = inv.getStackInSlot(slot); - - int finalSlot = slot; - long space = COMPAT - .stream() - .filter(compat -> compat.isFromThisMod(targetBlockEntity)) - .map(compat -> compat.getSpaceInSlot(inv, finalSlot)) - .findFirst() - .orElseGet(() -> (long) Math.min(stackInSlot.getMaxStackSize(), inv.getSlotLimit(finalSlot))); - - int count = stackInSlot.getCount(); - if (space == 0) - continue; - - totalSpace += 1; - if (filtering.test(stackInSlot)) - occupied += count * (1f / space); + try (Transaction t = TransferUtil.getTransaction()) { + for (StorageView view : inv.iterable(t)) { + long space = COMPAT + .stream() + .filter(compat -> compat.isFromThisMod(targetBlockEntity)) + .map(compat -> compat.getSpaceInSlot(view)) + .findFirst() + .orElseGet(() -> Math.min(view.getResource().toStack().getMaxStackSize(), view.getCapacity())); + + long count = view.getCapacity(); + if (space == 0) + continue; + + totalSpace += 1; + if (filtering.test(view.getResource().toStack())) + occupied += count * (1f / space); + } } } }