Class AbstractEntityAIBasic<J extends AbstractJob<?,J>,B extends AbstractBuilding>

java.lang.Object
com.minecolonies.core.entity.ai.workers.AbstractAISkeleton<J>
com.minecolonies.core.entity.ai.workers.AbstractEntityAIBasic<J,B>
Type Parameters:
J - The job this ai has to fulfil
All Implemented Interfaces:
ITickingStateAI
Direct Known Subclasses:
AbstractEntityAISkill

public abstract class AbstractEntityAIBasic<J extends AbstractJob<?,J>,B extends AbstractBuilding> extends AbstractAISkeleton<J>
This class provides basic ai functionality.
  • Field Details

    • NO_TOOL

      protected static final int NO_TOOL
      The returned slot if a slot hasn't been found.
      See Also:
    • STANDARD_DELAY

      protected static final int STANDARD_DELAY
      The standard delay after each terminated action.
      See Also:
    • REQUEST_DELAY

      protected static final int REQUEST_DELAY
      The standard delay after each terminated action.
      See Also:
    • currentWorkingLocation

      @Nullable protected @Nullable net.minecraft.core.BlockPos currentWorkingLocation
      The block the ai is currently working at or wants to work.
    • WALK_DELAY

      protected static final int WALK_DELAY
      Delay for walking.
      See Also:
    • needsCurrently

      protected Tuple<Predicate<net.minecraft.world.item.ItemStack>,Integer> needsCurrently
      What he currently might be needing.
    • walkTo

      protected net.minecraft.core.BlockPos walkTo
      The current position the worker should walk to.
    • building

      public final B extends AbstractBuilding building
      The building of the citizen.
    • workPosIndex

      public int workPosIndex
      Working pos index. Initialized with -1, not saved.
  • Constructor Details

    • AbstractEntityAIBasic

      protected AbstractEntityAIBasic(@NotNull J job)
      Sets up some important skeleton stuff for every ai.
      Parameters:
      job - the job class
  • Method Details

    • checkIfNeedsItem

      protected boolean checkIfNeedsItem()
      Check if
      Returns:
    • getStateAfterPickUp

      public IAIState getStateAfterPickUp()
      The state to transform to after picking up things.
      Returns:
      the next state to go to.
    • getExpectedBuildingClass

      public abstract Class<B> getExpectedBuildingClass()
      Can be overridden in implementations to return the exact building type the worker expects.
      Returns:
      the building type associated with this AI's worker.
    • onException

      protected void onException(RuntimeException e)
      Overrides:
      onException in class AbstractAISkeleton<J extends AbstractJob<?,J>>
    • setDelay

      public final void setDelay(int timeout)
      Set a delay in ticks.
      Parameters:
      timeout - the delay to wait after this tick.
    • inventoryNeedsDump

      protected boolean inventoryNeedsDump()
      Check if we need to dump the worker inventory.

      This will also ask the implementing ai if we need to dump on custom reasons. {see wantInventoryDumped}

      Returns:
      true if we need to dump the inventory.
    • getActionsDoneUntilDumping

      protected int getActionsDoneUntilDumping()
      Calculates after how many actions the ai should dump it's inventory.

      Override this to change the value.

      Returns:
      the number of actions done before item dump.
    • wantInventoryDumped

      protected boolean wantInventoryDumped()
      Has to be overridden by classes to specify when to dump inventory. Always dump on inventory full.
      Returns:
      true if inventory needs to be dumped now
    • updateRenderMetaData

      protected void updateRenderMetaData()
      Can be overridden in implementations.

      Here the AI can check if the chestBelt has to be re rendered and do it.

    • waitForRequests

      @NotNull protected @NotNull IAIState waitForRequests()
      If the worker has open requests their results will be queried until they all are completed Also waits for DELAY_RECHECK.
      Returns:
      NEEDS_ITEM
    • getPrimarySkillLevel

      protected int getPrimarySkillLevel()
      Get the primary skill of this worker.
      Returns:
      the primary skill.
    • getSecondarySkillLevel

      protected int getSecondarySkillLevel()
      Get the secondary skill of this worker.
      Returns:
      the secondary skill.
    • getModuleForJob

      protected WorkerBuildingModule getModuleForJob()
      Utility method to get a given module for this AI.
      Returns:
      the right module.
    • getEffectiveSkillLevel

      protected static int getEffectiveSkillLevel(int rawSkillLevel)
      Apply a early bonus curve to a skill level note: This adjusts the range from 0-99 to be 1-100
      Parameters:
      rawSkillLevel - to apply the curve to
      Returns:
      effective skill level to use in linear bonus functions
    • getInvertedEffectiveSkillLevel

      protected static int getInvertedEffectiveSkillLevel(int rawSkillLevel)
      Returns the inverted skill level, to use in chance based calcs
      Parameters:
      rawSkillLevel -
      Returns:
    • isAfterDumpPickupAllowed

      public boolean isAfterDumpPickupAllowed()
      Checks whether automatic pickups after dumps are allowed. Usually we want this, but if the worker is currently crafting/building, he will handle deliveries/afterdumps in their resolvers, so we can disable automatic pickups in that time. Note that this is just a efficiency-thing. It doesn't hurt when the dman does a pickup during crafting, it's just a wasted run. Therefore, this flag is only considered for *automatic* pickups after dump. It is *ignored* for player-triggered forcePickups, and when the inventory is full.
      Returns:
      true if after-dump pickups are allowed currently.
    • afterRequestPickUp

      public IAIState afterRequestPickUp()
      What to do after picking up a request.
      Returns:
      the next state to go to.
    • getTotalRequiredAmount

      public int getTotalRequiredAmount(net.minecraft.world.item.ItemStack deliveredItemStack)
      Get the total amount required for an itemStack. Workers have to override this if they have more information.
      Parameters:
      deliveredItemStack - the required stack.
      Returns:
      on default the size of the stack.
    • walkToBuilding

      protected final boolean walkToBuilding()
      Walk the worker to it's work building.
      Returns:
      true on arrival
    • walkToTaggedWorkPos

      protected final boolean walkToTaggedWorkPos()
      Walk the worker to a tagged workpos in building, or the building pos if non existent.
      Returns:
      true while walking
    • walkToWorkPos

      protected final boolean walkToWorkPos(net.minecraft.core.BlockPos pos)
      Walk the worker to the given building.
      Returns:
      true while walking
    • walkToBuilding

      protected final boolean walkToBuilding(IBuilding building)
      Walk the worker to the given building.
      Returns:
      true while walking
    • walkToSafePos

      protected final boolean walkToSafePos(net.minecraft.core.BlockPos pos)
      Walk the worker to the safe position
      Returns:
      false while walking
    • walkToUnSafePos

      protected final boolean walkToUnSafePos(net.minecraft.core.BlockPos pos)
      Walk the worker to the unsafe position
      Returns:
      false while walking
    • walkToUnSafePos

      protected final boolean walkToUnSafePos(net.minecraft.core.BlockPos pos, int distance)
      Walk the worker to the unsafe position
      Returns:
      false while walking
    • checkAndTransferFromHut

      public boolean checkAndTransferFromHut(@Nullable @Nullable net.minecraft.world.item.ItemStack is)
      Check all racks in the workers hut for a required item. Transfer to worker if found.
      Parameters:
      is - the type of item requested (amount is ignored)
      Returns:
      true if a stack of that type was found and transferred.
    • walkWithProxy

      protected final boolean walkWithProxy(@NotNull @NotNull net.minecraft.core.BlockPos stand)
      Sets the block the AI is currently walking to.
      Parameters:
      stand - where to walk to
      Returns:
      true while walking to the block
    • walkWithProxy

      protected final boolean walkWithProxy(@NotNull @NotNull net.minecraft.core.BlockPos stand, int range)
      Sets the block the AI is currently walking to.
      Parameters:
      stand - where to walk to
      range - how close we need to be
      Returns:
      true while walking to the block
    • retrieveToolInTileEntity

      public boolean retrieveToolInTileEntity(net.minecraft.world.level.block.entity.BlockEntity entity, EquipmentTypeEntry toolType, int minLevel, int maxLevel)
      Finds the first @see ItemStack the type of is. It will be taken from the chest and placed in the worker inventory. Make sure that the worker stands next the chest to not break immersion. Also make sure to have inventory space for the stack.
      Parameters:
      entity - the tileEntity chest or building.
      toolType - the type of tool.
      minLevel - the min tool level.
      maxLevel - the max tool level.
      Returns:
      true if found the tool.
    • takeItemStackFromProvider

      public void takeItemStackFromProvider(@NotNull @NotNull net.minecraftforge.common.capabilities.ICapabilityProvider provider, int slotIndex)
      Takes whatever is in that slot of the worker chest and puts it in his inventory. If the inventory is full, only the fitting part will be moved. Beware this method shouldn't be private, because the generic access won't work within a lambda won't work else.
      Parameters:
      provider - The provider to take from.
      slotIndex - The slot to take.
    • checkForToolOrWeapon

      public boolean checkForToolOrWeapon(@NotNull @NotNull EquipmentTypeEntry toolType)
      Ensures that we have a appropriate tool available. Will set needsTool accordingly.
      Parameters:
      toolType - type of tool we check for.
      Returns:
      false if we have the tool
    • checkForToolOrWeapon

      protected boolean checkForToolOrWeapon(@NotNull @NotNull EquipmentTypeEntry toolType, int minimalLevel)
    • checkForToolOrWeaponAsync

      protected void checkForToolOrWeaponAsync(@NotNull @NotNull EquipmentTypeEntry toolType, int minLevel, int maxLevel)
      Ensures that we have a appropriate tool available. ASync call on the tool.
      Parameters:
      toolType - Tool type that is requested
      minLevel - min. level of the tool
      maxLevel - min. level of the tool
    • cancelAsynchRequestForArmor

      protected void cancelAsynchRequestForArmor(EquipmentTypeEntry armorType)
      Cancel all requests for a certain armor type for a certain citizen.
      Parameters:
      armorType - the armor type.
    • retrieveToolInHut

      public boolean retrieveToolInHut(EquipmentTypeEntry toolType, int minimalLevel)
      Check all chests in the workers hut for a required tool.
      Parameters:
      toolType - the type of tool requested (amount is ignored)
      minimalLevel - the minimal level the tool should have.
      Returns:
      true if a stack of that type was found
    • getBuildingToDump

      protected IBuilding getBuildingToDump()
      Get the building to dump the inventory into.
      Returns:
      its own building by default.
    • afterDump

      public IAIState afterDump()
      State to go to after dumping.
      Returns:
      the next state.
    • itemsNiceToHave

      @NotNull protected @NotNull List<ItemStorage> itemsNiceToHave()
      Can be overridden by implementations to specify items useful for the worker. When the worker inventory is full, he will try to keep these items. ItemStack amounts are ignored, the first stack found will be taken.
      Returns:
      a list with items nice to have for the worker
    • getInventory

      @NotNull protected @NotNull InventoryCitizen getInventory()
      Calculate the citizens inventory.
      Returns:
      A InventoryCitizen matching this ai's citizen.
    • holdEfficientTool

      public boolean holdEfficientTool(@NotNull @NotNull net.minecraft.world.level.block.state.BlockState target, net.minecraft.core.BlockPos pos)
      Check and ensure that we hold the most efficient tool for the job.

      If we have no tool for the job, we will request on, return immediately.

      Parameters:
      target - the BlockState to mine
      pos - the pos to mine
      Returns:
      true if we have a tool for the job
    • getMostEfficientTool

      protected int getMostEfficientTool(@NotNull @NotNull net.minecraft.world.level.block.state.BlockState target, net.minecraft.core.BlockPos pos)
      Calculates the most efficient tool to use on that block.
      Parameters:
      target - the BlockState to mine
      pos - the pos it is at.
      Returns:
      the slot with the best tool
    • hasNotDelayed

      protected final boolean hasNotDelayed(int time)
      Will delay one time and pass through the second time. Use for convenience instead of SetDelay
      Parameters:
      time - the time to wait
      Returns:
      true if you should wait
    • incrementActionsDoneAndDecSaturation

      public final void incrementActionsDoneAndDecSaturation()
      Tell the ai that you have done one more action.

      if the actions exceed a certain number, the ai will dump it's inventory. this also triggers the AI to get hungry.

      For example:

      After x blocks, bring everything back.

    • incrementActionsDone

      public final void incrementActionsDone()
      Tell the ai that you have done one more action.

      if the actions exceed a certain number, the ai will dump it's inventory.

      For example:

      After x blocks, bring everything back.

      See Also:
    • resetActionsDone

      protected final void resetActionsDone()
      Reset the done actions of the AI.
      See Also:
    • incrementActionsDone

      protected final void incrementActionsDone(int numberOfActions)
      Tell the ai that you have done numberOfActions more actions.

      if the actions exceed a certain number, the ai will dump it's inventory.

      For example:

      After x blocks, bring everything back.

      Parameters:
      numberOfActions - number of actions to be added at once.
      See Also:
    • getWorkingPosition

      public net.minecraft.core.BlockPos getWorkingPosition(net.minecraft.core.BlockPos targetPosition)
      Calculates the working position.

      Takes the position where the worker would like to work on and return the most appropriate position for it.

      Parameters:
      targetPosition - the position to work at.
      Returns:
      BlockPos most appropiate position to work from.
    • getWorkingPosition

      public net.minecraft.core.BlockPos getWorkingPosition(int distance, net.minecraft.core.BlockPos targetPos, int offset)
      Calculates the working position.

      Takes a min distance from width and length.

      Then finds the floor level at that distance and then check if it does contain two air levels.

      Parameters:
      distance - the extra distance to apply away from the building.
      targetPos - the target position which needs to be worked.
      offset - an additional offset
      Returns:
      BlockPos position to work from.
    • checkIfRequestForItemExistOrCreate

      public boolean checkIfRequestForItemExistOrCreate(@NotNull @NotNull net.minecraft.world.item.ItemStack... stacks)
      Requests a list of itemstacks.
      Parameters:
      stacks - the stacks.
      Returns:
      true if they're in the inventory.
    • checkIfRequestForItemExistOrCreate

      public boolean checkIfRequestForItemExistOrCreate(@NotNull @NotNull Collection<net.minecraft.world.item.ItemStack> stacks)
      Check if any of the stacks is in the inventory.
      Parameters:
      stacks - the list of stacks.
      Returns:
      true if so.
    • checkIfRequestForItemExistOrCreate

      public boolean checkIfRequestForItemExistOrCreate(@NotNull @NotNull net.minecraft.world.item.ItemStack stack)
      Check if a stack has been requested already or is in the inventory. If not in the inventory and not requested already, create request
      Parameters:
      stack - the requested stack.
      Returns:
      true if in the inventory, else false.
    • checkIfRequestForItemExistOrCreate

      public boolean checkIfRequestForItemExistOrCreate(@NotNull @NotNull net.minecraft.world.item.ItemStack stack, int count, int minCount)
      Check if a stack has been requested already or is in the inventory. If not in the inventory and not requested already, create request
      Parameters:
      stack - the requested stack.
      count - count to request.
      minCount - the min count to fulfill.
      Returns:
      true if in the inventory, else false.
    • checkIfRequestForItemExistOrCreateAsync

      public boolean checkIfRequestForItemExistOrCreateAsync(@NotNull @NotNull net.minecraft.world.item.ItemStack... stacks)
      Requests a list of itemstacks.
      Parameters:
      stacks - the stacks.
      Returns:
      true if they're in the inventory.
    • checkIfRequestForItemExistOrCreateAsync

      public boolean checkIfRequestForItemExistOrCreateAsync(@NotNull @NotNull Collection<net.minecraft.world.item.ItemStack> stacks)
      Check if any of the stacks is in the inventory.
      Parameters:
      stacks - the list of stacks.
      Returns:
      true if so.
    • checkIfRequestForItemExistOrCreateAsync

      public boolean checkIfRequestForItemExistOrCreateAsync(@NotNull @NotNull net.minecraft.world.item.ItemStack stack)
      Check if a stack has been requested already or is in the inventory. If not in the inventory and not requested already, create request
      Parameters:
      stack - the requested stack.
      Returns:
      true if in the inventory, else false.
    • checkIfRequestForItemExistOrCreateAsync

      public boolean checkIfRequestForItemExistOrCreateAsync(@NotNull @NotNull net.minecraft.world.item.ItemStack stack, int count, int minCount)
      Check if a stack has been requested already or is in the inventory. If not in the inventory and not requested already, create request
      Parameters:
      stack - the requested stack.
      count - the count.
      minCount - the min count.
      Returns:
      true if in the inventory, else false.
    • checkIfRequestForItemExistOrCreateAsync

      public boolean checkIfRequestForItemExistOrCreateAsync(@NotNull @NotNull net.minecraft.world.item.ItemStack stack, int count, int minCount, boolean matchNBT)
      Check if a stack has been requested already or is in the inventory. If not in the inventory and not requested already, create request
      Parameters:
      stack - the requested stack.
      count - the total count.
      minCount - the minimum count.
      matchNBT - if nbt has to be matched.
      Returns:
      true if in the inventory, else false.
    • checkIfRequestForItemExistOrCreate

      public boolean checkIfRequestForItemExistOrCreate(@NotNull @NotNull net.minecraft.world.item.ItemStack stack, int count, int minCount, boolean matchNBT, boolean async)
      Check if a stack has been requested already or is in the inventory. If not in the inventory and not requested already, create request
      Parameters:
      stack - the requested stack.
      count - the total count.
      minCount - the minimum count.
      matchNBT - if nbt has to be matched.
      async - if should be an async request.
      Returns:
      true if in the inventory, else false.
    • checkIfRequestForItemExistOrCreate

      public boolean checkIfRequestForItemExistOrCreate(@NotNull @NotNull IDeliverable deliverable)
      Check if a request exists for a given deliverable.
      Parameters:
      deliverable - the deliverable to check of request.
      Returns:
      true if available or transfered.
    • checkIfRequestForTagExistOrCreateAsync

      public boolean checkIfRequestForTagExistOrCreateAsync(@NotNull @NotNull net.minecraft.tags.TagKey<net.minecraft.world.item.Item> tag, int count)
      Check if a tag has been requested already or is in the inventory. If not in the inventory and not requested already, create request
      Parameters:
      tag - the requested tag.
      Returns:
      true if in the inventory, else false.
    • getExceptionTimer

      public int getExceptionTimer()
      The current exception timer
      Returns:
      the timer.
    • getFakePlayer

      protected net.minecraftforge.common.util.FakePlayer getFakePlayer()
      Obtains a fake player class which can be used inside the AI logic to utilize in requests that require a player instance when none is available.
      Returns:
      a fake player.
    • canGoIdle

      public boolean canGoIdle()
      If the worker currently has work to do or could also just wander around.
      Returns:
      true if can go idle.