Search in sources :

Example 1 with AIOneTimeEventTarget

use of com.minecolonies.api.entity.ai.statemachine.AIOneTimeEventTarget in project minecolonies by Minecolonies.

the class CommandCitizenTriggerWalkTo method onExecute.

/**
 * What happens when the command is executed after preConditions are successful.
 *
 * @param context the context of the command execution
 */
@Override
public int onExecute(final CommandContext<CommandSource> context) {
    final Entity sender = context.getSource().getEntity();
    // Colony
    final int colonyID = IntegerArgumentType.getInteger(context, COLONYID_ARG);
    final IColony colony = IColonyManager.getInstance().getColonyByDimension(colonyID, sender == null ? World.OVERWORLD : context.getSource().getLevel().dimension());
    if (colony == null) {
        context.getSource().sendSuccess(LanguageHandler.buildChatComponent("com.minecolonies.command.colonyidnotfound", colonyID), true);
        return 0;
    }
    final ICitizenData citizenData = colony.getCitizenManager().getCivilian(IntegerArgumentType.getInteger(context, CITIZENID_ARG));
    if (citizenData == null) {
        context.getSource().sendSuccess(LanguageHandler.buildChatComponent("com.minecolonies.command.citizeninfo.notfound"), true);
        return 0;
    }
    final Optional<AbstractEntityCitizen> optionalEntityCitizen = citizenData.getEntity();
    if (!optionalEntityCitizen.isPresent()) {
        context.getSource().sendSuccess(LanguageHandler.buildChatComponent("com.minecolonies.command.citizeninfo.notloaded"), true);
        return 0;
    }
    final AbstractEntityCitizen entityCitizen = optionalEntityCitizen.get();
    final ILocationArgument targetLocation = Vec3Argument.getCoordinates(context, POS_ARG);
    final BlockPos targetPos = targetLocation.getBlockPos(context.getSource());
    if (context.getSource().getLevel() == entityCitizen.level) {
        if (entityCitizen instanceof EntityCitizen && entityCitizen.getCitizenJobHandler().getColonyJob() != null) {
            final AbstractEntityAIBasic basic = ((AbstractEntityAIBasic) entityCitizen.getCitizenJobHandler().getColonyJob().getWorkerAI());
            basic.setWalkTo(targetPos);
            basic.registerTarget(new AIOneTimeEventTarget(AIWorkerState.WALK_TO));
        } else {
            entityCitizen.getNavigation().moveTo(targetPos.getX(), targetPos.getY(), targetPos.getZ(), 1f);
        }
    }
    return 1;
}
Also used : Entity(net.minecraft.entity.Entity) AbstractEntityCitizen(com.minecolonies.api.entity.citizen.AbstractEntityCitizen) AIOneTimeEventTarget(com.minecolonies.api.entity.ai.statemachine.AIOneTimeEventTarget) IColony(com.minecolonies.api.colony.IColony) ICitizenData(com.minecolonies.api.colony.ICitizenData) ILocationArgument(net.minecraft.command.arguments.ILocationArgument) BlockPos(net.minecraft.util.math.BlockPos) AbstractEntityCitizen(com.minecolonies.api.entity.citizen.AbstractEntityCitizen) EntityCitizen(com.minecolonies.coremod.entity.citizen.EntityCitizen) AbstractEntityAIBasic(com.minecolonies.coremod.entity.ai.basic.AbstractEntityAIBasic)

Example 2 with AIOneTimeEventTarget

use of com.minecolonies.api.entity.ai.statemachine.AIOneTimeEventTarget in project minecolonies by Minecolonies.

the class ItemScrollGuardHelp method onItemUseSuccess.

