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 retain exact modes to fluid regulators and robot arms #2684

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions src/main/java/gregtech/api/mui/GTGuiTextures.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,11 @@ public static class IDs {
18, 18 * 2, 18, 18, true);

public static final UITexture[] TRANSFER_MODE_OVERLAY = slice("textures/gui/overlay/transfer_mode_overlay.png",
18, 18 * 3, 18, 18, true);
18, 18 * 4, 18, 18, true);

public static final UITexture[] FLUID_TRANSFER_MODE_OVERLAY = slice(
"textures/gui/overlay/fluid_transfer_mode_overlay.png",
18, 18 * 3, 18, 18, true);
18, 18 * 4, 18, 18, true);

public static final UITexture[] DISTRIBUTION_MODE_OVERLAY = slice(
"textures/gui/widget/button_distribution_mode.png",
Expand Down
18 changes: 11 additions & 7 deletions src/main/java/gregtech/common/covers/CoverFluidRegulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,14 @@ protected int doTransferFluidsInternal(IFluidHandler myFluidHandler, IFluidHandl
return switch (transferMode) {
case TRANSFER_ANY -> GTTransferUtils.transferFluids(sourceHandler, destHandler, transferLimit,
fluidFilterContainer::test);
case KEEP_EXACT -> doKeepExact(transferLimit, sourceHandler, destHandler,
fluidFilterContainer::test,
this.fluidFilterContainer.getTransferSize());
case TRANSFER_EXACT -> doTransferExact(transferLimit, sourceHandler, destHandler,
fluidFilterContainer::test, this.fluidFilterContainer.getTransferSize());
case KEEP_EXACT -> doKeepExact(transferLimit, sourceHandler, destHandler,
fluidFilterContainer::test,
this.fluidFilterContainer.getTransferSize(), true);
case RETAIN_EXACT -> doKeepExact(transferLimit, sourceHandler, destHandler,
fluidFilterContainer::test,
this.fluidFilterContainer.getTransferSize(), false);
};
}

Expand Down Expand Up @@ -107,7 +110,7 @@ protected int doKeepExact(final int transferLimit,
final IFluidHandler sourceHandler,
final IFluidHandler destHandler,
final Predicate<FluidStack> fluidFilter,
int keepAmount) {
int keepAmount, boolean direction) {
if (sourceHandler == null || destHandler == null || fluidFilter == null)
return 0;

Expand All @@ -127,11 +130,12 @@ protected int doKeepExact(final int transferLimit,

// if fluid needs to be moved to meet the Keep Exact value
int amountInDest;
if ((amountInDest = destFluids.getOrDefault(fluidStack, 0)) < keepAmount) {
if (direction ? (amountInDest = destFluids.getOrDefault(fluidStack, 0)) < keepAmount :
(amountInDest = sourceFluids.getOrDefault(fluidStack, 0)) > keepAmount) {

// move the lesser of the remaining transfer limit and the difference in actual vs keep exact amount
int amountToMove = Math.min(transferLimit - transferred,
keepAmount - amountInDest);
direction ? (keepAmount - amountInDest) : (amountInDest - keepAmount));

// Nothing to do here, try the next fluid.
if (amountToMove <= 0)
Expand Down Expand Up @@ -281,7 +285,7 @@ public int getMaxTransferRate() {
return switch (this.transferMode) {
case TRANSFER_ANY -> 1;
case TRANSFER_EXACT -> maxFluidTransferRate;
case KEEP_EXACT -> Integer.MAX_VALUE;
case KEEP_EXACT, RETAIN_EXACT -> Integer.MAX_VALUE;
};
}

Expand Down
24 changes: 17 additions & 7 deletions src/main/java/gregtech/common/covers/CoverRoboticArm.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ protected int doTransferItems(IItemHandler itemHandler, IItemHandler myItemHandl
return switch (transferMode) {
case TRANSFER_ANY -> doTransferItemsAny(itemHandler, myItemHandler, maxTransferAmount);
case TRANSFER_EXACT -> doTransferExact(itemHandler, myItemHandler, maxTransferAmount);
case KEEP_EXACT -> doKeepExact(itemHandler, myItemHandler, maxTransferAmount);
case KEEP_EXACT -> doKeepExact(itemHandler, myItemHandler, maxTransferAmount, true);
case RETAIN_EXACT -> doKeepExact(itemHandler, myItemHandler, maxTransferAmount, false);
};
}

Expand Down Expand Up @@ -122,7 +123,8 @@ protected int doTransferExact(IItemHandler itemHandler, IItemHandler myItemHandl
return Math.min(itemsTransferred, maxTransferAmount);
}

protected int doKeepExact(IItemHandler itemHandler, IItemHandler myItemHandler, int maxTransferAmount) {
protected int doKeepExact(IItemHandler itemHandler, IItemHandler myItemHandler, int maxTransferAmount,
boolean direction) {
Map<Integer, GroupItemInfo> currentItemAmount = doCountDestinationInventoryItemsByMatchIndex(itemHandler,
myItemHandler);
Map<Integer, GroupItemInfo> sourceItemAmounts = doCountDestinationInventoryItemsByMatchIndex(myItemHandler,
Expand All @@ -145,12 +147,20 @@ protected int doKeepExact(IItemHandler itemHandler, IItemHandler myItemHandler,
}

int itemAmount = 0;
if (currentItemAmount.containsKey(filterSlotIndex)) {
GroupItemInfo destItemInfo = currentItemAmount.get(filterSlotIndex);
itemAmount = destItemInfo.totalCount;
if (direction) {
if (currentItemAmount.containsKey(filterSlotIndex)) {
GroupItemInfo destItemInfo = currentItemAmount.get(filterSlotIndex);
itemAmount = destItemInfo.totalCount;
}
} else {
if (sourceItemAmounts.containsKey(filterSlotIndex)) {
GroupItemInfo sourceItemInfo = sourceItemAmounts.get(filterSlotIndex);
itemAmount = sourceItemInfo.totalCount;
}
}
if (itemAmount < itemToKeepAmount) {
sourceInfo.totalCount = itemToKeepAmount - itemAmount;

if (direction ? (itemAmount < itemToKeepAmount) : (itemAmount > itemToKeepAmount)) {
sourceInfo.totalCount = direction ? (itemToKeepAmount - itemAmount) : (itemAmount - itemToKeepAmount);
} else {
iterator.remove();
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/gregtech/common/covers/TransferMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ public enum TransferMode implements IStringSerializable {

TRANSFER_ANY("cover.robotic_arm.transfer_mode.transfer_any", 1),
TRANSFER_EXACT("cover.robotic_arm.transfer_mode.transfer_exact", 1024),
KEEP_EXACT("cover.robotic_arm.transfer_mode.keep_exact", Integer.MAX_VALUE);
KEEP_EXACT("cover.robotic_arm.transfer_mode.keep_exact", Integer.MAX_VALUE),
RETAIN_EXACT("cover.robotic_arm.transfer_mode.retain_exact", Integer.MAX_VALUE);

public static final TransferMode[] VALUES = values();
public final String localeName;
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/gregtech/lang/en_us.lang
Original file line number Diff line number Diff line change
Expand Up @@ -1335,6 +1335,7 @@ cover.robotic_arm.exact=§7items
cover.robotic_arm.transfer_mode.transfer_any=Transfer Any
cover.robotic_arm.transfer_mode.transfer_exact=Supply Exact
cover.robotic_arm.transfer_mode.keep_exact=Keep Exact
cover.robotic_arm.transfer_mode.retain_exact=Retain Exact
cover.robotic_arm.transfer_mode.description=§eTransfer Any§r - in this mode, cover will transfer as many items matching its filter as possible./n§eSupply Exact§r - in this mode, cover will supply items in portions specified in item filter slots (or variable under this button for ore dictionary filter). If amount of items is less than portion size, items won't be moved. If there's a §bSmart Item Filter§r, the variable under this button will act as a multiplier instead./n§eKeep Exact§r - in this mode, cover will keep specified amount of items in the destination inventory, supplying additional amount of items if required./n§7Tip: left/right click on filter slots to change item amount, use shift clicking to change amount faster.

cover.pump.title=Pump Cover Settings (%s)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void doKeepExact_does_nothing_if_no_destination_tank_exists() {
new FluidTankList(false, new FluidTank(water.copy(), 64000)));

// Tell it to keep exact from a machine with an empty fluid tank and null target fluid tank
int amountTransferred = cfr.doKeepExact(1000, source, null, isWater, 1000);
int amountTransferred = cfr.doKeepExact(1000, source, null, isWater, 1000, true);

MatcherAssert.assertThat("Unexpectedly moved fluids, nothing is supposed to happen", amountTransferred, is(0));
}
Expand All @@ -66,7 +66,7 @@ public void doKeepExact_moves_one_fluid_into_an_empty_tank() {
new FluidTankList(false));

// Tell it to keep exact from a machine with an empty fluid tank and no target fluid tank
int amountTransferred = cfr.doKeepExact(1000, source, dest, isWater, 1000);
int amountTransferred = cfr.doKeepExact(1000, source, dest, isWater, 1000, true);

MatcherAssert.assertThat("Wrong fluid amount moved", amountTransferred, is(1000));
}
Expand All @@ -84,7 +84,7 @@ public void doKeepExact_moves_only_as_much_fluid_as_exists_in_the_source() {
IFluidHandler dest = new FluidHandlerProxy(new FluidTankList(false, new FluidTank(64000)),
new FluidTankList(false));

int amountTransferred = cfr.doKeepExact(10000, source, dest, isWater, 10000);
int amountTransferred = cfr.doKeepExact(10000, source, dest, isWater, 10000, true);

MatcherAssert.assertThat("Wrong fluid amount moved", amountTransferred, is(1234));
}
Expand All @@ -104,7 +104,7 @@ public void doKeepExact_moves_only_the_fluid_required_if_more_could_be_moved() {
new FluidTank(new FluidStack(FluidRegistry.WATER, 100), 64000)),
new FluidTankList(false));

int amountTransferred = cfr.doKeepExact(10000, source, dest, isWater, 144);
int amountTransferred = cfr.doKeepExact(10000, source, dest, isWater, 144, true);

MatcherAssert.assertThat("Wrong fluid amount moved", amountTransferred, is(44));
}
Expand All @@ -129,7 +129,7 @@ public void doKeepExact_moves_multiple_valid_fluids() {
new FluidTankList(false));

// accept any fluid this time
int amountTransferred = cfr.doKeepExact(10000, source, dest, fs -> true, 144);
int amountTransferred = cfr.doKeepExact(10000, source, dest, fs -> true, 144, true);

// expect that 44mB of water and 144mB of lava will be moved
MatcherAssert.assertThat("Wrong fluid amount moved", amountTransferred, is(44 + 144));
Expand Down Expand Up @@ -164,7 +164,7 @@ public void doKeepExact_respects_transfer_limit_with_one_fluid() {
new FluidTankList(false));

// accept any fluid this time
int amountTransferred = cfr.doKeepExact(100, source, dest, fs -> true, 144);
int amountTransferred = cfr.doKeepExact(100, source, dest, fs -> true, 144, true);

// expect that at most 100mB of fluids total will be moved this tick, as if possible it would do 144mB
MatcherAssert.assertThat("Wrong fluid amount moved", amountTransferred, is(100));
Expand All @@ -190,7 +190,7 @@ public void doKeepExact_respects_transfer_limit_with_multiple_fluids() {
new FluidTankList(false));

// accept any fluid this time
int amountTransferred = cfr.doKeepExact(100, source, dest, fs -> true, 144);
int amountTransferred = cfr.doKeepExact(100, source, dest, fs -> true, 144, true);

// expect that at most 100mB of fluids total will be moved this tick, as if possible it would do 188mB
MatcherAssert.assertThat("Wrong fluid amount moved", amountTransferred, is(100));
Expand All @@ -216,7 +216,7 @@ public void doKeepExact_does_nothing_if_levels_are_already_correct_in_dest() {
new FluidTankList(false));

// accept any fluid this time
int amountTransferred = cfr.doKeepExact(10000, source, dest, fs -> true, 144);
int amountTransferred = cfr.doKeepExact(10000, source, dest, fs -> true, 144, true);

// expect that no fluids are moved because Keep Exact levels are already met
MatcherAssert.assertThat("Wrong fluid amount moved", amountTransferred, is(0));
Expand All @@ -242,7 +242,7 @@ public void doKeepExact_ignores_fluids_not_in_filter() {
new FluidTankList(false));

// accept any fluid this time
int amountTransferred = cfr.doKeepExact(10000, source, dest, isWater, 144);
int amountTransferred = cfr.doKeepExact(10000, source, dest, isWater, 144, true);

// expect that no fluids are moved because already have enough water and lava isn't in the filter
MatcherAssert.assertThat("Wrong fluid amount moved", amountTransferred, is(0));
Expand Down
Loading