forked from AppliedEnergistics/Applied-Energistics-2
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make ME Interfaces insert items/fluids directly into storage if possi…
…ble (#448)
- Loading branch information
Showing
4 changed files
with
122 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/java/appeng/fluids/util/AENetworkFluidInventory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package appeng.fluids.util; | ||
|
||
import appeng.api.AEApi; | ||
import appeng.api.config.Actionable; | ||
import appeng.api.networking.security.IActionSource; | ||
import appeng.api.networking.storage.IStorageGrid; | ||
import appeng.api.storage.IMEInventory; | ||
import appeng.api.storage.channels.IFluidStorageChannel; | ||
import appeng.api.storage.data.IAEFluidStack; | ||
import net.minecraftforge.fluids.FluidStack; | ||
|
||
import java.util.function.Supplier; | ||
|
||
public class AENetworkFluidInventory extends AEFluidInventory { | ||
|
||
private final Supplier<IStorageGrid> supplier; | ||
private final IActionSource source; | ||
|
||
public AENetworkFluidInventory(Supplier<IStorageGrid> networkSupplier, IActionSource source, IAEFluidInventory handler, int slots, int capcity) { | ||
super(handler, slots, capcity); | ||
this.supplier = networkSupplier; | ||
this.source = source; | ||
} | ||
|
||
@Override | ||
public int fill(final FluidStack fluid, final boolean doFill) { | ||
if (fluid == null || fluid.amount <= 0) { | ||
return 0; | ||
} | ||
IStorageGrid storage = supplier.get(); | ||
if (storage != null) { | ||
int originAmt = fluid.amount; | ||
IMEInventory<IAEFluidStack> dest = storage.getInventory(AEApi.instance().storage().getStorageChannel(IFluidStorageChannel.class)); | ||
IAEFluidStack overflow = dest.injectItems(AEFluidStack.fromFluidStack(fluid), doFill ? Actionable.MODULATE : Actionable.SIMULATE, this.source); | ||
if (overflow != null && overflow.getStackSize() == originAmt) { | ||
return super.fill(fluid, doFill); | ||
} else if (overflow != null) { | ||
return (int) (originAmt - overflow.getStackSize()); | ||
} else { | ||
return originAmt; | ||
} | ||
} else { | ||
return super.fill(fluid, doFill); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/main/java/appeng/tile/inventory/AppEngNetworkInventory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package appeng.tile.inventory; | ||
|
||
import appeng.api.AEApi; | ||
import appeng.api.config.Actionable; | ||
import appeng.api.networking.security.IActionSource; | ||
import appeng.api.networking.storage.IStorageGrid; | ||
import appeng.api.storage.IMEInventory; | ||
import appeng.api.storage.channels.IItemStorageChannel; | ||
import appeng.api.storage.data.IAEItemStack; | ||
import appeng.util.inv.IAEAppEngInventory; | ||
import appeng.util.item.AEItemStack; | ||
import net.minecraft.item.ItemStack; | ||
|
||
import javax.annotation.Nonnull; | ||
import java.util.function.Supplier; | ||
|
||
public class AppEngNetworkInventory extends AppEngInternalOversizedInventory { | ||
|
||
private final Supplier<IStorageGrid> supplier; | ||
private final IActionSource source; | ||
|
||
public AppEngNetworkInventory(Supplier<IStorageGrid> networkSupplier, IActionSource source, IAEAppEngInventory inventory, int size, int maxStack) { | ||
super(inventory, size, maxStack); | ||
this.supplier = networkSupplier; | ||
this.source = source; | ||
} | ||
|
||
@Override | ||
@Nonnull | ||
public ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate) { | ||
IStorageGrid storage = supplier.get(); | ||
if (storage != null) { | ||
int originAmt = stack.getCount(); | ||
IMEInventory<IAEItemStack> dest = storage.getInventory(AEApi.instance().storage().getStorageChannel(IItemStorageChannel.class)); | ||
IAEItemStack overflow = dest.injectItems(AEItemStack.fromItemStack(stack), simulate ? Actionable.SIMULATE : Actionable.MODULATE, this.source); | ||
if (overflow != null && overflow.getStackSize() == originAmt) { | ||
return super.insertItem(slot, stack, simulate); | ||
} else if (overflow != null) { | ||
return overflow.createItemStack(); | ||
} else { | ||
return ItemStack.EMPTY; | ||
} | ||
} else { | ||
return super.insertItem(slot, stack, simulate); | ||
} | ||
} | ||
|
||
} |