private void dropBlocks(World world, int x, int y, int z) {
    Block drop = world.getBlock(x, y, z);
    if (drop == Blocks.air) return;
    int dropmeta = world.getBlockMetadata(x, y, z);
    ItemStack sapling = tree.getSapling();
    Block logID = tree.getTreeType().getLogID();

    Collection<ItemStack> drops = this.getDrops(world, x, y, z, drop, dropmeta);
    if (drop == logID && logID != null) {
      if (rand.nextInt(3) == 0) {
        drops.add(
            ReikaItemHelper.getSizedItemStack(ItemStacks.sawdust.copy(), 1 + rand.nextInt(4)));
      }
    }

    for (ItemStack todrop : drops) {
      if (ReikaItemHelper.matchStacks(todrop, sapling)) {
        if (inv[0] != null && inv[0].stackSize >= inv[0].getMaxStackSize()) {
          if (!this.chestCheck(todrop))
            ReikaItemHelper.dropItem(world, dropx, yCoord - 0.25, dropz, todrop);
        } else ReikaInventoryHelper.addOrSetStack(todrop, inv, 0);
      } else {
        if (!this.chestCheck(todrop))
          ReikaItemHelper.dropItem(world, dropx, yCoord - 0.25, dropz, todrop);
      }
    }
  }
 public ItemStack getPlantedSapling() {
   if (!this.shouldPlantSapling()) return null;
   if (treeCopy.isDyeTree())
     return new ItemStack(TreeGetter.getSaplingID(), 1, treeCopy.getDyeTreeMeta());
   else if (treeCopy.getTreeType() != null) return treeCopy.getSapling();
   else return null;
 }
 private float getYield(Block b, int meta) {
   if (tree.getTreeType() == ModWoodList.SLIME) {
     Block log = tree.getTreeType().getLogID();
     if (b == log) {
       return 0.2F;
     }
   }
   return 1;
 }
 private void checkAndMatchInventory() {
   ItemStack sapling = null;
   if (tree.isDyeTree()) {
     sapling = new ItemStack(TreeGetter.getSaplingID(), 1, tree.getDyeTreeMeta());
   } else if (tree.getTreeType() != null) {
     sapling = tree.getSapling();
   }
   if (!ReikaItemHelper.matchStacks(inv[0], sapling)) {
     this.dumpInventory();
   }
 }
 private boolean shouldPlantSapling() {
   if (this.hasEnchantment(Enchantment.infinity)) return true;
   if (treeCopy.isDyeTree()) {
     return inv[0] != null
         && inv[0].stackSize > 0
         && Block.getBlockFromItem(inv[0].getItem()) == TreeGetter.getSaplingID();
   } else if (treeCopy.getTreeType() != null) {
     return inv[0] != null
         && inv[0].stackSize > 0
         && ReikaItemHelper.matchStacks(inv[0], treeCopy.getSapling());
   } else return false;
 }
 private Collection<ItemStack> getDrops(World world, int x, int y, int z, Block b, int meta) {
   float f = this.getYield(b, meta);
   if (ReikaRandomHelper.doWithChance(f)) {
     Collection<ItemStack> ret =
         b.getDrops(world, x, y, z, meta, this.getEnchantment(Enchantment.fortune));
     if (tree.getTreeType() == ModWoodList.SLIME) {
       Block log = tree.getTreeType().getLogID();
       if (b == log) {
         ret.clear();
         ret.add(new ItemStack(Items.slime_ball));
       }
     }
     return ret;
   } else return new ArrayList();
 }
  private void cutBlock(World world, int x, int y, int z, Coordinate c, Material mat) {
    // ReikaItemHelper.dropItems(world, dropx, y-0.25, dropz, dropBlocks.getDrops(world, c.xCoord,
    // c.yCoord, c.zCoord, dropmeta, 0));
    this.dropBlocks(world, c.xCoord, c.yCoord, c.zCoord);
    c.setBlock(world, Blocks.air);
    ReikaSoundHelper.playBreakSound(world, c.xCoord, c.yCoord, c.zCoord, Blocks.log);

    if (mat == Material.leaves)
      world.playSoundEffect(x + 0.5, y + 0.5, z + 0.5, "dig.grass", 0.5F + rand.nextFloat(), 1F);
    else world.playSoundEffect(x + 0.5, y + 0.5, z + 0.5, "dig.wood", 0.5F + rand.nextFloat(), 1F);
    if (tree.getTreeType() == ModWoodList.SLIME
        && c.getBlock(world) == tree.getTreeType().getLogID()) {
      jam++;
      jamColor =
          0xff000000
              | ReikaColorAPI.mixColors(
                  ModWoodList.SLIME.logColor, 0xffffff, (float) jam / MAX_JAM);
    }
  }
  @Override
  public void updateEntity(World world, int x, int y, int z, int meta) {
    super.updateTileEntity();
    tickcount++;

    tree.setWorld(world);
    treeCopy.setWorld(world);

    this.getIOSides(world, x, y, z, meta);
    this.getPower(false);

    if (power < MINPOWER || torque < MINTORQUE) {
      return;
    }

    if (this.isJammed()) return;

    if (world.isRemote) return;

    if (tree.isEmpty() && this.hasWood()) {
      tree.reset();
      ModWoodList wood =
          ModWoodList.getModWood(
              world.getBlock(editx, edity, editz), world.getBlockMetadata(editx, edity, editz));
      ReikaTreeHelper vanilla =
          ReikaTreeHelper.getTree(
              world.getBlock(editx, edity, editz), world.getBlockMetadata(editx, edity, editz));

      for (int i = -1; i <= 1; i++) {
        for (int j = -1; j <= 1; j++) {
          tree.checkAndAddDyeTree(world, editx + i, edity, editz + j);
          if (tree.isEmpty() || !tree.isValidTree()) tree.clear();
        }
      }
      if (tree.isEmpty()) {
        for (int i = -1; i <= 1; i++) {
          for (int j = -1; j <= 1; j++) {
            tree.checkAndAddRainbowTree(world, editx + i, edity, editz + j);
            if (tree.isEmpty() || !tree.isValidTree()) tree.clear();
          }
        }
      }

      if (tree.isEmpty()) {

        if (wood == ModWoodList.SEQUOIA) {
          for (int i = -32; i < 255; i += 16)
            tree.addSequoia(world, editx, edity + i, editz, RotaryCraft.logger.shouldDebug());
        } else if (wood == ModWoodList.DARKWOOD) {
          tree.addDarkForest(
              world,
              editx,
              edity,
              editz,
              editx - 8,
              editx + 8,
              editz - 8,
              editz + 8,
              RotaryCraft.logger.shouldDebug());
        } else if (wood == ModWoodList.IRONWOOD) {
          for (int i = -2; i < 128; i += 16)
            tree.addIronwood(world, editx, edity + i, editz, RotaryCraft.logger.shouldDebug());
        } else if (wood != null) {
          for (int i = -1; i <= 1; i++) {
            for (int j = -1; j <= 1; j++) {
              // tree.addGenerousTree(world, editx+i, edity, editz+j, 16);
              tree.setTree(wood);
              tree.addModTree(world, editx + i, edity, editz + j);
            }
            // ReikaJavaLibrary.pConsole(tree, Side.SERVER);
          }
        } else if (vanilla != null) {
          for (int i = -1; i <= 1; i++) {
            for (int j = -1; j <= 1; j++) {
              // tree.addGenerousTree(world, editx+i, edity, editz+j, 16);
              tree.setTree(vanilla);
              tree.addTree(world, editx + i, edity, editz + j);
            }
          }
        }
      }

      this.checkAndMatchInventory();

      tree.sortBlocksByHeight();
      tree.reverseBlockOrder();
      tree.sort(inwardsComparator);
      tree.sort(leafPriority);
      treeCopy = (TreeReader) tree.copy();
    }

    Block b = world.getBlock(x, y + 1, z);
    if (b != Blocks.air) {
      if (b.getMaterial() == Material.wood || b.getMaterial() == Material.leaves) {
        ReikaItemHelper.dropItems(
            world,
            dropx,
            y - 0.25,
            dropz,
            this.getDrops(world, x, y + 1, z, b, world.getBlockMetadata(x, y, z)));
        world.setBlockToAir(x, y + 1, z);
      }
    }

    // RotaryCraft.logger.debug(tree);

    if (tree.isEmpty()) return;

    if (tickcount < this.getOperationTime()) return;
    tickcount = 0;

    if (!tree.isValidTree()) {
      tree.reset();
      tree.clear();
      return;
    }

    for (int i = 0; i < this.getNumberConsecutiveOperations(); i++) {
      Coordinate c = tree.getNextAndMoveOn();
      Block drop = c.getBlock(world);
      int dropmeta = c.getBlockMetadata(world);

      if (drop != Blocks.air) {
        Material mat = ReikaWorldHelper.getMaterial(world, c.xCoord, c.yCoord, c.zCoord);
        if (ConfigRegistry.INSTACUT.getState()) {
          // ReikaItemHelper.dropItems(world, dropx, y-0.25, dropz, dropBlocks.getDrops(world,
          // c.xCoord, c.yCoord, c.zCoord, dropmeta, 0));
          this.cutBlock(world, x, y, z, c, mat);

          if (c.yCoord == edity) {
            Block idbelow = world.getBlock(c.xCoord, c.yCoord - 1, c.zCoord);
            Block root = TwilightForestHandler.BlockEntry.ROOT.getBlock();
            if (ReikaPlantHelper.SAPLING.canPlantAt(world, c.xCoord, c.yCoord, c.zCoord)) {
              ItemStack plant = this.getPlantedSapling();
              if (plant != null) {
                if (inv[0] != null && !this.hasEnchantment(Enchantment.infinity))
                  ReikaInventoryHelper.decrStack(0, inv);
                ReikaWorldHelper.setBlock(world, c.xCoord, c.yCoord, c.zCoord, plant);
              }
            } else if (tree.getTreeType() == ModWoodList.TIMEWOOD
                && (idbelow == root || idbelow == Blocks.air)) {
              ItemStack plant = this.getPlantedSapling();
              if (plant != null) {
                if (inv[0] != null && !this.hasEnchantment(Enchantment.infinity))
                  ReikaInventoryHelper.decrStack(0, inv);
                world.setBlock(c.xCoord, c.yCoord - 1, c.zCoord, Blocks.dirt);
                ReikaWorldHelper.setBlock(world, c.xCoord, c.yCoord, c.zCoord, plant);
              }
            }
          }
        } else {
          boolean fall = BlockSand.func_149831_e(world, c.xCoord, c.yCoord - 1, c.zCoord);
          if (fall) {
            EntityFallingBlock e =
                new EntityFallingBlock(
                    world, c.xCoord + 0.5, c.yCoord + 0.65, c.zCoord + 0.5, drop, dropmeta);
            e.field_145812_b = -5000;
            if (!world.isRemote) {
              world.spawnEntityInWorld(e);
            }
            c.setBlock(world, Blocks.air);
          } else {
            this.cutBlock(world, x, y, z, c, mat);
            if (c.yCoord == edity) {
              Block idbelow = world.getBlock(c.xCoord, c.yCoord - 1, c.zCoord);
              Block root = TwilightForestHandler.BlockEntry.ROOT.getBlock();
              if (ReikaPlantHelper.SAPLING.canPlantAt(world, c.xCoord, c.yCoord, c.zCoord)) {
                ItemStack plant = this.getPlantedSapling();
                if (plant != null) {
                  if (inv[0] != null && !this.hasEnchantment(Enchantment.infinity))
                    ReikaInventoryHelper.decrStack(0, inv);
                  ReikaWorldHelper.setBlock(world, c.xCoord, c.yCoord, c.zCoord, plant);
                }
              } else if (tree.getTreeType() == ModWoodList.TIMEWOOD
                  && (idbelow == root || idbelow == Blocks.air)) {
                ItemStack plant = this.getPlantedSapling();
                if (plant != null) {
                  if (inv[0] != null && !this.hasEnchantment(Enchantment.infinity))
                    ReikaInventoryHelper.decrStack(0, inv);
                  world.setBlock(c.xCoord, c.yCoord - 1, c.zCoord, Blocks.dirt);
                  ReikaWorldHelper.setBlock(world, c.xCoord, c.yCoord, c.zCoord, plant);
                }
              }
            }
          }
        }
      }
      if (tree.isEmpty()) break;
    }
  }