use of com.minecolonies.coremod.colony.buildings.modules.FarmerFieldModule in project minecolonies by ldtteam.
the class EntityAIWorkFarmer method workAtField.
/**
* 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();
final FarmerFieldModule module = buildingFarmer.getFirstModuleOccurance(FarmerFieldModule.class);
if (checkForToolOrWeapon(ToolType.HOE) || module.getCurrentField() == null) {
return PREPARING;
}
worker.getCitizenData().setVisibleStatus(FARMING_ICON);
@Nullable final BlockPos field = module.getCurrentField();
final TileEntity entity = world.getBlockEntity(field);
if (entity instanceof ScarecrowTileEntity) {
final ScarecrowTileEntity scarecrow = (ScarecrowTileEntity) entity;
if (workingOffset != null) {
if (scarecrow.getOwnerId() != worker.getCivilianID()) {
module.freeField(module.getCurrentField());
module.setCurrentField(null);
return getState();
}
final BlockPos position = field.below().south(workingOffset.getZ()).east(workingOffset.getX());
// Still moving to the block
if (walkToBlock(position.above())) {
return getState();
}
switch((AIWorkerState) getState()) {
case FARMER_HOE:
worker.getCitizenStatusHandler().setLatestStatus(new TranslationTextComponent("com.minecolonies.coremod.status.hoeing"));
if (!hoeIfAble(position, scarecrow)) {
return getState();
}
break;
case FARMER_PLANT:
worker.getCitizenStatusHandler().setLatestStatus(new TranslationTextComponent("com.minecolonies.coremod.status.planting"));
if (!tryToPlant(scarecrow, position)) {
return PREPARING;
}
break;
case FARMER_HARVEST:
worker.getCitizenStatusHandler().setLatestStatus(new TranslationTextComponent("com.minecolonies.coremod.status.harvesting"));
if (!harvestIfAble(position)) {
return getState();
}
break;
default:
return PREPARING;
}
prevPos = position;
setDelay(getLevelDelay());
}
workingOffset = nextValidCell(scarecrow);
if (workingOffset == null) {
shouldDumpInventory = true;
scarecrow.nextState();
prevPos = null;
return IDLE;
}
} else {
return IDLE;
}
return getState();
}
use of com.minecolonies.coremod.colony.buildings.modules.FarmerFieldModule in project minecolonies by Minecolonies.
the class EntityAIWorkFarmer method workAtField.
/**
* 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();
final FarmerFieldModule module = buildingFarmer.getFirstModuleOccurance(FarmerFieldModule.class);
if (checkForToolOrWeapon(ToolType.HOE) || module.getCurrentField() == null) {
return PREPARING;
}
worker.getCitizenData().setVisibleStatus(FARMING_ICON);
@Nullable final BlockPos field = module.getCurrentField();
final TileEntity entity = world.getBlockEntity(field);
if (entity instanceof ScarecrowTileEntity) {
final ScarecrowTileEntity scarecrow = (ScarecrowTileEntity) entity;
if (workingOffset != null) {
if (scarecrow.getOwnerId() != worker.getCivilianID()) {
module.freeField(module.getCurrentField());
module.setCurrentField(null);
return getState();
}
final BlockPos position = field.below().south(workingOffset.getZ()).east(workingOffset.getX());
// Still moving to the block
if (walkToBlock(position.above())) {
return getState();
}
switch((AIWorkerState) getState()) {
case FARMER_HOE:
worker.getCitizenStatusHandler().setLatestStatus(new TranslationTextComponent("com.minecolonies.coremod.status.hoeing"));
if (!hoeIfAble(position, scarecrow)) {
return getState();
}
break;
case FARMER_PLANT:
worker.getCitizenStatusHandler().setLatestStatus(new TranslationTextComponent("com.minecolonies.coremod.status.planting"));
if (!tryToPlant(scarecrow, position)) {
return PREPARING;
}
break;
case FARMER_HARVEST:
worker.getCitizenStatusHandler().setLatestStatus(new TranslationTextComponent("com.minecolonies.coremod.status.harvesting"));
if (!harvestIfAble(position)) {
return getState();
}
break;
default:
return PREPARING;
}
prevPos = position;
setDelay(getLevelDelay());
}
workingOffset = nextValidCell(scarecrow);
if (workingOffset == null) {
shouldDumpInventory = true;
scarecrow.nextState();
prevPos = null;
return IDLE;
}
} else {
return IDLE;
}
return getState();
}
use of com.minecolonies.coremod.colony.buildings.modules.FarmerFieldModule in project minecolonies by Minecolonies.
the class EntityAIWorkFarmer method canGoPlanting.
/**
* Checks if the farmer is ready to plant.
*
* @param currentField the field to plant.
* @param buildingFarmer the farmer building.
* @return true if he is ready.
*/
private IAIState canGoPlanting(@NotNull final ScarecrowTileEntity currentField, @NotNull final BuildingFarmer buildingFarmer) {
if (currentField.getSeed() == null || currentField.getSeed().isEmpty()) {
worker.getCitizenData().triggerInteraction(new PosBasedInteraction(new TranslationTextComponent(NO_SEED_SET, currentField.getBlockPos()), ChatPriority.BLOCKING, new TranslationTextComponent(NO_SEED_SET), currentField.getBlockPos()));
final FarmerFieldModule module = buildingFarmer.getFirstModuleOccurance(FarmerFieldModule.class);
module.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, seeds.getMaxStackSize(), 1);
currentField.nextState();
return PREPARING;
}
use of com.minecolonies.coremod.colony.buildings.modules.FarmerFieldModule in project minecolonies by Minecolonies.
the class EntityAIWorkFarmer method prepareForFarming.
/**
* Prepares the farmer for farming. Also requests the tools and checks if the farmer has sufficient fields.
*
* @return the next IAIState
*/
@NotNull
private IAIState prepareForFarming() {
@Nullable final BuildingFarmer building = getOwnBuilding();
if (building == null || building.getBuildingLevel() < 1) {
return PREPARING;
}
if (!job.getTaskQueue().isEmpty() || getActionsDoneUntilDumping() <= job.getActionsDone()) {
return START_WORKING;
}
worker.getCitizenData().setVisibleStatus(VisibleCitizenStatus.WORKING);
final FarmerFieldModule module = building.getFirstModuleOccurance(FarmerFieldModule.class);
module.syncWithColony(world);
if (module.getFarmerFields().size() < getOwnBuilding().getBuildingLevel() && !module.assignManually()) {
searchAndAddFields();
}
if (module.getFarmerFields().size() == getOwnBuilding().getMaxBuildingLevel()) {
AdvancementUtils.TriggerAdvancementPlayersForColony(building.getColony(), AdvancementTriggers.MAX_FIELDS::trigger);
}
final int amountOfCompostInBuilding = InventoryUtils.getCountFromBuilding(getOwnBuilding(), this::isCompost);
final int amountOfCompostInInv = InventoryUtils.getItemCountInItemHandler(worker.getInventoryCitizen(), this::isCompost);
if (amountOfCompostInBuilding + amountOfCompostInInv <= 0) {
if (getOwnBuilding().requestFertilizer() && !getOwnBuilding().hasWorkerOpenRequestsOfType(worker.getCitizenData().getId(), TypeToken.of(StackList.class))) {
final List<ItemStack> compostAbleItems = new ArrayList<>();
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 <= 0 && amountOfCompostInBuilding > 0) {
needsCurrently = new Tuple<>(this::isCompost, STACKSIZE);
return GATHERING_REQUIRED_MATERIALS;
}
if (module.hasNoFields()) {
if (worker.getCitizenData() != null) {
worker.getCitizenData().triggerInteraction(new StandardInteraction(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 (module.getCurrentField() == null && module.getFieldToWorkOn(world) == null) {
module.resetFields();
return IDLE;
}
@Nullable final BlockPos currentField = module.getCurrentField();
final TileEntity entity = world.getBlockEntity(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 -> this.shouldHoe(pos, (ScarecrowTileEntity) entity))) {
return FARMER_HOE;
}
((ScarecrowTileEntity) entity).nextState();
} else {
module.setCurrentField(null);
}
return PREPARING;
}
use of com.minecolonies.coremod.colony.buildings.modules.FarmerFieldModule in project minecolonies by ldtteam.
the class EntityAIWorkFarmer method prepareForFarming.
/**
* Prepares the farmer for farming. Also requests the tools and checks if the farmer has sufficient fields.
*
* @return the next IAIState
*/
@NotNull
private IAIState prepareForFarming() {
@Nullable final BuildingFarmer building = getOwnBuilding();
if (building == null || building.getBuildingLevel() < 1) {
return PREPARING;
}
if (!job.getTaskQueue().isEmpty() || getActionsDoneUntilDumping() <= job.getActionsDone()) {
return START_WORKING;
}
worker.getCitizenData().setVisibleStatus(VisibleCitizenStatus.WORKING);
final FarmerFieldModule module = building.getFirstModuleOccurance(FarmerFieldModule.class);
module.syncWithColony(world);
if (module.getFarmerFields().size() < getOwnBuilding().getBuildingLevel() && !module.assignManually()) {
searchAndAddFields();
}
if (module.getFarmerFields().size() == getOwnBuilding().getMaxBuildingLevel()) {
AdvancementUtils.TriggerAdvancementPlayersForColony(building.getColony(), AdvancementTriggers.MAX_FIELDS::trigger);
}
final int amountOfCompostInBuilding = InventoryUtils.getCountFromBuilding(getOwnBuilding(), this::isCompost);
final int amountOfCompostInInv = InventoryUtils.getItemCountInItemHandler(worker.getInventoryCitizen(), this::isCompost);
if (amountOfCompostInBuilding + amountOfCompostInInv <= 0) {
if (getOwnBuilding().requestFertilizer() && !getOwnBuilding().hasWorkerOpenRequestsOfType(worker.getCitizenData().getId(), TypeToken.of(StackList.class))) {
final List<ItemStack> compostAbleItems = new ArrayList<>();
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 <= 0 && amountOfCompostInBuilding > 0) {
needsCurrently = new Tuple<>(this::isCompost, STACKSIZE);
return GATHERING_REQUIRED_MATERIALS;
}
if (module.hasNoFields()) {
if (worker.getCitizenData() != null) {
worker.getCitizenData().triggerInteraction(new StandardInteraction(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 (module.getCurrentField() == null && module.getFieldToWorkOn(world) == null) {
module.resetFields();
return IDLE;
}
@Nullable final BlockPos currentField = module.getCurrentField();
final TileEntity entity = world.getBlockEntity(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 -> this.shouldHoe(pos, (ScarecrowTileEntity) entity))) {
return FARMER_HOE;
}
((ScarecrowTileEntity) entity).nextState();
} else {
module.setCurrentField(null);
}
return PREPARING;
}
Aggregations