Skip to content

Commit

Permalink
remove onMatched
Browse files Browse the repository at this point in the history
replace with MatchResult (again)
rename some methods
  • Loading branch information
ghzdude committed Jan 19, 2024
1 parent e6b08ed commit 82cc354
Show file tree
Hide file tree
Showing 17 changed files with 115 additions and 137 deletions.
86 changes: 37 additions & 49 deletions src/main/java/gregtech/common/covers/CoverConveyor.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package gregtech.common.covers;

import com.cleanroommc.modularui.value.sync.StringSyncValue;

import gregtech.api.GTValues;
import gregtech.api.capability.GregtechDataCodes;
import gregtech.api.capability.GregtechTileCapabilities;
Expand All @@ -28,8 +26,6 @@
import gregtech.common.covers.filter.ItemFilterContainer;
import gregtech.common.pipelike.itempipe.tile.TileEntityItemPipe;

import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;

import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
Expand Down Expand Up @@ -63,23 +59,22 @@
import com.cleanroommc.modularui.value.sync.EnumSyncValue;
import com.cleanroommc.modularui.value.sync.GuiSyncManager;
import com.cleanroommc.modularui.value.sync.IntSyncValue;
import com.cleanroommc.modularui.value.sync.StringSyncValue;
import com.cleanroommc.modularui.widget.ParentWidget;
import com.cleanroommc.modularui.widgets.ButtonWidget;
import com.cleanroommc.modularui.widgets.layout.Column;
import com.cleanroommc.modularui.widgets.layout.Row;
import com.cleanroommc.modularui.widgets.textfield.TextFieldWidget;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntList;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenCustomHashMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectOpenCustomHashSet;
import org.jetbrains.annotations.NotNull;

import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

public class CoverConveyor extends CoverBase implements CoverWithUI, ITickable, IControllable {

Expand Down Expand Up @@ -304,13 +299,9 @@ protected int moveInventoryItems(IItemHandler sourceInventory, IItemHandler targ
continue;
}

AtomicBoolean matchResult = new AtomicBoolean(true);
AtomicInteger matchSlotIndex = new AtomicInteger(-1);
itemFilterContainer.onMatch(itemStack, (matched, match, matchedSlot) -> {
matchResult.set(matched);
matchSlotIndex.set(matchedSlot);
});
if (!matchResult.get() || !itemInfos.containsKey(matchSlotIndex.get())) {
var matchResult = itemFilterContainer.match(itemStack);
int matchSlotIndex = matchResult.getFilterIndex();
if (!matchResult.isMatched() || !itemInfos.containsKey(matchSlotIndex)) {
continue;
}

Expand Down Expand Up @@ -355,11 +346,8 @@ protected int moveInventoryItems(IItemHandler sourceInventory, IItemHandler targ
continue;
}

AtomicBoolean didMatch = new AtomicBoolean(true);
itemFilterContainer.onMatch(sourceStack, (matched, match, matchedSlot) -> {
didMatch.set(matched);
});
if (!didMatch.get()) continue;
var result = itemFilterContainer.match(sourceStack);
if (!result.isMatched()) continue;

ItemStack remainder = GTTransferUtils.insertItem(targetInventory, sourceStack, true);
int amountToInsert = sourceStack.getCount() - remainder.getCount();
Expand Down Expand Up @@ -417,20 +405,19 @@ protected Map<ItemStack, TypeItemInfo> countInventoryItemsByType(@NotNull IItemH
continue;
}

int finalSrcIndex = srcIndex;
itemFilterContainer.onMatch(itemStack, (matched, match, matchedSlot) -> {
if (!matched) return;
if (!result.containsKey(itemStack)) {
TypeItemInfo itemInfo = new TypeItemInfo(itemStack.copy(), matchedSlot, new IntArrayList(), 0);
itemInfo.totalCount += itemStack.getCount();
itemInfo.slots.add(finalSrcIndex);
result.put(itemStack.copy(), itemInfo);
} else {
TypeItemInfo itemInfo = result.get(itemStack);
itemInfo.totalCount += itemStack.getCount();
itemInfo.slots.add(finalSrcIndex);
}
});
var matchResult = itemFilterContainer.match(itemStack);
if (!matchResult.isMatched()) continue;

if (!result.containsKey(itemStack)) {
TypeItemInfo itemInfo = new TypeItemInfo(itemStack.copy(), matchResult.getFilterIndex(), new IntArrayList(), 0);
itemInfo.totalCount += itemStack.getCount();
itemInfo.slots.add(srcIndex);
result.put(itemStack.copy(), itemInfo);
} else {
TypeItemInfo itemInfo = result.get(itemStack);
itemInfo.totalCount += itemStack.getCount();
itemInfo.slots.add(srcIndex);
}
}
return result;
}
Expand All @@ -444,20 +431,21 @@ protected Map<Integer, GroupItemInfo> countInventoryItemsByMatchSlot(@NotNull II
continue;
}

