Search in sources :

Example 1 with AbstractConnection

use of blusunrize.immersiveengineering.api.energy.wires.ImmersiveNetHandler.AbstractConnection in project ImmersiveEngineering by BluSunrize.

the class ItemIETool method onItemUseFirst.

//	@Override
//	public boolean doesContainerItemLeaveCraftingGrid(ItemStack stack)
//	{
//		return stack.getItemDamage()!=0;
//	}
@Override
public EnumActionResult onItemUseFirst(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ, EnumHand hand) {
    TileEntity tileEntity = world.getTileEntity(pos);
    if (stack.getItemDamage() == 0) {
        if (FMLCommonHandler.instance().getEffectiveSide() == Side.CLIENT)
            return EnumActionResult.PASS;
        String[] permittedMultiblocks = null;
        String[] interdictedMultiblocks = null;
        if (ItemNBTHelper.hasKey(stack, "multiblockPermission")) {
            NBTTagList list = stack.getTagCompound().getTagList("multiblockPermission", 8);
            permittedMultiblocks = new String[list.tagCount()];
            for (int i = 0; i < permittedMultiblocks.length; i++) permittedMultiblocks[i] = list.getStringTagAt(i);
        }
        if (ItemNBTHelper.hasKey(stack, "multiblockInterdiction")) {
            NBTTagList list = stack.getTagCompound().getTagList("multiblockInterdiction", 8);
            interdictedMultiblocks = new String[list.tagCount()];
            for (int i = 0; i < interdictedMultiblocks.length; i++) interdictedMultiblocks[i] = list.getStringTagAt(i);
        }
        for (IMultiblock mb : MultiblockHandler.getMultiblocks()) if (mb.isBlockTrigger(world.getBlockState(pos))) {
            boolean b = permittedMultiblocks == null;
            if (permittedMultiblocks != null)
                for (String s : permittedMultiblocks) if (mb.getUniqueName().equalsIgnoreCase(s)) {
                    b = true;
                    continue;
                }
            if (!b)
                break;
            if (interdictedMultiblocks != null)
                for (String s : interdictedMultiblocks) if (mb.getUniqueName().equalsIgnoreCase(s)) {
                    b = false;
                    continue;
                }
            if (!b)
                break;
            if (MultiblockHandler.postMultiblockFormationEvent(player, mb, pos, stack).isCanceled())
                continue;
            if (mb.createStructure(world, pos, side, player))
                return EnumActionResult.SUCCESS;
        }
        TileEntity tile = world.getTileEntity(pos);
        if (!(tile instanceof IDirectionalTile) && !(tile instanceof IHammerInteraction) && !(tile instanceof IConfigurableSides))
            return RotationUtil.rotateBlock(world, pos, side) ? EnumActionResult.SUCCESS : EnumActionResult.PASS;
    } else if (stack.getItemDamage() == 1 && tileEntity instanceof IImmersiveConnectable && !world.isRemote) {
        TargetingInfo target = new TargetingInfo(side, hitX, hitY, hitZ);
        BlockPos masterPos = ((IImmersiveConnectable) tileEntity).getConnectionMaster(null, target);
        tileEntity = world.getTileEntity(masterPos);
        if (!(tileEntity instanceof IImmersiveConnectable))
            return EnumActionResult.PASS;
        IImmersiveConnectable nodeHere = (IImmersiveConnectable) tileEntity;
        boolean cut = ImmersiveNetHandler.INSTANCE.clearAllConnectionsFor(Utils.toCC(nodeHere), world, target);
        IESaveData.setDirty(world.provider.getDimension());
        if (cut) {
            int nbtDamage = ItemNBTHelper.getInt(stack, "cutterDmg") + 1;
            if (nbtDamage < cutterMaxDamage)
                ItemNBTHelper.setInt(stack, "cutterDmg", nbtDamage);
            else {
                player.renderBrokenItemStack(stack);
                player.setItemStackToSlot(EntityEquipmentSlot.MAINHAND, null);
            }
        }
        return EnumActionResult.SUCCESS;
    } else if (stack.getItemDamage() == 2 && !world.isRemote) {
        if (!player.isSneaking() && (tileEntity instanceof IFluxReceiver || tileEntity instanceof IFluxProvider)) {
            int max = 0;
            int stored = 0;
            if (tileEntity instanceof IFluxReceiver) {
                max = ((IFluxReceiver) tileEntity).getMaxEnergyStored(side);
                stored = ((IFluxReceiver) tileEntity).getEnergyStored(side);
            } else {
                max = ((IFluxProvider) tileEntity).getMaxEnergyStored(side);
                stored = ((IFluxProvider) tileEntity).getEnergyStored(side);
            }
            if (max > 0)
                ChatUtils.sendServerNoSpamMessages(player, new TextComponentTranslation(Lib.CHAT_INFO + "energyStorage", stored, max));
            return EnumActionResult.SUCCESS;
        }
        if (player.isSneaking() && tileEntity instanceof IImmersiveConnectable) {
            if (!ItemNBTHelper.hasKey(stack, "linkingPos"))
                ItemNBTHelper.setIntArray(stack, "linkingPos", new int[] { world.provider.getDimension(), pos.getX(), pos.getY(), pos.getZ() });
            else {
                int[] array = ItemNBTHelper.getIntArray(stack, "linkingPos");
                BlockPos linkPos = new BlockPos(array[1], array[2], array[3]);
                TileEntity tileEntityLinkingPos = world.getTileEntity(linkPos);
                if (array[0] == world.provider.getDimension()) {
                    IImmersiveConnectable nodeHere = (IImmersiveConnectable) tileEntity;
                    IImmersiveConnectable nodeLink = (IImmersiveConnectable) tileEntityLinkingPos;
                    if (nodeLink != null) {
                        Set<AbstractConnection> connections = ImmersiveNetHandler.INSTANCE.getIndirectEnergyConnections(Utils.toCC(nodeLink), world, true);
                        for (AbstractConnection con : connections) if (Utils.toCC(nodeHere).equals(con.end))
                            player.addChatComponentMessage(new TextComponentTranslation(Lib.CHAT_INFO + "averageLoss", Utils.formatDouble(con.getAverageLossRate() * 100, "###.000")));
                    }
                }
                ItemNBTHelper.remove(stack, "linkingPos");
            }
            return EnumActionResult.SUCCESS;
        }
    }
    return EnumActionResult.PASS;
}
Also used : IHammerInteraction(blusunrize.immersiveengineering.common.blocks.IEBlockInterfaces.IHammerInteraction) TextComponentTranslation(net.minecraft.util.text.TextComponentTranslation) IImmersiveConnectable(blusunrize.immersiveengineering.api.energy.wires.IImmersiveConnectable) IFluxProvider(blusunrize.immersiveengineering.api.energy.immersiveflux.IFluxProvider) IDirectionalTile(blusunrize.immersiveengineering.common.blocks.IEBlockInterfaces.IDirectionalTile) TileEntity(net.minecraft.tileentity.TileEntity) NBTTagList(net.minecraft.nbt.NBTTagList) TargetingInfo(blusunrize.immersiveengineering.api.TargetingInfo) IMultiblock(blusunrize.immersiveengineering.api.MultiblockHandler.IMultiblock) IFluxReceiver(blusunrize.immersiveengineering.api.energy.immersiveflux.IFluxReceiver) AbstractConnection(blusunrize.immersiveengineering.api.energy.wires.ImmersiveNetHandler.AbstractConnection) IConfigurableSides(blusunrize.immersiveengineering.common.blocks.IEBlockInterfaces.IConfigurableSides) BlockPos(net.minecraft.util.math.BlockPos)

