Search in sources :

Example 1 with ConnectorTileEntity

use of mcjty.xnet.blocks.cables.ConnectorTileEntity in project XNet by McJty.

the class TileEntityController method findConnectedBlocks.

@Nonnull
private List<ConnectedBlockClientInfo> findConnectedBlocks() {
    WorldBlob worldBlob = XNetBlobData.getBlobData(getWorld()).getWorldBlob(getWorld());
    Set<ConnectedBlockClientInfo> set = new HashSet<>();
    for (BlockPos consumerPos : worldBlob.getConsumers(networkId)) {
        String name = "";
        TileEntity te = getWorld().getTileEntity(consumerPos);
        if (te instanceof ConnectorTileEntity) {
            // Should always be the case. @todo error?
            name = ((ConnectorTileEntity) te).getConnectorName();
        } else {
            XNet.logger.warn("What? The connector at " + BlockPosTools.toString(consumerPos) + " is not a connector?");
        }
        for (EnumFacing facing : EnumFacing.VALUES) {
            if (ConnectorBlock.isConnectable(getWorld(), consumerPos, facing)) {
                BlockPos pos = consumerPos.offset(facing);
                SidedPos sidedPos = new SidedPos(pos, facing.getOpposite());
                IBlockState state = getWorld().getBlockState(pos);
                ItemStack item = state.getBlock().getItem(getWorld(), pos, state);
                ConnectedBlockClientInfo info = new ConnectedBlockClientInfo(sidedPos, item, name);
                set.add(info);
            }
        }
    }
    List<ConnectedBlockClientInfo> list = new ArrayList<>(set);
    list.sort(Comparator.comparing(ConnectedBlockClientInfo::getBlockUnlocName).thenComparing(ConnectedBlockClientInfo::getPos));
    return list;
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) ConnectorTileEntity(mcjty.xnet.blocks.cables.ConnectorTileEntity) EnumFacing(net.minecraft.util.EnumFacing) SPacketUpdateTileEntity(net.minecraft.network.play.server.SPacketUpdateTileEntity) ConnectorTileEntity(mcjty.xnet.blocks.cables.ConnectorTileEntity) GenericEnergyReceiverTileEntity(mcjty.lib.entity.GenericEnergyReceiverTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) ConnectedBlockClientInfo(mcjty.xnet.clientinfo.ConnectedBlockClientInfo) SidedPos(mcjty.xnet.api.keys.SidedPos) WorldBlob(mcjty.xnet.multiblock.WorldBlob) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack) Nonnull(javax.annotation.Nonnull)

Example 2 with ConnectorTileEntity

use of mcjty.xnet.blocks.cables.ConnectorTileEntity 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;
        }
    }
}
Also used : HashMap(java.util.HashMap) SidedConsumer(mcjty.xnet.api.keys.SidedConsumer) ConnectorTileEntity(mcjty.xnet.blocks.cables.ConnectorTileEntity) EnumFacing(net.minecraft.util.EnumFacing) ArrayList(java.util.ArrayList) World(net.minecraft.world.World) ConnectorTileEntity(mcjty.xnet.blocks.cables.ConnectorTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) BlockPos(net.minecraft.util.math.BlockPos) Pair(org.apache.commons.lang3.tuple.Pair)

Example 3 with ConnectorTileEntity

use of mcjty.xnet.blocks.cables.ConnectorTileEntity in project XNet by McJty.

the class ConnectorUpgradeItem method onItemUse.

