Ejemplo n.º 1
0
 /**
  * Whether or not the declared Tile Entity is an instance of a BuildCraft power receptor.
  *
  * @param tileEntity - tile entity to check
  * @return if the tile entity is a power receptor
  */
 public boolean isPowerReceptor(TileEntity tileEntity) {
   if (tileEntity instanceof IPowerReceptor) {
     IPowerReceptor receptor = (IPowerReceptor) tileEntity;
     IPowerProvider provider = receptor.getPowerProvider();
     return provider != null && provider.getClass().getSuperclass().equals(PowerProvider.class);
   }
   return false;
 }
Ejemplo n.º 2
0
  @Override
  public void onUpdate() {
    super.onUpdate();

    if (powerProvider != null) {
      int received =
          (int)
              (powerProvider.useEnergy(
                      0,
                      (float) ((tier.MAX_ELECTRICITY - electricityStored) * Mekanism.TO_BC),
                      true)
                  * Mekanism.FROM_BC);
      setJoules(electricityStored + received);
    }

    if (inventory[0] != null && electricityStored > 0) {
      setJoules(
          getJoules() - ElectricItemHelper.chargeItem(inventory[0], getJoules(), getVoltage()));

      if (Mekanism.hooks.IC2Loaded && inventory[0].getItem() instanceof IElectricItem) {
        double sent =
            ElectricItem.charge(
                    inventory[0], (int) (electricityStored * Mekanism.TO_IC2), 3, false, false)
                * Mekanism.FROM_IC2;
        setJoules(electricityStored - sent);
      }
    }

    if (inventory[1] != null && electricityStored < tier.MAX_ELECTRICITY) {
      setJoules(
          getJoules()
              + ElectricItemHelper.dechargeItem(
                  inventory[1], getMaxJoules() - getJoules(), getVoltage()));

      if (Mekanism.hooks.IC2Loaded && inventory[1].getItem() instanceof IElectricItem) {
        IElectricItem item = (IElectricItem) inventory[1].getItem();
        if (item.canProvideEnergy()) {
          double gain =
              ElectricItem.discharge(
                      inventory[1],
                      (int) ((tier.MAX_ELECTRICITY - electricityStored) * Mekanism.TO_IC2),
                      3,
                      false,
                      false)
                  * Mekanism.FROM_IC2;
          setJoules(electricityStored + gain);
        }
      } else if (inventory[1].itemID == Item.redstone.itemID
          && electricityStored + 1000 <= tier.MAX_ELECTRICITY) {
        setJoules(electricityStored + 1000);
        --inventory[1].stackSize;

        if (inventory[1].stackSize <= 0) {
          inventory[1] = null;
        }
      }
    }

    if (electricityStored > 0) {
      TileEntity tileEntity =
          VectorHelper.getTileEntityFromSide(
              worldObj, new Vector3(this), ForgeDirection.getOrientation(facing));

      if (Mekanism.hooks.IC2Loaded) {
        if (electricityStored >= output) {
          EnergyTileSourceEvent event = new EnergyTileSourceEvent(this, output);
          MinecraftForge.EVENT_BUS.post(event);
          setJoules(electricityStored - (output - event.amount));
        }
      }

      if (tileEntity != null) {
        if (isPowerReceptor(tileEntity)) {
          IPowerReceptor receptor = (IPowerReceptor) tileEntity;
          double electricityNeeded =
              Math.min(
                      receptor.powerRequest(),
                      receptor.getPowerProvider().getMaxEnergyStored()
                          - receptor.getPowerProvider().getEnergyStored())
                  * Mekanism.FROM_BC;
          float transferEnergy =
              (float) Math.min(electricityStored, Math.min(electricityNeeded, output));
          receptor
              .getPowerProvider()
              .receiveEnergy(
                  (float) (transferEnergy * Mekanism.TO_BC),
                  ForgeDirection.getOrientation(facing).getOpposite());
          setJoules(electricityStored - transferEnergy);
        }
      }
    }

    if (!worldObj.isRemote) {
      ForgeDirection outputDirection = ForgeDirection.getOrientation(facing);
      ArrayList<IElectricityNetwork> inputNetworks = new ArrayList<IElectricityNetwork>();

      for (ForgeDirection direction : ForgeDirection.VALID_DIRECTIONS) {
        if (direction != outputDirection) {
          IElectricityNetwork network =
              ElectricityNetworkHelper.getNetworkFromTileEntity(
                  VectorHelper.getTileEntityFromSide(worldObj, new Vector3(this), direction),
                  direction);
          if (network != null) {
            inputNetworks.add(network);
          }
        }
      }

      TileEntity outputTile =
          VectorHelper.getTileEntityFromSide(worldObj, new Vector3(this), outputDirection);

      IElectricityNetwork outputNetwork =
          ElectricityNetworkHelper.getNetworkFromTileEntity(outputTile, outputDirection);

      if (outputNetwork != null && !inputNetworks.contains(outputNetwork)) {
        double outputWatts =
            Math.min(outputNetwork.getRequest().getWatts(), Math.min(getJoules(), 10000));

        if (getJoules() > 0 && outputWatts > 0 && getJoules() - outputWatts >= 0) {
          outputNetwork.startProducing(
              this, Math.min(outputWatts, getJoules()) / getVoltage(), getVoltage());
          setJoules(electricityStored - outputWatts);
        } else {
          outputNetwork.stopProducing(this);
        }
      }
    }
  }
  @Override
  public void updateEntity() {
    if (CoreProxy.proxy.isRenderWorld(worldObj)) return;

    step();

    init();

    // Send the power to nearby pipes who requested it

    Arrays.fill(displayPower, (short) 0);

    for (int i = 0; i < 6; ++i) {
      if (internalPower[i] > 0) {
        double div = 0;

        for (int j = 0; j < 6; ++j) {
          if (j != i && powerQuery[j] > 0)
            if (tiles[j] instanceof TileGenericPipe || tiles[j] instanceof IPowerReceptor) {
              div += powerQuery[j];
            }
        }

        double totalWatt = internalPower[i];

        for (int j = 0; j < 6; ++j) {
          if (j != i && powerQuery[j] > 0) {
            double watts = (totalWatt / div * powerQuery[j]);

            if (tiles[j] instanceof TileGenericPipe) {
              TileGenericPipe nearbyTile = (TileGenericPipe) tiles[j];

              PipeTransportPower nearbyTransport = (PipeTransportPower) nearbyTile.pipe.transport;

              nearbyTransport.receiveEnergy(
                  ForgeDirection.VALID_DIRECTIONS[j].getOpposite(), watts);

              displayPower[j] += (short) Math.ceil(watts * DISPLAY_POWER_FACTOR);
              displayPower[i] += (short) Math.ceil(watts * DISPLAY_POWER_FACTOR);

              internalPower[i] -= watts;
            } else if (tiles[j] instanceof IPowerReceptor) {
              IPowerReceptor pow = (IPowerReceptor) tiles[j];

              IPowerProvider prov = pow.getPowerProvider();

              if (prov != null) {
                prov.receiveEnergy((float) watts, ForgeDirection.VALID_DIRECTIONS[j].getOpposite());

                displayPower[j] += (short) Math.ceil(watts * DISPLAY_POWER_FACTOR);
                displayPower[i] += (short) Math.ceil(watts * DISPLAY_POWER_FACTOR);

                internalPower[i] -= watts;
              }
            }
          }
        }
      }
    }

    double highestPower = 0;
    for (int i = 0; i < 6; i++) {
      if (internalPower[i] > highestPower) {
        highestPower = internalPower[i];
      }
      displayPower[i] =
          (short) Math.max(displayPower[i], Math.ceil(internalPower[i] * DISPLAY_POWER_FACTOR));
      displayPower[i] = (short) Math.min(displayPower[i], MAX_DISPLAY);
    }
    overload = highestPower > OVERLOAD_LIMIT;

    // Compute the tiles requesting energy that are not pipes

    for (int i = 0; i < 6; ++i) {
      if (tiles[i] instanceof IPowerReceptor && !(tiles[i] instanceof TileGenericPipe)) {
        IPowerReceptor receptor = (IPowerReceptor) tiles[i];
        int request = receptor.powerRequest();

        if (request > 0) {
          requestEnergy(ForgeDirection.VALID_DIRECTIONS[i], request);
        }
      }
    }

    // Sum the amount of energy requested on each side

    int transferQuery[] = {0, 0, 0, 0, 0, 0};

    for (int i = 0; i < 6; ++i) {
      transferQuery[i] = 0;

      for (int j = 0; j < 6; ++j) {
        if (j != i) {
          transferQuery[i] += powerQuery[j];
        }
      }
    }

    // Transfer the requested energy to nearby pipes

    for (int i = 0; i < 6; ++i) {
      if (transferQuery[i] != 0) {
        if (tiles[i] != null) {
          TileEntity entity = tiles[i];

          if (entity instanceof TileGenericPipe) {
            TileGenericPipe nearbyTile = (TileGenericPipe) entity;

            if (nearbyTile.pipe == null) {
              continue;
            }

            PipeTransportPower nearbyTransport = (PipeTransportPower) nearbyTile.pipe.transport;
            nearbyTransport.requestEnergy(
                ForgeDirection.VALID_DIRECTIONS[i].getOpposite(), transferQuery[i]);
          }
        }
      }
    }

    if (tracker.markTimeIfDelay(worldObj, 2 * BuildCraftCore.updateFactor)) {
      PacketPowerUpdate packet = new PacketPowerUpdate(xCoord, yCoord, zCoord);
      packet.displayPower = displayPower;
      packet.overload = overload;
      CoreProxy.proxy.sendToPlayers(
          packet.getPacket(),
          worldObj,
          xCoord,
          yCoord,
          zCoord,
          DefaultProps.PIPE_CONTENTS_RENDER_DIST);
    }
  }