Skip to content

Commit

Permalink
Filter nitpicks (#2786)
Browse files Browse the repository at this point in the history
  • Loading branch information
omergunr100 authored Jan 28, 2025
1 parent e305c6c commit 8f899bf
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ public interface Filter<T, S extends Filter<T, S>> extends Predicate<T> {
default boolean isBlackList() {
return false;
}

default boolean isBlank() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,13 @@ public FilterHandler<T, F> onFilterUpdated(Consumer<F> onFilterUpdated) {

private CustomItemStackHandler getFilterSlot() {
if (this.filterSlot == null) {
this.filterSlot = new CustomItemStackHandler(this.filterItem);
this.filterSlot = new CustomItemStackHandler(this.filterItem) {

@Override
public int getSlotLimit(int slot) {
return 1;
}
};

this.filterSlot.setFilter(this::canInsertFilterItem);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,15 @@ public void setOnUpdated(Consumer<FluidFilter> onUpdated) {
};
}

@Override
public boolean isBlank() {
return !isBlackList && !ignoreNbt && Arrays.stream(matches).allMatch(FluidStack::isEmpty);
}

public CompoundTag saveFilter() {
if (isBlank()) {
return null;
}
var tag = new CompoundTag();
tag.putBoolean("isBlackList", isBlackList);
tag.putBoolean("matchNbt", ignoreNbt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,15 @@ public void setOnUpdated(Consumer<ItemFilter> onUpdated) {
};
}

@Override
public boolean isBlank() {
return !isBlackList && !ignoreNbt && Arrays.stream(matches).allMatch(ItemStack::isEmpty);
}

public CompoundTag saveFilter() {
if (isBlank()) {
return null;
}
var tag = new CompoundTag();
tag.putBoolean("isBlackList", isBlackList);
tag.putBoolean("matchNbt", ignoreNbt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,16 @@ public void setOnUpdated(Consumer<ItemFilter> onUpdated) {
};
}

@Override
public boolean isBlank() {
return filterMode.ordinal() == 0;
}

@Override
public CompoundTag saveFilter() {
if (isBlank()) {
return null;
}
var tag = new CompoundTag();
tag.putInt("filterMode", filterMode.ordinal());
return tag;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,15 @@ public abstract class TagFilter<T, S extends Filter<T, S>> implements Filter<T,

protected TagFilter() {}

@Override
public boolean isBlank() {
return oreDictFilterExpression.isBlank();
}

public CompoundTag saveFilter() {
if (isBlank()) {
return null;
}
var tag = new CompoundTag();
tag.putString("oreDict", oreDictFilterExpression);
return tag;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import it.unimi.dsi.fastutil.objects.Object2BooleanMap;
import it.unimi.dsi.fastutil.objects.Object2BooleanOpenHashMap;

import java.util.Objects;
import java.util.function.Consumer;

/**
Expand All @@ -24,7 +25,8 @@ public class TagFluidFilter extends TagFilter<FluidStack, FluidFilter> implement
protected TagFluidFilter() {}

public static TagFluidFilter loadFilter(ItemStack itemStack) {
return loadFilter(itemStack.getOrCreateTag(), filter -> itemStack.setTag(filter.saveFilter()));
return loadFilter(Objects.requireNonNullElseGet(itemStack.getTag(), CompoundTag::new),
filter -> itemStack.setTag(filter.saveFilter()));
}

private static TagFluidFilter loadFilter(CompoundTag tag, Consumer<FluidFilter> itemWriter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import it.unimi.dsi.fastutil.objects.Object2BooleanMap;
import it.unimi.dsi.fastutil.objects.Object2BooleanOpenHashMap;

import java.util.Objects;
import java.util.function.Consumer;

/**
Expand All @@ -23,7 +24,8 @@ public class TagItemFilter extends TagFilter<ItemStack, ItemFilter> implements I
protected TagItemFilter() {}

public static TagItemFilter loadFilter(ItemStack itemStack) {
return loadFilter(itemStack.getOrCreateTag(), filter -> itemStack.setTag(filter.saveFilter()));
return loadFilter(Objects.requireNonNullElseGet(itemStack.getTag(), CompoundTag::new),
filter -> itemStack.setTag(filter.saveFilter()));
}

private static TagItemFilter loadFilter(CompoundTag tag, Consumer<ItemFilter> itemWriter) {
Expand Down

0 comments on commit 8f899bf

Please sign in to comment.