侧边栏壁纸
  • 累计撰写 781 篇文章
  • 累计创建 1 个标签
  • 累计收到 1 条评论
标签搜索
MC

EntityAIWorkFarmer

Dettan
2021-07-10 / 0 评论 / 0 点赞 / 92 阅读 / 5,611 字
温馨提示:
本文最后更新于 2022-04-30,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。
private IAIState prepareForFarming()
{
    @Nullable final BuildingFarmer building = getOwnBuilding();
    if (building == null || building.getBuildingLevel() < 1)
    {
        return PREPARING;
    }
if (!job.getTaskQueue().isEmpty())
{
    return START_WORKING;
}

building.syncWithColony(world);
if (building.getFarmerFields().size() &lt; getOwnBuilding().getBuildingLevel() && !building.assignManually())
{
    searchAndAddFields();
}

final int amountOfCompostInBuilding = InventoryUtils.getItemCountInProvider(getOwnBuilding(), this::isCompost);
final int amountOfCompostInInv = InventoryUtils.getItemCountInItemHandler(worker.getInventoryCitizen(), this::isCompost);

if (amountOfCompostInBuilding + amountOfCompostInInv &lt;= 0)
{
    if (!getOwnBuilding().hasWorkerOpenRequestsOfType(Objects.requireNonNull(worker.getCitizenData()), TypeToken.of(StackList.class)))
    {
        final List&lt;ItemStack&gt; compostAbleItems = new ArrayList&lt;&gt;();
        compostAbleItems.add(new ItemStack(ModItems.compost, 1));
        compostAbleItems.add(new ItemStack(Items.BONE_MEAL, 1));
        worker.getCitizenData().createRequestAsync(new StackList(compostAbleItems, FERTLIZER, STACKSIZE, 1));
    }
}
else if (amountOfCompostInInv &lt;= 0 && amountOfCompostInBuilding &gt; 0)
{
    needsCurrently = new Tuple&lt;&gt;(this::isCompost, STACKSIZE);
    return GATHERING_REQUIRED_MATERIALS;
}

if (building.hasNoFields())
{
    if (worker.getCitizenData() != null)
    {
        worker.getCitizenData().triggerInteraction(new StandardInteractionResponseHandler(new TranslationTextComponent(NO_FREE_FIELDS), ChatPriority.BLOCKING));
    }
    worker.getCitizenData().setIdleAtJob(true);
    return PREPARING;
}
worker.getCitizenData().setIdleAtJob(false);

//If the farmer has no currentField and there is no field which needs work, check fields.
if (building.getCurrentField() == null && building.getFieldToWorkOn(world) == null)
{
    building.resetFields();
    return IDLE;
}

@Nullable final BlockPos currentField = building.getCurrentField();
final TileEntity entity = world.getTileEntity(currentField);
if (entity instanceof ScarecrowTileEntity && ((ScarecrowTileEntity) entity).needsWork())
{
    if (((ScarecrowTileEntity) entity).getFieldStage() == ScarecrowFieldStage.PLANTED && checkIfShouldExecute((ScarecrowTileEntity) entity, this::shouldHarvest))
    {
        return FARMER_HARVEST;
    }
    else if (((ScarecrowTileEntity) entity).getFieldStage() == ScarecrowFieldStage.HOED)
    {
        return canGoPlanting((ScarecrowTileEntity) entity, building);
    }
    else if (((ScarecrowTileEntity) entity).getFieldStage() == ScarecrowFieldStage.EMPTY && checkIfShouldExecute((ScarecrowTileEntity) entity,
      pos -&gt; this.shouldHoe(pos, (ScarecrowTileEntity) entity)))
    {
        return FARMER_HOE;
    }
    ((ScarecrowTileEntity) entity).nextState();
}
else
{
    getOwnBuilding().setCurrentField(null);
}
return PREPARING;

}




