use of mcjty.xnet.api.keys.SidedConsumer in project XNet by McJty.
the class ItemChannelSettings method insertStackSimulate.
// Returns what could not be inserted
public int insertStackSimulate(@Nonnull List<Pair<SidedConsumer, ItemConnectorSettings>> inserted, @Nonnull IControllerContext context, @Nonnull ItemStack stack) {
World world = context.getControllerWorld();
if (channelMode == ChannelMode.PRIORITY) {
// Always start at 0
roundRobinOffset = 0;
}
int total = stack.getCount();
for (int j = 0; j < itemConsumers.size(); j++) {
int i = (j + roundRobinOffset) % itemConsumers.size();
Pair<SidedConsumer, ItemConnectorSettings> entry = itemConsumers.get(i);
ItemConnectorSettings settings = entry.getValue();
if (settings.getMatcher().test(stack)) {
BlockPos consumerPos = context.findConsumerPosition(entry.getKey().getConsumerId());
if (consumerPos != null) {
if (!WorldTools.chunkLoaded(world, consumerPos)) {
continue;
}
if (checkRedstone(world, settings, consumerPos)) {
continue;
}
if (!context.matchColor(settings.getColorsMask())) {
continue;
}
EnumFacing side = entry.getKey().getSide();
BlockPos pos = consumerPos.offset(side);
TileEntity te = world.getTileEntity(pos);
int actuallyinserted;
int toinsert = total;
ItemStack remaining;
Integer count = settings.getCount();
if (XNet.rftools && RFToolsSupport.isStorageScanner(te)) {
if (count != null) {
int amount = RFToolsSupport.countItems(te, settings.getMatcher(), count);
int caninsert = count - amount;
if (caninsert <= 0) {
continue;
}
toinsert = Math.min(toinsert, caninsert);
stack = stack.copy();
if (toinsert <= 0) {
stack.setCount(0);
} else {
stack.setCount(toinsert);
}
}
remaining = RFToolsSupport.insertItem(te, stack, true);
} else {
IItemHandler handler = getItemHandlerAt(te, settings.getFacing());
if (handler != null) {
if (count != null) {
int amount = countItems(handler, settings.getMatcher());
int caninsert = count - amount;
if (caninsert <= 0) {
continue;
}
toinsert = Math.min(toinsert, caninsert);
stack = stack.copy();
if (toinsert <= 0) {
stack.setCount(0);
} else {
stack.setCount(toinsert);
}
}
remaining = ItemHandlerHelper.insertItem(handler, stack, true);
} else {
continue;
}
}
actuallyinserted = toinsert - remaining.getCount();
if (count == null) {
// If we are not using a count then we restore 'stack' here as that is what
// we actually have to keep inserting until it is empty. If we are using a count
// then we don't do this as we don't want to risk stack getting null (on 1.10.2)
// from the insertItem() and then not being able to set stacksize a few lines
// above this
stack = remaining;
}
if (actuallyinserted > 0) {
inserted.add(entry);
total -= actuallyinserted;
if (total <= 0) {
return 0;
}
}
}
}
}
return total;
}
use of mcjty.xnet.api.keys.SidedConsumer in project XNet by McJty.
the class ItemChannelSettings method insertStackReal.
public void insertStackReal(@Nonnull IControllerContext context, @Nonnull List<Pair<SidedConsumer, ItemConnectorSettings>> inserted, @Nonnull ItemStack stack) {
int total = stack.getCount();
for (Pair<SidedConsumer, ItemConnectorSettings> entry : inserted) {
BlockPos consumerPosition = context.findConsumerPosition(entry.getKey().getConsumerId());
EnumFacing side = entry.getKey().getSide();
ItemConnectorSettings settings = entry.getValue();
BlockPos pos = consumerPosition.offset(side);
TileEntity te = context.getControllerWorld().getTileEntity(pos);
if (XNet.rftools && RFToolsSupport.isStorageScanner(te)) {
int toinsert = total;
Integer count = settings.getCount();
if (count != null) {
int amount = RFToolsSupport.countItems(te, settings.getMatcher(), count);
int caninsert = count - amount;
if (caninsert <= 0) {
continue;
}
toinsert = Math.min(toinsert, caninsert);
stack = stack.copy();
if (toinsert <= 0) {
stack.setCount(0);
} else {
stack.setCount(toinsert);
}
}
ItemStack remaining = RFToolsSupport.insertItem(te, stack, false);
int actuallyinserted = toinsert - remaining.getCount();
if (count == null) {
// If we are not using a count then we restore 'stack' here as that is what
// we actually have to keep inserting until it is empty. If we are using a count
// then we don't do this as we don't want to risk stack getting null (on 1.10.2)
// from the insertItem() and then not being able to set stacksize a few lines
// above this
stack = remaining;
}
if (actuallyinserted > 0) {
roundRobinOffset = (roundRobinOffset + 1) % itemConsumers.size();
total -= actuallyinserted;
if (total <= 0) {
return;
}
}
} else {
IItemHandler handler = getItemHandlerAt(te, settings.getFacing());
int toinsert = total;
Integer count = settings.getCount();
if (count != null) {
int amount = countItems(handler, settings.getMatcher());
int caninsert = count - amount;
if (caninsert <= 0) {
continue;
}
toinsert = Math.min(toinsert, caninsert);
stack = stack.copy();
if (toinsert <= 0) {
stack.setCount(0);
} else {
stack.setCount(toinsert);
}
}
ItemStack remaining = ItemHandlerHelper.insertItem(handler, stack, false);
int actuallyinserted = toinsert - remaining.getCount();
if (count == null) {
// If we are not using a count then we restore 'stack' here as that is what
// we actually have to keep inserting until it is empty. If we are using a count
// then we don't do this as we don't want to risk stack getting null (on 1.10.2)
// from the insertItem() and then not being able to set stacksize a few lines
// above this
stack = remaining;
}
if (actuallyinserted > 0) {
roundRobinOffset = (roundRobinOffset + 1) % itemConsumers.size();
total -= actuallyinserted;
if (total <= 0) {
return;
}
}
}
}
}
use of mcjty.xnet.api.keys.SidedConsumer in project XNet by McJty.
the class EnergyChannelSettings method tick.
@Override
public void tick(int channel, IControllerContext context) {
updateCache(channel, context);
World world = context.getControllerWorld();
// First find out how much energy we have to distribute in total
int totalToDistribute = 0;
// Keep track of the connectors we already got energy from and how much energy we
// got from it
Map<BlockPos, Integer> alreadyHandled = new HashMap<>();
List<Pair<ConnectorTileEntity, Integer>> energyProducers = new ArrayList<>();
for (Pair<SidedConsumer, EnergyConnectorSettings> entry : energyExtractors) {
BlockPos connectorPos = context.findConsumerPosition(entry.getKey().getConsumerId());
if (connectorPos != null) {
EnumFacing side = entry.getKey().getSide();
BlockPos energyPos = connectorPos.offset(side);
if (!WorldTools.chunkLoaded(world, energyPos)) {
continue;
}
TileEntity te = world.getTileEntity(energyPos);
// @todo report error somewhere?
if (isEnergyTE(te, side.getOpposite())) {
EnergyConnectorSettings settings = entry.getValue();
ConnectorTileEntity connectorTE = (ConnectorTileEntity) world.getTileEntity(connectorPos);
if (checkRedstone(world, settings, connectorPos)) {
continue;
}
if (!context.matchColor(settings.getColorsMask())) {
continue;
}
Integer count = settings.getMinmax();
if (count != null) {
int level = getEnergyLevel(te, side.getOpposite());
if (level < count) {
continue;
}
}
Integer rate = settings.getRate();
if (rate == null) {
boolean advanced = ConnectorBlock.isAdvancedConnector(world, connectorPos);
rate = advanced ? GeneralConfiguration.maxRfRateAdvanced : GeneralConfiguration.maxRfRateNormal;
}
connectorTE.setEnergyInputFrom(side, rate);
if (!alreadyHandled.containsKey(connectorPos)) {
// We did not handle this connector yet. Remember the amount of energy in it
alreadyHandled.put(connectorPos, connectorTE.getEnergy());
}
// Check how much energy we can still send from that connector
int connectorEnergy = alreadyHandled.get(connectorPos);
int tosend = Math.min(rate, connectorEnergy);
if (tosend > 0) {
// Decrease the energy from our temporary datastructure
alreadyHandled.put(connectorPos, connectorEnergy - tosend);
totalToDistribute += tosend;
energyProducers.add(Pair.of(connectorTE, tosend));
}
}
}
}
if (totalToDistribute <= 0) {
// Nothing to do
return;
}
if (!context.checkAndConsumeRF(GeneralConfiguration.controllerOperationRFT)) {
// Not enough energy for this operation
return;
}
int actuallyConsumed = insertEnergy(context, totalToDistribute);
if (actuallyConsumed <= 0) {
// Nothing was done
return;
}
// Now we need to actually fetch the energy from the producers
for (Pair<ConnectorTileEntity, Integer> entry : energyProducers) {
ConnectorTileEntity connectorTE = entry.getKey();
int amount = entry.getValue();
int actuallySpent = Math.min(amount, actuallyConsumed);
connectorTE.setEnergy(connectorTE.getEnergy() - actuallySpent);
actuallyConsumed -= actuallySpent;
if (actuallyConsumed <= 0) {
break;
}
}
}
use of mcjty.xnet.api.keys.SidedConsumer in project XNet by McJty.
the class ChannelInfo method writeToNBT.
public void writeToNBT(NBTTagCompound tag) {
channelSettings.writeToNBT(tag);
tag.setBoolean("enabled", enabled);
if (channelName != null && !channelName.isEmpty()) {
tag.setString("name", channelName);
}
NBTTagList conlist = new NBTTagList();
for (Map.Entry<SidedConsumer, ConnectorInfo> entry : connectors.entrySet()) {
NBTTagCompound tc = new NBTTagCompound();
ConnectorInfo connectorInfo = entry.getValue();
connectorInfo.writeToNBT(tc);
tc.setInteger("consumerId", entry.getKey().getConsumerId().getId());
tc.setInteger("side", entry.getKey().getSide().ordinal());
tc.setString("type", connectorInfo.getType().getID());
tc.setBoolean("advanced", connectorInfo.isAdvanced());
conlist.appendTag(tc);
}
tag.setTag("connectors", conlist);
}
use of mcjty.xnet.api.keys.SidedConsumer in project XNet by McJty.
the class TileEntityRouter method addRoutedConnectors.
public void addRoutedConnectors(Map<SidedConsumer, IConnectorSettings> connectors, @Nonnull BlockPos controllerPos, int channel, IChannelType type) {
if (inError()) {
// We are in error. Don't do anything
return;
}
LocalChannelId id = new LocalChannelId(controllerPos, channel);
String publishedName = publishedChannels.get(id);
if (publishedName != null && !publishedName.isEmpty()) {
NetworkId networkId = findRoutingNetwork();
if (networkId != null) {
LogicTools.consumers(getWorld(), networkId).forEach(consumerPos -> LogicTools.routers(getWorld(), consumerPos).forEach(router -> router.addConnectorsFromConnectedNetworks(connectors, publishedName, type)));
} else {
// If there is no routing network that means we have a local network only
addConnectorsFromConnectedNetworks(connectors, publishedName, type);
}
}
}
Aggregations