Search in sources :

Example 6 with IImmersiveConnectable

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

the class TileEntityConnectorLV method transferEnergy.

public int transferEnergy(int energy, boolean simulate, final int energyType) {
    int received = 0;
    if (!worldObj.isRemote) {
        Set<AbstractConnection> outputs = ImmersiveNetHandler.INSTANCE.getIndirectEnergyConnections(Utils.toCC(this), worldObj);
        int powerLeft = Math.min(Math.min(getMaxOutput(), getMaxInput()), energy);
        final int powerForSort = powerLeft;
        if (outputs.size() < 1)
            return 0;
        int sum = 0;
        HashMap<AbstractConnection, Integer> powerSorting = new HashMap<AbstractConnection, Integer>();
        for (AbstractConnection con : outputs) {
            IImmersiveConnectable end = ApiUtils.toIIC(con.end, worldObj);
            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, worldObj);
                if (con.cableType != null && end != null) {
                    float prio = powerSorting.get(con) / (float) sum;
                    int output = (int) (powerForSort * prio);
                    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;
                    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_float(intermediaryLoss + length * (baseLoss + baseLoss * mod), 0, 1);
                        int transferredPerCon = ImmersiveNetHandler.INSTANCE.getTransferedRates(worldObj.provider.getDimension()).containsKey(sub) ? ImmersiveNetHandler.INSTANCE.getTransferedRates(worldObj.provider.getDimension()).get(sub) : 0;
                        transferredPerCon += r;
                        if (!simulate) {
                            ImmersiveNetHandler.INSTANCE.getTransferedRates(worldObj.provider.getDimension()).put(sub, transferredPerCon);
                            IImmersiveConnectable subStart = ApiUtils.toIIC(sub.start, worldObj);
                            IImmersiveConnectable subEnd = ApiUtils.toIIC(sub.end, worldObj);
                            if (subStart != null && passedConnectors.add(subStart))
                                subStart.onEnergyPassthrough((int) (r - r * intermediaryLoss));
                            if (subEnd != null && passedConnectors.add(subEnd))
                                subEnd.onEnergyPassthrough((int) (r - r * intermediaryLoss));
                        }
                    }
                    received += r;
                    powerLeft -= r;
                    if (powerLeft <= 0)
                        break;
                }
            }
    }
    return received;
}
Also used : HashMap(java.util.HashMap) 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) HashSet(java.util.HashSet)

Example 7 with IImmersiveConnectable

use of blusunrize.immersiveengineering.api.energy.wires.IImmersiveConnectable 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 8 with IImmersiveConnectable

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

the class EntitySkylineHook method onUpdate.