private IAIState canGoPlanting(@NotNull final ScarecrowTileEntity currentField, @NotNull final BuildingFarmer buildingFarmer)
{
		//地块设置种什么了没?
    if (currentField.getSeed() == null)
    {
        worker.getCitizenData()
          .triggerInteraction(new PosBasedInteractionResponseHandler(new TranslationTextComponent(NO_SEED_SET, currentField.getPos()),
            ChatPriority.BLOCKING,
            new TranslationTextComponent(NO_SEED_SET),
            currentField.getPos()));
        buildingFarmer.setCurrentField(null);
        worker.getCitizenData().setIdleAtJob(true);
        return PREPARING;
    }
    worker.getCitizenData().setIdleAtJob(false);
final ItemStack seeds = currentField.getSeed().copy();
	//查找并获得对应的种子
final int slot = worker.getCitizenInventoryHandler().findFirstSlotInInventoryWith(seeds.getItem());
//有种子,转为种地状态
	if (slot != -1)
{
    return FARMER_PLANT;
}

if (walkToBuilding())
{
    return PREPARING;
}
	//请求一组种子
seeds.setCount(seeds.getMaxStackSize());
checkIfRequestForItemExistOrCreateAsynch(seeds);

currentField.nextState();
return PREPARING;

}




/**
 * This (re)initializes a field. Checks the block above to see if it is a plant, if so, breaks it. Then tills.
 *
 * @return the next state to go into.
 */
private IAIState workAtField()
{
    @Nullable final BuildingFarmer buildingFarmer = getOwnBuilding();
if (buildingFarmer == null || checkForToolOrWeapon(ToolType.HOE) || buildingFarmer.getCurrentField() == null)
{
    return PREPARING;
}
@Nullable final BlockPos field = buildingFarmer.getCurrentField();
final TileEntity entity = world.getTileEntity(field);
if (entity instanceof ScarecrowTileEntity)
{
    final ScarecrowTileEntity scarecrow = (ScarecrowTileEntity) entity;
    if (workingOffset != null)
    {
        if (scarecrow.getOwnerId() != worker.getCitizenId())
        {
            buildingFarmer.freeField(buildingFarmer.getCurrentField());
            buildingFarmer.setCurrentField(null);
            return getState();
        }

        final BlockPos position = field.down().south(workingOffset.getZ()).east(workingOffset.getX());

        if (workingOffset.getX() &lt;= scarecrow.getLengthPlusX()
              && workingOffset.getZ() &lt;= scarecrow.getWidthPlusZ()
              && workingOffset.getX() &gt;= -scarecrow.getLengthMinusX()
              && workingOffset.getZ() &gt;= -scarecrow.getWidthMinusZ())
        {
            // Still moving to the block
            if (walkToBlock(position.up()))
            {
                return getState();
            }

            switch ((AIWorkerState) getState())
            {
                case FARMER_HOE:
                    worker.getCitizenStatusHandler().setLatestStatus(new TranslationTextComponent(&quot;com.minecolonies.coremod.status.hoeing&quot;));

                    if (!hoeIfAble(position, scarecrow))
                    {
                        return getState();
                    }
                    break;
                case FARMER_PLANT:
                    worker.getCitizenStatusHandler().setLatestStatus(new TranslationTextComponent(&quot;com.minecolonies.coremod.status.planting&quot;));
                    if (!tryToPlant(scarecrow, position))
                    {
                        return PREPARING;
                    }
                    break;
                case FARMER_HARVEST:
                    worker.getCitizenStatusHandler().setLatestStatus(new TranslationTextComponent(&quot;com.minecolonies.coremod.status.harvesting&quot;));
                    if (!harvestIfAble(position))
                    {
                        return getState();
                    }
                    break;
                default:
                    return PREPARING;
            }
            prevPos = position;
        }
        setDelay(getLevelDelay());
    }

    if (!handleOffset(scarecrow))
    {
        shouldDumpInventory = true;
        scarecrow.nextState();
        prevPos = null;
        return IDLE;
    }
}
else
{
    return IDLE;
}
return getState();

}

0

评论区