Search in sources :

Example 11 with SidedConsumer

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);
    }
}
Also used : SidedConsumer(mcjty.xnet.api.keys.SidedConsumer) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) Map(java.util.Map) HashMap(java.util.HashMap)

Example 12 with SidedConsumer

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);
    }
}
Also used : NBTTagList(net.minecraft.nbt.NBTTagList) ConnectorInfo(mcjty.xnet.clientinfo.ConnectorInfo) SidedConsumer(mcjty.xnet.api.keys.SidedConsumer) EnumFacing(net.minecraft.util.EnumFacing) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) ConsumerId(mcjty.xnet.api.keys.ConsumerId) IChannelType(mcjty.xnet.api.channels.IChannelType)

Example 13 with SidedConsumer

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;
            }
        }
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) SidedConsumer(mcjty.xnet.api.keys.SidedConsumer) FluidStack(net.minecraftforge.fluids.FluidStack) EnumFacing(net.minecraft.util.EnumFacing) BlockPos(net.minecraft.util.math.BlockPos) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler)

Example 14 with SidedConsumer

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);
            }
        }
    }
}
Also used : ConnectorTileEntity(mcjty.xnet.blocks.cables.ConnectorTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) SidedConsumer(mcjty.xnet.api.keys.SidedConsumer) ConnectorTileEntity(mcjty.xnet.blocks.cables.ConnectorTileEntity) EnumFacing(net.minecraft.util.EnumFacing) BlockPos(net.minecraft.util.math.BlockPos) World(net.minecraft.world.World)

Example 15 with SidedConsumer

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;
}
Also used : ConnectorTileEntity(mcjty.xnet.blocks.cables.ConnectorTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) SidedConsumer(mcjty.xnet.api.keys.SidedConsumer) EnumFacing(net.minecraft.util.EnumFacing) BlockPos(net.minecraft.util.math.BlockPos) World(net.minecraft.world.World)

Aggregations

SidedConsumer (mcjty.xnet.api.keys.SidedConsumer)20 BlockPos (net.minecraft.util.math.BlockPos)14 EnumFacing (net.minecraft.util.EnumFacing)13 TileEntity (net.minecraft.tileentity.TileEntity)10 World (net.minecraft.world.World)8 ConnectorInfo (mcjty.xnet.clientinfo.ConnectorInfo)6 WorldBlob (mcjty.xnet.multiblock.WorldBlob)6 ConsumerId (mcjty.xnet.api.keys.ConsumerId)5 HashMap (java.util.HashMap)4 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)4 Map (java.util.Map)3 Nonnull (javax.annotation.Nonnull)3 ConnectorTileEntity (mcjty.xnet.blocks.cables.ConnectorTileEntity)3 NBTTagList (net.minecraft.nbt.NBTTagList)3 FluidStack (net.minecraftforge.fluids.FluidStack)3 IFluidHandler (net.minecraftforge.fluids.capability.IFluidHandler)3 IItemHandler (net.minecraftforge.items.IItemHandler)3 ArrayList (java.util.ArrayList)2 Argument (mcjty.lib.network.Argument)2 IChannelType (mcjty.xnet.api.channels.IChannelType)2