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

    if (!SimukraftReloaded.isDayTime()) {
      theStage = Stage.IDLE;
    }

    super.onUpdateGoingToWork(theFolk);

    if (System.currentTimeMillis() - timeSinceLastRun < runDelay) {
      return;
    }

    timeSinceLastRun = System.currentTimeMillis();

    // ////////////////IDLE
    if (theStage == Stage.IDLE && SimukraftReloaded.isDayTime()) {
      theStage = Stage.SCANFORTREE;
    } else if (theStage == Stage.ARRIVEDATMILL) {
      theStage = Stage.SCANFORTREE;
    } else if (theStage == Stage.SCANFORTREE) {
      stageScanForTree();
    } else if (theStage == Stage.GOTOTREE) {
      pickUpSaplings();
      stageGotoTree();
    } else if (theStage == Stage.CHOPPINGTREE) {
      stageChoppingTree();
      pickUpSaplings();
    } else if (theStage == Stage.RETURNWOOD) {
      stageReturnWood();
      pickUpSaplings();
    }
  }
  private void stageReturnWood() {
    theFolk.isWorking = false;

    if (step == 1) {
      theFolk.statusText = "Delivering wood back to base";
      theFolk.gotoXYZ(theFolk.employedAt, null);
      step = 2;
    } else if (step == 2) {
      if (theFolk.gotoMethod == GotoMethod.WALK) {
        theFolk.updateLocationFromEntity();
      }

      int dist = theFolk.location.getDistanceTo(theFolk.employedAt);

      if (dist <= 1) {
        step = 3;
      } else {
        if (theFolk.destination == null && theFolk.theEntity != null) {
          // step=1;
        }
      }
    } else if (step == 3) {
      theFolk.stayPut = true;
      int count = getInventoryCount(theFolk, Item.getItemFromBlock(Blocks.log));
      millChests = inventoriesFindClosest(theFolk.employedAt, 6);
      inventoriesTransferFromFolk(
          theFolk.inventory, millChests, new ItemStack(Item.getItemFromBlock(Blocks.log)));
      pay = (float) count * 0.03f;
      SimukraftReloaded.states.credits -= pay;
      SimukraftReloaded.sendChat(
          theFolk.name + " has delivered " + count + " logs at the lumbermill");
      theStage = Stage.SCANFORTREE;
      step = 1;
    }
  }
  private void stageScanForTree() {
    theFolk.action = FolkAction.ATWORK;
    theFolk.isWorking = false;
    V3 searchXYZ = null;
    lumbermill = Building.getBuilding(theFolk.employedAt);
    V3 ts = null;

    try {
      if (lumbermill.lumbermillMarker != null) {
        searchXYZ = lumbermill.lumbermillMarker; // use a marker if there is one
      } else if (theFolk.employedAt != null) {
        searchXYZ = theFolk.employedAt.clone();
      } else {
        searchXYZ = theFolk.location.clone();
      }

      ts = searchXYZ.clone();
    } catch (Exception e) {
      e.printStackTrace();
    }

    V3 searchpos;

    if (ts != null) {
      searchpos = ts.clone();
    } else {
      searchpos = theFolk.location.clone();
    }

    try {
      foundWoodAt =
          findClosestBlockType(
              searchpos, Blocks.log, SimukraftReloadedConfig.configLumberArea, false);
      foundWoodAt.theDimension = jobWorld.provider.dimensionId;

      if (foundWoodAt.getDistanceTo(Building.getNearestBuilding(foundWoodAt).primaryXYZ) <= 5) {}

    } catch (Exception e) {
      e.printStackTrace();
    }

    theStage = Stage.GOTOTREE;
    onRoute = false;

    if (foundWoodAt == null) {
      SimukraftReloaded.sendChat(theFolk.name + " could not find any wood in the area.");
      theFolk.selfFire();
      return;
    }
  }
  @Override
  public void onBlockPlacedBy(
      World world, int i, int j, int k, EntityLivingBase player, ItemStack is) {

    hasPlaced = true;
    Marker ma;
    if (world.isRemote) {
      markers.add(ma = new Marker(i, j, k, world.provider.dimensionId));

      String markerCaption = "";
      String helpText = "";

      if (markers.size() == 1) {

        markerCaption = "Front-Left";
        helpText =
            "You can place two more markers to mark out an area for a farm or mine etc. If you wish to do this, place another marker at the front-right position now";
        System.out.println(markers.size());

      } else if (markers.size() == 2) {

        markerCaption = "Front-Right";
        helpText = "Finally, place a marker at the Rear-Left position";
        System.out.println(markers.size());

      } else if (markers.size() == 3) {

        markerCaption = "Rear-Left";
        helpText =
            "You're done, now you can place down a mining box, farming box or right-click the front-left marker to copy a structure!";
        System.out.println(markers.size());
      } else {
        System.out.println(markers.size());
        markerCaption = "Too many Markers!";
      }

      if (markers.size() < 4) {
        V3 pos = new V3((double) i, (double) j, (double) k, world.provider.dimensionId);
        pos.y += 0.01d;

        if (SimukraftReloadedConfig.configEnableMarkerAlignmentBeams) {
          EntityAlignBeam beam = new EntityAlignBeam(world);
          ma.caption = markerCaption;
          beam.setLocationAndAngles(pos.x, pos.y, pos.z, 0f, 0f);
          beam.yaw = 0f;

          if (!world.isRemote) {
            world.spawnEntityInWorld(beam);
          }

          ma.beams.add(beam);
          EntityAlignBeam beam2 = new EntityAlignBeam(world);
          beam2.setLocationAndAngles(pos.x, pos.y, pos.z, 90f, 0f);
          beam2.yaw = 90f;

          if (!world.isRemote) {
            world.spawnEntityInWorld(beam2);
          }

          ma.beams.add(beam2);
          EntityAlignBeam beam3 = new EntityAlignBeam(world);
          beam3.setLocationAndAngles(pos.x, pos.y, pos.z, 180f, 0f);
          beam3.yaw = 180f;

          if (!world.isRemote) {
            world.spawnEntityInWorld(beam3);
          }

          ma.beams.add(beam3);
          EntityAlignBeam beam4 = new EntityAlignBeam(world);
          beam4.setLocationAndAngles(pos.x, pos.y, pos.z, 270f, 0f);
          beam4.yaw = 270f;

          if (!world.isRemote) {
            world.spawnEntityInWorld(beam4);
          }

          ma.beams.add(beam4);
        }
      }

      if (!helpText.contentEquals("")) {
        SimukraftReloaded.sendChat(helpText);
      }

      super.onBlockPlacedBy(world, i, j, k, player, is);
    }
  }