Example 2 with AbstractConnection

use of blusunrize.immersiveengineering.api.energy.wires.ImmersiveNetHandler.AbstractConnection in project ImmersiveEngineering by BluSunrize.

the class TileEntityConnectorLV method transferEnergy.

public int transferEnergy(int energy, boolean simulate, final int energyType) {
    int received = 0;
    if (!world.isRemote) {
        Set<AbstractConnection> outputs = ImmersiveNetHandler.INSTANCE.getIndirectEnergyConnections(Utils.toCC(this), world, true);
        int powerLeft = Math.min(Math.min(getMaxOutput(), getMaxInput()), energy);
        final int powerForSort = powerLeft;
        if (outputs.isEmpty())
            return 0;
        int sum = 0;
        // TreeMap to prioritize outputs close to this connector if more energy is requested than available
        // (energy will be provided to the nearby outputs rather than some random ones)
        Map<AbstractConnection, Integer> powerSorting = new TreeMap<>();
        for (AbstractConnection con : outputs) if (con.isEnergyOutput) {
            IImmersiveConnectable end = ApiUtils.toIIC(con.end, world);
            if (con.cableType != null && end != null) {
                int atmOut = Math.min(powerForSort, con.cableType.getTransferRate());
                int tempR = end.outputEnergy(atmOut, true, energyType);
                if (tempR > 0) {
                    powerSorting.put(con, tempR);
                    sum += tempR;
                }
            }
        }
        if (sum > 0)
            for (AbstractConnection con : powerSorting.keySet()) {
                IImmersiveConnectable end = ApiUtils.toIIC(con.end, world);
                if (con.cableType != null && end != null) {
                    float prio = powerSorting.get(con) / (float) sum;
                    int output = Math.min(MathHelper.ceil(powerForSort * prio), powerLeft);
                    int tempR = end.outputEnergy(Math.min(output, con.cableType.getTransferRate()), true, energyType);
                    int r = tempR;
                    int maxInput = getMaxInput();
                    tempR -= (int) Math.max(0, Math.floor(tempR * con.getPreciseLossRate(tempR, maxInput)));
                    end.outputEnergy(tempR, simulate, energyType);
                    HashSet<IImmersiveConnectable> passedConnectors = new HashSet<IImmersiveConnectable>();
                    float intermediaryLoss = 0;
                    // <editor-fold desc="Transfer rate and passed energy">
                    for (Connection sub : con.subConnections) {
                        float length = sub.length / (float) sub.cableType.getMaxLength();
                        float baseLoss = (float) sub.cableType.getLossRatio();
                        float mod = (((maxInput - tempR) / (float) maxInput) / .25f) * .1f;
                        intermediaryLoss = MathHelper.clamp(intermediaryLoss + length * (baseLoss + baseLoss * mod), 0, 1);
                        int transferredPerCon = ImmersiveNetHandler.INSTANCE.getTransferedRates(world.provider.getDimension()).getOrDefault(sub, 0);
                        transferredPerCon += r;
                        if (!simulate) {
                            ImmersiveNetHandler.INSTANCE.getTransferedRates(world.provider.getDimension()).put(sub, transferredPerCon);
                            IImmersiveConnectable subStart = ApiUtils.toIIC(sub.start, world);
                            IImmersiveConnectable subEnd = ApiUtils.toIIC(sub.end, world);
                            if (subStart != null && passedConnectors.add(subStart))
                                subStart.onEnergyPassthrough(r - r * intermediaryLoss);
                            if (subEnd != null && passedConnectors.add(subEnd))
                                subEnd.onEnergyPassthrough(r - r * intermediaryLoss);
                        }
                    }
                    // </editor-fold>
                    received += r;
                    powerLeft -= r;
                    if (powerLeft <= 0)
                        break;
                }
            }
    }
    return received;
}
Also used : AbstractConnection(blusunrize.immersiveengineering.api.energy.wires.ImmersiveNetHandler.AbstractConnection) Connection(blusunrize.immersiveengineering.api.energy.wires.ImmersiveNetHandler.Connection) AbstractConnection(blusunrize.immersiveengineering.api.energy.wires.ImmersiveNetHandler.AbstractConnection) IImmersiveConnectable(blusunrize.immersiveengineering.api.energy.wires.IImmersiveConnectable) TreeMap(java.util.TreeMap) HashSet(java.util.HashSet)

