API improvements to the reader writer stuff

This commit is contained in:
Raoul Van den Berge
2016-11-11 11:47:23 +01:00
parent 172feb7917
commit 18e239985d
10 changed files with 175 additions and 51 deletions

View File

@@ -2,6 +2,12 @@ package com.raoulvdberge.refinedstorage.api.network.readerwriter;
import com.raoulvdberge.refinedstorage.api.network.INetworkNode;
/**
* Represents a reader block in the world.
*/
public interface IReader extends INetworkNode {
/**
* @return the redstone strength this reader is receiving
*/
int getRedstoneStrength();
}

View File

@@ -4,12 +4,37 @@ import net.minecraft.nbt.NBTTagCompound;
import java.util.List;
/**
* Represents a reader writer channel in the RS network.
*/
public interface IReaderWriterChannel {
void addHandlers();
/**
* @return the handlers that this channel has
*/
List<IReaderWriterHandler> getHandlers();
/**
* @return a list of readers using this channel
*/
List<IReader> getReaders();
/**
* @return a list of writers using this channel
*/
List<IWriter> getWriters();
/**
* Writes this channel to NBT.
*
* @param tag the tag to write to
* @return the written tag
*/
NBTTagCompound writeToNBT(NBTTagCompound tag);
/**
* Reads this channel from NBT.
*
* @param tag the tag to read from
*/
void readFromNBT(NBTTagCompound tag);
}

View File

@@ -3,10 +3,34 @@ package com.raoulvdberge.refinedstorage.api.network.readerwriter;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
/**
* Represents a reader writer handler. Can be for example: items, fluids, energy, ...
*/
public interface IReaderWriterHandler extends ICapabilityProvider {
void update(IReader reader, IWriter writer);
/**
* Updates this reader writer handler.
*
* @param channel the channel this reader writer handler is assigned to
*/
void update(IReaderWriterChannel channel);
/**
* Called when the network connection state changes.
*
* @param state the new connection state
*/
void onConnectionChange(boolean state);
/**
* Writes this reader writer handler to NBT.
*
* @param tag the tag to write to
* @return the written tag
*/
NBTTagCompound writeToNBT(NBTTagCompound tag);
/**
* @return the id of this writer, as assigned to the {@link IReaderWriterHandlerRegistry}
*/
String getId();
}

View File

@@ -5,7 +5,16 @@ import net.minecraft.nbt.NBTTagCompound;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* A factory that is able to create reader writer handlers.
*/
public interface IReaderWriterHandlerFactory {
/**
* Creates a reader writer handler (based on NBT tag if there is any).
*
* @param tag the tag to read from, null if this reader writer handler is created on demand
* @return the reader writer handler
*/
@Nonnull
IReaderWriterHandler create(@Nullable NBTTagCompound tag);
}

View File

@@ -3,11 +3,29 @@ package com.raoulvdberge.refinedstorage.api.network.readerwriter;
import javax.annotation.Nullable;
import java.util.Collection;
/**
* A registry for reader writer handler factories.
*/
public interface IReaderWriterHandlerRegistry {
/**
* Adds a factory to the registry.
*
* @param id the id of this reader writer handler. Analog to {@link IReaderWriterHandler#getId()}
* @param factory the factory
*/
void add(String id, IReaderWriterHandlerFactory factory);
/**
* Gets a factory from the registry.
*
* @param id the id of the factory to get
* @return the factory, or null if no factory was found
*/
@Nullable
IReaderWriterHandlerFactory getFactory(String id);
/**
* @return a list of reader writer handler factories
*/
Collection<IReaderWriterHandlerFactory> getFactories();
}

View File

@@ -3,12 +3,27 @@ package com.raoulvdberge.refinedstorage.api.network.readerwriter;
import com.raoulvdberge.refinedstorage.api.network.INetworkNode;
import net.minecraft.util.EnumFacing;
/**
* Represents a writer block in the world.
*/
public interface IWriter extends INetworkNode {
/**
* @return the redstone strength this writer block is emitting
*/
int getRedstoneStrength();
/**
* @param strength the redstone strength to set to be emitted
*/
void setRedstoneStrength(int strength);
/**
* @return the direction of the writer
*/
EnumFacing getDirection();
/**
* @return true if this writer has a stack upgrade, false otherwise
*/
boolean hasStackUpgrade();
}

View File