@Override
protected ItemStack onItemUseSuccess(final ItemStack itemStack, final World world, final ServerPlayerEntity player) {
    final IColony colony = getColony(itemStack);
    final BlockPos buildingPos = BlockPosUtil.read(itemStack.getTag(), TAG_BUILDING_POS);
    final IBuilding building = colony.getBuildingManager().getBuilding(buildingPos);
    if (!(building instanceof AbstractBuildingGuards)) {
        LanguageHandler.sendPlayerMessage(player, "minecolonies.scroll.noguardbuilding");
        return itemStack;
    }
    itemStack.shrink(1);
    final List<ICitizenData> guards = new ArrayList<>(building.getAllAssignedCitizen());
    if (world.random.nextInt(10) == 0 || colony.getWorld() != world) {
        // Fail
        final LlamaEntity entity = EntityType.LLAMA.create(world);
        entity.setPos(player.getX(), player.getY(), player.getZ());
        world.addFreshEntity(entity);
        player.displayClientMessage(new TranslationTextComponent("minecolonies.scroll.failed" + (world.random.nextInt(FAIL_RESPONSES_TOTAL) + 1)).setStyle(Style.EMPTY.withColor(TextFormatting.GOLD)), true);
        SoundUtils.playSoundForPlayer(player, SoundEvents.EVOKER_CAST_SPELL, 0.5f, 1.0f);
        return itemStack;
    } else {
        for (final ICitizenData citizenData : guards) {
            final AbstractJobGuard job = citizenData.getJob(AbstractJobGuard.class);
            if (job != null && job.getWorkerAI() != null && !((AbstractEntityAIGuard) job.getWorkerAI()).hasTool()) {
                continue;
            }
            if (citizenData.getEntity().isPresent()) {
                if (citizenData.getEntity().get().getCitizenDiseaseHandler().isSick()) {
                    continue;
                }
                citizenData.getEntity().get().remove();
            }
            colony.getCitizenManager().spawnOrCreateCivilian(citizenData, world, player.blockPosition(), true);
            citizenData.setNextRespawnPosition(buildingPos);
            ((AbstractBuildingGuards) building).getSetting(AbstractBuildingGuards.GUARD_TASK).set(GuardTaskSetting.FOLLOW);
            ((AbstractBuildingGuards) building).setPlayerToFollow(player);
            final FollowModeSetting grouping = ((AbstractBuildingGuards) building).getSetting(AbstractBuildingGuards.FOLLOW_MODE);
            if (grouping.getValue().equals(FollowModeSetting.LOOSE)) {
                grouping.trigger();
            }
            citizenData.setSaturation(100);
            colony.getPackageManager().addCloseSubscriber(player);
            if (job != null && job.getWorkerAI() != null) {
                final long spawnTime = world.getGameTime() + TICKS_SECOND * 900;
                // Timed despawn
                job.getWorkerAI().registerTarget(new AIOneTimeEventTarget(() -> {
                    if (world.getGameTime() - spawnTime > 0) {
                        ((AbstractBuildingGuards) building).getSetting(AbstractBuildingGuards.GUARD_TASK).set(GuardTaskSetting.PATROL);
                        citizenData.getEntity().ifPresent(Entity::remove);
                        colony.getPackageManager().removeCloseSubscriber(player);
                        return true;
                    }
                    return false;
                }, AIWorkerState.DECIDE));
            }
        }
        SoundUtils.playSoundForPlayer(player, SoundEvents.UI_TOAST_CHALLENGE_COMPLETE, 0.3f, 1.0f);
    }
    return itemStack;
}
Also used : AbstractBuildingGuards(com.minecolonies.coremod.colony.buildings.AbstractBuildingGuards) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) ArrayList(java.util.ArrayList) AIOneTimeEventTarget(com.minecolonies.api.entity.ai.statemachine.AIOneTimeEventTarget) IColony(com.minecolonies.api.colony.IColony) ICitizenData(com.minecolonies.api.colony.ICitizenData) AbstractJobGuard(com.minecolonies.coremod.colony.jobs.AbstractJobGuard) LlamaEntity(net.minecraft.entity.passive.horse.LlamaEntity) AbstractEntityAIGuard(com.minecolonies.coremod.entity.ai.citizen.guard.AbstractEntityAIGuard) BlockPos(net.minecraft.util.math.BlockPos) FollowModeSetting(com.minecolonies.coremod.colony.buildings.modules.settings.FollowModeSetting)