Example 3 with AbstractConnection

use of blusunrize.immersiveengineering.api.energy.wires.ImmersiveNetHandler.AbstractConnection in project ImmersiveEngineering by BluSunrize.

the class TileEntityConnectorLV method notifyAvailableEnergy.

private void notifyAvailableEnergy(int energyStored, @Nullable Set<AbstractConnection> outputs) {
    if (outputs == null)
        outputs = ImmersiveNetHandler.INSTANCE.getIndirectEnergyConnections(pos, world, true);
    for (AbstractConnection con : outputs) {
        IImmersiveConnectable end = ApiUtils.toIIC(con.end, world);
        if (con.cableType != null && end != null && end.allowEnergyToPass(null)) {
            Pair<Float, Consumer<Float>> e = getEnergyForConnection(con);
            end.addAvailableEnergy(e.getKey(), e.getValue());
        }
    }
}
Also used : Consumer(java.util.function.Consumer) AbstractConnection(blusunrize.immersiveengineering.api.energy.wires.ImmersiveNetHandler.AbstractConnection) IImmersiveConnectable(blusunrize.immersiveengineering.api.energy.wires.IImmersiveConnectable)

Example 4 with AbstractConnection

use of blusunrize.immersiveengineering.api.energy.wires.ImmersiveNetHandler.AbstractConnection in project ImmersiveEngineering by BluSunrize.