@@ -1,26 +1,24 @@
package com.raoulvdberge.refinedstorage.apiimpl.network.readerwriter;
import com.raoulvdberge.refinedstorage.api.network.readerwriter.IReaderWriterChannel;
import com.raoulvdberge.refinedstorage.api.network.readerwriter.IReaderWriterHandler;
import com.raoulvdberge.refinedstorage.api.network.readerwriter.IReaderWriterHandlerFactory;
import com.raoulvdberge.refinedstorage.api.network.INetworkMaster;
import com.raoulvdberge.refinedstorage.api.network.readerwriter.*;
import com.raoulvdberge.refinedstorage.apiimpl.API;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraftforge.common.util.Constants;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class ReaderWriterChannel implements IReaderWriterChannel {
private static final String NBT_HANDLER_ID = "HandlerID";
private static final String NBT_HANDLERS = "Handlers";
private static final String NBT_HANDLER = "Handler_%s";
private INetworkMaster network;
private List<IReaderWriterHandler> handlers = new ArrayList<>();
@Override
public void addHandlers() {
handlers.addAll(API.instance().getReaderWriterHandlerRegistry().getFactories().stream().map(f -> f.create(null)).collect(Collectors.toList()));
public ReaderWriterChannel(INetworkMaster network) {
this.network = network;
this.handlers.addAll(API.instance().getReaderWriterHandlerRegistry().getFactories().stream().map(f -> f.create(null)).collect(Collectors.toList()));
}
@Override
@@ -29,39 +27,35 @@ public class ReaderWriterChannel implements IReaderWriterChannel {
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound tag) {
NBTTagList handlersList = new NBTTagList();
for (IReaderWriterHandler handler : handlers) {
NBTTagCompound handlerTag = handler.writeToNBT(new NBTTagCompound());
handlerTag.setString(NBT_HANDLER_ID, handler.getId());
handlersList.appendTag(handlerTag);
public List<IReader> getReaders() {
return network.getNodeGraph().all().stream().filter(n -> n instanceof IReader).map(n -> (IReader) n).collect(Collectors.toList());
}
tag.setTag(NBT_HANDLERS, handlersList);
@Override
public List<IWriter> getWriters() {
return network.getNodeGraph().all().stream().filter(n -> n instanceof IWriter).map(n -> (IWriter) n).collect(Collectors.toList());
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound tag) {
for (IReaderWriterHandler handler : handlers) {
tag.setTag(String.format(NBT_HANDLER, handler.getId()), handler.writeToNBT(new NBTTagCompound()));
}
return tag;
}
@Override
public void readFromNBT(NBTTagCompound tag) {
if (!tag.hasKey(NBT_HANDLER_ID)) {
return;
}
NBTTagList handlersList = tag.getTagList(NBT_HANDLERS, Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < handlersList.tagCount(); ++i) {
NBTTagCompound handlerTag = handlersList.getCompoundTagAt(i);
String id = handlerTag.getString(NBT_HANDLER_ID);
for (IReaderWriterHandler handler : handlers) {
String id = String.format(NBT_HANDLER, handler.getId());
if (tag.hasKey(id)) {
IReaderWriterHandlerFactory factory = API.instance().getReaderWriterHandlerRegistry().getFactory(id);
if (factory != null) {
handlers.add(factory.create(tag));
handlers.add(factory.create(tag.getCompoundTag(id)));
}
}
}
}

View File

@@ -1,7 +1,7 @@
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 net.minecraft.item.ItemStack;
@@ -29,7 +29,8 @@ public class ReaderWriterHandlerItems implements IReaderWriterHandler {
}
@Override
public void update(IReader reader, IWriter writer) {
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) {
@@ -54,6 +55,7 @@ public class ReaderWriterHandlerItems implements IReaderWriterHandler {
}
}
}
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound tag) {

View File

@@ -19,6 +19,7 @@ import com.raoulvdberge.refinedstorage.api.network.grid.IFluidGridHandler;
import com.raoulvdberge.refinedstorage.api.network.grid.IItemGridHandler;
import com.raoulvdberge.refinedstorage.api.network.item.INetworkItemHandler;
import com.raoulvdberge.refinedstorage.api.network.readerwriter.IReaderWriterChannel;
import com.raoulvdberge.refinedstorage.api.network.readerwriter.IReaderWriterHandler;
import com.raoulvdberge.refinedstorage.api.storage.AccessType;
import com.raoulvdberge.refinedstorage.api.storage.IStorage;
import com.raoulvdberge.refinedstorage.api.storage.fluid.IFluidStorage;
@@ -286,6 +287,12 @@ public class TileController extends TileBase implements INetworkMaster, IEnergyR
}
}
for (IReaderWriterChannel channel : readerWriterChannels.values()) {
for (IReaderWriterHandler handler : channel.getHandlers()) {
handler.update(channel);
}
}
if (!craftingTasks.isEmpty() || !readerWriterChannels.isEmpty()) {
markDirty();
}
@@ -295,7 +302,6 @@ public class TileController extends TileBase implements INetworkMaster, IEnergyR
sendCraftingMonitorUpdate();
}
}
networkItemHandler.update();
@@ -315,6 +321,12 @@ public class TileController extends TileBase implements INetworkMaster, IEnergyR
if (couldRun != canRun()) {
couldRun = canRun();
for (IReaderWriterChannel channel : readerWriterChannels.values()) {
for (IReaderWriterHandler handler : channel.getHandlers()) {
handler.onConnectionChange(couldRun);
}
}
nodeGraph.rebuild();
}

View File

@@ -0,0 +1,19 @@
package com.raoulvdberge.refinedstorage.tile;
import com.raoulvdberge.refinedstorage.api.network.readerwriter.IReader;
public class TileReader extends TileNode implements IReader {
@Override
public int getEnergyUsage() {
return 0; // @TODO
}
@Override
public void updateNode() {
}
@Override
public int getRedstoneStrength() {
return worldObj.getRedstonePower(pos, getDirection().getOpposite());
}
}