|
|
|
|
@@ -2,9 +2,12 @@ package refinedstorage.tile;
|
|
|
|
|
|
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
|
import net.minecraft.network.datasync.DataSerializers;
|
|
|
|
|
import net.minecraft.util.EnumFacing;
|
|
|
|
|
import net.minecraftforge.common.capabilities.Capability;
|
|
|
|
|
import net.minecraftforge.fluids.FluidStack;
|
|
|
|
|
import net.minecraftforge.fml.common.FMLCommonHandler;
|
|
|
|
|
import net.minecraftforge.fml.relauncher.Side;
|
|
|
|
|
import net.minecraftforge.items.CapabilityItemHandler;
|
|
|
|
|
import net.minecraftforge.items.IItemHandler;
|
|
|
|
|
import net.minecraftforge.items.ItemHandlerHelper;
|
|
|
|
|
@@ -20,12 +23,27 @@ import refinedstorage.inventory.ItemValidatorBasic;
|
|
|
|
|
import refinedstorage.tile.config.IComparable;
|
|
|
|
|
import refinedstorage.tile.config.IFilterable;
|
|
|
|
|
import refinedstorage.tile.config.IType;
|
|
|
|
|
import refinedstorage.tile.data.ITileDataConsumer;
|
|
|
|
|
import refinedstorage.tile.data.ITileDataProducer;
|
|
|
|
|
import refinedstorage.tile.data.TileDataParameter;
|
|
|
|
|
|
|
|
|
|
public class TileDiskManipulator extends TileNode implements IComparable, IFilterable, IType {
|
|
|
|
|
public static final TileDataParameter<Integer> COMPARE = IComparable.createParameter();
|
|
|
|
|
public static final TileDataParameter<Integer> MODE = IFilterable.createParameter();
|
|
|
|
|
public static final TileDataParameter<Integer> TYPE = IType.createParameter();
|
|
|
|
|
public static final TileDataParameter<Integer> IOMODE = new TileDataParameter<>(DataSerializers.VARINT, 1, new ITileDataProducer<Integer, TileDiskManipulator>() {
|
|
|
|
|
@Override
|
|
|
|
|
public Integer getValue(TileDiskManipulator tile) {
|
|
|
|
|
return tile.ioMode;
|
|
|
|
|
}
|
|
|
|
|
}, new ITileDataConsumer<Integer, TileDiskManipulator>() {
|
|
|
|
|
@Override
|
|
|
|
|
public void setValue(TileDiskManipulator tile, Integer value) {
|
|
|
|
|
tile.ioMode = value;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
public static final int INSERT = 0, EXTRACT = 1;
|
|
|
|
|
|
|
|
|
|
private static final String NBT_COMPARE = "Compare";
|
|
|
|
|
private static final String NBT_MODE = "Mode";
|
|
|
|
|
@@ -34,11 +52,18 @@ public class TileDiskManipulator extends TileNode implements IComparable, IFilte
|
|
|
|
|
private int compare = 0;
|
|
|
|
|
private int mode = IFilterable.WHITELIST;
|
|
|
|
|
private int type = IType.ITEMS;
|
|
|
|
|
private int ioMode = INSERT;
|
|
|
|
|
|
|
|
|
|
private ItemStorage[] itemStorages;
|
|
|
|
|
private FluidStorage[] fluidStorages;
|
|
|
|
|
|
|
|
|
|
public TileDiskManipulator() {
|
|
|
|
|
dataManager.addWatchedParameter(COMPARE);
|
|
|
|
|
dataManager.addWatchedParameter(MODE);
|
|
|
|
|
dataManager.addWatchedParameter(TYPE);
|
|
|
|
|
|
|
|
|
|
itemStorages = new ItemStorage[6];
|
|
|
|
|
fluidStorages = new FluidStorage[6];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ItemHandlerBasic disks = new ItemHandlerBasic(12, this, new ItemValidatorBasic(RefinedStorageItems.STORAGE_DISK) {
|
|
|
|
|
@@ -51,7 +76,41 @@ public class TileDiskManipulator extends TileNode implements IComparable, IFilte
|
|
|
|
|
public boolean isValid(ItemStack disk) {
|
|
|
|
|
return super.isValid(disk) && FluidStorageNBT.isValid(disk);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}) {
|
|
|
|
|
@Override
|
|
|
|
|
protected void onContentsChanged(int slot) {
|
|
|
|
|
super.onContentsChanged(slot);
|
|
|
|
|
|
|
|
|
|
if (FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER && slot < 6) {
|
|
|
|
|
ItemStack disk = getStackInSlot(slot);
|
|
|
|
|
|
|
|
|
|
if (disk == null) {
|
|
|
|
|
itemStorages[slot] = null;
|
|
|
|
|
fluidStorages[slot] = null;
|
|
|
|
|
} else {
|
|
|
|
|
if (disk.getItem() == RefinedStorageItems.STORAGE_DISK) {
|
|
|
|
|
itemStorages[slot] = new ItemStorage(disk);
|
|
|
|
|
} else if (disk.getItem() == RefinedStorageItems.FLUID_STORAGE_DISK) {
|
|
|
|
|
fluidStorages[slot] = new FluidStorage(disk);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public ItemStack extractItem(int slot, int amount, boolean simulate) {
|
|
|
|
|
if (slot < 6) {
|
|
|
|
|
if (itemStorages[slot] != null) {
|
|
|
|
|
itemStorages[slot].writeToNBT();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (fluidStorages[slot] != null) {
|
|
|
|
|
fluidStorages[slot].writeToNBT();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return super.extractItem(slot, amount, simulate);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public class ItemStorage extends ItemStorageNBT {
|
|
|
|
|
public ItemStorage(ItemStack disk) {
|
|
|
|
|
@@ -71,6 +130,15 @@ public class TileDiskManipulator extends TileNode implements IComparable, IFilte
|
|
|
|
|
|
|
|
|
|
return super.insertItem(stack, size, simulate);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public ItemStack extractItem(ItemStack stack, int size, int flags) {
|
|
|
|
|
if (!IFilterable.canTake(itemFilters, mode, getCompare(), stack)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return super.extractItem(stack, size, flags);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class FluidStorage extends FluidStorageNBT {
|
|
|
|
|
@@ -91,6 +159,15 @@ public class TileDiskManipulator extends TileNode implements IComparable, IFilte
|
|
|
|
|
|
|
|
|
|
return super.insertFluid(stack, size, simulate);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public FluidStack extractFluid(FluidStack stack, int size, int flags) {
|
|
|
|
|
if (!IFilterable.canTakeFluids(fluidFilters, mode, getCompare(), stack)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return super.extractFluid(stack, size, flags);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ItemHandlerBasic itemFilters = new ItemHandlerBasic(9, this);
|
|
|
|
|
@@ -103,17 +180,96 @@ public class TileDiskManipulator extends TileNode implements IComparable, IFilte
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void updateNode() {
|
|
|
|
|
int i = 0;
|
|
|
|
|
ItemStack disk = disks.getStackInSlot(i);
|
|
|
|
|
while (disk == null && i < 6) i++;
|
|
|
|
|
if (disk == null) return;
|
|
|
|
|
int slot = 0;
|
|
|
|
|
if (type == IType.ITEMS) {
|
|
|
|
|
while (slot < itemStorages.length && itemStorages[slot] == null) slot++;
|
|
|
|
|
if (slot == itemStorages.length) return;
|
|
|
|
|
ItemStorage storage = itemStorages[slot];
|
|
|
|
|
if (ioMode == INSERT) {
|
|
|
|
|
insertIntoNetwork(storage, slot);
|
|
|
|
|
} else if (ioMode == EXTRACT) {
|
|
|
|
|
extractFromNetwork(storage, slot);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (disk.getItem() == RefinedStorageItems.STORAGE_DISK) {
|
|
|
|
|
ItemStorage storage = new ItemStorage(disk);
|
|
|
|
|
} else if (disk.getItem() == RefinedStorageItems.FLUID_STORAGE_DISK) {
|
|
|
|
|
FluidStorage storage = new FluidStorage(disk);
|
|
|
|
|
} else if (type == IType.FLUIDS) {
|
|
|
|
|
while (slot < fluidStorages.length && fluidStorages[slot] == null) slot++;
|
|
|
|
|
if (slot == fluidStorages.length) return;
|
|
|
|
|
FluidStorage storage = fluidStorages[slot];
|
|
|
|
|
if (ioMode == INSERT) {
|
|
|
|
|
insertIntoNetwork(storage, slot);
|
|
|
|
|
} else if (ioMode == EXTRACT) {
|
|
|
|
|
extractFromNetwork(storage, slot);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void insertIntoNetwork(ItemStorage storage, int slot) {
|
|
|
|
|
if (storage.getStored() == 0) {
|
|
|
|
|
moveDriveToOutput(slot);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
ItemStack extracted = null;
|
|
|
|
|
int ii = 0;
|
|
|
|
|
do {
|
|
|
|
|
ItemStack stack = null;
|
|
|
|
|
while (storage.getItems().size() > ii && stack == null)
|
|
|
|
|
stack = storage.getItems().get(ii++);
|
|
|
|
|
if (stack != null) extracted = storage.extractItem(stack, 1, compare);
|
|
|
|
|
} while (storage.getItems().size() > ii && extracted == null);
|
|
|
|
|
if (extracted == null) {
|
|
|
|
|
moveDriveToOutput(slot);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
ItemStack leftOver = network.insertItem(extracted, extracted.stackSize, false);
|
|
|
|
|
if (leftOver != null) storage.insertItem(leftOver, leftOver.stackSize, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void extractFromNetwork(ItemStorage storage, int slot) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void insertIntoNetwork(FluidStorage storage, int slot) {
|
|
|
|
|
if (storage.getStored() == 0) {
|
|
|
|
|
moveDriveToOutput(slot);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
FluidStack extracted = null;
|
|
|
|
|
int ii = 0;
|
|
|
|
|
do {
|
|
|
|
|
FluidStack stack = storage.getStacks().get(ii);
|
|
|
|
|
while (stack == null && storage.getStacks().size() > ii) ii++;
|
|
|
|
|
if (stack != null) extracted = storage.extractFluid(stack, 1, compare);
|
|
|
|
|
} while (extracted == null && storage.getStacks().size() > ii);
|
|
|
|
|
if (extracted == null) {
|
|
|
|
|
moveDriveToOutput(slot);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
FluidStack leftOver = network.insertFluid(extracted, extracted.amount, false);
|
|
|
|
|
if (leftOver != null) storage.insertFluid(leftOver, leftOver.amount, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void extractFromNetwork(FluidStorage storage, int slot) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void moveDriveToOutput(int slot) {
|
|
|
|
|
ItemStack disk = disks.getStackInSlot(slot);
|
|
|
|
|
if (disk != null) {
|
|
|
|
|
int i = 6;
|
|
|
|
|
while (disks.getStackInSlot(i) != null && i < 12) i++;
|
|
|
|
|
if (i == 12) return;
|
|
|
|
|
if (slot < 6) {
|
|
|
|
|
if (itemStorages[slot] != null) {
|
|
|
|
|
itemStorages[slot].writeToNBT();
|
|
|
|
|
itemStorages[slot] = null;
|
|
|
|
|
}
|
|
|
|
|
if (fluidStorages[slot] != null) {
|
|
|
|
|
fluidStorages[slot].writeToNBT();
|
|
|
|
|
fluidStorages[slot] = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
disks.extractItem(slot, 1, false);
|
|
|
|
|
disks.insertItem(i, disk, false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@@ -209,4 +365,13 @@ public class TileDiskManipulator extends TileNode implements IComparable, IFilte
|
|
|
|
|
public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
|
|
|
|
|
return capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY || super.hasCapability(capability, facing);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onBreak() {
|
|
|
|
|
for (ItemStorage storage : itemStorages)
|
|
|
|
|
if (storage != null)
|
|
|
|
|
storage.writeToNBT();
|
|
|
|
|
for (FluidStorage storage : fluidStorages)
|
|
|
|
|
if (storage != null)
|
|
|
|
|
storage.writeToNBT();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|