@Override
  public void updateEntity() {
    super.updateEntity();

    if (!this.worldObj.isRemote) {
      if (!this.isDisabled() && this.isActive()) {
        if (this.isInversed && Settings.ENABLE_ELECTRICITY) {
          // Convert Fortron to Electricity
          double watts = Math.min(this.getFortronEnergy() * FORTRON_UE_RATIO, WATTAGE);

          ElectricityPack remainder = this.produce(watts);

          double electricItemGiven = 0;

          if (remainder.getWatts() > 0) {
            electricItemGiven =
                ElectricItemHelper.chargeItem(
                    this.getStackInSlot(SLOT_BATTERY), remainder.getWatts(), this.getVoltage());
          }

          this.requestFortron(
              (int) ((watts - (remainder.getWatts() - electricItemGiven)) / FORTRON_UE_RATIO),
              true);

          this.animation++;
        } else {
          // Convert Electricity to Fortron
          this.wattsReceived +=
              ElectricItemHelper.dechargeItem(
                  this.getStackInSlot(SLOT_BATTERY), WATTAGE, this.getVoltage());

          if (this.wattsReceived >= TileEntityCoercionDeriver.WATTAGE
              || !Settings.ENABLE_ELECTRICITY) {
            int production = 3;

            if (this.isStackValidForSlot(SLOT_FUEL, this.getStackInSlot(SLOT_FUEL))) {
              production *= NORMAL_PRODUCTION;
            }

            this.fortronTank.fill(
                FortronHelper.getFortron(production + this.worldObj.rand.nextInt(production)),
                true);

            if (this.processTime == 0) {
              this.decrStackSize(SLOT_FUEL, 1);
              this.processTime = REQUIRED_TIME;

              if (this.getModuleCount(ModularForceFieldSystem.itemModuleSpeed) > 0) {
                this.processTime =
                    this.processTime
                        * this.getModuleCount(ModularForceFieldSystem.itemModuleSpeed)
                        / 30;
              }
            }

            if (this.processTime > 0) {
              // We are processing
              this.processTime--;

              if (this.processTime < 1) {
                this.processTime = 0;
              }
            } else {
              this.processTime = 0;
            }

            this.wattsReceived -= WATTAGE;
          }
        }
      }
    } else if (this.isActive()) {
      this.animation++;
    }
  }
コード例 #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);
        }
      }
    }
  }