-
Notifications
You must be signed in to change notification settings - Fork 393
Small filter refactor + composite filter #5023
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gustovafing
wants to merge
14
commits into
1.20.1
Choose a base branch
from
gus/composite-filter
base: 1.20.1
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
bb57416
refactor filter ctors
gustovafing 053a1c7
refactor filters
gustovafing c5989fc
update filter handler
gustovafing 36fcb58
remove extra generic from filter handler
gustovafing f9ad277
start working on composite filter
gustovafing 2868452
more filter work
gustovafing 670eece
basic composite filter impl
gustovafing 309fc7b
clean up filter handler
gustovafing 3f476a2
update test code
gustovafing 59d6a5f
small changes and fixes
gustovafing 44b18f2
Merge branch '1.20.1' into gus/composite-filter
gustovafing 6e1b46d
recipes
gustovafing ac1417f
update mui version
gustovafing ad455db
Merge branch '1.20.1' into gus/composite-filter
gustovafing File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
107 changes: 107 additions & 0 deletions
107
src/main/java/com/gregtechceu/gtceu/api/cover/filter/CompositeFilter.java
This file contains hidden or 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,107 @@ | ||
| package com.gregtechceu.gtceu.api.cover.filter; | ||
|
|
||
| import com.gregtechceu.gtceu.api.transfer.item.CustomItemStackHandler; | ||
|
|
||
| import net.minecraft.nbt.CompoundTag; | ||
| import net.minecraft.world.item.ItemStack; | ||
|
|
||
| import brachy.modularui.factory.GuiData; | ||
| import brachy.modularui.screen.UISettings; | ||
| import brachy.modularui.value.sync.PanelSyncManager; | ||
| import brachy.modularui.value.sync.SyncHandlers; | ||
| import brachy.modularui.widgets.layout.Flow; | ||
| import brachy.modularui.widgets.layout.Grid; | ||
| import brachy.modularui.widgets.slot.ItemSlot; | ||
| import brachy.modularui.widgets.slot.SlotGroup; | ||
| import lombok.Getter; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| public class CompositeFilter<T> extends Filter<T> { | ||
|
|
||
| private final Class<T> filterableType; | ||
|
|
||
| private final CustomItemStackHandler itemStacks = new CustomItemStackHandler(9) { | ||
|
|
||
| @Override | ||
| public void onContentsChanged(int slot) { | ||
| onFilterItemChanged(slot); | ||
| } | ||
|
|
||
| @Override | ||
| public int getSlotLimit(int slot) { | ||
| return 1; | ||
| } | ||
| }; | ||
|
|
||
| @Getter | ||
| @SuppressWarnings("unchecked") | ||
| protected @Nullable Filter<T>[] filters = (Filter<T>[]) new Filter[9]; | ||
|
|
||
| public CompositeFilter(ItemStack stack, Class<T> filterableType) { | ||
| super(stack); | ||
|
|
||
| var tag = stack.getOrCreateTag(); | ||
| this.filterableType = filterableType; | ||
| this.itemStacks.setFilter(s -> Filters.isValidFilter(filterableType, s.getItem())); | ||
|
|
||
| if (tag.isEmpty()) return; | ||
| itemStacks.deserializeNBT(tag.getCompound("filters")); | ||
|
|
||
| for (int i = 0; i < 9; i++) { | ||
| var item = itemStacks.getStackInSlot(i); | ||
| if (item.isEmpty()) continue; | ||
| filters[i] = Filters.loadFilter(filterableType, item); | ||
| Objects.requireNonNull(filters[i]).setOnUpdated($ -> updateAndSaveFilter()); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| protected @Nullable CompoundTag writeFilterNBT() { | ||
| if (itemStacks.toList().stream().allMatch(i -> i == ItemStack.EMPTY)) return null; | ||
|
|
||
| var tag = new CompoundTag(); | ||
| tag.put("filters", itemStacks.serializeNBT()); | ||
| return tag; | ||
| } | ||
|
|
||
| private void onFilterItemChanged(int slot) { | ||
| var newItem = itemStacks.getStackInSlot(slot); | ||
|
gustovafing marked this conversation as resolved.
Outdated
|
||
| filters[slot] = null; | ||
| if (!newItem.isEmpty()) { | ||
| filters[slot] = Filters.loadFilter(filterableType, newItem); | ||
| Objects.requireNonNull(filters[slot]).setOnUpdated($ -> updateAndSaveFilter()); | ||
| } | ||
|
|
||
| updateAndSaveFilter(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean supportsAmounts() { | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean test(T t) { | ||
| for (int i=0; i<9; i++) { | ||
| Filter<T> filter = filters[i]; | ||
| if (filter != null && filter.test(t)) return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public Flow getFilterUI(GuiData data, PanelSyncManager syncManager, UISettings settings) { | ||
| SlotGroup slotGroup = new SlotGroup("filters", 9); | ||
|
|
||
| Grid filterGrid = new Grid() | ||
| .coverChildren() | ||
| .gridOfSizeWidth(9, 3, (x, y, i) -> new ItemSlot() | ||
| .slot(SyncHandlers.itemSlot(itemStacks, i).slotGroup(slotGroup))); | ||
|
|
||
| return Flow.row() | ||
| .coverChildrenHeight() | ||
| .child(filterGrid.horizontalCenter()); | ||
| } | ||
| } | ||
91 changes: 81 additions & 10 deletions
91
src/main/java/com/gregtechceu/gtceu/api/cover/filter/Filter.java
This file contains hidden or 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 |
|---|---|---|
| @@ -1,34 +1,105 @@ | ||
| package com.gregtechceu.gtceu.api.cover.filter; | ||
|
|
||
| import com.gregtechceu.gtceu.common.mui.GTGuiTextures; | ||
| import com.gregtechceu.gtceu.common.mui.GTMuiWidgets; | ||
|
|
||
| import net.minecraft.core.registries.BuiltInRegistries; | ||
| import net.minecraft.nbt.CompoundTag; | ||
| import net.minecraft.world.item.ItemStack; | ||
|
|
||
| import brachy.modularui.factory.GuiData; | ||
| import brachy.modularui.screen.ModularPanel; | ||
| import brachy.modularui.screen.UISettings; | ||
| import brachy.modularui.value.sync.PanelSyncManager; | ||
| import brachy.modularui.widgets.Dialog; | ||
| import brachy.modularui.widgets.SlotGroupWidget; | ||
| import brachy.modularui.widgets.layout.Flow; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
| import org.apache.commons.lang3.NotImplementedException; | ||
| import org.jetbrains.annotations.Nullable; | ||
| import org.jetbrains.annotations.Range; | ||
|
|
||
| import java.util.function.Consumer; | ||
| import java.util.function.Predicate; | ||
|
|
||
| public interface Filter<T, S extends Filter<T, S>> extends Predicate<T> { | ||
| public abstract class Filter<T> implements Predicate<T> { | ||
|
|
||
| @Setter | ||
| private Consumer<Filter<T>> onUpdated = $ -> {}; | ||
|
|
||
| @Getter | ||
| protected final ItemStack filterItemStack; | ||
|
|
||
| public Filter(ItemStack stack) { | ||
| this.filterItemStack = stack; | ||
| } | ||
|
|
||
| /** | ||
| * @return Filter panel when opened by itself (including the player inventory) | ||
| */ | ||
| ModularPanel<?> getPanel(GuiData data, PanelSyncManager syncManager, UISettings settings); | ||
| @SuppressWarnings("deprecation") | ||
| public ModularPanel<?> getPanel(GuiData data, PanelSyncManager syncManager, UISettings settings, | ||
| boolean displayPlayerInventory) { | ||
| return new Dialog<>(BuiltInRegistries.ITEM.getKey(filterItemStack.getItem()).toString()) | ||
| .disablePanelsBelow(false) | ||
| .draggable(true) | ||
| .closeOnOutOfBoundsClick(false) | ||
| .child(GTMuiWidgets.createTitleBar(() -> filterItemStack, 176, GTGuiTextures.BACKGROUND)) | ||
| .child(getFilterUI(data, syncManager, settings).top(10)) | ||
| .childIf(displayPlayerInventory, () -> SlotGroupWidget.playerInventory(false).left(7).bottom(7)); | ||
| } | ||
|
|
||
| /** | ||
| * Writes this filter to the filter item's NBT and calls the {@link #onUpdated} listener. | ||
| */ | ||
| public void updateAndSaveFilter() { | ||
| filterItemStack.setTag(writeFilterNBT()); | ||
| onUpdated.accept(this); | ||
| } | ||
|
|
||
| /** | ||
| * Called when a filter is loaded by a filter handler. | ||
| * | ||
| * @param handler The filter handler | ||
| */ | ||
| public void onFilterLoaded(FilterHandler<T> handler) {} | ||
|
|
||
| Flow getFilterUI(GuiData data, PanelSyncManager syncManager, UISettings settings); | ||
| /** | ||
| * @return The filter UI. | ||
| */ | ||
| public abstract Flow getFilterUI(GuiData data, PanelSyncManager syncManager, UISettings settings); | ||
|
|
||
| CompoundTag saveFilter(); | ||
| /** | ||
| * Writes this filter's data to a tag | ||
| * | ||
| * @return The data tag. | ||
| */ | ||
| @Nullable | ||
| protected abstract CompoundTag writeFilterNBT(); | ||
|
|
||
| void setOnUpdated(Consumer<S> onUpdated); | ||
| /** | ||
| * @return Whether this filter supports querying for exact content amounts. | ||
| */ | ||
| public abstract boolean supportsAmounts(); | ||
|
gustovafing marked this conversation as resolved.
Outdated
|
||
|
|
||
| default boolean isBlackList() { | ||
| return false; | ||
| } | ||
| /** | ||
| * Tests if the given stack matches this filter. | ||
| * | ||
| * @return If the given stack matches this filter. | ||
| */ | ||
| @Override | ||
| public abstract boolean test(T t); | ||
|
|
||
| default boolean isBlank() { | ||
| return false; | ||
| /** | ||
| * Retrieves the given amount that matches the filter for the stack. {@link #supportsAmounts()} should be checked | ||
| * before calling this. | ||
| * | ||
| * @return The exact amount that matches the filter.<br> | ||
| * If the stack is not matched by this filter, 0 is returned instead. | ||
| */ | ||
| @Range(from = 0, to = Integer.MAX_VALUE) | ||
| public int testAmount(T stack) { | ||
| throw new NotImplementedException("This filter does not support testing amounts."); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.