the class ItemIETool method onItemUse.

@Nonnull
@Override
public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
    ItemStack stack = player.getHeldItem(hand);
    TileEntity tileEntity = world.getTileEntity(pos);
    if (stack.getMetadata() == HAMMER_META) {
        if (!(tileEntity instanceof IDirectionalTile) && !(tileEntity instanceof IHammerInteraction) && !(tileEntity instanceof IConfigurableSides))
            return RotationUtil.rotateBlock(world, pos, side) ? EnumActionResult.SUCCESS : EnumActionResult.PASS;
    } else if (stack.getMetadata() == CUTTER_META && tileEntity instanceof IImmersiveConnectable) {
        TargetingInfo target = new TargetingInfo(side, hitX, hitY, hitZ);
        BlockPos masterPos = ((IImmersiveConnectable) tileEntity).getConnectionMaster(null, target);
        tileEntity = world.getTileEntity(masterPos);
        if (!(tileEntity instanceof IImmersiveConnectable))
            return EnumActionResult.PASS;
        if (!world.isRemote) {
            IImmersiveConnectable nodeHere = (IImmersiveConnectable) tileEntity;
            boolean cut = ImmersiveNetHandler.INSTANCE.clearAllConnectionsFor(Utils.toCC(nodeHere), world, target);
            IESaveData.setDirty(world.provider.getDimension());
            if (cut) {
                int nbtDamage = ItemNBTHelper.getInt(stack, Lib.NBT_DAMAGE) + 1;
                if (nbtDamage < cutterDurabiliy)
                    ItemNBTHelper.setInt(stack, Lib.NBT_DAMAGE, nbtDamage);
                else {
                    player.renderBrokenItemStack(stack);
                    player.setItemStackToSlot(EntityEquipmentSlot.MAINHAND, ItemStack.EMPTY);
                }
            }
        }
        return EnumActionResult.SUCCESS;
    } else if (stack.getMetadata() == VOLTMETER_META && !world.isRemote) {
        if (!player.isSneaking() && (tileEntity instanceof IFluxReceiver || tileEntity instanceof IFluxProvider)) {
            int max = 0;
            int stored = 0;
            if (tileEntity instanceof IFluxReceiver) {
                max = ((IFluxReceiver) tileEntity).getMaxEnergyStored(side);
                stored = ((IFluxReceiver) tileEntity).getEnergyStored(side);
            } else {
                max = ((IFluxProvider) tileEntity).getMaxEnergyStored(side);
                stored = ((IFluxProvider) tileEntity).getEnergyStored(side);
            }
            if (max > 0)
                ChatUtils.sendServerNoSpamMessages(player, new TextComponentTranslation(Lib.CHAT_INFO + "energyStorage", stored, max));
            return EnumActionResult.SUCCESS;
        }
        if (player.isSneaking() && tileEntity instanceof IImmersiveConnectable) {
            if (!ItemNBTHelper.hasKey(stack, "linkingPos"))
                ItemNBTHelper.setIntArray(stack, "linkingPos", new int[] { world.provider.getDimension(), pos.getX(), pos.getY(), pos.getZ() });
            else {
                int[] array = ItemNBTHelper.getIntArray(stack, "linkingPos");
                BlockPos linkPos = new BlockPos(array[1], array[2], array[3]);
                TileEntity tileEntityLinkingPos = world.getTileEntity(linkPos);
                if (array[0] == world.provider.getDimension()) {
                    IImmersiveConnectable nodeHere = (IImmersiveConnectable) tileEntity;
                    IImmersiveConnectable nodeLink = (IImmersiveConnectable) tileEntityLinkingPos;
                    if (nodeLink != null) {
                        Set<AbstractConnection> connections = ImmersiveNetHandler.INSTANCE.getIndirectEnergyConnections(Utils.toCC(nodeLink), world, true);
                        for (AbstractConnection con : connections) if (Utils.toCC(nodeHere).equals(con.end))
                            player.sendMessage(new TextComponentTranslation(Lib.CHAT_INFO + "averageLoss", Utils.formatDouble(con.getAverageLossRate() * 100, "###.000")));
                    }
                }
                ItemNBTHelper.remove(stack, "linkingPos");
            }
            return EnumActionResult.SUCCESS;
        }
    }
    return EnumActionResult.PASS;
}
Also used : IHammerInteraction(blusunrize.immersiveengineering.common.blocks.IEBlockInterfaces.IHammerInteraction) TextComponentTranslation(net.minecraft.util.text.TextComponentTranslation) IImmersiveConnectable(blusunrize.immersiveengineering.api.energy.wires.IImmersiveConnectable) IFluxProvider(blusunrize.immersiveengineering.api.energy.immersiveflux.IFluxProvider) IDirectionalTile(blusunrize.immersiveengineering.common.blocks.IEBlockInterfaces.IDirectionalTile) TileEntity(net.minecraft.tileentity.TileEntity) TargetingInfo(blusunrize.immersiveengineering.api.TargetingInfo) IFluxReceiver(blusunrize.immersiveengineering.api.energy.immersiveflux.IFluxReceiver) AbstractConnection(blusunrize.immersiveengineering.api.energy.wires.ImmersiveNetHandler.AbstractConnection) IConfigurableSides(blusunrize.immersiveengineering.common.blocks.IEBlockInterfaces.IConfigurableSides) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack) Nonnull(javax.annotation.Nonnull)

