@Override
  public void update() {
    // no recipeeeh
    if (recipe == null) {
      return;
    }

    // fully filled
    if (tank.getFluidAmount() == tank.getCapacity()) {
      timer++;
      if (timer >= recipe.getTime()) {
        TinkerCastingEvent.OnCasted event = TinkerCastingEvent.OnCasted.fire(recipe, this);
        // done, finish!
        if (event.consumeCast) {
          setInventorySlotContents(0, null);
        }

        // put result into output
        if (event.switchOutputs) {
          setInventorySlotContents(1, getStackInSlot(0));
          setInventorySlotContents(0, event.output);
        } else {
          setInventorySlotContents(1, event.output);
        }

        // reset state
        reset();
      }
    }
  }
  @Override
  public int fill(ForgeDirection from, FluidStack resource, boolean doFill) {
    if (resource == null) {
      return 0;
    }

    FluidStack resourceCopy = resource.copy();
    int totalUsed = 0;

    FluidStack liquid = tank.getFluid();
    if (liquid != null && liquid.amount > 0 && !liquid.isFluidEqual(resourceCopy)) {
      return 0;
    }

    while (resourceCopy.amount > 0) {
      int used = tank.fill(resourceCopy, doFill);
      resourceCopy.amount -= used;
      if (used > 0) {
        markDirty();
      }

      totalUsed += used;
    }

    return totalUsed;
  }
 @Override
 public void updateEntity() {
   if (pos == 4
       && !worldObj.isRemote
       && worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord))
     for (int i = 0; i < 6; i++)
       if (i != 1 && tank.getFluidAmount() > 0) {
         ForgeDirection f = ForgeDirection.getOrientation(i);
         int out = Math.min(144, tank.getFluidAmount());
         TileEntity te =
             this.worldObj.getTileEntity(
                 xCoord + (i == 4 ? -1 : i == 5 ? 1 : 0),
                 yCoord + (i == 0 ? -1 : 0),
                 zCoord + (i == 2 ? -1 : i == 3 ? 1 : 0));
         if (te != null
             && te instanceof IFluidHandler
             && ((IFluidHandler) te).canFill(f.getOpposite(), tank.getFluid().getFluid())) {
           updateComparatorValuesPart1();
           int accepted =
               ((IFluidHandler) te)
                   .fill(f.getOpposite(), new FluidStack(tank.getFluid().getFluid(), out), false);
           FluidStack drained = this.tank.drain(accepted, true);
           ((IFluidHandler) te).fill(f.getOpposite(), drained, true);
           updateComparatorValuesPart2();
         }
       }
 }
 @Override
 public void writeSustainedData(ItemStack itemStack) {
   if (lavaTank.getFluid() != null) {
     itemStack.stackTagCompound.setTag(
         "lavaTank", lavaTank.getFluid().writeToNBT(new NBTTagCompound()));
   }
 }