itemFilterContainer.onMatch(itemStack, (matched, match, matchedSlot) -> {
if (!matched) return;
if (!result.containsKey(matchedSlot)) {
GroupItemInfo itemInfo = new GroupItemInfo(matchedSlot,
new ObjectOpenCustomHashSet<>(ItemStackHashStrategy.comparingAllButCount()), 0);
itemInfo.itemStackTypes.add(itemStack.copy());
itemInfo.totalCount += itemStack.getCount();
result.put(matchedSlot, itemInfo);
} else {
GroupItemInfo itemInfo = result.get(matchedSlot);
itemInfo.itemStackTypes.add(itemStack.copy());
itemInfo.totalCount += itemStack.getCount();
}
});
var matchResult = itemFilterContainer.match(itemStack);
if (!matchResult.isMatched()) continue;
int matchedSlot = matchResult.getFilterIndex();

if (!result.containsKey(matchedSlot)) {
GroupItemInfo itemInfo = new GroupItemInfo(matchedSlot,
new ObjectOpenCustomHashSet<>(ItemStackHashStrategy.comparingAllButCount()), 0);
itemInfo.itemStackTypes.add(itemStack.copy());
itemInfo.totalCount += itemStack.getCount();
result.put(matchedSlot, itemInfo);
} else {
GroupItemInfo itemInfo = result.get(matchedSlot);
itemInfo.itemStackTypes.add(itemStack.copy());
itemInfo.totalCount += itemStack.getCount();
}

}
return result;
Expand Down Expand Up @@ -775,7 +763,7 @@ public ItemStack insertItem(int slot, @NotNull ItemStack stack, boolean simulate
return stack;
}
if (manualImportExportMode == ManualImportExportMode.FILTERED &&
!itemFilterContainer.testItemStack(stack)) {
!itemFilterContainer.test(stack)) {
return stack;
}
return super.insertItem(slot, stack, simulate);
Expand All @@ -789,7 +777,7 @@ public ItemStack extractItem(int slot, int amount, boolean simulate) {
}
if (manualImportExportMode == ManualImportExportMode.FILTERED) {
ItemStack result = super.extractItem(slot, amount, true);
if (result.isEmpty() || !itemFilterContainer.testItemStack(result)) {
if (result.isEmpty() || !itemFilterContainer.test(result)) {
return ItemStack.EMPTY;
}
return simulate ? result : super.extractItem(slot, amount, false);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/gregtech/common/covers/CoverItemFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public boolean canPipePassThrough() {
}

public boolean testItemStack(ItemStack stack) {
return itemFilter.testItemStack(stack);
return itemFilter.test(stack);
}

@Override
Expand Down Expand Up @@ -213,7 +213,7 @@ public ItemHandlerFiltered(IItemHandler delegate) {
@NotNull
@Override
public ItemStack insertItem(int slot, @NotNull ItemStack stack, boolean simulate) {
if (getFilterMode() == ItemFilterMode.FILTER_EXTRACT || !itemFilter.testItemStack(stack)) {
if (getFilterMode() == ItemFilterMode.FILTER_EXTRACT || !itemFilter.test(stack)) {
return stack;
}
return super.insertItem(slot, stack, simulate);
Expand All @@ -224,7 +224,7 @@ public ItemStack insertItem(int slot, @NotNull ItemStack stack, boolean simulate
public ItemStack extractItem(int slot, int amount, boolean simulate) {
if (getFilterMode() != ItemFilterMode.FILTER_INSERT) {
ItemStack result = super.extractItem(slot, amount, true);
if (result.isEmpty() || !itemFilter.testItemStack(result)) {
if (result.isEmpty() || !itemFilter.test(result)) {
return ItemStack.EMPTY;
}
return simulate ? result : super.extractItem(slot, amount, false);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/gregtech/common/covers/CoverItemVoiding.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void voidAny(IItemHandler myItemHandler) {
if (sourceStack.isEmpty()) {
continue;
}
if (!itemFilterContainer.testItemStack(sourceStack)) {
if (!itemFilterContainer.test(sourceStack)) {
continue;
}
myItemHandler.extractItem(srcIndex, Integer.MAX_VALUE, false);
Expand Down Expand Up @@ -179,7 +179,7 @@ public ItemStack getStackInSlot(int slot) {
@NotNull
@Override
public ItemStack insertItem(int slot, @NotNull ItemStack stack, boolean simulate) {
if (!itemFilterContainer.testItemStack(stack)) {
if (!itemFilterContainer.test(stack)) {
return stack;
}
return ItemStack.EMPTY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import org.jetbrains.annotations.NotNull;

import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;

public class CoverItemVoidingAdvanced extends CoverItemVoiding {
Expand Down Expand Up @@ -78,13 +77,8 @@ protected void voidOverflow(IItemHandler myItemHandler) {
if (!getItemFilterContainer().hasFilter()) {
itemToVoidAmount = typeItemInfo.totalCount - itemFilterContainer.getTransferSize();
} else {
AtomicInteger atomicInt = new AtomicInteger(itemToVoidAmount);
itemFilterContainer.onMatch(typeItemInfo.itemStack, (matched, match, matchedSlot) -> {
if (matched) {
atomicInt.set(typeItemInfo.totalCount - itemFilterContainer.getTransferLimit(matchedSlot));
}
});
itemToVoidAmount = atomicInt.get();
var result = itemFilterContainer.match(typeItemInfo.itemStack);
itemToVoidAmount = result.getMatchedStack().getCount();
}

if (itemToVoidAmount <= 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public void update() {
int storedItems = 0;

for (int i = 0; i < itemHandler.getSlots(); i++) {
if (itemFilter.testItemStack(itemHandler.getStackInSlot(i)))
if (itemFilter.test(itemHandler.getStackInSlot(i)))
storedItems += itemHandler.getStackInSlot(i).getCount();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,17 @@ public int getTransferLimit(int slotIndex) {
return currentItemFilter.getTransferLimit(slotIndex, getTransferSize());
}

public boolean testItemStack(R toTest) {
public boolean test(R toTest) {
return !hasFilter() || getFilter().test(toTest);
}

public MatchResult<R> match(R toMatch) {
if (!hasFilter())
return new MatchResult<>(true, toMatch, -1);

return getFilter().match(toMatch);
}

public int getTransferLimit(R stack) {
if (isBlacklistFilter() || currentItemFilter == null) {
return getTransferSize();
Expand Down Expand Up @@ -116,11 +123,6 @@ public boolean isBlacklistFilter() {
return hasFilter() && getFilter().isBlacklistFilter();
}

public void onMatch(R toTest, Filter.OnMatch<R> onMatch) {
this.getFilter().setOnMatched(onMatch);
this.getFilter().match(toTest);
}

/** Uses Cleanroom MUI*/
public abstract IWidget initUI(ModularPanel main, GuiSyncManager manager);

Expand Down
9 changes: 1 addition & 8 deletions src/main/java/gregtech/common/covers/filter/Filter.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,14 @@ public interface Filter<T> {

@NotNull Widget<?> createWidgets(GuiSyncManager syncManager);

void match(T toMatch);
MatchResult<T> match(T toMatch);

boolean test(T toTest);

void setDirtyNotifiable(IDirtyNotifiable dirtyNotifiable);

void markDirty();

void setOnMatched(OnMatch<T> onMatch);

int getMaxTransferSize();

void setMaxTransferSize(int maxTransferSize);
Expand All @@ -42,9 +40,4 @@ public interface Filter<T> {
boolean isBlacklistFilter();

void setBlacklistFilter(boolean blacklistFilter);

@FunctionalInterface
interface OnMatch<T> {
void onMatch(boolean matched, T match, int matchedSlot);
}
}
19 changes: 4 additions & 15 deletions src/main/java/gregtech/common/covers/filter/FluidFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,12 @@
public abstract class FluidFilter implements Filter<FluidStack> {

private IDirtyNotifiable dirtyNotifiable;

private OnMatch<FluidStack> onMatch = null;
private BaseFluidFilterReader filterReader;

protected void setFilterReader(BaseFluidFilterReader filterReader) {
this.filterReader = filterReader;
}

public abstract void match(FluidStack toMatch);

public abstract boolean test(FluidStack fluidStack);

@Override
public final void setOnMatched(OnMatch<FluidStack> onMatch) {
this.onMatch = onMatch;
}

protected final void onMatch(boolean matched, FluidStack stack, int index) {
if (this.onMatch != null) this.onMatch.onMatch(matched, stack, index);
}

public int getTransferLimit(FluidStack fluidStack, int transferSize) {
return 0;
}
Expand All @@ -66,6 +51,10 @@ public int getTransferLimit(FluidStack stack) {
@Deprecated
public abstract void initUI(Consumer<gregtech.api.gui.Widget> widgetGroup);

protected static MatchResult<FluidStack> createResult(boolean matched, FluidStack fluidStack, int index) {
return new MatchResult<>(matched, fluidStack, index);
}

public @NotNull Widget<?> createWidgets(GuiSyncManager syncManager) {
var blacklist = new BooleanSyncValue(this.filterReader::isBlacklistFilter, this.filterReader::setBlacklistFilter);
return new ParentWidget<>().coverChildren()
Expand Down
15 changes: 2 additions & 13 deletions src/main/java/gregtech/common/covers/filter/ItemFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ public abstract class ItemFilter implements Filter<ItemStack> {
private IDirtyNotifiable dirtyNotifiable;
private BaseItemFilterReader filterReader;

private OnMatch<ItemStack> onMatch = null;

protected void setFilterReader(BaseItemFilterReader reader) {
this.filterReader = reader;
}
Expand Down Expand Up @@ -94,14 +92,10 @@ public void readFromNBT(NBTTagCompound tagCompound) {
markDirty();
}

public abstract void match(ItemStack itemStack);

protected final void onMatch(boolean matched, ItemStack stack, int matchSlot) {
if (this.onMatch != null) this.onMatch.onMatch(matched, stack, matchSlot);
protected static MatchResult<ItemStack> createResult(boolean matched, ItemStack stack, int index) {
return new MatchResult<>(matched, stack, index);
}

public abstract boolean test(ItemStack toTest);

public final void setDirtyNotifiable(IDirtyNotifiable dirtyNotifiable) {
this.dirtyNotifiable = dirtyNotifiable;
}
Expand All @@ -112,11 +106,6 @@ public final void markDirty() {
}
}

@Override
public void setOnMatched(OnMatch<ItemStack> onMatch) {
this.onMatch = onMatch;
}

protected static class BaseItemFilterReader extends BaseFilterReader {
protected static final String COUNT = "Count";
public BaseItemFilterReader(ItemStack container, int slots) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ public int getSlotTransferLimit(int matchSlot, int globalTransferLimit) {
}

public boolean testItemStack(ItemStack itemStack) {
return container.testItemStack(itemStack);
return container.test(itemStack);
}
}
Loading

0 comments on commit 82cc354

Please sign in to comment.