Skip to content

Commit

Permalink
Manual IO modes on covers (#536)
Browse files Browse the repository at this point in the history
* style: reformat some code in MetaMachine

* refactor: allow covers to override item and fluid transfer capabilities

* feat: UI controls for distribution & manual IO mode

* feat: handle manual IO mode when transferring through covers

* refactor: make previously added @ExpectPlatform method static

* refactor: improve tooltip for insert_first distribution mode
  • Loading branch information
mikerooni authored Nov 12, 2023
1 parent 36bab84 commit a1ab43d
Show file tree
Hide file tree
Showing 35 changed files with 676 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import com.lowdragmc.lowdraglib.LDLib;
import com.lowdragmc.lowdraglib.gui.texture.IGuiTexture;
import com.lowdragmc.lowdraglib.gui.widget.Widget;
import com.lowdragmc.lowdraglib.side.fluid.IFluidTransfer;
import com.lowdragmc.lowdraglib.side.item.IItemTransfer;
import com.lowdragmc.lowdraglib.utils.RayTraceHelper;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
Expand Down Expand Up @@ -61,6 +63,10 @@ public interface ICoverable extends ITickSubscription, IAppearance, IFancyConfig

boolean shouldRenderBackSide();

// TODO replace getItemTransferCap and getFluidTransferCap with a cross-platform capability implementation
IItemTransfer getItemTransferCap(@Nullable Direction side, boolean useCoverCapability);
IFluidTransfer getFluidTransferCap(@Nullable Direction side, boolean useCoverCapability);

/**
* Its an internal method, you should never call it yourself.
* <br>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.gregtechceu.gtceu.api.syncdata.IEnhancedManaged;
import com.gregtechceu.gtceu.client.renderer.cover.ICoverRenderer;
import com.lowdragmc.lowdraglib.gui.texture.ResourceTexture;
import com.lowdragmc.lowdraglib.side.fluid.IFluidTransfer;
import com.lowdragmc.lowdraglib.side.item.IItemTransfer;
import com.lowdragmc.lowdraglib.syncdata.annotation.DescSynced;
import com.lowdragmc.lowdraglib.syncdata.annotation.Persisted;
import com.lowdragmc.lowdraglib.syncdata.field.ManagedFieldHolder;
Expand Down Expand Up @@ -199,4 +201,17 @@ public BlockState getAppearance(BlockState sourceState, BlockPos sourcePos) {
return null;
}

//////////////////////////////////////
//******* Capabilities *******//
//////////////////////////////////////

// TODO possibly refactor this in the future, using some sort of cross-platform capability-like system

public IItemTransfer getItemTransferCap(Direction side, IItemTransfer defaultValue) {
return defaultValue;
}

public IFluidTransfer getFluidTransferCap(Direction side, IFluidTransfer defaultValue) {
return defaultValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ public F getFilter() {
return this.filter;
}

public boolean test(T resource) {
return getFilter().test(resource);
}

public FilterHandler<T, F> onFilterLoaded(Consumer<F> onFilterLoaded) {
this.onFilterLoaded = onFilterLoaded;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.gregtechceu.gtceu.api.syncdata.EnhancedFieldManagedStorage;
import com.gregtechceu.gtceu.api.syncdata.IEnhancedManaged;
import com.gregtechceu.gtceu.api.syncdata.UpdateListener;
import com.lowdragmc.lowdraglib.side.fluid.IFluidTransfer;
import com.lowdragmc.lowdraglib.side.item.IItemTransfer;
import com.lowdragmc.lowdraglib.syncdata.annotation.DescSynced;
import com.lowdragmc.lowdraglib.syncdata.annotation.Persisted;
import com.lowdragmc.lowdraglib.syncdata.annotation.ReadOnlyManaged;
Expand Down Expand Up @@ -172,6 +174,16 @@ public void setCoverAtSide(@Nullable CoverBehavior coverBehavior, Direction side
}
}

@Override
public IItemTransfer getItemTransferCap(@Nullable Direction side, boolean useCoverCapability) {
return machine.getItemTransferCap(side, useCoverCapability);
}

@Override
public IFluidTransfer getFluidTransferCap(@Nullable Direction side, boolean useCoverCapability) {
return machine.getFluidTransferCap(side, useCoverCapability);
}

@SuppressWarnings("unused")
private boolean onCoverDirty(CoverBehavior coverBehavior) {
if (coverBehavior != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
import com.lowdragmc.lowdraglib.LDLib;
import com.lowdragmc.lowdraglib.gui.texture.IGuiTexture;
import com.lowdragmc.lowdraglib.gui.texture.ResourceTexture;
import com.lowdragmc.lowdraglib.misc.FluidTransferList;
import com.lowdragmc.lowdraglib.misc.ItemTransferList;
import com.lowdragmc.lowdraglib.side.fluid.FluidStack;
import com.lowdragmc.lowdraglib.side.fluid.IFluidTransfer;
import com.lowdragmc.lowdraglib.side.item.IItemTransfer;
Expand Down Expand Up @@ -563,29 +561,47 @@ protected Predicate<FluidStack> getFluidCapFilter(@Nullable Direction side) {
}

@Nullable
public ItemTransferList getItemTransferCap(@Nullable Direction side) {
var list = getTraits().stream().filter(IItemTransfer.class::isInstance).filter(t -> t.hasCapability(side)).map(IItemTransfer.class::cast).toList();
if (!list.isEmpty()) {
var io = IO.BOTH;
if (side != null && this instanceof IAutoOutputItem autoOutput && autoOutput.getOutputFacingItems() == side && !autoOutput.isAllowInputFromOutputSideItems()) {
io = IO.OUT;
}
return new IOItemTransferList(list, io, getItemCapFilter(side));
public IItemTransfer getItemTransferCap(@Nullable Direction side, boolean useCoverCapability) {
var list = getTraits().stream()
.filter(IItemTransfer.class::isInstance)
.filter(t -> t.hasCapability(side))
.map(IItemTransfer.class::cast)
.toList();

if (list.isEmpty()) return null;

var io = IO.BOTH;
if (side != null && this instanceof IAutoOutputItem autoOutput && autoOutput.getOutputFacingItems() == side && !autoOutput.isAllowInputFromOutputSideItems()) {
io = IO.OUT;
}
return null;

IOItemTransferList transferList = new IOItemTransferList(list, io, getItemCapFilter(side));
if (!useCoverCapability || side == null) return transferList;

CoverBehavior cover = getCoverContainer().getCoverAtSide(side);
return cover != null ? cover.getItemTransferCap(side, transferList) : transferList;
}

@Nullable
public FluidTransferList getFluidTransferCap(@Nullable Direction side) {
var list = getTraits().stream().filter(IFluidTransfer.class::isInstance).filter(t -> t.hasCapability(side)).map(IFluidTransfer.class::cast).toList();
if (!list.isEmpty()) {
var io = IO.BOTH;
if (side != null && this instanceof IAutoOutputFluid autoOutput && autoOutput.getOutputFacingFluids() == side && !autoOutput.isAllowInputFromOutputSideFluids()) {
io = IO.OUT;
}
return new IOFluidTransferList(list, io, getFluidCapFilter(side));
public IFluidTransfer getFluidTransferCap(@Nullable Direction side, boolean useCoverCapability) {
var list = getTraits().stream()
.filter(IFluidTransfer.class::isInstance)
.filter(t -> t.hasCapability(side))
.map(IFluidTransfer.class::cast)
.toList();

if (list.isEmpty()) return null;

var io = IO.BOTH;
if (side != null && this instanceof IAutoOutputFluid autoOutput && autoOutput.getOutputFacingFluids() == side && !autoOutput.isAllowInputFromOutputSideFluids()) {
io = IO.OUT;
}
return null;

IOFluidTransferList transferList = new IOFluidTransferList(list, io, getFluidCapFilter(side));
if (!useCoverCapability || side == null) return transferList;

CoverBehavior cover = getCoverContainer().getCoverAtSide(side);
return cover != null ? cover.getFluidTransferCap(side, transferList) : transferList;
}

//////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
import com.gregtechceu.gtceu.api.syncdata.EnhancedFieldManagedStorage;
import com.gregtechceu.gtceu.api.syncdata.IEnhancedManaged;
import com.gregtechceu.gtceu.api.syncdata.UpdateListener;
import com.gregtechceu.gtceu.api.transfer.fluid.NoOpFluidTransfer;
import com.gregtechceu.gtceu.api.transfer.item.NoOpItemTransfer;
import com.gregtechceu.gtceu.common.blockentity.FluidPipeBlockEntity;
import com.gregtechceu.gtceu.common.blockentity.ItemPipeBlockEntity;
import com.lowdragmc.lowdraglib.side.fluid.IFluidTransfer;
import com.lowdragmc.lowdraglib.side.item.IItemTransfer;
import com.lowdragmc.lowdraglib.syncdata.annotation.DescSynced;
import com.lowdragmc.lowdraglib.syncdata.annotation.Persisted;
import com.lowdragmc.lowdraglib.syncdata.annotation.ReadOnlyManaged;
Expand Down Expand Up @@ -143,6 +149,24 @@ public void unsubscribe(@Nullable TickableSubscription current) {
pipeTile.unsubscribe(current);
}

@Override
public IItemTransfer getItemTransferCap(@Nullable Direction side, boolean useCoverCapability) {
if (pipeTile instanceof ItemPipeBlockEntity itemPipe) {
return itemPipe.getHandler(side, useCoverCapability);
} else {
return NoOpItemTransfer.INSTANCE;
}
}

@Override
public IFluidTransfer getFluidTransferCap(@Nullable Direction side, boolean useCoverCapability) {
if (pipeTile instanceof FluidPipeBlockEntity fluidPipe) {
return fluidPipe.getHandler(side, useCoverCapability);
} else {
return NoOpFluidTransfer.INSTANCE;
}
}

@Override
public CoverBehavior getCoverAtSide(Direction side) {
return switch (side) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.gregtechceu.gtceu.api.transfer.fluid;

import com.lowdragmc.lowdraglib.side.fluid.FluidStack;
import com.lowdragmc.lowdraglib.side.fluid.IFluidTransfer;
import net.minecraft.MethodsReturnNonnullByDefault;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;


@MethodsReturnNonnullByDefault
@ParametersAreNonnullByDefault
public abstract class FluidTransferDelegate implements IFluidTransfer {
protected IFluidTransfer delegate;

public FluidTransferDelegate(IFluidTransfer delegate) {
this.delegate = delegate;
}

protected void setDelegate(IFluidTransfer delegate) {
this.delegate = delegate;
}


//////////////////////////////////////
//****** OVERRIDE THESE ******//
//////////////////////////////////////


@Override
public int getTanks() {
return delegate.getTanks();
}

@Override
@Nonnull
public FluidStack getFluidInTank(int tank) {
return delegate.getFluidInTank(tank);
}

@Override
@ApiStatus.Internal
public void setFluidInTank(int tank, @NotNull FluidStack fluidStack) {
delegate.setFluidInTank(tank, fluidStack);
}

@Override
public long getTankCapacity(int tank) {
return delegate.getTankCapacity(tank);
}

@Override
public boolean isFluidValid(int tank, @NotNull FluidStack stack) {
return delegate.isFluidValid(tank, stack);
}

@Override
@ApiStatus.Internal
public long fill(int tank, FluidStack resource, boolean simulate, boolean notifyChanges) {
return delegate.fill(tank, resource, simulate, notifyChanges);
}

@Override
public boolean supportsFill(int tank) {
return delegate.supportsFill(tank);
}

@Override
@ApiStatus.Internal
@Nonnull
public FluidStack drain(int tank, FluidStack resource, boolean simulate, boolean notifyChanges) {
return delegate.drain(tank, resource, simulate, notifyChanges);
}

@Override
public boolean supportsDrain(int tank) {
return delegate.supportsDrain(tank);
}

@Override
@ApiStatus.Internal
@Nonnull
public Object createSnapshot() {
return delegate.createSnapshot();
}

@Override
@ApiStatus.Internal
public void restoreFromSnapshot(Object snapshot) {
delegate.restoreFromSnapshot(snapshot);
}

@Override
public void onContentsChanged() {
delegate.onContentsChanged();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.gregtechceu.gtceu.api.transfer.fluid;

import com.lowdragmc.lowdraglib.side.fluid.FluidStack;
import com.lowdragmc.lowdraglib.side.fluid.IFluidTransfer;
import net.minecraft.MethodsReturnNonnullByDefault;
import org.jetbrains.annotations.NotNull;

import javax.annotation.ParametersAreNonnullByDefault;


@MethodsReturnNonnullByDefault
@ParametersAreNonnullByDefault
public class NoOpFluidTransfer implements IFluidTransfer {
public static final NoOpFluidTransfer INSTANCE = new NoOpFluidTransfer();

private NoOpFluidTransfer() {
}

@Override
public int getTanks() {
return 0;
}

@NotNull
@Override
public FluidStack getFluidInTank(int tank) {
return FluidStack.empty();
}

@Override
public void setFluidInTank(int tank, @NotNull FluidStack fluidStack) {
}

@Override
public long getTankCapacity(int tank) {
return 0;
}

@Override
public boolean isFluidValid(int tank, @NotNull FluidStack stack) {
return false;
}

@Override
public long fill(int tank, FluidStack resource, boolean simulate, boolean notifyChanges) {
return 0;
}

@Override
public boolean supportsFill(int tank) {
return false;
}

@NotNull
@Override
public FluidStack drain(int tank, FluidStack resource, boolean simulate, boolean notifyChanges) {
return FluidStack.empty();
}

@Override
public boolean supportsDrain(int tank) {
return false;
}

@NotNull
@Override
public Object createSnapshot() {
return new Object();
}

@Override
public void restoreFromSnapshot(Object snapshot) {

}
}
Loading

0 comments on commit a1ab43d

Please sign in to comment.