use of com.minecolonies.api.entity.ai.statemachine.states.IAIState in project minecolonies by ldtteam.
the class AbstractEntityAICrafting method getRecipe.
/**
* Query the IRecipeStorage of the first request in the queue.
*
* @return the next state to go to.
*/
protected IAIState getRecipe() {
final IRequest<? extends PublicCrafting> currentTask = job.getCurrentTask();
if (currentTask == null) {
return START_WORKING;
}
final ICraftingBuildingModule module = getOwnBuilding().getCraftingModuleForRecipe(currentTask.getRequest().getRecipeID());
if (module == null) {
job.finishRequest(false);
incrementActionsDone(getActionRewardForCraftingSuccess());
return START_WORKING;
}
currentRecipeStorage = module.getFirstFulfillableRecipe(stack -> ItemStackUtils.compareItemStacksIgnoreStackSize(stack, currentTask.getRequest().getStack()), 1, false);
if (currentRecipeStorage == null) {
job.finishRequest(false);
incrementActionsDone(getActionRewardForCraftingSuccess());
return START_WORKING;
}
currentRequest = currentTask;
job.setMaxCraftingCount(currentRequest.getRequest().getCount());
final int currentCount = InventoryUtils.getItemCountInItemHandler(worker.getInventoryCitizen(), stack -> ItemStackUtils.compareItemStacksIgnoreStackSize(stack, currentRecipeStorage.getPrimaryOutput()));
final int inProgressCount = getExtendedCount(currentRecipeStorage.getPrimaryOutput());
final int countPerIteration = currentRecipeStorage.getPrimaryOutput().getCount();
final int doneOpsCount = currentCount / countPerIteration;
final int progressOpsCount = inProgressCount / countPerIteration;
final int remainingOpsCount = currentRequest.getRequest().getCount() - doneOpsCount - progressOpsCount;
final List<ItemStorage> input = currentRecipeStorage.getCleanedInput();
for (final ItemStorage inputStorage : input) {
final ItemStack container = inputStorage.getItem().getContainerItem(inputStorage.getItemStack());
final int remaining;
if (!currentRecipeStorage.getCraftingToolsAndSecondaryOutputs().isEmpty() && ItemStackUtils.compareItemStackListIgnoreStackSize(currentRecipeStorage.getCraftingToolsAndSecondaryOutputs(), inputStorage.getItemStack(), false, true)) {
remaining = inputStorage.getAmount();
} else if (!ItemStackUtils.isEmpty(container) && ItemStackUtils.compareItemStacksIgnoreStackSize(inputStorage.getItemStack(), container, false, true)) {
remaining = inputStorage.getAmount();
} else {
remaining = inputStorage.getAmount() * remainingOpsCount;
}
if (InventoryUtils.getCountFromBuilding(getOwnBuilding(), itemStack -> ItemStackUtils.compareItemStacksIgnoreStackSize(itemStack, inputStorage.getItemStack(), false, true)) + InventoryUtils.getItemCountInItemHandler(worker.getInventoryCitizen(), itemStack -> ItemStackUtils.compareItemStacksIgnoreStackSize(itemStack, inputStorage.getItemStack(), false, true)) + getExtendedCount(inputStorage.getItemStack()) < remaining) {
job.finishRequest(false);
incrementActionsDone(getActionRewardForCraftingSuccess());
return START_WORKING;
}
}
job.setCraftCounter(doneOpsCount);
return QUERY_ITEMS;
}
use of com.minecolonies.api.entity.ai.statemachine.states.IAIState in project minecolonies by ldtteam.
the class EntityAIWorkBeekeeper method decideWhatToDo.
/**
* Decides what job the beekeeper should switch to, breeding or harvesting.
*
* @return The next {@link IAIState} the beekeeper should switch to, after executing this method.
*/
private IAIState decideWhatToDo() {
setDelay(DECIDING_DELAY + (99 / getSecondarySkillLevel() - 1));
final Set<BlockPos> hives = getOwnBuilding().getHives();
if (hives.isEmpty()) {
worker.getCitizenData().triggerInteraction(new StandardInteraction(new TranslationTextComponent(NO_HIVES), ChatPriority.BLOCKING));
setDelay(NO_HIVES_DELAY);
return DECIDE;
}
ItemListModule flowersModule = getOwnBuilding().getModuleMatching(ItemListModule.class, m -> m.getId().equals(BUILDING_FLOWER_LIST));
if (flowersModule.getList().isEmpty()) {
worker.getCitizenData().triggerInteraction(new StandardInteraction(new TranslationTextComponent(COM_MINECOLONIES_COREMOD_BEEKEEPER_NOFLOWERS), ChatPriority.BLOCKING));
setDelay(NO_FLOWERS_DELAY);
return DECIDE;
}
for (BlockPos pos : hives) {
if (!(world.getBlockState(pos).getBlock() instanceof BeehiveBlock)) {
getOwnBuilding().removeHive(pos);
}
}
final Optional<BlockPos> hive = getOwnBuilding().getHives().stream().filter(pos -> BeehiveTileEntity.getHoneyLevel(world.getBlockState(pos)) >= 5).findFirst();
if (hive.isPresent()) {
return BEEKEEPER_HARVEST;
}
final List<BeeEntity> bees = new ArrayList<>(searchForAnimals(world, getOwnBuilding()));
final JobBeekeeper job = worker.getCitizenJobHandler().getColonyJob(JobBeekeeper.class);
if (bees.isEmpty()) {
if (getBeesInHives() <= 0) {
job.tickNoBees();
if (job.checkForBeeInteraction()) {
worker.getCitizenData().triggerInteraction(new StandardInteraction(new TranslationTextComponent(NO_BEES), ChatPriority.BLOCKING));
}
} else {
job.resetCounter();
}
setDelay(NO_ANIMALS_DELAY);
return DECIDE;
} else {
job.resetCounter();
}
worker.getCitizenStatusHandler().setLatestStatus(new TranslationTextComponent(TranslationConstants.COM_MINECOLONIES_COREMOD_STATUS_DECIDING));
final int breedableAnimals = (int) bees.stream().filter(animal -> animal.getAge() == 0).count();
final boolean hasBreedingItem = InventoryUtils.hasItemInItemHandler(worker.getInventoryCitizen(), (stack) -> flowersModule.isItemInList(new ItemStorage(stack)));
if (getOwnBuilding().getSetting(BuildingBeekeeper.BREEDING).getValue() && !hasMaxAnimals(bees) && breedableAnimals >= NUM_OF_ANIMALS_TO_BREED && hasBreedingItem) {
return HERDER_BREED;
}
return START_WORKING;
}
use of com.minecolonies.api.entity.ai.statemachine.states.IAIState in project minecolonies by ldtteam.
the class EntityAIWorkBeekeeper method prepareForHerding.
/**
* Prepares the beekeeper for herding
*
* @return The next {@link IAIState}.
*/
private IAIState prepareForHerding() {
setDelay(DECIDING_DELAY);
if (!getOwnBuilding().getHarvestTypes().equals(BuildingBeekeeper.HONEY)) {
if (checkForToolOrWeapon(ToolType.SHEARS)) {
return getState();
}
}
if (!getOwnBuilding().getHarvestTypes().equals(BuildingBeekeeper.HONEYCOMB)) {
checkIfRequestForItemExistOrCreateAsynch(new ItemStack(Items.GLASS_BOTTLE));
}
List<ItemStorage> allowedFlowers = getOwnBuilding().getModuleMatching(ItemListModule.class, m -> m.getId().equals(BUILDING_FLOWER_LIST)).getList();
;
if (!InventoryUtils.hasItemInItemHandler(worker.getInventoryCitizen(), (stack) -> allowedFlowers.contains(new ItemStorage(stack))) && InventoryUtils.getCountFromBuilding(getOwnBuilding(), allowedFlowers) == 0 && !getOwnBuilding().hasWorkerOpenRequestsOfType(worker.getCitizenData().getId(), TypeToken.of(StackList.class))) {
worker.getCitizenData().createRequestAsync(new StackList(allowedFlowers.stream().map((item) -> item.getItemStack()).peek((stack) -> stack.setCount(16)).collect(Collectors.toList()), COM_MINECOLONIES_COREMOD_REQUEST_FLOWERS, 16, 1));
}
return DECIDE;
}
use of com.minecolonies.api.entity.ai.statemachine.states.IAIState in project minecolonies by ldtteam.
the class EntityAIWorkLumberjack method findTree.
/**
* Search for a tree.
*
* @return LUMBERJACK_GATHERING if job was canceled.
*/
private IAIState findTree() {
final BuildingLumberjack building = getOwnBuilding();
worker.getCitizenData().setVisibleStatus(SEARCH);
if (pathResult != null && pathResult.isComputing()) {
return getState();
}
if (pathResult == null) {
if (building.shouldRestrict()) {
final BlockPos startPos = building.getStartRestriction();
final BlockPos endPos = building.getEndRestriction();
pathResult = worker.getNavigation().moveToTree(startPos, endPos, 1.0D, building.getModuleMatching(ItemListModule.class, m -> m.getId().equals(SAPLINGS_LIST)).getList(), building.getSetting(BuildingLumberjack.DYNAMIC_TREES_SIZE).getValue(), worker.getCitizenColonyHandler().getColony());
} else {
pathResult = worker.getNavigation().moveToTree(SEARCH_RANGE + searchIncrement, 1.0D, building.getModuleMatching(ItemListModule.class, m -> m.getId().equals(SAPLINGS_LIST)).getList(), building.getSetting(BuildingLumberjack.DYNAMIC_TREES_SIZE).getValue(), worker.getCitizenColonyHandler().getColony());
}
return getState();
}
if (pathResult.isDone()) {
return setNewTree(building);
}
// None of the above yielded a result, report no trees found.
return LUMBERJACK_NO_TREES_FOUND;
}
use of com.minecolonies.api.entity.ai.statemachine.states.IAIState in project minecolonies by ldtteam.
the class EntityAIWorkHealer method cure.
/**
* Give a citizen the cure.
*
* @return the next state to go to.
*/
private IAIState cure() {
if (currentPatient == null) {
return DECIDE;
}
final ICitizenData data = getOwnBuilding().getColony().getCitizenManager().getCivilian(currentPatient.getId());
if (data == null || !data.getEntity().isPresent() || !data.getEntity().get().getCitizenDiseaseHandler().isSick()) {
currentPatient = null;
return DECIDE;
}
final EntityCitizen citizen = (EntityCitizen) data.getEntity().get();
if (walkToBlock(data.getEntity().get().blockPosition())) {
return CURE;
}
final String diseaseName = citizen.getCitizenDiseaseHandler().getDisease();
final Disease disease = IColonyManager.getInstance().getCompatibilityManager().getDisease(diseaseName);
if (diseaseName.isEmpty()) {
currentPatient = null;
citizen.heal(10);
worker.getCitizenExperienceHandler().addExperience(BASE_XP_GAIN);
citizen.markDirty();
return DECIDE;
}
if (!hasCureInInventory(disease, worker.getInventoryCitizen())) {
if (hasCureInInventory(disease, getOwnBuilding().getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).orElseGet(null))) {
for (final ItemStack cure : disease.getCure()) {
if (InventoryUtils.getItemCountInItemHandler(worker.getInventoryCitizen(), stack -> stack.sameItem(cure)) < cure.getCount()) {
needsCurrently = new Tuple<>(stack -> stack.sameItem(cure), 1);
return GATHERING_REQUIRED_MATERIALS;
}
}
}
currentPatient = null;
return DECIDE;
}
if (!hasCureInInventory(disease, citizen.getInventoryCitizen())) {
for (final ItemStack cure : disease.getCure()) {
if (InventoryUtils.getItemCountInItemHandler(citizen.getInventoryCitizen(), stack -> stack.sameItem(cure)) < cure.getCount()) {
if (InventoryUtils.isItemHandlerFull(citizen.getInventoryCitizen())) {
data.triggerInteraction(new StandardInteraction(new TranslationTextComponent(PATIENT_FULL_INVENTORY), ChatPriority.BLOCKING));
currentPatient = null;
return DECIDE;
}
InventoryUtils.transferXOfFirstSlotInItemHandlerWithIntoNextFreeSlotInItemHandler(worker.getInventoryCitizen(), cure::sameItem, cure.getCount(), citizen.getInventoryCitizen());
}
}
}
worker.getCitizenExperienceHandler().addExperience(BASE_XP_GAIN);
currentPatient.setState(Patient.PatientState.TREATED);
currentPatient = null;
return DECIDE;
}
Aggregations