-
Notifications
You must be signed in to change notification settings - Fork 180
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
Rework Multiblock Abilities #2420
base: master
Are you sure you want to change the base?
Changes from all commits
1e83be0
f6daf31
d4b52ff
1f29159
1a9347f
48692b7
97879c3
32fcbca
ce8a6f4
9adc061
3f1a93e
d875cbb
3008757
0e7c8df
edfc5ec
c856a10
1a0c0a0
94ea753
7495168
4f08937
8198390
a6c25cd
647d062
14e1f5a
82abff1
63c2d41
55bda4c
1da6d2b
cb8b678
0c5b3fc
ac7c42d
c67797a
85b0cc2
f4726f6
352654d
af5fa0c
23576e0
3d6ad01
ba3300c
6c2e89d
06fad3b
2355238
2aeaffe
496107f
ebccd04
7666816
ac3aa2d
21d2a7a
88cf1ef
671f64b
fa2a6a6
6c5c857
11fe724
d6a9484
6526f61
a355042
b7dc3bc
e8fd9ba
c6356f0
d945704
7d2f3ac
2491727
7786efc
8d83336
bbf145b
b5370c3
694532c
9e83d60
6376a61
0fb50cc
48c3ac9
ecab010
8a28d6f
e75c48f
040a5a5
7e04dff
bce0914
4cd8e62
4b62204
b75f2e8
5c7bfa4
8b3d528
ccff22d
66183ff
ff47a7a
b3387eb
9b0295f
92caafe
35be693
e716fff
8018399
f15c3b8
3df691c
a70e49d
d9656da
69f24e7
52177f4
f268ffa
1d4e47b
e80cedd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,242 @@ | ||
package gregtech.api.capability; | ||
|
||
import gregtech.api.capability.impl.FluidTankList; | ||
import gregtech.api.metatileentity.MetaTileEntity; | ||
import gregtech.api.util.ItemStackHashStrategy; | ||
|
||
import net.minecraft.item.ItemStack; | ||
import net.minecraftforge.fluids.FluidStack; | ||
import net.minecraftforge.fluids.IFluidTank; | ||
import net.minecraftforge.fluids.capability.IFluidTankProperties; | ||
import net.minecraftforge.items.IItemHandlerModifiable; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class DualHandler implements IItemHandlerModifiable, IMultipleTankHandler, INotifiableHandler { | ||
|
||
private static final ItemStackHashStrategy strategy = ItemStackHashStrategy.builder() | ||
.compareItem(true) | ||
.compareDamage(true) | ||
.compareTag(true) | ||
.compareCount(true) | ||
.build(); | ||
@NotNull | ||
IItemHandlerModifiable itemDelegate; | ||
@NotNull | ||
IMultipleTankHandler fluidDelegate; | ||
|
||
private final List<ITankEntry> unwrapped; | ||
|
||
List<MetaTileEntity> notifiableEntities = new ArrayList<>(); | ||
private final boolean isExport; | ||
|
||
public DualHandler(@NotNull IItemHandlerModifiable itemDelegate, | ||
@NotNull IMultipleTankHandler fluidDelegate, | ||
boolean isExport) { | ||
this.itemDelegate = itemDelegate; | ||
this.fluidDelegate = fluidDelegate; | ||
this.isExport = isExport; | ||
|
||
List<ITankEntry> list = new ArrayList<>(); | ||
for (var tank : this.fluidDelegate) { | ||
list.add(new DualEntry(this, tank)); | ||
} | ||
this.unwrapped = list; | ||
} | ||
|
||
public DualHandler(@NotNull IItemHandlerModifiable itemDelegate, | ||
@NotNull IFluidTank fluidTank, | ||
boolean isExport) { | ||
this(itemDelegate, new FluidTankList(false, fluidTank), isExport); | ||
} | ||
|
||
public IItemHandlerModifiable getItemDelegate() { | ||
return this.itemDelegate; | ||
} | ||
|
||
public IMultipleTankHandler getFluidDelegate() { | ||
return this.fluidDelegate; | ||
} | ||
|
||
public boolean isExport() { | ||
return this.isExport; | ||
} | ||
|
||
@Override | ||
public int getSlots() { | ||
return itemDelegate.getSlots(); | ||
} | ||
|
||
@Override | ||
public ItemStack getStackInSlot(int slot) { | ||
return itemDelegate.getStackInSlot(slot); | ||
} | ||
|
||
@Override | ||
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) { | ||
var remainder = itemDelegate.insertItem(slot, stack, simulate); | ||
if (!simulate && !strategy.equals(remainder, stack)) | ||
ghzdude marked this conversation as resolved.
Show resolved
Hide resolved
|
||
onContentsChanged(); | ||
return remainder; | ||
} | ||
|
||
@Override | ||
public ItemStack extractItem(int slot, int amount, boolean simulate) { | ||
var extracted = itemDelegate.extractItem(slot, amount, simulate); | ||
if (!simulate && !extracted.isEmpty()) | ||
onContentsChanged(); | ||
return extracted; | ||
} | ||
|
||
@Override | ||
public int getSlotLimit(int slot) { | ||
return itemDelegate.getSlotLimit(slot); | ||
} | ||
|
||
@Override | ||
public void setStackInSlot(int slot, ItemStack stack) { | ||
var oldStack = itemDelegate.getStackInSlot(slot); | ||
itemDelegate.setStackInSlot(slot, stack); | ||
if (!strategy.equals(oldStack, stack)) | ||
ghzdude marked this conversation as resolved.
Show resolved
Hide resolved
|
||
onContentsChanged(); | ||
} | ||
|
||
@Override | ||
public IFluidTankProperties[] getTankProperties() { | ||
return this.fluidDelegate.getTankProperties(); | ||
} | ||
|
||
@Override | ||
public int fill(FluidStack resource, boolean doFill) { | ||
int filled = fluidDelegate.fill(resource, doFill); | ||
if (doFill && filled > 0) onContentsChanged(); | ||
return filled; | ||
} | ||
|
||
@Override | ||
public FluidStack drain(FluidStack resource, boolean doDrain) { | ||
var drained = fluidDelegate.drain(resource, doDrain); | ||
if (doDrain && drained != null) onContentsChanged(); | ||
return drained; | ||
} | ||
|
||
@Override | ||
public FluidStack drain(int maxDrain, boolean doDrain) { | ||
var drained = fluidDelegate.drain(maxDrain, doDrain); | ||
if (doDrain && drained != null) onContentsChanged(); | ||
return drained; | ||
} | ||
|
||
@Override | ||
public @NotNull List<ITankEntry> getFluidTanks() { | ||
return this.unwrapped; | ||
} | ||
|
||
@Override | ||
public int getTanks() { | ||
return this.fluidDelegate.getTanks(); | ||
} | ||
|
||
@Override | ||
public @NotNull ITankEntry getTankAt(int index) { | ||
return this.unwrapped.get(index); | ||
} | ||
|
||
@Override | ||
public boolean allowSameFluidFill() { | ||
return this.fluidDelegate.allowSameFluidFill(); | ||
} | ||
|
||
public void onContentsChanged(Object handler) { | ||
for (MetaTileEntity metaTileEntity : notifiableEntities) { | ||
addToNotifiedList(metaTileEntity, handler, isExport); | ||
} | ||
} | ||
|
||
public void onContentsChanged() { | ||
onContentsChanged(this); | ||
} | ||
|
||
@Override | ||
public void addNotifiableMetaTileEntity(MetaTileEntity metaTileEntity) { | ||
if (metaTileEntity == null || this.notifiableEntities.contains(metaTileEntity)) | ||
return; | ||
this.notifiableEntities.add(metaTileEntity); | ||
} | ||
|
||
@Override | ||
public void removeNotifiableMetaTileEntity(MetaTileEntity metaTileEntity) { | ||
this.notifiableEntities.remove(metaTileEntity); | ||
} | ||
|
||
public static class DualEntry implements ITankEntry, INotifiableHandler { | ||
|
||
@NotNull | ||
private final DualHandler tank; | ||
|
||
@NotNull | ||
private final ITankEntry delegate; | ||
|
||
public DualEntry(@NotNull DualHandler tank, @NotNull ITankEntry delegate) { | ||
this.delegate = delegate; | ||
this.tank = tank; | ||
} | ||
|
||
@Override | ||
public @NotNull IMultipleTankHandler getParent() { | ||
return this.tank; | ||
} | ||
|
||
@Override | ||
public @NotNull IFluidTank getDelegate() { | ||
return this.delegate; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are these two backwards? Returning the wrong thing for the method? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. with respect to MultiFluidTankEntry, these methods return the correct objects, but their names are reversed |
||
|
||
@Override | ||
public IFluidTankProperties[] getTankProperties() { | ||
return this.getTank().getTankProperties(); | ||
} | ||
|
||
@Override | ||
public int fill(FluidStack resource, boolean doFill) { | ||
int filled = getTank().fill(resource, doFill); | ||
if (doFill && filled > 0) | ||
tank.onContentsChanged(this); | ||
return filled; | ||
} | ||
|
||
@Override | ||
public FluidStack drain(FluidStack resource, boolean doDrain) { | ||
var drained = getTank().drain(resource, doDrain); | ||
if (doDrain && drained != null) | ||
tank.onContentsChanged(this); | ||
return drained; | ||
} | ||
|
||
@Override | ||
public FluidStack drain(int maxDrain, boolean doDrain) { | ||
var drained = getTank().drain(maxDrain, doDrain); | ||
if (doDrain && drained != null) | ||
tank.onContentsChanged(this); | ||
return drained; | ||
} | ||
|
||
// this method might be redundant | ||
private @NotNull ITankEntry getTank() { | ||
return this.delegate; | ||
} | ||
|
||
@Override | ||
public void addNotifiableMetaTileEntity(MetaTileEntity metaTileEntity) { | ||
this.tank.addNotifiableMetaTileEntity(metaTileEntity); | ||
} | ||
|
||
@Override | ||
public void removeNotifiableMetaTileEntity(MetaTileEntity metaTileEntity) { | ||
this.tank.removeNotifiableMetaTileEntity(metaTileEntity); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor, missing some annotations in this class, notnull, etc