Moving to source sets
This commit is contained in:
@@ -1,311 +0,0 @@
|
||||
|
||||
package appeng.fluids.util;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import alexiil.mc.lib.attributes.fluid.volume.FluidVolume;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
|
||||
import appeng.api.storage.data.IAEFluidStack;
|
||||
import appeng.core.AELog;
|
||||
import appeng.util.Platform;
|
||||
|
||||
public class AEFluidInventory implements IAEFluidTank {
|
||||
private final IAEFluidStack[] fluids;
|
||||
private final IAEFluidInventory handler;
|
||||
private final int capacity;
|
||||
|
||||
public AEFluidInventory(final IAEFluidInventory handler, final int slots, final int capacity) {
|
||||
this.fluids = new IAEFluidStack[slots];
|
||||
this.handler = handler;
|
||||
this.capacity = capacity;
|
||||
}
|
||||
|
||||
public AEFluidInventory(final IAEFluidInventory handler, final int slots) {
|
||||
this(handler, slots, Integer.MAX_VALUE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setFluidInSlot(final int slot, final IAEFluidStack fluid) {
|
||||
if (slot >= 0 && slot < this.getSlots()) {
|
||||
if (Objects.equals(this.fluids[slot], fluid)) {
|
||||
if (fluid != null && fluid.getStackSize() != this.fluids[slot].getStackSize()) {
|
||||
this.fluids[slot].setStackSize(Math.min(fluid.getStackSize(), this.capacity));
|
||||
this.onContentChanged(slot);
|
||||
}
|
||||
} else {
|
||||
if (fluid == null) {
|
||||
this.fluids[slot] = null;
|
||||
} else {
|
||||
this.fluids[slot] = fluid.copy();
|
||||
this.fluids[slot].setStackSize(Math.min(fluid.getStackSize(), this.capacity));
|
||||
}
|
||||
|
||||
this.onContentChanged(slot);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void onContentChanged(final int slot) {
|
||||
if (this.handler != null && Platform.isServer()) {
|
||||
this.handler.onFluidInventoryChanged(this, slot);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IAEFluidStack getFluidInSlot(final int slot) {
|
||||
if (slot >= 0 && slot < this.getSlots()) {
|
||||
return this.fluids[slot];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSlots() {
|
||||
return this.fluids.length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTanks() {
|
||||
return this.fluids.length;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public FluidVolume getFluidInTank(int tank) {
|
||||
if (tank < 0 || tank >= fluids.length) {
|
||||
return FluidVolumeUtil.EMPTY;
|
||||
}
|
||||
return fluids[tank] == null ? FluidVolumeUtil.EMPTY : fluids[tank].getFluidStack();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTankCapacity(int tank) {
|
||||
return Math.min(this.capacity, Integer.MAX_VALUE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFluidValid(int tank, @Nonnull FluidVolume stack) {
|
||||
return stack != FluidVolumeUtil.EMPTY;
|
||||
}
|
||||
|
||||
public int fill(final int slot, final FluidVolume resource, final boolean doFill) {
|
||||
if (resource.isEmpty() || resource.getAmount() <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
final IAEFluidStack fluid = this.fluids[slot];
|
||||
|
||||
if (fluid != null && !fluid.getFluidStack().equals(resource)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int amountToStore = this.capacity;
|
||||
|
||||
if (fluid != null) {
|
||||
amountToStore -= fluid.getStackSize();
|
||||
}
|
||||
|
||||
amountToStore = Math.min(amountToStore, resource.getAmount());
|
||||
|
||||
if (doFill) {
|
||||
if (fluid == null) {
|
||||
this.setFluidInSlot(slot, AEFluidStack.fromFluidStack(resource));
|
||||
} else {
|
||||
fluid.setStackSize(fluid.getStackSize() + amountToStore);
|
||||
this.onContentChanged(slot);
|
||||
}
|
||||
}
|
||||
|
||||
return amountToStore;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int fill(FluidVolume resource, FluidAction action) {
|
||||
if (resource.isEmpty() || resource.getAmount() <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Find a suitable slot
|
||||
int slot = indexOfFluid(resource);
|
||||
if (slot == -1) {
|
||||
slot = indexOfEmptySlot();
|
||||
if (slot == -1) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
final IAEFluidStack fluid = this.fluids[slot];
|
||||
|
||||
int amountToStore = this.capacity;
|
||||
|
||||
if (fluid != null) {
|
||||
amountToStore -= fluid.getStackSize();
|
||||
}
|
||||
|
||||
amountToStore = Math.min(amountToStore, resource.getAmount());
|
||||
|
||||
if (action == FluidAction.EXECUTE) {
|
||||
if (fluid == null) {
|
||||
this.setFluidInSlot(slot, AEFluidStack.fromFluidStack(resource));
|
||||
} else {
|
||||
fluid.setStackSize(fluid.getStackSize() + amountToStore);
|
||||
this.onContentChanged(slot);
|
||||
}
|
||||
}
|
||||
|
||||
return amountToStore;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FluidVolume drain(final FluidVolume fluid, final FluidAction action) {
|
||||
if (fluid.isEmpty() || fluid.getAmount() <= 0) {
|
||||
return FluidVolumeUtil.EMPTY;
|
||||
}
|
||||
|
||||
final FluidVolume resource = fluid.copy();
|
||||
|
||||
FluidVolume totalDrained = FluidVolumeUtil.EMPTY;
|
||||
for (int slot = 0; slot < this.getSlots(); ++slot) {
|
||||
FluidVolume drain = this.drain(slot, resource, action == FluidAction.EXECUTE);
|
||||
if (drain != null) {
|
||||
if (totalDrained.isEmpty()) {
|
||||
totalDrained = drain;
|
||||
} else {
|
||||
totalDrained.setAmount(totalDrained.getAmount() + drain.getAmount());
|
||||
}
|
||||
|
||||
resource.setAmount(resource.getAmount() - drain.getAmount());
|
||||
if (resource.getAmount() <= 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return totalDrained;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FluidVolume drain(final int maxDrain, final FluidAction action) {
|
||||
if (maxDrain == 0) {
|
||||
return FluidVolumeUtil.EMPTY;
|
||||
}
|
||||
|
||||
FluidVolume totalDrained = FluidVolumeUtil.EMPTY;
|
||||
int toDrain = maxDrain;
|
||||
|
||||
for (int slot = 0; slot < this.getSlots(); ++slot) {
|
||||
if (totalDrained.isEmpty()) {
|
||||
totalDrained = this.drain(slot, toDrain, action == FluidAction.EXECUTE);
|
||||
if (totalDrained.isEmpty()) {
|
||||
toDrain -= totalDrained.getAmount();
|
||||
}
|
||||
} else {
|
||||
FluidVolume copy = totalDrained.copy();
|
||||
copy.setAmount(toDrain);
|
||||
FluidVolume drain = this.drain(slot, copy, action == FluidAction.EXECUTE);
|
||||
if (drain != null) {
|
||||
totalDrained.setAmount(totalDrained.getAmount() + drain.getAmount());
|
||||
toDrain -= drain.getAmount();
|
||||
}
|
||||
}
|
||||
|
||||
if (toDrain <= 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return totalDrained;
|
||||
}
|
||||
|
||||
private int indexOfFluid(FluidVolume resource) {
|
||||
for (int slot = 0; slot < fluids.length; slot++) {
|
||||
if (fluids[slot] != null && fluids[slot].getFluidStack().isFluidEqual(resource)) {
|
||||
return slot;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
private int indexOfEmptySlot() {
|
||||
for (int slot = 0; slot < fluids.length; slot++) {
|
||||
if (fluids[slot] == null) {
|
||||
return slot;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public FluidVolume drain(final int slot, final FluidVolume resource, final boolean doDrain) {
|
||||
final IAEFluidStack fluid = this.fluids[slot];
|
||||
if (resource.isEmpty() || fluid == null || !fluid.getFluidStack().equals(resource)) {
|
||||
return null;
|
||||
}
|
||||
return this.drain(slot, resource.getAmount(), doDrain);
|
||||
}
|
||||
|
||||
public FluidVolume drain(final int slot, final int maxDrain, boolean doDrain) {
|
||||
final IAEFluidStack fluid = this.fluids[slot];
|
||||
if (fluid == null || maxDrain <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
int drained = maxDrain;
|
||||
if (fluid.getStackSize() < drained) {
|
||||
drained = (int) fluid.getStackSize();
|
||||
}
|
||||
|
||||
FluidVolume stack = new FluidVolume(fluid.getFluid(), drained);
|
||||
if (doDrain) {
|
||||
fluid.setStackSize(fluid.getStackSize() - drained);
|
||||
if (fluid.getStackSize() <= 0) {
|
||||
this.fluids[slot] = null;
|
||||
}
|
||||
this.onContentChanged(slot);
|
||||
}
|
||||
return stack;
|
||||
}
|
||||
|
||||
public void writeToNBT(final CompoundTag data, final String name) {
|
||||
final CompoundTag c = new CompoundTag();
|
||||
this.writeToNBT(c);
|
||||
data.put(name, c);
|
||||
}
|
||||
|
||||
private void writeToNBT(final CompoundTag target) {
|
||||
for (int x = 0; x < this.fluids.length; x++) {
|
||||
try {
|
||||
final CompoundTag c = new CompoundTag();
|
||||
|
||||
if (this.fluids[x] != null) {
|
||||
this.fluids[x].writeToNBT(c);
|
||||
}
|
||||
|
||||
target.put("#" + x, c);
|
||||
} catch (final Exception ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void readFromNBT(final CompoundTag data, final String name) {
|
||||
final CompoundTag c = data.getCompound(name);
|
||||
if (!c.isEmpty()) {
|
||||
this.readFromNBT(c);
|
||||
}
|
||||
}
|
||||
|
||||
private void readFromNBT(final CompoundTag target) {
|
||||
for (int x = 0; x < this.fluids.length; x++) {
|
||||
try {
|
||||
final CompoundTag c = target.getCompound("#" + x);
|
||||
|
||||
if (!c.isEmpty()) {
|
||||
this.fluids[x] = AEFluidStack.fromNBT(c);
|
||||
}
|
||||
} catch (final Exception e) {
|
||||
AELog.debug(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,259 @@
|
||||
/*
|
||||
* This file is part of Applied Energistics 2.
|
||||
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
|
||||
*
|
||||
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Applied Energistics 2 is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
|
||||
*/
|
||||
|
||||
package appeng.fluids.util;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import alexiil.mc.lib.attributes.fluid.amount.FluidAmount;
|
||||
import alexiil.mc.lib.attributes.fluid.volume.FluidKey;
|
||||
import alexiil.mc.lib.attributes.fluid.volume.FluidKeys;
|
||||
import alexiil.mc.lib.attributes.fluid.volume.FluidVolume;
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
import net.fabricmc.fabric.api.util.NbtType;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.network.PacketByteBuf;
|
||||
|
||||
import appeng.api.AEApi;
|
||||
import appeng.api.config.FuzzyMode;
|
||||
import appeng.api.storage.IStorageChannel;
|
||||
import appeng.api.storage.channels.IFluidStorageChannel;
|
||||
import appeng.api.storage.data.IAEFluidStack;
|
||||
import appeng.fluids.items.FluidDummyItem;
|
||||
import appeng.util.Platform;
|
||||
import appeng.util.item.AEStack;
|
||||
|
||||
public final class AEFluidStack extends AEStack<IAEFluidStack> implements IAEFluidStack, Comparable<AEFluidStack> {
|
||||
private static final String NBT_STACKSIZE = "cnt";
|
||||
private static final String NBT_REQUESTABLE = "req";
|
||||
private static final String NBT_CRAFTABLE = "craft";
|
||||
private static final String NBT_FLUID_ID = "f";
|
||||
private static final String NBT_FLUID_TAG = "ft";
|
||||
|
||||
private final FluidKey fluid;
|
||||
private CompoundTag tagCompound;
|
||||
|
||||
private AEFluidStack(final AEFluidStack fluidStack) {
|
||||
this.fluid = fluidStack.fluid;
|
||||
this.setStackSize(fluidStack.getStackSize());
|
||||
|
||||
// priority = is.priority;
|
||||
this.setCraftable(fluidStack.isCraftable());
|
||||
this.setCountRequestable(fluidStack.getCountRequestable());
|
||||
|
||||
if (fluidStack.hasTagCompound()) {
|
||||
this.tagCompound = fluidStack.tagCompound.copy();
|
||||
}
|
||||
}
|
||||
|
||||
private AEFluidStack(@Nonnull FluidKey fluid, long amount, @Nullable CompoundTag tag) {
|
||||
this.fluid = Preconditions.checkNotNull(fluid);
|
||||
this.setStackSize(amount);
|
||||
this.setCraftable(false);
|
||||
this.setCountRequestable(0);
|
||||
this.tagCompound = tag;
|
||||
}
|
||||
|
||||
public static AEFluidStack fromFluidStack(final FluidVolume input) {
|
||||
if (input.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
FluidKey fluid = input.getFluidKey();
|
||||
if (fluid == null) {
|
||||
throw new IllegalArgumentException("Fluid is null.");
|
||||
}
|
||||
|
||||
CompoundTag tag = input.toTag();
|
||||
if (tag.isEmpty()) {
|
||||
tag = null;
|
||||
}
|
||||
|
||||
// FIXME FABRIC NOPE NO FRACTIONS YOU FREAKS THIS IS NOT FROG FRACTIONS
|
||||
long amount = (long)(input.amount().asInexactDouble() * 1000.0);
|
||||
|
||||
return new AEFluidStack(fluid, amount, tag);
|
||||
}
|
||||
|
||||
public static IAEFluidStack fromNBT(final CompoundTag data) {
|
||||
CompoundTag fluidId = data.getCompound(NBT_FLUID_ID);
|
||||
FluidKey fluid = FluidKey.fromTag(fluidId);
|
||||
if (fluid == FluidKeys.EMPTY) {
|
||||
return null;
|
||||
}
|
||||
|
||||
CompoundTag tag = null;
|
||||
if (data.contains(NBT_FLUID_TAG, NbtType.COMPOUND)) {
|
||||
tag = data.getCompound(NBT_FLUID_TAG);
|
||||
}
|
||||
|
||||
long amount = data.getLong(NBT_STACKSIZE);
|
||||
|
||||
AEFluidStack fluidStack = new AEFluidStack(fluid, amount, tag);
|
||||
fluidStack.setCountRequestable(data.getLong(NBT_REQUESTABLE));
|
||||
fluidStack.setCraftable(data.getBoolean(NBT_CRAFTABLE));
|
||||
return fluidStack;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(final IAEFluidStack option) {
|
||||
if (option == null) {
|
||||
return;
|
||||
}
|
||||
this.incStackSize(option.getStackSize());
|
||||
this.setCountRequestable(this.getCountRequestable() + option.getCountRequestable());
|
||||
this.setCraftable(this.isCraftable() || option.isCraftable());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToNBT(final CompoundTag data) {
|
||||
data.put(NBT_FLUID_ID, this.fluid.toTag());
|
||||
if (this.hasTagCompound()) {
|
||||
data.put(NBT_FLUID_TAG, this.tagCompound);
|
||||
}
|
||||
data.putLong(NBT_STACKSIZE, this.getStackSize());
|
||||
data.putLong(NBT_REQUESTABLE, this.getCountRequestable());
|
||||
data.putBoolean(NBT_CRAFTABLE, this.isCraftable());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean fuzzyComparison(final IAEFluidStack other, final FuzzyMode mode) {
|
||||
return this.fluid == other.getFluid();
|
||||
}
|
||||
|
||||
@Override
|
||||
public IAEFluidStack copy() {
|
||||
return new AEFluidStack(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IAEFluidStack empty() {
|
||||
final IAEFluidStack dup = this.copy();
|
||||
dup.reset();
|
||||
return dup;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isItem() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFluid() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IStorageChannel<IAEFluidStack> getChannel() {
|
||||
return AEApi.instance().storage().getStorageChannel(IFluidStorageChannel.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(final AEFluidStack other) {
|
||||
if (this.fluid != other.fluid) {
|
||||
return this.fluid.entry.getId().compareTo(other.fluid.entry.getId());
|
||||
}
|
||||
|
||||
if (Platform.itemComparisons().isNbtTagEqual(this.tagCompound, other.tagCompound)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return this.tagCompound.hashCode() - other.tagCompound.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((this.fluid == null) ? 0 : this.fluid.hashCode());
|
||||
result = prime * result + ((this.tagCompound == null) ? 0 : this.tagCompound.hashCode());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object other) {
|
||||
if (other instanceof AEFluidStack) {
|
||||
final AEFluidStack is = (AEFluidStack) other;
|
||||
return is.fluid == this.fluid && Platform.itemComparisons().isNbtTagEqual(this.tagCompound, is.tagCompound);
|
||||
} else if (other instanceof FluidVolume) {
|
||||
final FluidVolume is = (FluidVolume) other;
|
||||
return is.getFluidKey() == this.fluid
|
||||
&& Platform.itemComparisons().isNbtTagEqual(this.tagCompound, is.toTag());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.getStackSize() + "x" + this.getFluidStack().getFluidKey().entry.getId() + " " + this.tagCompound;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasTagCompound() {
|
||||
return this.tagCompound != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FluidVolume getFluidStack() {
|
||||
FluidAmount amount = FluidAmount.of(this.getStackSize(), 1000);
|
||||
return this.fluid.readVolume(tagCompound).withAmount(amount);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FluidKey getFluid() {
|
||||
return this.fluid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack asItemStackRepresentation() {
|
||||
ItemStack is = AEApi.instance().definitions().items().dummyFluidItem().maybeStack(1).orElse(ItemStack.EMPTY);
|
||||
if (!is.isEmpty()) {
|
||||
FluidDummyItem item = (FluidDummyItem) is.getItem();
|
||||
item.setFluidStack(is, this.getFluidStack());
|
||||
return is;
|
||||
}
|
||||
return ItemStack.EMPTY;
|
||||
}
|
||||
|
||||
public static IAEFluidStack fromPacket(final PacketByteBuf buffer) {
|
||||
final boolean isCraftable = buffer.readBoolean();
|
||||
|
||||
FluidKey fluid = FluidKey.fromTag(buffer.readCompoundTag());
|
||||
CompoundTag compoundTag = buffer.readCompoundTag();
|
||||
final long amount = buffer.readVarLong();
|
||||
final long countRequestable = buffer.readVarLong();
|
||||
|
||||
final AEFluidStack fluidStack = new AEFluidStack(fluid, amount, compoundTag);
|
||||
fluidStack.setCountRequestable(countRequestable);
|
||||
fluidStack.setCraftable(isCraftable);
|
||||
return fluidStack;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToPacket(final PacketByteBuf buffer) {
|
||||
buffer.writeBoolean(this.isCraftable());
|
||||
buffer.writeCompoundTag(fluid.toTag());
|
||||
buffer.writeCompoundTag(tagCompound);
|
||||
buffer.writeVarLong(this.getStackSize());
|
||||
buffer.writeVarLong(this.getCountRequestable());
|
||||
}
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
/*
|
||||
* This file is part of Applied Energistics 2.
|
||||
* Copyright (c) 2013 - 2018, AlgorithmX2, All rights reserved.
|
||||
*
|
||||
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Applied Energistics 2 is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
|
||||
*/
|
||||
|
||||
package appeng.fluids.util;
|
||||
|
||||
import net.minecraftforge.fluids.capability.templates.FluidTank;
|
||||
|
||||
import appeng.api.storage.data.IAEFluidStack;
|
||||
import appeng.util.Platform;
|
||||
|
||||
public class AEFluidTank extends FluidTank implements IAEFluidTank {
|
||||
private final IAEFluidInventory host;
|
||||
|
||||
public AEFluidTank(IAEFluidInventory host, int capacity) {
|
||||
super(capacity);
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onContentsChanged() {
|
||||
if (this.host != null && Platform.isServer()) {
|
||||
this.host.onFluidInventoryChanged(this, 0);
|
||||
}
|
||||
super.onContentsChanged();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setFluidInSlot(int slot, IAEFluidStack fluid) {
|
||||
if (slot == 0) {
|
||||
this.setFluid(fluid == null ? null : fluid.getFluidStack());
|
||||
this.onContentsChanged();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IAEFluidStack getFluidInSlot(int slot) {
|
||||
if (slot == 0) {
|
||||
return AEFluidStack.fromFluidStack(this.getFluid());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSlots() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
/*
|
||||
* This file is part of Applied Energistics 2.
|
||||
* Copyright (c) 2013 - 2015, AlgorithmX2, All rights reserved.
|
||||
*
|
||||
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Applied Energistics 2 is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
|
||||
*/
|
||||
|
||||
package appeng.fluids.util;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import appeng.api.config.FuzzyMode;
|
||||
import appeng.api.storage.data.IAEFluidStack;
|
||||
import appeng.api.storage.data.IItemList;
|
||||
|
||||
public final class FluidList implements IItemList<IAEFluidStack> {
|
||||
|
||||
private final Map<IAEFluidStack, IAEFluidStack> records = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public void add(final IAEFluidStack option) {
|
||||
if (option == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final IAEFluidStack st = this.getFluidRecord(option);
|
||||
|
||||
if (st != null) {
|
||||
st.add(option);
|
||||
return;
|
||||
}
|
||||
|
||||
final IAEFluidStack opt = option.copy();
|
||||
|
||||
this.putFluidRecord(opt);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IAEFluidStack findPrecise(final IAEFluidStack fluidStack) {
|
||||
if (fluidStack == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return this.getFluidRecord(fluidStack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<IAEFluidStack> findFuzzy(final IAEFluidStack filter, final FuzzyMode fuzzy) {
|
||||
if (filter == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
return Collections.singletonList(this.findPrecise(filter));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return !this.iterator().hasNext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addStorage(final IAEFluidStack option) {
|
||||
if (option == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final IAEFluidStack st = this.getFluidRecord(option);
|
||||
|
||||
if (st != null) {
|
||||
st.incStackSize(option.getStackSize());
|
||||
return;
|
||||
}
|
||||
|
||||
final IAEFluidStack opt = option.copy();
|
||||
|
||||
this.putFluidRecord(opt);
|
||||
}
|
||||
|
||||
/*
|
||||
* public synchronized void clean() { Iterator<StackType> i = iterator(); while
|
||||
* (i.hasNext()) { StackType AEI = i.next(); if ( !AEI.isMeaningful() )
|
||||
* i.remove(); } }
|
||||
*/
|
||||
|
||||
@Override
|
||||
public void addCrafting(final IAEFluidStack option) {
|
||||
if (option == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final IAEFluidStack st = this.getFluidRecord(option);
|
||||
|
||||
if (st != null) {
|
||||
st.setCraftable(true);
|
||||
return;
|
||||
}
|
||||
|
||||
final IAEFluidStack opt = option.copy();
|
||||
opt.setStackSize(0);
|
||||
opt.setCraftable(true);
|
||||
|
||||
this.putFluidRecord(opt);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addRequestable(final IAEFluidStack option) {
|
||||
if (option == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final IAEFluidStack st = this.getFluidRecord(option);
|
||||
|
||||
if (st != null) {
|
||||
st.setCountRequestable(st.getCountRequestable() + option.getCountRequestable());
|
||||
return;
|
||||
}
|
||||
|
||||
final IAEFluidStack opt = option.copy();
|
||||
opt.setStackSize(0);
|
||||
opt.setCraftable(false);
|
||||
opt.setCountRequestable(option.getCountRequestable());
|
||||
|
||||
this.putFluidRecord(opt);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IAEFluidStack getFirstItem() {
|
||||
for (final IAEFluidStack stackType : this) {
|
||||
return stackType;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return this.records.values().size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<IAEFluidStack> iterator() {
|
||||
return new MeaningfulFluidIterator<>(this.records.values().iterator());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resetStatus() {
|
||||
for (final IAEFluidStack i : this) {
|
||||
i.reset();
|
||||
}
|
||||
}
|
||||
|
||||
private IAEFluidStack getFluidRecord(final IAEFluidStack fluid) {
|
||||
return this.records.get(fluid);
|
||||
}
|
||||
|
||||
private IAEFluidStack putFluidRecord(final IAEFluidStack fluid) {
|
||||
return this.records.put(fluid, fluid);
|
||||
}
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
/*
|
||||
* This file is part of Applied Energistics 2.
|
||||
* Copyright (c) 2013 - 2018, AlgorithmX2, All rights reserved.
|
||||
*
|
||||
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Applied Energistics 2 is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
|
||||
*/
|
||||
|
||||
package appeng.fluids.util;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
import appeng.api.config.SortDir;
|
||||
import appeng.api.storage.data.IAEFluidStack;
|
||||
import appeng.util.Platform;
|
||||
|
||||
/**
|
||||
* @author BrockWS
|
||||
* @version rv6 - 22/05/2018
|
||||
* @since rv6 22/05/2018
|
||||
*/
|
||||
public class FluidSorters {
|
||||
private static SortDir Direction = SortDir.ASCENDING;
|
||||
|
||||
public static final Comparator<IAEFluidStack> CONFIG_BASED_SORT_BY_NAME = (o1, o2) -> {
|
||||
// FIXME: Calling .getString() to compare two untranslated strings is a problem,
|
||||
// we need to investigate how to do this better
|
||||
if (getDirection() == SortDir.ASCENDING) {
|
||||
return Platform.getFluidDisplayName(o1).getString()
|
||||
.compareToIgnoreCase(Platform.getFluidDisplayName(o2).getString());
|
||||
}
|
||||
return Platform.getFluidDisplayName(o2).getString()
|
||||
.compareToIgnoreCase(Platform.getFluidDisplayName(o1).getString());
|
||||
};
|
||||
|
||||
public static final Comparator<IAEFluidStack> CONFIG_BASED_SORT_BY_MOD = new Comparator<IAEFluidStack>() {
|
||||
|
||||
@Override
|
||||
public int compare(final IAEFluidStack o1, final IAEFluidStack o2) {
|
||||
final AEFluidStack op1 = (AEFluidStack) o1;
|
||||
final AEFluidStack op2 = (AEFluidStack) o2;
|
||||
|
||||
if (getDirection() == SortDir.ASCENDING) {
|
||||
return this.secondarySort(Platform.getModId(op1).compareToIgnoreCase(Platform.getModId(op2)), o2, o1);
|
||||
}
|
||||
return this.secondarySort(Platform.getModId(op2).compareToIgnoreCase(Platform.getModId(op1)), o1, o2);
|
||||
}
|
||||
|
||||
private int secondarySort(final int compareToIgnoreCase, final IAEFluidStack o1, final IAEFluidStack o2) {
|
||||
if (compareToIgnoreCase == 0) {
|
||||
// FIXME: Calling .getString() to compare two untranslated strings is a problem,
|
||||
// we need to investigate how to do this better
|
||||
return Platform.getFluidDisplayName(o2).getString()
|
||||
.compareToIgnoreCase(Platform.getFluidDisplayName(o1).getString());
|
||||
}
|
||||
|
||||
return compareToIgnoreCase;
|
||||
}
|
||||
};
|
||||
|
||||
public static final Comparator<IAEFluidStack> CONFIG_BASED_SORT_BY_SIZE = (o1, o2) -> {
|
||||
if (getDirection() == SortDir.ASCENDING) {
|
||||
return Long.compare(o2.getStackSize(), o1.getStackSize());
|
||||
}
|
||||
return Long.compare(o1.getStackSize(), o2.getStackSize());
|
||||
};
|
||||
|
||||
private static SortDir getDirection() {
|
||||
return Direction;
|
||||
}
|
||||
|
||||
public static void setDirection(final SortDir direction) {
|
||||
Direction = direction;
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
/*
|
||||
* This file is part of Applied Energistics 2.
|
||||
* Copyright (c) 2013 - 2018, AlgorithmX2, All rights reserved.
|
||||
*
|
||||
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Applied Energistics 2 is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
|
||||
*/
|
||||
|
||||
package appeng.fluids.util;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface IAEFluidInventory {
|
||||
void onFluidInventoryChanged(final IAEFluidTank inv, final int slot);
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
|
||||
package appeng.fluids.util;
|
||||
|
||||
import net.minecraftforge.fluids.capability.IFluidHandler;
|
||||
|
||||
import appeng.api.storage.data.IAEFluidStack;
|
||||
|
||||
public interface IAEFluidTank extends IFluidHandler {
|
||||
void setFluidInSlot(final int slot, final IAEFluidStack fluid);
|
||||
|
||||
IAEFluidStack getFluidInSlot(final int slot);
|
||||
|
||||
int getSlots();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* This file is part of Applied Energistics 2.
|
||||
* Copyright (c) 2013 - 2015, AlgorithmX2, All rights reserved.
|
||||
*
|
||||
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Applied Energistics 2 is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
|
||||
*/
|
||||
|
||||
package appeng.fluids.util;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import appeng.api.storage.data.IAEStack;
|
||||
|
||||
public class MeaningfulFluidIterator<T extends IAEStack> implements Iterator<T> {
|
||||
|
||||
private final Iterator<T> parent;
|
||||
private T next;
|
||||
|
||||
public MeaningfulFluidIterator(final Iterator<T> iterator) {
|
||||
this.parent = iterator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
while (this.parent.hasNext()) {
|
||||
this.next = this.parent.next();
|
||||
if (this.next.isMeaningful()) {
|
||||
return true;
|
||||
} else {
|
||||
this.parent.remove(); // self cleaning :3
|
||||
}
|
||||
}
|
||||
|
||||
this.next = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T next() {
|
||||
if (this.next == null) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
|
||||
return this.next;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
this.parent.remove();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user