Aggregations

IImmersiveConnectable (blusunrize.immersiveengineering.api.energy.wires.IImmersiveConnectable)4 AbstractConnection (blusunrize.immersiveengineering.api.energy.wires.ImmersiveNetHandler.AbstractConnection)4 TargetingInfo (blusunrize.immersiveengineering.api.TargetingInfo)2 IFluxProvider (blusunrize.immersiveengineering.api.energy.immersiveflux.IFluxProvider)2 IFluxReceiver (blusunrize.immersiveengineering.api.energy.immersiveflux.IFluxReceiver)2 IConfigurableSides (blusunrize.immersiveengineering.common.blocks.IEBlockInterfaces.IConfigurableSides)2 IDirectionalTile (blusunrize.immersiveengineering.common.blocks.IEBlockInterfaces.IDirectionalTile)2 IHammerInteraction (blusunrize.immersiveengineering.common.blocks.IEBlockInterfaces.IHammerInteraction)2 TileEntity (net.minecraft.tileentity.TileEntity)2 BlockPos (net.minecraft.util.math.BlockPos)2 TextComponentTranslation (net.minecraft.util.text.TextComponentTranslation)2 IMultiblock (blusunrize.immersiveengineering.api.MultiblockHandler.IMultiblock)1 Connection (blusunrize.immersiveengineering.api.energy.wires.ImmersiveNetHandler.Connection)1 HashSet (java.util.HashSet)1 TreeMap (java.util.TreeMap)1 Consumer (java.util.function.Consumer)1 Nonnull (javax.annotation.Nonnull)1 ItemStack (net.minecraft.item.ItemStack)1 NBTTagList (net.minecraft.nbt.NBTTagList)1