Example 3 with AIOneTimeEventTarget

use of com.minecolonies.api.entity.ai.statemachine.AIOneTimeEventTarget in project minecolonies by Minecolonies.

the class AbstractEntityAIGuard method setWakeCitizen.

/**
 * Set the citizen to wakeup
 *
 * @param citizen
 */
public void setWakeCitizen(final EntityCitizen citizen) {
    sleepingGuard = new WeakReference<>(citizen);
    wakeTimer = 0;
    registerTarget(new AIOneTimeEventTarget(GUARD_WAKE));
}
Also used : AIOneTimeEventTarget(com.minecolonies.api.entity.ai.statemachine.AIOneTimeEventTarget)

Example 4 with AIOneTimeEventTarget

use of com.minecolonies.api.entity.ai.statemachine.AIOneTimeEventTarget in project minecolonies by ldtteam.

the class AbstractEntityAIGuard method setWakeCitizen.

/**
 * Set the citizen to wakeup
 *
 * @param citizen
 */
public void setWakeCitizen(final EntityCitizen citizen) {
    sleepingGuard = new WeakReference<>(citizen);
    wakeTimer = 0;
    registerTarget(new AIOneTimeEventTarget(GUARD_WAKE));
}
Also used : AIOneTimeEventTarget(com.minecolonies.api.entity.ai.statemachine.AIOneTimeEventTarget)

Example 5 with AIOneTimeEventTarget

use of com.minecolonies.api.entity.ai.statemachine.AIOneTimeEventTarget in project minecolonies by Minecolonies.

the class AbstractEntityAIGuard method startHelpCitizen.

/**
 * Assigning the guard to help a citizen.
 *
 * @param attacker the citizens attacker.
 */
public void startHelpCitizen(final LivingEntity attacker) {
    if (canHelp()) {
        ((IThreatTableEntity) worker).getThreatTable().addThreat(attacker, 20);
        registerTarget(new AIOneTimeEventTarget(CombatAIStates.ATTACKING));
    }
}
Also used : AIOneTimeEventTarget(com.minecolonies.api.entity.ai.statemachine.AIOneTimeEventTarget)

Aggregations

AIOneTimeEventTarget (com.minecolonies.api.entity.ai.statemachine.AIOneTimeEventTarget)8 ICitizenData (com.minecolonies.api.colony.ICitizenData)4 IColony (com.minecolonies.api.colony.IColony)4 BlockPos (net.minecraft.util.math.BlockPos)4 IBuilding (com.minecolonies.api.colony.buildings.IBuilding)2 AbstractEntityCitizen (com.minecolonies.api.entity.citizen.AbstractEntityCitizen)2 AbstractBuildingGuards (com.minecolonies.coremod.colony.buildings.AbstractBuildingGuards)2 FollowModeSetting (com.minecolonies.coremod.colony.buildings.modules.settings.FollowModeSetting)2 AbstractJobGuard (com.minecolonies.coremod.colony.jobs.AbstractJobGuard)2 AbstractEntityAIBasic (com.minecolonies.coremod.entity.ai.basic.AbstractEntityAIBasic)2 AbstractEntityAIGuard (com.minecolonies.coremod.entity.ai.citizen.guard.AbstractEntityAIGuard)2 EntityCitizen (com.minecolonies.coremod.entity.citizen.EntityCitizen)2 ArrayList (java.util.ArrayList)2 ILocationArgument (net.minecraft.command.arguments.ILocationArgument)2 Entity (net.minecraft.entity.Entity)2 LlamaEntity (net.minecraft.entity.passive.horse.LlamaEntity)2