use of com.minecolonies.api.entity.ai.statemachine.states.IAIState in project minecolonies by Minecolonies.
the class EntityAIWorkComposter method getMaterials.
/**
* Method for the AI to try to get the materials needed for the task he's doing. Will request if there are no materials
*
* @return the new IAIState after doing this
*/
private IAIState getMaterials() {
if (walkToBuilding()) {
setDelay(2);
return getState();
}
final List<ItemStorage> list = building.getModuleMatching(ItemListModule.class, m -> m.getId().equals(COMPOSTABLE_LIST)).getList();
if (list.isEmpty()) {
complain();
return getState();
}
if (InventoryUtils.hasItemInProvider(building, stack -> list.contains(new ItemStorage(stack)))) {
InventoryUtils.transferItemStackIntoNextFreeSlotFromProvider(building, InventoryUtils.findFirstSlotInProviderNotEmptyWith(building, stack -> list.contains(new ItemStorage(stack))), worker.getInventoryCitizen());
}
final int slot = InventoryUtils.findFirstSlotInItemHandlerWith(worker.getInventoryCitizen(), stack -> list.contains(new ItemStorage(stack)));
if (slot >= 0) {
worker.setItemInHand(Hand.MAIN_HAND, worker.getInventoryCitizen().getStackInSlot(slot));
return START_WORKING;
}
worker.setItemInHand(Hand.MAIN_HAND, ItemStack.EMPTY);
if (!building.hasWorkerOpenRequests(worker.getCitizenData().getId())) {
final ArrayList<ItemStack> itemList = new ArrayList<>();
for (final ItemStorage item : list) {
final ItemStack itemStack = item.getItemStack();
itemStack.setCount(itemStack.getMaxStackSize());
itemList.add(itemStack);
}
if (!itemList.isEmpty()) {
worker.getCitizenData().createRequestAsync(new StackList(itemList, RequestSystemTranslationConstants.REQUESTS_TYPE_COMPOSTABLE, Constants.STACKSIZE * building.getBarrels().size(), 1, building.getSetting(BuildingComposter.MIN).getValue()));
}
}
setDelay(2);
return START_WORKING;
}
use of com.minecolonies.api.entity.ai.statemachine.states.IAIState in project minecolonies by Minecolonies.
the class EntityAIConcreteMixer method craft.
@Override
protected IAIState craft() {
if (currentRecipeStorage == null) {
return START_WORKING;
}
if (currentRequest == null && job.getCurrentTask() != null) {
return GET_RECIPE;
}
if (walkTo == null && walkToBuilding()) {
return getState();
}
currentRequest = job.getCurrentTask();
if (currentRequest != null && (currentRequest.getState() == RequestState.CANCELLED || currentRequest.getState() == RequestState.FAILED)) {
currentRequest = null;
incrementActionsDone(getActionRewardForCraftingSuccess());
currentRecipeStorage = null;
return START_WORKING;
}
final ItemStack concrete = currentRecipeStorage.getPrimaryOutput();
if (concrete.getItem() instanceof BlockItem && ((BlockItem) concrete.getItem()).getBlock() instanceof ConcretePowderBlock) {
return super.craft();
}
final IAIState mixState = mixConcrete();
if (mixState == getState()) {
currentRequest.addDelivery(new ItemStack(concrete.getItem(), 1));
job.setCraftCounter(job.getCraftCounter() + 1);
if (job.getCraftCounter() >= job.getMaxCraftingCount()) {
incrementActionsDone(getActionRewardForCraftingSuccess());
currentRecipeStorage = null;
resetValues();
if (inventoryNeedsDump()) {
if (job.getMaxCraftingCount() == 0 && job.getCraftCounter() == 0 && currentRequest != null) {
job.finishRequest(true);
worker.getCitizenExperienceHandler().addExperience(currentRequest.getRequest().getCount() / 2.0);
}
}
}
}
return mixState;
}
use of com.minecolonies.api.entity.ai.statemachine.states.IAIState in project minecolonies by Minecolonies.
the class EntityAIWorkCrusher method craft.
/**
* The actual crafting logic.
*
* @return the next state to go to.
*/
@Override
protected IAIState craft() {
if (currentRecipeStorage == null) {
return START_WORKING;
}
if (currentRequest == null && job.getCurrentTask() != null) {
return GET_RECIPE;
}
if (walkToBuilding()) {
return getState();
}
job.setProgress(job.getProgress() + 1);
worker.setItemInHand(Hand.MAIN_HAND, currentRecipeStorage.getCleanedInput().get(worker.getRandom().nextInt(currentRecipeStorage.getCleanedInput().size())).getItemStack().copy());
worker.setItemInHand(Hand.OFF_HAND, currentRecipeStorage.getPrimaryOutput().copy());
worker.getCitizenItemHandler().hitBlockWithToolInHand(building.getPosition());
currentRequest = job.getCurrentTask();
if (currentRequest != null && (currentRequest.getState() == RequestState.CANCELLED || currentRequest.getState() == RequestState.FAILED)) {
currentRequest = null;
incrementActionsDone(getActionRewardForCraftingSuccess());
currentRecipeStorage = null;
return START_WORKING;
}
final IAIState check = crush();
if (check == getState()) {
if (job.getCraftCounter() >= job.getMaxCraftingCount()) {
incrementActionsDone(getActionRewardForCraftingSuccess());
currentRecipeStorage = null;
resetValues();
if (inventoryNeedsDump()) {
if (job.getMaxCraftingCount() == 0 && job.getProgress() == 0 && job.getCraftCounter() == 0 && currentRequest != null) {
job.finishRequest(true);
}
}
}
} else {
currentRequest = null;
job.finishRequest(false);
incrementActionsDoneAndDecSaturation();
resetValues();
}
return getState();
}
use of com.minecolonies.api.entity.ai.statemachine.states.IAIState in project minecolonies by Minecolonies.
the class EntityAIWorkCrusher method crush.
/**
* The crushing process.
*
* @return the next AiState to go to.
*/
protected IAIState crush() {
if (walkToBuilding()) {
return getState();
}
worker.getCitizenData().setVisibleStatus(CRUSHING);
job.setProgress(job.getProgress() + TICK_DELAY);
final BuildingCrusher crusherBuilding = building;
WorkerUtil.faceBlock(crusherBuilding.getPosition(), worker);
final IRecipeStorage recipeMode = crusherBuilding.getSetting(BuildingCrusher.MODE).getValue(crusherBuilding);
final int dailyLimit = crusherBuilding.getSetting(BuildingCrusher.DAILY_LIMIT).getValue();
if (currentRecipeStorage == null) {
currentRecipeStorage = recipeMode;
}
if ((getState() != CRAFT && crusherBuilding.getCurrentDailyQuantity() >= dailyLimit) || currentRecipeStorage == null) {
return START_WORKING;
}
final IAIState check = checkForItems(currentRecipeStorage);
if (job.getProgress() > MAX_LEVEL - Math.min((getSecondarySkillLevel() / 2) + 1, MAX_LEVEL)) {
job.setProgress(0);
if (check == CRAFT) {
if (getState() != CRAFT) {
crusherBuilding.setCurrentDailyQuantity(crusherBuilding.getCurrentDailyQuantity() + 1);
if (crusherBuilding.getCurrentDailyQuantity() >= dailyLimit) {
incrementActionsDoneAndDecSaturation();
}
}
if (currentRequest != null) {
currentRequest.addDelivery(currentRecipeStorage.getPrimaryOutput());
}
worker.swing(Hand.MAIN_HAND);
job.setCraftCounter(job.getCraftCounter() + 1);
currentRecipeStorage.fullfillRecipe(getLootContext(), ImmutableList.of(worker.getItemHandlerCitizen()));
worker.decreaseSaturationForContinuousAction();
worker.getCitizenExperienceHandler().addExperience(0.1);
} else if (getState() != CRAFT) {
currentRecipeStorage = recipeMode;
final int requestQty = Math.min((dailyLimit - crusherBuilding.getCurrentDailyQuantity()) * 2, STACKSIZE);
if (requestQty <= 0) {
return START_WORKING;
}
final ItemStack stack = currentRecipeStorage.getInput().get(0).getItemStack().copy();
stack.setCount(requestQty);
checkIfRequestForItemExistOrCreateAsync(stack);
return START_WORKING;
} else {
return check;
}
}
if (check == CRAFT) {
Network.getNetwork().sendToTrackingEntity(new LocalizedParticleEffectMessage(currentRecipeStorage.getInput().get(0).getItemStack().copy(), crusherBuilding.getID()), worker);
Network.getNetwork().sendToTrackingEntity(new LocalizedParticleEffectMessage(currentRecipeStorage.getPrimaryOutput().copy(), crusherBuilding.getID().below()), worker);
SoundUtils.playSoundAtCitizen(world, building.getID(), SoundEvents.STONE_BREAK);
}
return getState();
}
use of com.minecolonies.api.entity.ai.statemachine.states.IAIState in project minecolonies by Minecolonies.
the class EntityAIWorkDeliveryman method deliver.
/**
* Deliver the items to the hut. TODO: Current precondition: The dman's inventory may only consist of the requested itemstack.
*
* @return the next state.
*/
private IAIState deliver() {
final IRequest<? extends IDeliverymanRequestable> currentTask = job.getCurrentTask();
if (!(currentTask instanceof DeliveryRequest)) {
// Since prepareDelivery() was called earlier, go dumping first and then restart.
return DUMPING;
}
worker.getCitizenData().setVisibleStatus(DELIVERING);
worker.getCitizenStatusHandler().setLatestStatus(new TranslationTextComponent("com.minecolonies.coremod.status.delivering"));
final ILocation targetBuildingLocation = ((Delivery) currentTask.getRequest()).getTarget();
if (!targetBuildingLocation.isReachableFromLocation(worker.getLocation())) {
Log.getLogger().info(worker.getCitizenColonyHandler().getColony().getName() + ": " + worker.getName() + ": Can't inter dimension yet: ");
return START_WORKING;
}
if (!worker.isWorkerAtSiteWithMove(targetBuildingLocation.getInDimensionLocation(), MIN_DISTANCE_TO_WAREHOUSE)) {
setDelay(WALK_DELAY);
return DELIVERY;
}
final TileEntity tileEntity = world.getBlockEntity(targetBuildingLocation.getInDimensionLocation());
if (!(tileEntity instanceof TileEntityColonyBuilding)) {
// TODO: Non-Colony deliveries are unsupported yet. Fix that at some point in time.
job.finishRequest(true);
return START_WORKING;
}
final IBuilding targetBuilding = ((AbstractTileEntityColonyBuilding) tileEntity).getBuilding();
boolean success = true;
boolean extracted = false;
final IItemHandler workerInventory = worker.getInventoryCitizen();
final List<ItemStorage> itemsToDeliver = job.getTaskListWithSameDestination((IRequest<? extends Delivery>) currentTask).stream().map(r -> new ItemStorage(r.getRequest().getStack())).collect(Collectors.toList());
for (int i = 0; i < workerInventory.getSlots(); i++) {
if (workerInventory.getStackInSlot(i).isEmpty()) {
continue;
}
if (!itemsToDeliver.contains(new ItemStorage(workerInventory.getStackInSlot(i)))) {
continue;
}
final ItemStack stack = workerInventory.extractItem(i, Integer.MAX_VALUE, false);
if (ItemStackUtils.isEmpty(stack)) {
continue;
}
extracted = true;
final ItemStack insertionResultStack;
// TODO: Please only push items into the target that were actually requested.
if (targetBuilding instanceof AbstractBuilding) {
insertionResultStack = InventoryUtils.forceItemStackToItemHandler(targetBuilding.getCapability(ITEM_HANDLER_CAPABILITY, null).orElseGet(null), stack, ((IBuilding) targetBuilding)::isItemStackInRequest);
} else {
// Buildings that are not inherently part of the request system, but just receive a delivery, cannot have their items replaced.
// Therefore, the keep-predicate always returns true.
insertionResultStack = InventoryUtils.forceItemStackToItemHandler(targetBuilding.getCapability(ITEM_HANDLER_CAPABILITY, null).orElseGet(null), stack, itemStack -> true);
}
if (!ItemStackUtils.isEmpty(insertionResultStack)) {
if (ItemStack.matches(insertionResultStack, stack) && worker.getCitizenData() != null) {
// The replaced stack is the same as the one we tried to put into the inventory.
// Meaning, replacing failed.
success = false;
if (targetBuilding.hasModule(WorkerBuildingModule.class)) {
worker.getCitizenData().triggerInteraction(new PosBasedInteraction(new TranslationTextComponent(COM_MINECOLONIES_COREMOD_JOB_DELIVERYMAN_NAMEDCHESTFULL, targetBuilding.getFirstModuleOccurance(WorkerBuildingModule.class).getFirstCitizen().getName()), ChatPriority.IMPORTANT, new TranslationTextComponent(COM_MINECOLONIES_COREMOD_JOB_DELIVERYMAN_CHESTFULL), targetBuilding.getID()));
} else {
worker.getCitizenData().triggerInteraction(new PosBasedInteraction(new TranslationTextComponent(COM_MINECOLONIES_COREMOD_JOB_DELIVERYMAN_CHESTFULL, new StringTextComponent(" :" + targetBuilding.getSchematicName())), ChatPriority.IMPORTANT, new TranslationTextComponent(COM_MINECOLONIES_COREMOD_JOB_DELIVERYMAN_CHESTFULL), targetBuildingLocation.getInDimensionLocation()));
}
}
// Insert the result back into the inventory so we do not lose it.
workerInventory.insertItem(i, insertionResultStack, false);
}
}
if (!extracted) {
// This can only happen if the dman's inventory was completely empty.
// Let the retry-system handle this case.
worker.decreaseSaturationForContinuousAction();
worker.getCitizenItemHandler().setHeldItem(Hand.MAIN_HAND, SLOT_HAND);
job.finishRequest(false);
// No need to go dumping in this case.
return START_WORKING;
}
worker.getCitizenExperienceHandler().addExperience(1.5D);
worker.decreaseSaturationForContinuousAction();
worker.getCitizenItemHandler().setHeldItem(Hand.MAIN_HAND, SLOT_HAND);
job.finishRequest(true);
return success ? START_WORKING : DUMPING;
}
Aggregations