Skip to content

Commit

Permalink
feat: ✨ Changed hopper upgrade to filter by contents of the target in…
Browse files Browse the repository at this point in the history
…ventory when pushing into that inventory and set to filter by contents (instead of the current filtering by source inventory)
  • Loading branch information
P3pp3rF1y committed Sep 29, 2024
1 parent b4bb0eb commit bb0f8aa
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ org.gradle.daemon=false

mod_id=sophisticatedstorage
mod_group_id=sophisticatedstorage
mod_version=0.10.41
mod_version=0.10.42
sonar_project_key=sophisticatedstorage:SophisticatedStorage
github_package_url=https://maven.pkg.github.com/P3pp3rF1y/SophisticatedStorage

Expand All @@ -30,6 +30,6 @@ jade_cf_file_id=4614153
chipped_cf_file_id=5077656
resourcefullib_cf_file_id=5070629
athena_cf_file_id=4764357
sc_version=[1.20.1-0.6.31,1.20.4)
sc_version=[1.20.1-0.6.33,1.20.4)
sb_version=[1.20.1-3.20.5,1.20.4)
parchment_version=1.19.3-2023.03.12-1.20
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemHandlerHelper;
import net.minecraftforge.items.wrapper.EmptyHandler;
import net.p3pp3rf1y.sophisticatedcore.api.IStorageWrapper;
import net.p3pp3rf1y.sophisticatedcore.inventory.ITrackedContentsItemHandler;
import net.p3pp3rf1y.sophisticatedcore.settings.memory.MemorySettingsCategory;
Expand All @@ -25,6 +26,7 @@
import net.p3pp3rf1y.sophisticatedcore.util.NBTHelper;
import net.p3pp3rf1y.sophisticatedcore.util.WorldHelper;
import net.p3pp3rf1y.sophisticatedstorage.block.StorageBlockBase;
import net.p3pp3rf1y.sophisticatedstorage.block.StorageInputBlockEntity;
import net.p3pp3rf1y.sophisticatedstorage.block.VerticalFacing;
import net.p3pp3rf1y.sophisticatedstorage.common.gui.BlockSide;
import net.p3pp3rf1y.sophisticatedstorage.init.ModItems;
Expand All @@ -43,14 +45,14 @@ public class HopperUpgradeWrapper extends UpgradeWrapperBase<HopperUpgradeWrappe
private final Map<Direction, List<LazyOptional<IItemHandler>>> handlerCache = new EnumMap<>(Direction.class);

private final ContentsFilterLogic inputFilterLogic;
private final ContentsFilterLogic outputFilterLogic;
private final TargetContentsFilterLogic outputFilterLogic;
private long coolDownTime = 0;

protected HopperUpgradeWrapper(IStorageWrapper storageWrapper, ItemStack upgrade, Consumer<ItemStack> upgradeSaveHandler) {
super(storageWrapper, upgrade, upgradeSaveHandler);
inputFilterLogic = new ContentsFilterLogic(upgrade, upgradeSaveHandler, upgradeItem.getInputFilterSlotCount(), storageWrapper::getInventoryHandler,
storageWrapper.getSettingsHandler().getTypeCategory(MemorySettingsCategory.class), "inputFilter");
outputFilterLogic = new ContentsFilterLogic(upgrade, upgradeSaveHandler, upgradeItem.getOutputFilterSlotCount(), storageWrapper::getInventoryHandler,
outputFilterLogic = new TargetContentsFilterLogic(upgrade, upgradeSaveHandler, upgradeItem.getOutputFilterSlotCount(), storageWrapper::getInventoryHandler,
storageWrapper.getSettingsHandler().getTypeCategory(MemorySettingsCategory.class), "outputFilter");

deserialize();
Expand Down Expand Up @@ -105,6 +107,7 @@ public void tick(@Nullable LivingEntity entity, Level level, BlockPos pos) {
private boolean pushItemsToContainer(WorldlyContainer worldlyContainer, Direction face) {
ITrackedContentsItemHandler fromHandler = storageWrapper.getInventoryForUpgradeProcessing();

outputFilterLogic.setInventory(EmptyHandler.INSTANCE);
for (int slot = 0; slot < fromHandler.getSlots(); slot++) {
ItemStack slotStack = fromHandler.getStackInSlot(slot);
if (!slotStack.isEmpty() && outputFilterLogic.matchesFilter(slotStack)) {
Expand Down Expand Up @@ -192,6 +195,7 @@ private boolean pullItems(IItemHandler fromHandler) {
}

private boolean pushItems(IItemHandler toHandler) {
outputFilterLogic.setInventory(toHandler);
return moveItems(storageWrapper.getInventoryForUpgradeProcessing(), toHandler, outputFilterLogic);
}

Expand Down Expand Up @@ -246,6 +250,10 @@ public void updateCacheOnSide(Level level, BlockPos pos, Direction direction) {
List<LazyOptional<IItemHandler>> caches = new ArrayList<>();
offsetPositions.forEach(offsetPos ->
WorldHelper.getLoadedBlockEntity(level, offsetPos).ifPresent(blockEntity -> {
if (blockEntity instanceof StorageInputBlockEntity input && input.getControllerPos().isPresent()) {
blockEntity = level.getBlockEntity(input.getControllerPos().get());
}

LazyOptional<IItemHandler> lazyOptional = blockEntity.getCapability(ForgeCapabilities.ITEM_HANDLER, direction.getOpposite());
if (lazyOptional.isPresent()) {
lazyOptional.addListener(l -> updateCacheOnSide(level, pos, direction));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package net.p3pp3rf1y.sophisticatedstorage.upgrades.hopper;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.items.IItemHandler;
import net.p3pp3rf1y.sophisticatedcore.inventory.InventoryHandler;
import net.p3pp3rf1y.sophisticatedcore.inventory.ItemStackKey;
import net.p3pp3rf1y.sophisticatedcore.settings.memory.MemorySettingsCategory;
import net.p3pp3rf1y.sophisticatedcore.upgrades.ContentsFilterLogic;
import net.p3pp3rf1y.sophisticatedcore.util.InventoryHelper;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.Supplier;

public class TargetContentsFilterLogic extends ContentsFilterLogic {
private Set<ItemStackKey> inventoryFilterStacks = new HashSet<>();
private final LoadingCache<IItemHandler, Set<ItemStackKey>> inventoryCache = CacheBuilder.newBuilder().expireAfterWrite(5, TimeUnit.SECONDS).build(new CacheLoader<>() {
@Override
public Set<ItemStackKey> load(IItemHandler inventory) {
return InventoryHelper.getUniqueStacks(inventory);
}
});

public TargetContentsFilterLogic(ItemStack upgrade, Consumer<ItemStack> saveHandler, int filterSlotCount, Supplier<InventoryHandler> getInventoryHandler, MemorySettingsCategory memorySettings, String parentTagKey) {
super(upgrade, saveHandler, filterSlotCount, getInventoryHandler, memorySettings, parentTagKey);
}

public void setInventory(IItemHandler inventory) {
inventoryFilterStacks = inventoryCache.getUnchecked(inventory);
}

@Override
public boolean matchesFilter(ItemStack stack) {
if (!shouldFilterByStorage()) {
return super.matchesFilter(stack);
}

for (ItemStackKey filterStack : inventoryFilterStacks) {
if (stackMatchesFilter(stack, filterStack.getStack())) {
return true;
}
}
return false;
}
}

0 comments on commit bb0f8aa

Please sign in to comment.