@Override
public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
    IBlockState state = world.getBlockState(pos);
    Block block = state.getBlock();
    if (block == NetCableSetup.connectorBlock) {
        if (!world.isRemote) {
            TileEntity te = world.getTileEntity(pos);
            if (te instanceof ConnectorTileEntity) {
                NBTTagCompound tag = new NBTTagCompound();
                te.writeToNBT(tag);
                CableColor color = world.getBlockState(pos).getValue(GenericCableBlock.COLOR);
                XNetBlobData blobData = XNetBlobData.getBlobData(world);
                WorldBlob worldBlob = blobData.getWorldBlob(world);
                ConsumerId consumer = worldBlob.getConsumerAt(pos);
                ((ConnectorBlock) block).unlinkBlock(world, pos);
                world.setBlockState(pos, NetCableSetup.advancedConnectorBlock.getDefaultState().withProperty(GenericCableBlock.COLOR, color));
                IBlockState blockState = world.getBlockState(pos);
                ((ConnectorBlock) blockState.getBlock()).createCableSegment(world, pos, consumer);
                te = TileEntity.create(world, tag);
                if (te != null) {
                    world.getChunkFromBlockCoords(pos).addTileEntity(te);
                    te.markDirty();
                    world.notifyBlockUpdate(pos, blockState, blockState, 3);
                    player.inventory.decrStackSize(player.inventory.currentItem, 1);
                    player.openContainer.detectAndSendChanges();
                    player.sendStatusMessage(new TextComponentString(TextFormatting.GREEN + "Connector was upgraded"), false);
                } else {
                    player.sendStatusMessage(new TextComponentString(TextFormatting.RED + "Something went wrong during upgrade!"), false);
                    return EnumActionResult.FAIL;
                }
            }
        }
        return EnumActionResult.SUCCESS;
    } else if (block == NetCableSetup.advancedConnectorBlock) {
        if (!world.isRemote) {
            player.sendStatusMessage(new TextComponentString(TextFormatting.YELLOW + "This connector is already advanced!"), false);
        }
        return EnumActionResult.SUCCESS;
    } else {
        if (!world.isRemote) {
            player.sendStatusMessage(new TextComponentString(TextFormatting.RED + "Use this item on a connector to upgrade it!"), false);
        }
        return EnumActionResult.SUCCESS;
    }
}
Also used : ConnectorTileEntity(mcjty.xnet.blocks.cables.ConnectorTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) IBlockState(net.minecraft.block.state.IBlockState) XNetBlobData(mcjty.xnet.multiblock.XNetBlobData) ConnectorTileEntity(mcjty.xnet.blocks.cables.ConnectorTileEntity) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) ConsumerId(mcjty.xnet.api.keys.ConsumerId) GenericCableBlock(mcjty.xnet.blocks.generic.GenericCableBlock) Block(net.minecraft.block.Block) ConnectorBlock(mcjty.xnet.blocks.cables.ConnectorBlock) WorldBlob(mcjty.xnet.multiblock.WorldBlob) CableColor(mcjty.xnet.blocks.generic.CableColor) ConnectorBlock(mcjty.xnet.blocks.cables.ConnectorBlock) TextComponentString(net.minecraft.util.text.TextComponentString)

Example 4 with ConnectorTileEntity

use of mcjty.xnet.blocks.cables.ConnectorTileEntity in project XNet by McJty.

the class GuiProxy method getClientGuiElement.

@Override
public Object getClientGuiElement(int guiid, EntityPlayer entityPlayer, World world, int x, int y, int z) {
    if (guiid == GUI_MANUAL_XNET) {
        return new GuiXNetManual(GuiXNetManual.MANUAL_XNET);
    }
    BlockPos pos = new BlockPos(x, y, z);
    Block block = world.getBlockState(pos).getBlock();
    if (block instanceof GenericBlock) {
        GenericBlock<?, ?> genericBlock = (GenericBlock<?, ?>) block;
        TileEntity te = world.getTileEntity(pos);
        return genericBlock.createClientGui(entityPlayer, te);
    } else if (block instanceof ConnectorBlock) {
        TileEntity te = world.getTileEntity(pos);
        return new GuiConnector((ConnectorTileEntity) te, new EmptyContainer(entityPlayer, null));
    }
    return null;
}
Also used : GuiXNetManual(mcjty.xnet.items.manual.GuiXNetManual) GenericBlock(mcjty.lib.container.GenericBlock) ConnectorTileEntity(mcjty.xnet.blocks.cables.ConnectorTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) EmptyContainer(mcjty.lib.container.EmptyContainer) GuiConnector(mcjty.xnet.blocks.cables.GuiConnector) ConnectorTileEntity(mcjty.xnet.blocks.cables.ConnectorTileEntity) GenericBlock(mcjty.lib.container.GenericBlock) Block(net.minecraft.block.Block) ConnectorBlock(mcjty.xnet.blocks.cables.ConnectorBlock) BlockPos(net.minecraft.util.math.BlockPos) ConnectorBlock(mcjty.xnet.blocks.cables.ConnectorBlock)

Example 5 with ConnectorTileEntity

use of mcjty.xnet.blocks.cables.ConnectorTileEntity 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)

Aggregations

ConnectorTileEntity (mcjty.xnet.blocks.cables.ConnectorTileEntity)7 TileEntity (net.minecraft.tileentity.TileEntity)7 BlockPos (net.minecraft.util.math.BlockPos)5 EnumFacing (net.minecraft.util.EnumFacing)4 WorldBlob (mcjty.xnet.multiblock.WorldBlob)3 Block (net.minecraft.block.Block)3 IBlockState (net.minecraft.block.state.IBlockState)3 GenericEnergyReceiverTileEntity (mcjty.lib.entity.GenericEnergyReceiverTileEntity)2 SidedConsumer (mcjty.xnet.api.keys.SidedConsumer)2 SidedPos (mcjty.xnet.api.keys.SidedPos)2 ConnectorBlock (mcjty.xnet.blocks.cables.ConnectorBlock)2 GenericCableBlock (mcjty.xnet.blocks.generic.GenericCableBlock)2 ConnectedBlockClientInfo (mcjty.xnet.clientinfo.ConnectedBlockClientInfo)2 ItemStack (net.minecraft.item.ItemStack)2 SPacketUpdateTileEntity (net.minecraft.network.play.server.SPacketUpdateTileEntity)2 TextComponentString (net.minecraft.util.text.TextComponentString)2 World (net.minecraft.world.World)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Nonnull (javax.annotation.Nonnull)1