-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #493 from P3pp3rF1y/1.20.x-dev
feat: ✨ Changed hopper upgrade to filter by contents of the target in…
- Loading branch information
Showing
3 changed files
with
76 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...in/java/net/p3pp3rf1y/sophisticatedstorage/upgrades/hopper/TargetContentsFilterLogic.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |