Skip to content

Commit

Permalink
add filtering to base handler classes
Browse files Browse the repository at this point in the history
rename and rework IFilteredHandler for item handlers as well
deprecate some classes
  • Loading branch information
ghzdude committed Dec 27, 2024
1 parent 642b702 commit 5309d47
Show file tree
Hide file tree
Showing 15 changed files with 142 additions and 102 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gregtech.api.capability;

import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;

import org.jetbrains.annotations.Nullable;
Expand All @@ -10,18 +11,23 @@
* Interface for fluid containers ({@link net.minecraftforge.fluids.IFluidTank IFluidTank} or
* {@link net.minecraftforge.fluids.capability.IFluidHandler IFluidHandler}) associated with {@link IFilter}.
*/
public interface IFilteredFluidContainer {
public interface IFilteredHandler<T> {

/**
* Compare logic for filtered instances.
*/
Comparator<IFilteredFluidContainer> COMPARATOR = Comparator.nullsLast(
Comparator.comparing(IFilteredFluidContainer::getFilter, IFilter.FILTER_COMPARATOR));
Comparator<IFilteredHandler<?>> COMPARATOR = Comparator.nullsLast(
Comparator.comparing(IFilteredHandler::getFilter, IFilter.FILTER_COMPARATOR));

/**
* @return instance of {@link IFilter} associated to this object, or {@code null} if there's no filter
* associated.
*/
@Nullable
IFilter<FluidStack> getFilter();
IFilter<T> getFilter();

// for type safe casting
interface FluidHandler extends IFilteredHandler<FluidStack> {}

interface ItemHandler extends IFilteredHandler<ItemStack> {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public MultipleTankHandler addTanks(IFluidTank... tanks) {
* Entry of multi fluid tanks. Retains reference to original {@link MultipleTankHandler} for accessing
* information such as {@link MultipleTankHandler#allowSameFluidFill()}.
*/
public static class Entry implements IFluidTank, IFilteredFluidContainer, INBTSerializable<NBTTagCompound>,
public static class Entry implements IFluidTank, IFilteredHandler<FluidStack>, INBTSerializable<NBTTagCompound>,
IFluidTankProperties {

private final IFluidTank tank;
Expand All @@ -128,7 +128,7 @@ public boolean allowSameFluidFill() {
@Nullable
@Override
public IFilter<FluidStack> getFilter() {
return getDelegate() instanceof IFilteredFluidContainer filtered ? filtered.getFilter() : null;
return getDelegate() instanceof IFilteredHandler.FluidHandler filter ? filter.getFilter() : null;
}

@Nullable
Expand Down
36 changes: 21 additions & 15 deletions src/main/java/gregtech/api/capability/impl/CommonFluidFilters.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@

import org.jetbrains.annotations.NotNull;

import static gregtech.api.capability.IFilter.*;

/**
* Common fluid filter implementations.
*/
public enum CommonFluidFilters implements IFilter<FluidStack> {
public class CommonFluidFilters {

ALLOW_ALL {
public static final IFilter<FluidStack> ALLOW_ALL = new IFilter<>() {

@Override
public boolean test(@NotNull FluidStack fluid) {
Expand All @@ -29,11 +31,12 @@ public int getPriority() {
}

@Override
public IFilter<FluidStack> negate() {
public @NotNull IFilter<FluidStack> negate() {
return DISALLOW_ALL;
}
},
DISALLOW_ALL {
};

public static final IFilter<FluidStack> DISALLOW_ALL = new IFilter<>() {

@Override
public boolean test(@NotNull FluidStack fluid) {
Expand All @@ -46,11 +49,12 @@ public int getPriority() {
}

@Override
public IFilter<FluidStack> negate() {
public @NotNull IFilter<FluidStack> negate() {
return ALLOW_ALL;
}
},
BOILER_FLUID {
};

public static final IFilter<FluidStack> BOILER_FLUID = new IFilter<>() {

@Override
public boolean test(@NotNull FluidStack fluid) {
Expand All @@ -69,10 +73,11 @@ public boolean test(@NotNull FluidStack fluid) {

@Override
public int getPriority() {
return IFilter.whitelistLikePriority();
return whitelistLikePriority();
}
},
STEAM {
};

public static final IFilter<FluidStack> STEAM = new IFilter<>() {

@Override
public boolean test(@NotNull FluidStack fluid) {
Expand All @@ -81,10 +86,11 @@ public boolean test(@NotNull FluidStack fluid) {

@Override
public int getPriority() {
return IFilter.whitelistPriority(1);
return whitelistPriority(1);
}
},
LIGHTER_FUEL {
};

public static final IFilter<FluidStack> LIGHTER_FUEL = new IFilter<>() {

@Override
public boolean test(@NotNull FluidStack fluidStack) {
Expand All @@ -93,7 +99,7 @@ public boolean test(@NotNull FluidStack fluidStack) {

@Override
public int getPriority() {
return IFilter.whitelistPriority(2);
return whitelistPriority(2);
}
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package gregtech.api.capability.impl;

import gregtech.api.capability.IFilter;
import gregtech.api.capability.IFilteredFluidContainer;
import gregtech.api.capability.IFilteredHandler;

import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
Expand All @@ -10,7 +10,8 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class FilteredFluidHandler extends FluidTank implements IFilteredFluidContainer {
@Deprecated
public class FilteredFluidHandler extends FluidTank implements IFilteredHandler.FluidHandler {

@Nullable
private IFilter<FluidStack> filter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gregtech.api.capability.impl;

import gregtech.api.capability.IFilter;
import gregtech.api.items.itemhandlers.GTItemStackHandler;
import gregtech.api.metatileentity.MetaTileEntity;

Expand All @@ -11,14 +12,13 @@

import java.util.function.Predicate;

@Deprecated
public class FilteredItemHandler extends GTItemStackHandler {

public static Predicate<ItemStack> getCapabilityFilter(Capability<?> cap) {
return stack -> stack.hasCapability(cap, null);
}

private Predicate<ItemStack> fillPredicate;

public FilteredItemHandler(MetaTileEntity metaTileEntity) {
super(metaTileEntity, 1);
}
Expand All @@ -32,12 +32,18 @@ public FilteredItemHandler(MetaTileEntity metaTileEntity, NonNullList<ItemStack>
}

public FilteredItemHandler setFillPredicate(Predicate<ItemStack> fillPredicate) {
this.fillPredicate = fillPredicate;
setFilter(new IFilter<>() {

@Override
public boolean test(@NotNull ItemStack stack) {
return fillPredicate.test(stack);
}

@Override
public int getPriority() {
return IFilter.noPriority();
}
});
return this;
}

@Override
public boolean isItemValid(int slot, @NotNull ItemStack stack) {
return fillPredicate == null || fillPredicate.test(stack);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package gregtech.api.capability.impl;

import gregtech.api.capability.IFilter;
import gregtech.api.capability.IFilteredFluidContainer;
import gregtech.api.capability.IFilteredHandler;

import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;
Expand All @@ -11,7 +11,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class GTFluidHandlerItemStack extends FluidHandlerItemStack implements IFilteredFluidContainer {
public class GTFluidHandlerItemStack extends FluidHandlerItemStack implements IFilteredHandler.FluidHandler {

@Nullable
private IFilter<FluidStack> filter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package gregtech.api.capability.impl;

import gregtech.api.capability.IFilter;
import gregtech.api.capability.IFilteredFluidContainer;
import gregtech.api.capability.IFilteredHandler;

import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;
Expand All @@ -11,7 +11,8 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class GTSimpleFluidHandlerItemStack extends FluidHandlerItemStackSimple implements IFilteredFluidContainer {
public class GTSimpleFluidHandlerItemStack extends FluidHandlerItemStackSimple
implements IFilteredHandler.FluidHandler {

@Nullable
private IFilter<FluidStack> filter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,11 @@
package gregtech.api.capability.impl;

import gregtech.api.capability.INotifiableHandler;
import gregtech.api.metatileentity.MetaTileEntity;

import java.util.ArrayList;
import java.util.List;

public class NotifiableFilteredFluidHandler extends FilteredFluidHandler implements INotifiableHandler {

List<MetaTileEntity> notifiableEntities = new ArrayList<>();
private final boolean isExport;
@Deprecated
public class NotifiableFilteredFluidHandler extends NotifiableFluidTank {

public NotifiableFilteredFluidHandler(int capacity, MetaTileEntity entityToNotify, boolean isExport) {
super(capacity);
this.notifiableEntities.add(entityToNotify);
this.isExport = isExport;
}

@Override
protected void onContentsChanged() {
super.onContentsChanged();
for (MetaTileEntity metaTileEntity : notifiableEntities) {
if (metaTileEntity != null && metaTileEntity.isValid()) {
addToNotifiedList(metaTileEntity, this, isExport);
}
}
}

@Override
public void addNotifiableMetaTileEntity(MetaTileEntity metaTileEntity) {
this.notifiableEntities.add(metaTileEntity);
}

@Override
public void removeNotifiableMetaTileEntity(MetaTileEntity metaTileEntity) {
this.notifiableEntities.remove(metaTileEntity);
super(capacity, entityToNotify, isExport);
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
package gregtech.api.capability.impl;

import gregtech.api.capability.IFilter;
import gregtech.api.capability.IFilteredHandler;
import gregtech.api.capability.INotifiableHandler;
import gregtech.api.metatileentity.MetaTileEntity;

import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTank;

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

import java.util.ArrayList;
import java.util.List;

public class NotifiableFluidTank extends FluidTank implements INotifiableHandler {
public class NotifiableFluidTank extends FluidTank implements INotifiableHandler, IFilteredHandler.FluidHandler {

List<MetaTileEntity> notifiableEntities = new ArrayList<>();
private final boolean isExport;
private IFilter<FluidStack> filter;

public NotifiableFluidTank(int capacity, MetaTileEntity entityToNotify, boolean isExport) {
super(capacity);
Expand All @@ -38,4 +45,27 @@ public void addNotifiableMetaTileEntity(MetaTileEntity metaTileEntity) {
public void removeNotifiableMetaTileEntity(MetaTileEntity metaTileEntity) {
this.notifiableEntities.remove(metaTileEntity);
}

@Nullable
@Override
public IFilter<FluidStack> getFilter() {
return this.filter;
}

/**
* Set filter instance. If {@code null} is given, then the filter is set to be
*
* @param filter new filter instance
* @return this
*/
@NotNull
public NotifiableFluidTank setFilter(@Nullable IFilter<FluidStack> filter) {
this.filter = filter;
return this;
}

@Override
public boolean canFillFluidType(FluidStack fluid) {
return canFill() && (this.filter == null || this.filter.test(fluid));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@
import gregtech.api.items.itemhandlers.GTItemStackHandler;
import gregtech.api.metatileentity.MetaTileEntity;

import net.minecraftforge.items.IItemHandlerModifiable;

import java.util.ArrayList;
import java.util.List;

public class NotifiableItemStackHandler extends GTItemStackHandler
implements IItemHandlerModifiable, INotifiableHandler {
public class NotifiableItemStackHandler extends GTItemStackHandler implements INotifiableHandler {

List<MetaTileEntity> notifiableEntities = new ArrayList<>();
private final boolean isExport;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public int getPriority() {
}

@Override
public IFilter<FluidStack> negate() {
public @NotNull IFilter<FluidStack> negate() {
return new SingleFluidFilter(this.fluid, !this.blacklist);
}

Expand Down
Loading

0 comments on commit 5309d47

Please sign in to comment.