Also expose a capability on the writers
This commit is contained in:
@@ -1,12 +1,13 @@
|
|||||||
package com.raoulvdberge.refinedstorage.api.network.readerwriter;
|
package com.raoulvdberge.refinedstorage.api.network.readerwriter;
|
||||||
|
|
||||||
|
import com.raoulvdberge.refinedstorage.tile.IReaderWriter;
|
||||||
import net.minecraft.nbt.NBTTagCompound;
|
import net.minecraft.nbt.NBTTagCompound;
|
||||||
import net.minecraftforge.common.capabilities.ICapabilityProvider;
|
import net.minecraftforge.common.capabilities.Capability;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a reader writer handler. Can be for example: items, fluids, energy, ...
|
* Represents a reader writer handler. Can be for example: items, fluids, energy, ...
|
||||||
*/
|
*/
|
||||||
public interface IReaderWriterHandler extends ICapabilityProvider {
|
public interface IReaderWriterHandler {
|
||||||
/**
|
/**
|
||||||
* Updates this reader writer handler.
|
* Updates this reader writer handler.
|
||||||
*
|
*
|
||||||
@@ -21,6 +22,20 @@ public interface IReaderWriterHandler extends ICapabilityProvider {
|
|||||||
*/
|
*/
|
||||||
void onWriterDisabled(IWriter writer);
|
void onWriterDisabled(IWriter writer);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param readerWriter the reader writer
|
||||||
|
* @param capability the capability
|
||||||
|
* @return whether we have the given capability for the reader writer
|
||||||
|
*/
|
||||||
|
boolean hasCapability(IReaderWriter readerWriter, Capability<?> capability);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param readerWriter the reader writer
|
||||||
|
* @param capability the capability
|
||||||
|
* @return the capability for the given reader writer
|
||||||
|
*/
|
||||||
|
<T> T getCapability(IReaderWriter readerWriter, Capability<T> capability);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes this reader writer handler to NBT.
|
* Writes this reader writer handler to NBT.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -1,16 +1,16 @@
|
|||||||
package com.raoulvdberge.refinedstorage.apiimpl.network.readerwriter;
|
package com.raoulvdberge.refinedstorage.apiimpl.network.readerwriter;
|
||||||
|
|
||||||
import com.raoulvdberge.refinedstorage.RSUtils;
|
import com.raoulvdberge.refinedstorage.RSUtils;
|
||||||
|
import com.raoulvdberge.refinedstorage.api.network.readerwriter.IReader;
|
||||||
import com.raoulvdberge.refinedstorage.api.network.readerwriter.IReaderWriterChannel;
|
import com.raoulvdberge.refinedstorage.api.network.readerwriter.IReaderWriterChannel;
|
||||||
import com.raoulvdberge.refinedstorage.api.network.readerwriter.IReaderWriterHandler;
|
import com.raoulvdberge.refinedstorage.api.network.readerwriter.IReaderWriterHandler;
|
||||||
import com.raoulvdberge.refinedstorage.api.network.readerwriter.IWriter;
|
import com.raoulvdberge.refinedstorage.api.network.readerwriter.IWriter;
|
||||||
|
import com.raoulvdberge.refinedstorage.tile.IReaderWriter;
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
import net.minecraft.nbt.NBTTagCompound;
|
import net.minecraft.nbt.NBTTagCompound;
|
||||||
import net.minecraft.util.EnumFacing;
|
|
||||||
import net.minecraftforge.common.capabilities.Capability;
|
import net.minecraftforge.common.capabilities.Capability;
|
||||||
import net.minecraftforge.items.CapabilityItemHandler;
|
import net.minecraftforge.items.CapabilityItemHandler;
|
||||||
import net.minecraftforge.items.IItemHandler;
|
import net.minecraftforge.items.IItemHandler;
|
||||||
import net.minecraftforge.items.ItemHandlerHelper;
|
|
||||||
import net.minecraftforge.items.ItemStackHandler;
|
import net.minecraftforge.items.ItemStackHandler;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
@@ -18,45 +18,22 @@ import javax.annotation.Nullable;
|
|||||||
public class ReaderWriterHandlerItems implements IReaderWriterHandler {
|
public class ReaderWriterHandlerItems implements IReaderWriterHandler {
|
||||||
public static final String ID = "items";
|
public static final String ID = "items";
|
||||||
|
|
||||||
private ItemStackHandler internalInv;
|
private ItemStackHandler items;
|
||||||
|
private ItemHandlerReaderWriter itemsReader, itemsWriter;
|
||||||
|
|
||||||
public ReaderWriterHandlerItems(@Nullable NBTTagCompound tag) {
|
public ReaderWriterHandlerItems(@Nullable NBTTagCompound tag) {
|
||||||
this.internalInv = new ItemHandlerReaderWriter(4);
|
this.items = new ItemStackHandler(4);
|
||||||
|
this.itemsWriter = new ItemHandlerReaderWriter(items, false, true);
|
||||||
|
this.itemsReader = new ItemHandlerReaderWriter(items, true, false);
|
||||||
|
|
||||||
if (tag != null) {
|
if (tag != null) {
|
||||||
RSUtils.readItems(internalInv, 0, tag);
|
RSUtils.readItems(items, 0, tag);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update(IReaderWriterChannel channel) {
|
public void update(IReaderWriterChannel channel) {
|
||||||
for (IWriter writer : channel.getWriters()) {
|
// NO OP
|
||||||
IItemHandler handler = RSUtils.getItemHandler(writer.getNodeWorld().getTileEntity(writer.getPosition().offset(writer.getDirection())), writer.getDirection().getOpposite());
|
|
||||||
|
|
||||||
if (handler == null) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < internalInv.getSlots(); ++i) {
|
|
||||||
ItemStack slot = internalInv.getStackInSlot(i);
|
|
||||||
|
|
||||||
if (slot == null) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
ItemStack toInsert = ItemHandlerHelper.copyStackWithSize(slot, 1);
|
|
||||||
|
|
||||||
if (ItemHandlerHelper.insertItem(handler, toInsert, true) == null) {
|
|
||||||
ItemHandlerHelper.insertItem(handler, toInsert, false);
|
|
||||||
|
|
||||||
internalInv.getStackInSlot(i).stackSize -= toInsert.stackSize;
|
|
||||||
|
|
||||||
if (internalInv.getStackInSlot(i).stackSize <= 0) {
|
|
||||||
internalInv.setStackInSlot(i, null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -64,9 +41,27 @@ public class ReaderWriterHandlerItems implements IReaderWriterHandler {
|
|||||||
// NO OP
|
// NO OP
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasCapability(IReaderWriter readerWriter, Capability<?> capability) {
|
||||||
|
return capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY && (readerWriter instanceof IReader || readerWriter instanceof IWriter);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> T getCapability(IReaderWriter readerWriter, Capability<T> capability) {
|
||||||
|
if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
|
||||||
|
if (readerWriter instanceof IReader) {
|
||||||
|
return (T) itemsReader;
|
||||||
|
} else if (readerWriter instanceof IWriter) {
|
||||||
|
return (T) itemsWriter;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NBTTagCompound writeToNBT(NBTTagCompound tag) {
|
public NBTTagCompound writeToNBT(NBTTagCompound tag) {
|
||||||
RSUtils.writeItems(internalInv, 0, tag);
|
RSUtils.writeItems(items, 0, tag);
|
||||||
|
|
||||||
return tag;
|
return tag;
|
||||||
}
|
}
|
||||||
@@ -76,24 +71,34 @@ public class ReaderWriterHandlerItems implements IReaderWriterHandler {
|
|||||||
return ID;
|
return ID;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private class ItemHandlerReaderWriter implements IItemHandler {
|
||||||
public boolean hasCapability(Capability<?> capability, @Nullable EnumFacing facing) {
|
private IItemHandler parent;
|
||||||
return capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY;
|
private boolean canInsert, canExtract;
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
public ItemHandlerReaderWriter(IItemHandler parent, boolean canInsert, boolean canExtract) {
|
||||||
public <T> T getCapability(Capability<T> capability, @Nullable EnumFacing facing) {
|
this.parent = parent;
|
||||||
return (T) internalInv;
|
this.canInsert = canInsert;
|
||||||
}
|
this.canExtract = canExtract;
|
||||||
|
}
|
||||||
|
|
||||||
private class ItemHandlerReaderWriter extends ItemStackHandler {
|
@Override
|
||||||
private ItemHandlerReaderWriter(int size) {
|
public int getSlots() {
|
||||||
super(size);
|
return parent.getSlots();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ItemStack getStackInSlot(int slot) {
|
||||||
|
return parent.getStackInSlot(slot);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) {
|
||||||
|
return canInsert ? parent.insertItem(slot, stack, simulate) : stack;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ItemStack extractItem(int slot, int amount, boolean simulate) {
|
public ItemStack extractItem(int slot, int amount, boolean simulate) {
|
||||||
return null;
|
return canExtract ? parent.extractItem(slot, amount, simulate) : null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,12 +4,10 @@ import com.raoulvdberge.refinedstorage.api.network.readerwriter.IReader;
|
|||||||
import com.raoulvdberge.refinedstorage.api.network.readerwriter.IReaderWriterChannel;
|
import com.raoulvdberge.refinedstorage.api.network.readerwriter.IReaderWriterChannel;
|
||||||
import com.raoulvdberge.refinedstorage.api.network.readerwriter.IReaderWriterHandler;
|
import com.raoulvdberge.refinedstorage.api.network.readerwriter.IReaderWriterHandler;
|
||||||
import com.raoulvdberge.refinedstorage.api.network.readerwriter.IWriter;
|
import com.raoulvdberge.refinedstorage.api.network.readerwriter.IWriter;
|
||||||
|
import com.raoulvdberge.refinedstorage.tile.IReaderWriter;
|
||||||
import net.minecraft.nbt.NBTTagCompound;
|
import net.minecraft.nbt.NBTTagCompound;
|
||||||
import net.minecraft.util.EnumFacing;
|
|
||||||
import net.minecraftforge.common.capabilities.Capability;
|
import net.minecraftforge.common.capabilities.Capability;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
|
||||||
|
|
||||||
public class ReaderWriterHandlerRedstone implements IReaderWriterHandler {
|
public class ReaderWriterHandlerRedstone implements IReaderWriterHandler {
|
||||||
public static final String ID = "redstone";
|
public static final String ID = "redstone";
|
||||||
|
|
||||||
@@ -31,6 +29,16 @@ public class ReaderWriterHandlerRedstone implements IReaderWriterHandler {
|
|||||||
writer.setRedstoneStrength(0);
|
writer.setRedstoneStrength(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasCapability(IReaderWriter readerWriter, Capability<?> capability) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> T getCapability(IReaderWriter readerWriter, Capability<T> capability) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NBTTagCompound writeToNBT(NBTTagCompound tag) {
|
public NBTTagCompound writeToNBT(NBTTagCompound tag) {
|
||||||
return tag;
|
return tag;
|
||||||
@@ -40,14 +48,4 @@ public class ReaderWriterHandlerRedstone implements IReaderWriterHandler {
|
|||||||
public String getId() {
|
public String getId() {
|
||||||
return ID;
|
return ID;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean hasCapability(Capability<?> capability, @Nullable EnumFacing facing) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public <T> T getCapability(Capability<T> capability, @Nullable EnumFacing facing) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -88,54 +88,54 @@ public class TileReader extends TileNode implements IReader {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
|
public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
|
||||||
if (!super.hasCapability(capability, facing)) {
|
if (super.hasCapability(capability, facing)) {
|
||||||
if (network == null) {
|
return true;
|
||||||
return false;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
IReaderWriterChannel foundChannel = network.getReaderWriterChannel(channel);
|
|
||||||
|
|
||||||
if (foundChannel == null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (IReaderWriterHandler handler : foundChannel.getHandlers()) {
|
|
||||||
if (handler.hasCapability(capability, facing)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (facing != getDirection() || network == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
IReaderWriterChannel channel = network.getReaderWriterChannel(this.channel);
|
||||||
|
|
||||||
|
if (channel == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (IReaderWriterHandler handler : channel.getHandlers()) {
|
||||||
|
if (handler.hasCapability(this, capability)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
|
public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
|
||||||
T foundCap = super.getCapability(capability, facing);
|
T foundCapability = super.getCapability(capability, facing);
|
||||||
|
|
||||||
if (foundCap == null) {
|
if (foundCapability == null) {
|
||||||
if (network == null) {
|
if (facing != getDirection() || network == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
IReaderWriterChannel foundChannel = network.getReaderWriterChannel(channel);
|
IReaderWriterChannel channel = network.getReaderWriterChannel(this.channel);
|
||||||
|
|
||||||
if (foundChannel == null) {
|
if (channel == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (IReaderWriterHandler handler : foundChannel.getHandlers()) {
|
for (IReaderWriterHandler handler : channel.getHandlers()) {
|
||||||
foundCap = handler.getCapability(capability, facing);
|
foundCapability = handler.getCapability(this, capability);
|
||||||
|
|
||||||
if (foundCap != null) {
|
if (foundCapability != null) {
|
||||||
return foundCap;
|
return foundCapability;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return foundCap;
|
return foundCapability;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import net.minecraft.entity.player.EntityPlayer;
|
|||||||
import net.minecraft.entity.player.EntityPlayerMP;
|
import net.minecraft.entity.player.EntityPlayerMP;
|
||||||
import net.minecraft.nbt.NBTTagCompound;
|
import net.minecraft.nbt.NBTTagCompound;
|
||||||
import net.minecraft.util.EnumFacing;
|
import net.minecraft.util.EnumFacing;
|
||||||
|
import net.minecraftforge.common.capabilities.Capability;
|
||||||
|
|
||||||
public class TileWriter extends TileNode implements IWriter {
|
public class TileWriter extends TileNode implements IWriter {
|
||||||
private static final String NBT_CHANNEL = "Channel";
|
private static final String NBT_CHANNEL = "Channel";
|
||||||
@@ -107,6 +108,58 @@ public class TileWriter extends TileNode implements IWriter {
|
|||||||
return tag;
|
return tag;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
|
||||||
|
if (super.hasCapability(capability, facing)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (facing != getDirection() || network == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
IReaderWriterChannel channel = network.getReaderWriterChannel(this.channel);
|
||||||
|
|
||||||
|
if (channel == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (IReaderWriterHandler handler : channel.getHandlers()) {
|
||||||
|
if (handler.hasCapability(this, capability)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
|
||||||
|
T foundCapability = super.getCapability(capability, facing);
|
||||||
|
|
||||||
|
if (foundCapability == null) {
|
||||||
|
if (facing != getDirection() || network == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
IReaderWriterChannel channel = network.getReaderWriterChannel(this.channel);
|
||||||
|
|
||||||
|
if (channel == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (IReaderWriterHandler handler : channel.getHandlers()) {
|
||||||
|
foundCapability = handler.getCapability(this, capability);
|
||||||
|
|
||||||
|
if (foundCapability != null) {
|
||||||
|
return foundCapability;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return foundCapability;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setDirection(EnumFacing direction) {
|
public void setDirection(EnumFacing direction) {
|
||||||
super.setDirection(direction);
|
super.setDirection(direction);
|
||||||
|
|||||||
Reference in New Issue
Block a user