Skip to content
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

Add in-line filtering for item and fluid pipes #2126

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@
import com.gregtechceu.gtceu.api.cover.CoverDefinition;
import com.gregtechceu.gtceu.api.cover.IUICover;
import com.gregtechceu.gtceu.api.cover.filter.FluidFilter;
import com.gregtechceu.gtceu.api.transfer.fluid.FluidTransferDelegate;

import com.lowdragmc.lowdraglib.gui.widget.LabelWidget;
import com.lowdragmc.lowdraglib.gui.widget.Widget;
import com.lowdragmc.lowdraglib.gui.widget.WidgetGroup;
import com.lowdragmc.lowdraglib.side.fluid.FluidStack;
import com.lowdragmc.lowdraglib.side.fluid.FluidTransferHelper;
import com.lowdragmc.lowdraglib.side.fluid.IFluidTransfer;

import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.core.Direction;

import org.jetbrains.annotations.Nullable;

import javax.annotation.ParametersAreNonnullByDefault;

/**
Expand All @@ -26,6 +31,7 @@
public class FluidFilterCover extends CoverBehavior implements IUICover {

protected FluidFilter fluidFilter;
private FilteredFluidTransferWrapper fluidFilterWrapper;

public FluidFilterCover(CoverDefinition definition, ICoverable coverHolder, Direction attachedSide) {
super(definition, coverHolder, attachedSide);
Expand All @@ -43,11 +49,45 @@ public FluidFilter getFluidFilter() {
return fluidFilter;
}

@Override
public @Nullable IFluidTransfer getFluidTransferCap(@Nullable IFluidTransfer defaultValue) {
if (defaultValue == null) {
return null;
}

if (fluidFilterWrapper == null || fluidFilterWrapper.delegate != defaultValue) {
this.fluidFilterWrapper = new FilteredFluidTransferWrapper(defaultValue);
}

return fluidFilterWrapper;
}

@Override
public Widget createUIWidget() {
final var group = new WidgetGroup(0, 0, 176, 80);
group.addWidget(new LabelWidget(5, 3, attachItem.getDescriptionId()));
group.addWidget(getFluidFilter().openConfigurator((176 - 80) / 2, (60 - 55) / 2 + 15));
return group;
}

private class FilteredFluidTransferWrapper extends FluidTransferDelegate {

public FilteredFluidTransferWrapper(IFluidTransfer delegate) {
super(delegate);
}

@Override
public long fill(int tank, FluidStack resource, boolean simulate, boolean notifyChanges) {
if (!fluidFilter.test(resource))
return 0;
return super.fill(tank, resource, simulate, notifyChanges);
}

@Override
public FluidStack drain(int tank, FluidStack resource, boolean simulate, boolean notifyChanges) {
if (!fluidFilter.test(resource))
return FluidStack.empty();
return super.drain(tank, resource, simulate, notifyChanges);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,25 @@
import com.gregtechceu.gtceu.api.cover.IUICover;
import com.gregtechceu.gtceu.api.cover.filter.ItemFilter;
import com.gregtechceu.gtceu.api.gui.widget.EnumSelectorWidget;
import com.gregtechceu.gtceu.api.transfer.item.ItemTransferDelegate;
import com.gregtechceu.gtceu.common.cover.data.ItemFilterMode;

import com.lowdragmc.lowdraglib.gui.widget.LabelWidget;
import com.lowdragmc.lowdraglib.gui.widget.Widget;
import com.lowdragmc.lowdraglib.gui.widget.WidgetGroup;
import com.lowdragmc.lowdraglib.side.item.IItemTransfer;
import com.lowdragmc.lowdraglib.side.item.ItemTransferHelper;
import com.lowdragmc.lowdraglib.syncdata.annotation.DescSynced;
import com.lowdragmc.lowdraglib.syncdata.annotation.Persisted;
import com.lowdragmc.lowdraglib.syncdata.field.ManagedFieldHolder;

import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.core.Direction;
import net.minecraft.world.item.ItemStack;

import lombok.Getter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.annotation.ParametersAreNonnullByDefault;

Expand All @@ -40,6 +45,7 @@ public class ItemFilterCover extends CoverBehavior implements IUICover {
@DescSynced
@Getter
protected ItemFilterMode filterMode = ItemFilterMode.FILTER_INSERT;
private FilteredItemTransferWrapper itemFilterWrapper;

public ItemFilterCover(CoverDefinition definition, ICoverable coverHolder, Direction attachedSide) {
super(definition, coverHolder, attachedSide);
Expand All @@ -62,6 +68,17 @@ public boolean canAttach() {
return ItemTransferHelper.getItemTransfer(coverHolder.getLevel(), coverHolder.getPos(), attachedSide) != null;
}

@Override
public @Nullable IItemTransfer getItemTransferCap(IItemTransfer defaultValue) {
if (defaultValue == null) {
return null;
}
if (itemFilterWrapper == null || itemFilterWrapper.delegate != defaultValue) {
this.itemFilterWrapper = new ItemFilterCover.FilteredItemTransferWrapper(defaultValue);
}
return itemFilterWrapper;
}

@Override
public Widget createUIWidget() {
final var group = new WidgetGroup(0, 0, 176, 85);
Expand All @@ -76,4 +93,28 @@ public Widget createUIWidget() {
public ManagedFieldHolder getFieldHolder() {
return MANAGED_FIELD_HOLDER;
}

private class FilteredItemTransferWrapper extends ItemTransferDelegate {

public FilteredItemTransferWrapper(IItemTransfer delegate) {
super(delegate);
}

@Override
public @NotNull ItemStack insertItem(int slot, @NotNull ItemStack stack, boolean simulate,
boolean notifyChanges) {
if (filterMode != ItemFilterMode.FILTER_EXTRACT && !itemFilter.test(stack))
return stack;
return super.insertItem(slot, stack, simulate, notifyChanges);
}

@Override
public @NotNull ItemStack extractItem(int slot, int amount, boolean simulate, boolean notifyChanges) {
ItemStack result = super.extractItem(slot, amount, true, notifyChanges);
if (result.isEmpty() || (filterMode != ItemFilterMode.FILTER_INSERT && !itemFilter.test(result))) {
return ItemStack.EMPTY;
}
return simulate ? result : super.extractItem(slot, amount, false, notifyChanges);
}
}
}