use of mcjty.xnet.api.keys.SidedConsumer in project XNet by McJty.
the class ChannelClientInfo method writeToNBT.
public void writeToNBT(@Nonnull ByteBuf buf) {
NetworkTools.writeStringUTF8(buf, channelName);
buf.writeBoolean(enabled);
NetworkTools.writeString(buf, type.getID());
NBTTagCompound tag = new NBTTagCompound();
channelSettings.writeToNBT(tag);
NetworkTools.writeTag(buf, tag);
buf.writeInt(connectors.size());
for (Map.Entry<SidedConsumer, ConnectorClientInfo> entry : connectors.entrySet()) {
SidedConsumer key = entry.getKey();
ConnectorClientInfo info = entry.getValue();
buf.writeInt(key.getConsumerId().getId());
buf.writeByte(key.getSide().ordinal());
info.writeToBuf(buf);
}
}
use of mcjty.xnet.api.keys.SidedConsumer in project XNet by McJty.
the class ChannelInfo method readFromNBT.
public void readFromNBT(NBTTagCompound tag) {
channelSettings.readFromNBT(tag);
enabled = tag.getBoolean("enabled");
if (tag.hasKey("name")) {
channelName = tag.getString("name");
} else {
channelName = null;
}
NBTTagList conlist = tag.getTagList("connectors", Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < conlist.tagCount(); i++) {
NBTTagCompound tc = conlist.getCompoundTagAt(i);
String id = tc.getString("type");
IChannelType type = XNet.xNetApi.findType(id);
if (type == null) {
XNet.logger.warn("Unsupported type " + id + "!");
continue;
}
if (!getType().equals(type)) {
XNet.logger.warn("Trying to load a connector with non-matching type " + type + "!");
continue;
}
ConsumerId consumerId = new ConsumerId(tc.getInteger("consumerId"));
EnumFacing side = EnumFacing.VALUES[tc.getInteger("side")];
SidedConsumer key = new SidedConsumer(consumerId, side);
boolean advanced = tc.getBoolean("advanced");
ConnectorInfo connectorInfo = new ConnectorInfo(type, key, advanced);
connectorInfo.readFromNBT(tc);
connectors.put(key, connectorInfo);
}
}
use of mcjty.xnet.api.keys.SidedConsumer in project XNet by McJty.
the class FluidChannelSettings method insertFluidReal.
private void insertFluidReal(@Nonnull IControllerContext context, @Nonnull List<Pair<SidedConsumer, FluidConnectorSettings>> inserted, @Nonnull FluidStack stack) {
int amount = stack.amount;
for (Pair<SidedConsumer, FluidConnectorSettings> pair : inserted) {
BlockPos consumerPosition = context.findConsumerPosition(pair.getKey().getConsumerId());
EnumFacing side = pair.getKey().getSide();
FluidConnectorSettings settings = pair.getValue();
BlockPos pos = consumerPosition.offset(side);
TileEntity te = context.getControllerWorld().getTileEntity(pos);
IFluidHandler handler = getFluidHandlerAt(te, settings.getFacing());
int toinsert = Math.min(settings.getRate(), amount);
Integer count = settings.getMinmax();
if (count != null) {
int a = countFluid(handler, settings.getMatcher());
int caninsert = count - a;
if (caninsert <= 0) {
continue;
}
toinsert = Math.min(toinsert, caninsert);
}
FluidStack copy = stack.copy();
copy.amount = toinsert;
int filled = handler.fill(copy, true);
if (filled > 0) {
roundRobinOffset = (roundRobinOffset + 1) % fluidConsumers.size();
amount -= filled;
if (amount <= 0) {
return;
}
}
}
}
use of mcjty.xnet.api.keys.SidedConsumer in project XNet by McJty.
the class LogicChannelSettings method tick.
@Override
public void tick(int channel, IControllerContext context) {
delay--;
if (delay <= 0) {
// Multiply of the different speeds we have
delay = 200 * 6;
}
if (delay % 5 != 0) {
return;
}
int d = delay / 5;
updateCache(channel, context);
World world = context.getControllerWorld();
colors = 0;
for (Pair<SidedConsumer, LogicConnectorSettings> entry : sensors) {
LogicConnectorSettings settings = entry.getValue();
if (d % settings.getSpeed() != 0) {
// Use the color settings from this connector as we last remembered it
colors |= settings.getColorMask();
continue;
}
int sensorColors = 0;
BlockPos connectorPos = context.findConsumerPosition(entry.getKey().getConsumerId());
if (connectorPos != null) {
EnumFacing side = entry.getKey().getSide();
BlockPos pos = connectorPos.offset(side);
if (!WorldTools.chunkLoaded(world, pos)) {
// If it is not chunkloaded we just use the color settings as we last remembered it
colors |= settings.getColorMask();
continue;
}
boolean sense = true;
sense = !checkRedstone(world, settings, connectorPos);
if (sense && !context.matchColor(settings.getColorsMask())) {
sense = false;
}
// If sense is false the sensor is disabled which means the colors from it will also be disabled
if (sense) {
TileEntity te = world.getTileEntity(pos);
for (Sensor sensor : settings.getSensors()) {
if (sensor.test(te, world, pos, settings)) {
sensorColors |= 1 << sensor.getOutputColor().ordinal();
}
}
}
}
settings.setColorMask(sensorColors);
colors |= sensorColors;
}
for (Pair<SidedConsumer, LogicConnectorSettings> entry : outputs) {
LogicConnectorSettings settings = entry.getValue();
if (d % settings.getSpeed() != 0) {
continue;
}
BlockPos connectorPos = context.findConsumerPosition(entry.getKey().getConsumerId());
if (connectorPos != null) {
EnumFacing side = entry.getKey().getSide();
if (!WorldTools.chunkLoaded(world, connectorPos)) {
continue;
}
TileEntity te = world.getTileEntity(connectorPos);
if (te instanceof ConnectorTileEntity) {
ConnectorTileEntity connectorTE = (ConnectorTileEntity) te;
int powerOut;
if (checkRedstone(world, settings, connectorPos)) {
powerOut = 0;
} else if (!context.matchColor(settings.getColorsMask())) {
powerOut = 0;
} else {
powerOut = settings.getRedstoneOut() == null ? 0 : settings.getRedstoneOut();
}
connectorTE.setPowerOut(side, powerOut);
}
}
}
}
use of mcjty.xnet.api.keys.SidedConsumer in project XNet by McJty.
the class EnergyChannelSettings method insertEnergy.
private int insertEnergy(@Nonnull IControllerContext context, int energy) {
int total = 0;
World world = context.getControllerWorld();
for (Pair<SidedConsumer, EnergyConnectorSettings> entry : energyConsumers) {
EnergyConnectorSettings settings = entry.getValue();
BlockPos extractorPos = context.findConsumerPosition(entry.getKey().getConsumerId());
if (extractorPos != null) {
EnumFacing side = entry.getKey().getSide();
BlockPos pos = extractorPos.offset(side);
if (!WorldTools.chunkLoaded(world, pos)) {
continue;
}
TileEntity te = world.getTileEntity(pos);
// @todo report error somewhere?
if (isEnergyTE(te, settings.getFacing())) {
if (checkRedstone(world, settings, extractorPos)) {
continue;
}
if (!context.matchColor(settings.getColorsMask())) {
continue;
}
Integer count = settings.getMinmax();
if (count != null) {
int level = getEnergyLevel(te, settings.getFacing());
if (level >= count) {
continue;
}
}
Integer rate = settings.getRate();
if (rate == null) {
boolean advanced = ConnectorBlock.isAdvancedConnector(world, extractorPos);
rate = advanced ? GeneralConfiguration.maxRfRateAdvanced : GeneralConfiguration.maxRfRateNormal;
}
int totransfer = Math.min(rate, energy);
int e = EnergyTools.receiveEnergy(te, settings.getFacing(), totransfer);
energy -= e;
total += e;
if (energy <= 0) {
return total;
}
}
}
}
return total;
}
Aggregations