@Override
public void onUpdate() {
    EntityPlayer player = null;
    //		if(this.getControllingPassenger() instanceof EntityPlayer)
    //			player = ((EntityPlayer)this.getControllingPassenger());
    List<Entity> list = this.getPassengers();
    if (!list.isEmpty() && list.get(0) instanceof EntityPlayer)
        player = (EntityPlayer) list.get(0);
    if (this.ticksExisted == 1 && !worldObj.isRemote) {
        IELogger.debug("init tick at " + System.currentTimeMillis());
        if (player instanceof EntityPlayerMP)
            ImmersiveEngineering.packetHandler.sendTo(new MessageSkyhookSync(this), (EntityPlayerMP) player);
    }
    super.onUpdate();
    //			return;
    if (subPoints != null && targetPoint < subPoints.length - 1) {
        double dist = subPoints[targetPoint].distanceTo(new Vec3d(posX, posY, posZ));
        IELogger.debug("dist: " + dist);
        if (dist <= 0) {
            this.posX = subPoints[targetPoint].xCoord;
            this.posY = subPoints[targetPoint].yCoord;
            this.posZ = subPoints[targetPoint].zCoord;
            targetPoint++;
            if (player instanceof EntityPlayerMP)
                ImmersiveEngineering.packetHandler.sendTo(new MessageSkyhookSync(this), (EntityPlayerMP) player);
            IELogger.debug("next vertex: " + targetPoint);
            return;
        }
        float speed = 2f;
        if (player != null && player.getActiveItemStack() != null && player.getActiveItemStack().getItem() instanceof ItemSkyhook)
            speed = ((ItemSkyhook) player.getActiveItemStack().getItem()).getSkylineSpeed(player.getActiveItemStack());
        Vec3d moveVec = SkylineHelper.getSubMovementVector(new Vec3d(posX, posY, posZ), subPoints[targetPoint], speed);
        //*speed;
        motionX = moveVec.xCoord;
        //*speed;
        motionY = moveVec.yCoord;
        //*speed;
        motionZ = moveVec.zCoord;
    }
    if (target != null && targetPoint == subPoints.length - 1) {
        TileEntity end = this.worldObj.getTileEntity(target);
        IImmersiveConnectable iicEnd = ApiUtils.toIIC(end, worldObj);
        if (iicEnd == null) {
            this.setDead();
            return;
        }
        Vec3d vEnd = new Vec3d(target.getX(), target.getY(), target.getZ());
        vEnd = Utils.addVectors(vEnd, iicEnd.getConnectionOffset(connection));
        double gDist = vEnd.distanceTo(new Vec3d(posX, posY, posZ));
        IELogger.debug("distance to goal: " + gDist);
        if (gDist <= .3) {
            reachedTarget(end);
            return;
        } else if (gDist > 5) {
            setDead();
            return;
        }
    }
    this.posX += this.motionX;
    this.posY += this.motionY;
    this.posZ += this.motionZ;
    float f1 = MathHelper.sqrt_double(this.motionX * this.motionX + this.motionZ * this.motionZ);
    this.rotationYaw = (float) (Math.atan2(this.motionZ, this.motionX) * 180.0D / Math.PI) + 90.0F;
    for (this.rotationPitch = (float) (Math.atan2((double) f1, this.motionY) * 180.0D / Math.PI) - 90.0F; this.rotationPitch - this.prevRotationPitch < -180.0F; this.prevRotationPitch -= 360.0F) ;
    while (this.rotationPitch - this.prevRotationPitch >= 180.0F) this.prevRotationPitch += 360.0F;
    while (this.rotationYaw - this.prevRotationYaw < -180.0F) this.prevRotationYaw -= 360.0F;
    while (this.rotationYaw - this.prevRotationYaw >= 180.0F) this.prevRotationYaw += 360.0F;
    this.rotationPitch = this.prevRotationPitch + (this.rotationPitch - this.prevRotationPitch) * 0.2F;
    this.rotationYaw = this.prevRotationYaw + (this.rotationYaw - this.prevRotationYaw) * 0.2F;
    if (this.isInWater()) {
        for (int j = 0; j < 4; ++j) {
            float f3 = 0.25F;
            this.worldObj.spawnParticle(EnumParticleTypes.WATER_BUBBLE, this.posX - this.motionX * (double) f3, this.posY - this.motionY * (double) f3, this.posZ - this.motionZ * (double) f3, this.motionX, this.motionY, this.motionZ);
        }
    }
    if (player != null) {
        double dx = this.posX - this.prevPosX;
        double dy = this.posY - this.prevPosY;
        double dz = this.posZ - this.prevPosZ;
        int distTrvl = Math.round(MathHelper.sqrt_double(dx * dx + dy * dy + dz * dz) * 100.0F);
        if (distTrvl > 0)
            player.addStat(IEAchievements.statDistanceSkyhook, distTrvl);
        if (!worldObj.isRemote && SkylineHelper.isInBlock(player, worldObj)) {
        //				setDead();
        //				player.setPosition(posX-3*dx, posY-3*dy+getMountedYOffset(),posZ-3*dz);
        }
    //TODO
    //			if(player instanceof EntityPlayerMP)
    //				if(((EntityPlayerMP)player).getStatFile().func_150870_b(IEAchievements.statDistanceSkyhook)>100000)
    //					player.triggerAchievement(IEAchievements.skyhookPro);
    }
    this.setPosition(this.posX, this.posY, this.posZ);
}
Also used : MessageSkyhookSync(blusunrize.immersiveengineering.common.util.network.MessageSkyhookSync) TileEntity(net.minecraft.tileentity.TileEntity) Entity(net.minecraft.entity.Entity) TileEntity(net.minecraft.tileentity.TileEntity) ItemSkyhook(blusunrize.immersiveengineering.common.items.ItemSkyhook) EntityPlayer(net.minecraft.entity.player.EntityPlayer) IImmersiveConnectable(blusunrize.immersiveengineering.api.energy.wires.IImmersiveConnectable) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) Vec3d(net.minecraft.util.math.Vec3d)

