use of com.minecolonies.api.entity.ai.statemachine.states.IAIState in project minecolonies by Minecolonies.
the class EntityAIWorkAlchemist method checkBrewingStandFuel.
/**
* Check Fuel levels in the brewingStand
*/
private IAIState checkBrewingStandFuel() {
if (currentRecipeStorage == null || currentRecipeStorage.getIntermediate() != Blocks.BREWING_STAND) {
return getState();
}
final World world = building.getColony().getWorld();
if (!InventoryUtils.hasItemInItemHandler(worker.getInventoryCitizen(), Items.BLAZE_POWDER) && !InventoryUtils.hasItemInProvider(building, Items.BLAZE_POWDER) && !building.hasWorkerOpenRequestsOfType(worker.getCitizenData().getId(), TypeToken.of(StackList.class))) {
worker.getCitizenData().createRequestAsync(new Stack(new ItemStack(Items.BLAZE_POWDER), BREWING_MIN_FUEL_COUNT * building.getAllBrewingStandPositions().size(), 1));
return getState();
}
for (final BlockPos pos : building.getAllBrewingStandPositions()) {
if (WorldUtil.isBlockLoaded(world, pos)) {
final TileEntity entity = world.getBlockEntity(pos);
if (entity instanceof BrewingStandTileEntity) {
final BrewingStandTileEntity brewingStand = (BrewingStandTileEntity) entity;
if (brewingStand.brewTime <= 0 && (hasBrewableAndNoFuel(brewingStand) || hasNeitherFuelNorBrewable(brewingStand))) {
if (!InventoryUtils.hasItemInItemHandler(worker.getInventoryCitizen(), Items.BLAZE_POWDER)) {
if (InventoryUtils.hasItemInProvider(building, Items.BLAZE_POWDER)) {
needsCurrently = new Tuple<>(item -> item.getItem() == Items.BLAZE_POWDER, BREWING_MIN_FUEL_COUNT);
walkTo = null;
return GATHERING_REQUIRED_MATERIALS;
}
// We need to wait for Fuel to arrive
return getState();
}
fuelPos = pos;
if (preFuelState == null) {
preFuelState = getState();
}
return ADD_FUEL_TO_BREWINGSTAND;
}
} else {
building.removeBrewingStand(pos);
}
}
}
return getState();
}
use of com.minecolonies.api.entity.ai.statemachine.states.IAIState in project minecolonies by Minecolonies.
the class EntityAIWorkBeekeeper method harvestHoney.
/**
* Harvest honey/honeycomb from full beehives.
*
* @return The next {@link IAIState}.
*/
private IAIState harvestHoney() {
worker.getCitizenStatusHandler().setLatestStatus(new TranslationTextComponent(COM_MINECOLONIES_COREMOD_STATUS_BEEKEEPER_HARVESTING));
final List<BlockPos> hives = building.getHives().stream().filter(pos -> BeehiveTileEntity.getHoneyLevel(world.getBlockState(pos)) >= 5).collect(Collectors.toList());
if (hives.isEmpty()) {
return DECIDE;
}
if (building.getHarvestTypes().equals(BuildingBeekeeper.HONEYCOMB) || (building.getHarvestTypes().equals(BuildingBeekeeper.BOTH) && lastHarvestedBottle)) {
if (!equipTool(Hand.MAIN_HAND, ToolType.SHEARS)) {
return PREPARING;
}
} else {
if (!equipItem(Hand.MAIN_HAND, new ItemStack(Items.GLASS_BOTTLE))) {
return PREPARING;
}
}
final BlockPos hive = hives.get(0);
if (!world.getBlockState(hive).is(BlockTags.BEEHIVES)) {
building.removeHive(hive);
return PREPARING;
}
if (walkToBlock(hive)) {
return getState();
}
worker.swing(Hand.MAIN_HAND);
final ItemStack itemStack = worker.getMainHandItem();
if (!building.getHarvestTypes().equals(BuildingBeekeeper.HONEY) && ItemStackUtils.isTool(itemStack, ToolType.SHEARS)) {
worker.getCitizenItemHandler().damageItemInHand(Hand.MAIN_HAND, 1);
for (ItemStack stackItem : Compatibility.getCombsFromHive(hive, world, getHoneycombsPerHarvest())) {
InventoryUtils.transferItemStackIntoNextBestSlotInItemHandler(stackItem, worker.getItemHandlerCitizen());
}
world.setBlockAndUpdate(hive, world.getBlockState(hive).setValue(BlockStateProperties.LEVEL_HONEY, 0));
worker.getCitizenExperienceHandler().addExperience(EXP_PER_HARVEST);
lastHarvestedBottle = false;
} else if (!building.getHarvestTypes().equals(BuildingBeekeeper.HONEYCOMB) && itemStack.getItem() == Items.GLASS_BOTTLE) {
int i;
for (i = 0; i < getHoneyBottlesPerHarvest() && !itemStack.isEmpty(); i++) {
itemStack.shrink(1);
}
InventoryUtils.transferItemStackIntoNextBestSlotInItemHandler(new ItemStack(Items.HONEY_BOTTLE, i), worker.getItemHandlerCitizen());
world.setBlockAndUpdate(hive, world.getBlockState(hive).setValue(BlockStateProperties.LEVEL_HONEY, 0));
worker.getCitizenExperienceHandler().addExperience(EXP_PER_HARVEST);
lastHarvestedBottle = true;
}
final int dex = getPrimarySkillLevel();
if ((50 - (dex / 99. * 50.)) / 100 > worker.getRandom().nextDouble()) {
final List<Entity> bees = ((BeehiveTileEntity) world.getBlockEntity(hive)).releaseAllOccupants(world.getBlockState(hive), BeehiveTileEntity.State.EMERGENCY);
bees.stream().filter(entity -> entity instanceof BeeEntity).map(entity -> (BeeEntity) entity).filter(bee -> worker.position().distanceToSqr(bee.position()) <= 16.0D).forEach(bee -> {
bee.setRemainingPersistentAngerTime(400 + worker.getRandom().nextInt(400));
bee.setLastHurtByMob(worker);
});
}
incrementActionsDoneAndDecSaturation();
return START_WORKING;
}
use of com.minecolonies.api.entity.ai.statemachine.states.IAIState in project minecolonies by Minecolonies.
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 = building.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(building, 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 Minecolonies.
the class AbstractEntityAIRequestSmelter method retrieveSmeltableFromFurnace.
/**
* Retrieve ready bars from the furnaces. If no position has been set return. Else navigate to the position of the furnace. On arrival execute the extract method of the
* specialized worker.
*
* @return the next state to go to.
*/
private IAIState retrieveSmeltableFromFurnace() {
worker.getCitizenStatusHandler().setLatestStatus(new TranslationTextComponent(COM_MINECOLONIES_COREMOD_STATUS_RETRIEVING));
if (walkTo == null || currentRequest == null) {
return START_WORKING;
}
final TileEntity entity = world.getBlockEntity(walkTo);
if (!(entity instanceof FurnaceTileEntity) || (isEmpty(((FurnaceTileEntity) entity).getItem(RESULT_SLOT)))) {
walkTo = null;
return START_WORKING;
}
if (walkToBlock(walkTo)) {
return getState();
}
walkTo = null;
final int preExtractCount = InventoryUtils.getItemCountInItemHandler(worker.getInventoryCitizen(), stack -> ItemStackUtils.compareItemStacksIgnoreStackSize(currentRequest.getRequest().getStack(), stack));
extractFromFurnaceSlot((FurnaceTileEntity) entity, RESULT_SLOT);
// Do we have the requested item in the inventory now?
final int resultCount = InventoryUtils.getItemCountInItemHandler(worker.getInventoryCitizen(), stack -> ItemStackUtils.compareItemStacksIgnoreStackSize(currentRequest.getRequest().getStack(), stack)) - preExtractCount;
if (resultCount > 0) {
final ItemStack stack = currentRequest.getRequest().getStack().copy();
stack.setCount(resultCount);
currentRequest.addDelivery(stack);
job.setCraftCounter(job.getCraftCounter() + resultCount);
job.setProgress(job.getProgress() - resultCount);
if (job.getMaxCraftingCount() == 0) {
job.setMaxCraftingCount(currentRequest.getRequest().getCount());
}
if (job.getCraftCounter() >= job.getMaxCraftingCount() && job.getProgress() <= 0) {
job.finishRequest(true);
resetValues();
currentRecipeStorage = null;
incrementActionsDoneAndDecSaturation();
return INVENTORY_FULL;
}
}
setDelay(STANDARD_DELAY);
return START_WORKING;
}
use of com.minecolonies.api.entity.ai.statemachine.states.IAIState in project minecolonies by Minecolonies.
the class AbstractEntityAIRequestSmelter method addFuelToFurnace.
/**
* Add furnace fuel when necessary
* @return
*/
private IAIState addFuelToFurnace() {
final List<ItemStack> possibleFuels = getActivePossibleFuels();
if (!InventoryUtils.hasItemInItemHandler(worker.getInventoryCitizen(), isCorrectFuel(possibleFuels))) {
if (InventoryUtils.hasItemInProvider(building, isCorrectFuel(possibleFuels))) {
needsCurrently = new Tuple<>(isCorrectFuel(possibleFuels), STACKSIZE);
return GATHERING_REQUIRED_MATERIALS;
}
// We shouldn't get here, unless something changed between the checkFurnaceFuel and the addFueltoFurnace calls
preFuelState = null;
fuelPos = null;
return START_WORKING;
}
if (fuelPos == null || walkToBlock(fuelPos)) {
return getState();
}
if (WorldUtil.isBlockLoaded(world, fuelPos)) {
final TileEntity entity = world.getBlockEntity(fuelPos);
if (entity instanceof FurnaceTileEntity) {
final FurnaceTileEntity furnace = (FurnaceTileEntity) entity;
// Stoke the furnaces
if (InventoryUtils.hasItemInItemHandler(worker.getInventoryCitizen(), isCorrectFuel(possibleFuels)) && (hasSmeltableInFurnaceAndNoFuel(furnace) || hasNeitherFuelNorSmeltAble(furnace))) {
InventoryUtils.transferXOfFirstSlotInItemHandlerWithIntoInItemHandler(worker.getInventoryCitizen(), isCorrectFuel(possibleFuels), STACKSIZE, new InvWrapper(furnace), FUEL_SLOT);
if (preFuelState != null && preFuelState != ADD_FUEL_TO_FURNACE) {
IAIState returnState = preFuelState;
preFuelState = null;
fuelPos = null;
return returnState;
}
}
}
}
// Fueling is confused, start over.
preFuelState = null;
fuelPos = null;
return START_WORKING;
}
Aggregations