Also expose a capability on the writers

This commit is contained in:
Raoul Van den Berge
2016-11-12 01:44:48 +01:00
parent 9630f66b6d
commit 2a49e3faf0
5 changed files with 158 additions and 87 deletions

View File

@@ -1,12 +1,13 @@
package com.raoulvdberge.refinedstorage.api.network.readerwriter;
import com.raoulvdberge.refinedstorage.tile.IReaderWriter;
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, ...
*/
public interface IReaderWriterHandler extends ICapabilityProvider {
public interface IReaderWriterHandler {
/**
* Updates this reader writer handler.
*
@@ -21,6 +22,20 @@ public interface IReaderWriterHandler extends ICapabilityProvider {
*/
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.
*

View File

@@ -1,16 +1,16 @@
package com.raoulvdberge.refinedstorage.apiimpl.network.readerwriter;
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.IReaderWriterHandler;
import com.raoulvdberge.refinedstorage.api.network.readerwriter.IWriter;
import com.raoulvdberge.refinedstorage.tile.IReaderWriter;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemHandlerHelper;
import net.minecraftforge.items.ItemStackHandler;
import javax.annotation.Nullable;
@@ -18,45 +18,22 @@ import javax.annotation.Nullable;
public class ReaderWriterHandlerItems implements IReaderWriterHandler {
public static final String ID = "items";
private ItemStackHandler internalInv;
private ItemStackHandler items;
private ItemHandlerReaderWriter itemsReader, itemsWriter;
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) {
RSUtils.readItems(internalInv, 0, tag);
RSUtils.readItems(items, 0, tag);
}
}
@Override
public void update(IReaderWriterChannel channel) {
for (IWriter writer : channel.getWriters()) {
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);
}
}
}
}
// NO OP
}
@Override
@@ -64,9 +41,27 @@ public class ReaderWriterHandlerItems implements IReaderWriterHandler {
// 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
public NBTTagCompound writeToNBT(NBTTagCompound tag) {
RSUtils.writeItems(internalInv, 0, tag);
RSUtils.writeItems(items, 0, tag);
return tag;
}
@@ -76,24 +71,34 @@ public class ReaderWriterHandlerItems implements IReaderWriterHandler {
return ID;
}
@Override
public boolean hasCapability(Capability<?> capability, @Nullable EnumFacing facing) {
return capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY;
private class ItemHandlerReaderWriter implements IItemHandler {
private IItemHandler parent;
private boolean canInsert, canExtract;
public ItemHandlerReaderWriter(IItemHandler parent, boolean canInsert, boolean canExtract) {
this.parent = parent;
this.canInsert = canInsert;
this.canExtract = canExtract;
}
@Override
public <T> T getCapability(Capability<T> capability, @Nullable EnumFacing facing) {
return (T) internalInv;
public int getSlots() {
return parent.getSlots();
}
private class ItemHandlerReaderWriter extends ItemStackHandler {
private ItemHandlerReaderWriter(int size) {
super(size);
@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
public ItemStack extractItem(int slot, int amount, boolean simulate) {
return null;
return canExtract ? parent.extractItem(slot, amount, simulate) : null;
}
}
}

View File

@@ -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.IReaderWriterHandler;
import com.raoulvdberge.refinedstorage.api.network.readerwriter.IWriter;
import com.raoulvdberge.refinedstorage.tile.IReaderWriter;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import javax.annotation.Nullable;
public class ReaderWriterHandlerRedstone implements IReaderWriterHandler {
public static final String ID = "redstone";
@@ -31,6 +29,16 @@ public class ReaderWriterHandlerRedstone implements IReaderWriterHandler {
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
public NBTTagCompound writeToNBT(NBTTagCompound tag) {
return tag;
@@ -40,14 +48,4 @@ public class ReaderWriterHandlerRedstone implements IReaderWriterHandler {
public String getId() {
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;
}
}

View File

@@ -88,19 +88,22 @@ public class TileReader extends TileNode implements IReader {
@Override
public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
if (!super.hasCapability(capability, facing)) {
if (network == null) {
if (super.hasCapability(capability, facing)) {
return true;
}
if (facing != getDirection() || network == null) {
return false;
}
IReaderWriterChannel foundChannel = network.getReaderWriterChannel(channel);
IReaderWriterChannel channel = network.getReaderWriterChannel(this.channel);
if (foundChannel == null) {
if (channel == null) {
return false;
}
for (IReaderWriterHandler handler : foundChannel.getHandlers()) {
if (handler.hasCapability(capability, facing)) {
for (IReaderWriterHandler handler : channel.getHandlers()) {
if (handler.hasCapability(this, capability)) {
return true;
}
}
@@ -108,34 +111,31 @@ public class TileReader extends TileNode implements IReader {
return false;
}
return true;
}
@Override
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 (network == null) {
if (foundCapability == null) {
if (facing != getDirection() || network == null) {
return null;
}
IReaderWriterChannel foundChannel = network.getReaderWriterChannel(channel);
IReaderWriterChannel channel = network.getReaderWriterChannel(this.channel);
if (foundChannel == null) {
if (channel == null) {
return null;
}
for (IReaderWriterHandler handler : foundChannel.getHandlers()) {
foundCap = handler.getCapability(capability, facing);
for (IReaderWriterHandler handler : channel.getHandlers()) {
foundCapability = handler.getCapability(this, capability);
if (foundCap != null) {
return foundCap;
if (foundCapability != null) {
return foundCapability;
}
}
}
return foundCap;
return foundCapability;
}
@Override

View File

@@ -9,6 +9,7 @@ import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
public class TileWriter extends TileNode implements IWriter {
private static final String NBT_CHANNEL = "Channel";
@@ -107,6 +108,58 @@ public class TileWriter extends TileNode implements IWriter {
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
public void setDirection(EnumFacing direction) {
super.setDirection(direction);