Example 9 with IImmersiveConnectable

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

the class BlockIETileProvider method breakBlock.

@Override
public void breakBlock(World world, BlockPos pos, IBlockState state) {
    TileEntity tile = world.getTileEntity(pos);
    if (tile != null && (!(tile instanceof ITileDrop) || !((ITileDrop) tile).preventInventoryDrop())) {
        if (tile instanceof IIEInventory && ((IIEInventory) tile).getDroppedItems() != null) {
            for (ItemStack s : ((IIEInventory) tile).getDroppedItems()) if (s != null)
                spawnAsEntity(world, pos, s);
        } else if (tile.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null)) {
            IItemHandler h = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
            if (h instanceof IEInventoryHandler)
                for (int i = 0; i < h.getSlots(); i++) if (h.getStackInSlot(i) != null) {
                    spawnAsEntity(world, pos, h.getStackInSlot(i));
                    ((IEInventoryHandler) h).setStackInSlot(i, null);
                }
        }
    }
    if (tile instanceof IHasDummyBlocks) {
        ((IHasDummyBlocks) tile).breakDummies(pos, state);
    }
    if (tile instanceof IImmersiveConnectable)
        if (!world.isRemote || !Minecraft.getMinecraft().isSingleplayer())
            ImmersiveNetHandler.INSTANCE.clearAllConnectionsFor(Utils.toCC(tile), world, !world.isRemote && world.getGameRules().getBoolean("doTileDrops"));
    super.breakBlock(world, pos, state);
    world.removeTileEntity(pos);
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IEInventoryHandler(blusunrize.immersiveengineering.common.util.inventory.IEInventoryHandler) IItemHandler(net.minecraftforge.items.IItemHandler) IImmersiveConnectable(blusunrize.immersiveengineering.api.energy.wires.IImmersiveConnectable) IIEInventory(blusunrize.immersiveengineering.common.util.inventory.IIEInventory) ItemStack(net.minecraft.item.ItemStack)

Aggregations

IImmersiveConnectable (blusunrize.immersiveengineering.api.energy.wires.IImmersiveConnectable)9 Connection (blusunrize.immersiveengineering.api.energy.wires.ImmersiveNetHandler.Connection)5 TileEntity (net.minecraft.tileentity.TileEntity)5 Vec3d (net.minecraft.util.math.Vec3d)5 BlockPos (net.minecraft.util.math.BlockPos)4 TargetingInfo (blusunrize.immersiveengineering.api.TargetingInfo)2 AbstractConnection (blusunrize.immersiveengineering.api.energy.wires.ImmersiveNetHandler.AbstractConnection)2 ItemSkyhook (blusunrize.immersiveengineering.common.items.ItemSkyhook)2 HashSet (java.util.HashSet)2 IBlockState (net.minecraft.block.state.IBlockState)2 TextComponentTranslation (net.minecraft.util.text.TextComponentTranslation)2 DimensionBlockPos (blusunrize.immersiveengineering.api.DimensionBlockPos)1 IMultiblock (blusunrize.immersiveengineering.api.MultiblockHandler.IMultiblock)1 IFluxProvider (blusunrize.immersiveengineering.api.energy.immersiveflux.IFluxProvider)1 IFluxReceiver (blusunrize.immersiveengineering.api.energy.immersiveflux.IFluxReceiver)1 IICProxy (blusunrize.immersiveengineering.api.energy.wires.IICProxy)1 WireType (blusunrize.immersiveengineering.api.energy.wires.WireType)1 IConfigurableSides (blusunrize.immersiveengineering.common.blocks.IEBlockInterfaces.IConfigurableSides)1 IDirectionalTile (blusunrize.immersiveengineering.common.blocks.IEBlockInterfaces.IDirectionalTile)1 IHammerInteraction (blusunrize.immersiveengineering.common.blocks.IEBlockInterfaces.IHammerInteraction)1