Exemple #5
0
  public void pushFluidOut() {
    int tanks = 0;
    boolean[] sides = new boolean[ForgeDirection.VALID_DIRECTIONS.length];
    for (int i = 0; i < sides.length; i++) {
      ForgeDirection side = ForgeDirection.VALID_DIRECTIONS[i];
      TileEntity tile =
          worldObj.getTileEntity(
              xCoord + side.offsetX, yCoord + side.offsetY, zCoord + side.offsetZ);
      if (tile != null && tile instanceof IFluidHandler) {
        tanks++;
        sides[i] = true;
      }
    }

    int amountPerSide = lava.getFluidAmount();
    for (int i = 0; i < sides.length; i++) {
      if (sides[i]) {
        ForgeDirection side = ForgeDirection.getOrientation(i);
        TileEntity tile =
            worldObj.getTileEntity(
                xCoord + side.offsetX, yCoord + side.offsetY, zCoord + side.offsetZ);
        if (tile != null
            && tile instanceof IFluidHandler
            && !(tile instanceof Heater)
            && !(tile instanceof Melter)) {
          IFluidHandler fluid = (IFluidHandler) tile;
          if (fluid.fill(side.getOpposite(), lava.drain(amountPerSide, false), false)
              == amountPerSide) {
            amountPerSide -= fluid.fill(side.getOpposite(), lava.drain(amountPerSide, true), true);
          }
        }
      }
    }
  }
 @Override
 public boolean isItemValidForSlot(int slot, ItemStack stack) {
   if (!formed) return false;
   if (master() != null) return master().isItemValidForSlot(slot, stack);
   if (slot == 1 || slot == 3 || slot == 5) return false;
   if (slot == 4)
     return (tank2.getFluidAmount() <= 0
         ? FluidContainerRegistry.isEmptyContainer(stack)
         : FluidContainerRegistry.fillFluidContainer(
                 tank2.getFluid(), Utils.copyStackWithAmount(stack, 1))
             != null);
   FluidStack fs = FluidContainerRegistry.getFluidForFilledItem(stack);
   if (fs == null) return false;
   RefineryRecipe partialRecipe = DieselHandler.findIncompleteRefineryRecipe(fs, null);
   if (partialRecipe == null) return false;
   if (slot == 0)
     return (tank0.getFluidAmount() <= 0 || fs.isFluidEqual(tank0.getFluid()))
         && (tank1.getFluidAmount() <= 0
             || DieselHandler.findIncompleteRefineryRecipe(fs, tank1.getFluid()) != null);
   if (slot == 2)
     return (tank1.getFluidAmount() <= 0 || fs.isFluidEqual(tank1.getFluid()))
         && (tank0.getFluidAmount() <= 0
             || DieselHandler.findIncompleteRefineryRecipe(fs, tank0.getFluid()) != null);
   return false;
 }
 @Override
 public FluidStack drain(ForgeDirection from, FluidStack resource, boolean doDrain) {
   if (resource != null)
     for (FluidTank _tank : (FluidTank[]) getTanks())
       if (resource.isFluidEqual(_tank.getFluid())) return _tank.drain(resource.amount, doDrain);
   return null;
 }
  @Override
  public void writeToNBT(NBTTagCompound nbtTags) {
    super.writeToNBT(nbtTags);

    if (fluidTank.getFluid() != null) {
      nbtTags.setTag("fluidTank", fluidTank.writeToNBT(new NBTTagCompound()));
    }

    NBTTagList recurringList = new NBTTagList();

    for (Object3D wrapper : recurringNodes) {
      NBTTagCompound tagCompound = new NBTTagCompound();
      wrapper.write(tagCompound);
      recurringList.appendTag(tagCompound);
    }

    if (recurringList.tagCount() != 0) {
      nbtTags.setTag("recurringNodes", recurringList);
    }

    NBTTagList cleaningList = new NBTTagList();

    for (Object3D obj : cleaningNodes) {
      cleaningList.appendTag(obj.write(new NBTTagCompound()));
    }

    if (cleaningList.tagCount() != 0) {
      nbtTags.setTag("cleaningNodes", cleaningList);
    }
  }
  @Override
  public void writeToNBT(NBTTagCompound nbtTags) {
    super.writeToNBT(nbtTags);

    nbtTags.setBoolean("finishedCalc", finishedCalc);

    if (fluidTank.getFluid() != null) {
      nbtTags.setTag("fluidTank", fluidTank.writeToNBT(new NBTTagCompound()));
    }

    NBTTagList activeList = new NBTTagList();

    for (Coord4D wrapper : activeNodes) {
      NBTTagCompound tagCompound = new NBTTagCompound();
      wrapper.write(tagCompound);
      activeList.appendTag(tagCompound);
    }

    if (activeList.tagCount() != 0) {
      nbtTags.setTag("activeNodes", activeList);
    }

    NBTTagList usedList = new NBTTagList();

    for (Coord4D obj : usedNodes) {
      activeList.appendTag(obj.write(new NBTTagCompound()));
    }

    if (activeList.tagCount() != 0) {
      nbtTags.setTag("usedNodes", usedList);
    }
  }
  private void doPlenish() {
    if (usedNodes.size() >= MAX_NODES) {
      finishedCalc = true;
      return;
    }

    if (activeNodes.isEmpty()) {
      if (usedNodes.isEmpty()) {
        Coord4D below = Coord4D.get(this).getFromSide(ForgeDirection.DOWN);

        if (!canReplace(below, true, true)) {
          finishedCalc = true;
          return;
        }

        activeNodes.add(below);
      } else {
        finishedCalc = true;
        return;
      }
    }

    Set<Coord4D> toRemove = new HashSet<Coord4D>();

    for (Coord4D coord : activeNodes) {
      if (coord.exists(worldObj)) {
        if (canReplace(coord, true, false)) {
          worldObj.setBlock(
              coord.xCoord,
              coord.yCoord,
              coord.zCoord,
              MekanismUtils.getFlowingBlock(fluidTank.getFluid().getFluid()),
              0,
              3);

          setEnergy(getEnergy() - usage.fluidicPlenisherUsage);
          fluidTank.drain(FluidContainerRegistry.BUCKET_VOLUME, true);
        }

        for (ForgeDirection dir : dirs) {
          Coord4D sideCoord = coord.getFromSide(dir);

          if (sideCoord.exists(worldObj) && canReplace(sideCoord, true, true)) {
            activeNodes.add(sideCoord);
          }
        }

        toRemove.add(coord);
        break;
      } else {
        toRemove.add(coord);
      }
    }

    for (Coord4D coord : toRemove) {
      activeNodes.remove(coord);
      usedNodes.add(coord);
    }
  }
  @Override
  public void writeToNBT(NBTTagCompound nbtTags) {
    super.writeToNBT(nbtTags);

    if (lavaTank.getFluid() != null) {
      nbtTags.setTag("lavaTank", lavaTank.writeToNBT(new NBTTagCompound()));
    }
  }
 @Override
 public int fill(ForgeDirection from, FluidStack resource, boolean doFill) {
   if (resource != null)
     for (FluidTank _tank : (FluidTank[]) getTanks())
       if (_tank.getFluidAmount() == 0 || resource.isFluidEqual(_tank.getFluid()))
         return _tank.fill(resource, doFill);
   return 0;
 }
 @Override
 public FluidStack getFluid(ItemStack container) {
   FluidTank tank = readTank(container);
   if (tank == null) return null;
   FluidStack result = tank.getFluid();
   if (result != null) result.amount *= container.stackSize;
   return result;
 }
 @Override
 public FluidStack drain(ForgeDirection from, int maxDrain, boolean doDrain) {
   FluidStack drained = tank.drain(maxDrain, doDrain);
   if (tank.getFluidAmount() == 0 && doDrain) {
     tank.setFluid(null);
   }
   return drained;
 }
 private void updateComparatorValuesPart1() {
   oldComps = new int[4];
   int vol = tank.getCapacity() / 4;
   for (int i = 0; i < 4; i++) {
     int filled = tank.getFluidAmount() - i * vol;
     oldComps[i] = Math.min(15, Math.max((15 * filled) / vol, 0));
   }
   masterCompOld = (15 * tank.getFluidAmount()) / tank.getCapacity();
 }
  protected void reset() {
    timer = 0;
    recipe = null;
    tank.setCapacity(0);
    tank.setFluid(null);

    if (!worldObj.isRemote && worldObj instanceof WorldServer) {
      TinkerNetwork.sendToClients((WorldServer) worldObj, pos, new FluidUpdatePacket(pos, null));
    }
  }
  @Override
  public FluidStack drain(ForgeDirection from, FluidStack resource, boolean doDrain) {
    if (fluidTank.getFluid() != null
        && fluidTank.getFluid().getFluid() == resource.getFluid()
        && from == ForgeDirection.getOrientation(1)) {
      return drain(from, resource.amount, doDrain);
    }

    return null;
  }
 @SuppressWarnings({"rawtypes", "unchecked"})
 @Override
 public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean par4) {
   FluidTank fakeTank = readTank(stack);
   FluidStack fluidStack = fakeTank.getFluid();
   if (fluidStack != null && fluidStack.amount > 0) {
     float percent = Math.max(100.0f / fakeTank.getCapacity() * fluidStack.amount, 1);
     list.add(String.format("%d mB (%.0f%%)", fluidStack.amount, percent));
   }
 }
 public static ItemStack drainFluidContainer(FluidTank tank, ItemStack containerIn) {
   if (FluidContainerRegistry.isFilledContainer(containerIn)) {
     FluidStack fs = FluidContainerRegistry.getFluidForFilledItem(containerIn);
     if (fs != null && tank.getFluidAmount() + fs.amount <= tank.getCapacity()) {
       ItemStack emptyContainer = FluidContainerRegistry.drainFluidContainer(containerIn);
       if (emptyContainer != null && tank.fill(fs, true) == fs.amount) return emptyContainer;
     }
   }
   return null;
 }
  public ItemStack getFilledItem(ItemStack empty, boolean drainTank) {
    if (empty == null || tank.getFluid() == null) return null;
    BottlingMachineRecipe recipe = BottlingMachineRecipe.findRecipe(empty, tank.getFluid());
    if (recipe != null && recipe.output != null) {
      if (drainTank) tank.drain(recipe.fluidInput.amount, true);
      return recipe.output;
    }

    ItemStack filled =
        FluidContainerRegistry.fillFluidContainer(
            new FluidStack(tank.getFluid(), Integer.MAX_VALUE), empty);
    FluidStack fs = FluidContainerRegistry.getFluidForFilledItem(filled);
    if (filled != null && fs.amount <= tank.getFluidAmount()) {
      if (drainTank) tank.drain(fs.amount, true);
      return filled;
    }

    if (empty.getItem() instanceof IFluidContainerItem) {
      int accepted = ((IFluidContainerItem) empty.getItem()).fill(empty, tank.getFluid(), false);
      if (accepted > 0) {
        filled = empty.copy();
        ((IFluidContainerItem) filled.getItem())
            .fill(filled, new FluidStack(tank.getFluid(), accepted), true);
        if (drainTank) tank.drain(accepted, true);
        return filled;
      }
    }
    return null;
  }
  @Override
  public boolean canDrain(EnumFacing from, Fluid fluid) {
    if (fluidTank != null) {
      if (fluid == null
          || fluidTank.getFluid() != null && fluidTank.getFluid().getFluid() == fluid) {
        return !(isActive && from == EnumFacing.DOWN);
      }
    }

    return false;
  }
  @Override
  public void handlePacketData(ByteBuf dataStream) {
    if (!worldObj.isRemote) {
      int type = dataStream.readInt();

      switch (type) {
        case 0:
          if (getReactor() != null) getReactor().setInjectionRate(dataStream.readInt());
          break;
      }

      return;
    }

    super.handlePacketData(dataStream);

    if (worldObj.isRemote) {
      boolean formed = dataStream.readBoolean();

      if (formed) {
        if (getReactor() == null || !((FusionReactor) getReactor()).formed) {
          Mekanism.proxy.doGenericSparkle(
              this,
              new INodeChecker() {
                @Override
                public boolean isNode(TileEntity tile) {
                  return tile instanceof TileEntityReactorBlock;
                }
              });
        }

        if (getReactor() == null) {
          setReactor(new FusionReactor(this));
          MekanismUtils.updateBlock(worldObj, xCoord, yCoord, zCoord);
        }

        ((FusionReactor) getReactor()).formed = true;
        getReactor().setPlasmaTemp(dataStream.readDouble());
        getReactor().setCaseTemp(dataStream.readDouble());
        getReactor().setInjectionRate(dataStream.readInt());
        getReactor().setBurning(dataStream.readBoolean());
        fuelTank.setGas(new GasStack(GasRegistry.getGas("fusionFuelDT"), dataStream.readInt()));
        deuteriumTank.setGas(new GasStack(GasRegistry.getGas("deuterium"), dataStream.readInt()));
        tritiumTank.setGas(new GasStack(GasRegistry.getGas("tritium"), dataStream.readInt()));
        waterTank.setCapacity(dataStream.readInt());
        waterTank.setFluid(new FluidStack(FluidRegistry.getFluid("water"), dataStream.readInt()));
        steamTank.setCapacity(dataStream.readInt());
        steamTank.setFluid(new FluidStack(FluidRegistry.getFluid("steam"), dataStream.readInt()));
      } else if (getReactor() != null) {
        setReactor(null);
        MekanismUtils.updateBlock(worldObj, xCoord, yCoord, zCoord);
      }
    }
  }
  private static FluidTank readTank(ItemStack stack) {
    FluidTank tank = new FluidTank(TileEntityTank.getTankCapacity());

    final NBTTagCompound itemTag = stack.getTagCompound();
    if (itemTag != null && itemTag.hasKey(TANK_TAG)) {
      tank.readFromNBT(itemTag.getCompoundTag(TANK_TAG));
      return tank;
    }

    return tank;
  }
  private static void saveTank(ItemStack container, FluidTank tank) {
    if (tank.getFluidAmount() > 0) {
      NBTTagCompound itemTag = ItemUtils.getItemTag(container);

      NBTTagCompound tankTag = new NBTTagCompound();
      tank.writeToNBT(tankTag);
      itemTag.setTag(TANK_TAG, tankTag);
    } else {
      container.stackTagCompound = null;
    }
  }
  @Override
  public boolean canFill(EnumFacing from, Fluid fluid) {
    if (from == EnumFacing.DOWN && worldObj != null && getPos() != null) {
      TileEntity tile = worldObj.getTileEntity(getPos().offset(EnumFacing.DOWN));

      if (isActive && !(tile instanceof TileEntityFluidTank)) {
        return false;
      }
    }

    return fluidTank.getFluid() == null || fluidTank.getFluid().getFluid() == fluid;
  }
  @Override
  public void handlePacketData(ByteArrayDataInput dataStream) {
    super.handlePacketData(dataStream);

    if (dataStream.readInt() == 1) {
      fluidTank.setFluid(new FluidStack(dataStream.readInt(), dataStream.readInt()));
    } else {
      fluidTank.setFluid(null);
    }

    MekanismUtils.updateBlock(worldObj, xCoord, yCoord, zCoord);
  }
  public static ItemStack createFilledTank(Fluid fluid) {
    final int tankCapacity = TileEntityTank.getTankCapacity();
    FluidStack stack = FluidRegistry.getFluidStack(fluid.getName(), tankCapacity);
    if (stack == null) return null;

    FluidTank tank = new FluidTank(tankCapacity);
    tank.setFluid(stack);

    ItemStack item = new ItemStack(OpenBlocks.Blocks.tank);
    saveTank(item, tank);
    return item;
  }
 @Override
 public void readCustomNBT(NBTTagCompound nbt, boolean descPacket) {
   super.readCustomNBT(nbt, descPacket);
   facing = nbt.getInteger("facing");
   tank0.readFromNBT(nbt.getCompoundTag("tank0"));
   tank1.readFromNBT(nbt.getCompoundTag("tank1"));
   tank2.readFromNBT(nbt.getCompoundTag("tank2"));
   energyStorage.readFromNBT(nbt);
   if (!descPacket) {
     inventory = Utils.readInventory(nbt.getTagList("inventory", 10), 6);
   }
 }
 public boolean use(
     ItemStack[] inventory, int index, FluidTank fluidTank, GasTank gasTank, boolean deplete) {
   if (meets(new PressurizedInput(inventory[index], fluidTank.getFluid(), gasTank.getGas()))) {
     if (deplete) {
       inventory[index] = StackUtils.subtract(inventory[index], theSolid);
       fluidTank.drain(theFluid.amount, true);
       gasTank.draw(theGas.amount, true);
     }
     return true;
   }
   return false;
 }
  @Override
  public FluidStack drain(EnumFacing from, FluidStack resource, boolean doDrain) {
    if (tank.getFluidAmount() == 0) {
      return null;
    }
    if (tank.getFluid().getFluid() != resource.getFluid()) {
      return null;
    }

    // same fluid, k
    return this.drain(from, resource.amount, doDrain);
  }