Search in sources :

Example 11 with AbstractJobGuard

use of com.minecolonies.coremod.colony.jobs.AbstractJobGuard in project minecolonies by Minecolonies.

the class CitizenJobHandler method onJobChanged.

/**
 * Defines job changes and state changes of the citizen.
 *
 * @param job the set job.
 */
@Override
public void onJobChanged(@Nullable final IJob<?> job) {
    // Model
    setModelDependingOnJob(job);
    // AI Tasks
    for (@NotNull final PrioritizedGoal task : new ArrayList<>(citizen.getTasks().availableGoals)) {
        if (task.getGoal() instanceof AbstractAISkeleton) {
            citizen.getTasks().removeGoal(task.getGoal());
        }
    }
    citizen.getCitizenData().setIdleAtJob(false);
    if (job != null) {
        job.addWorkerAIToTaskList(citizen.getTasks());
        if (citizen.getTicksExisted() > 0 && citizen.getCitizenColonyHandler().getWorkBuilding() != null && citizen.getDesiredActivity() == DesiredActivity.WORK) {
            BlockPosUtil.tryMoveBaseCitizenEntityToXYZ(citizen, citizen.getCitizenColonyHandler().getWorkBuilding().getPosition());
        }
        // Calculate the number of guards for some advancements
        if (job instanceof AbstractJobGuard) {
            IColony colony = citizen.getCitizenColonyHandler().getColony();
            int guards = ((int) colony.getCitizenManager().getCitizens().stream().filter(citizen -> citizen.getJob() instanceof AbstractJobGuard).count());
            AdvancementUtils.TriggerAdvancementPlayersForColony(citizen.getCitizenColonyHandler().getColony(), player -> AdvancementTriggers.ARMY_POPULATION.trigger(player, guards));
        }
        job.initEntityValues(citizen);
    }
}
Also used : ModModelTypes(com.minecolonies.api.client.render.modeltype.ModModelTypes) DesiredActivity(com.minecolonies.api.entity.ai.DesiredActivity) AdvancementUtils(com.minecolonies.coremod.util.AdvancementUtils) BlockPosUtil(com.minecolonies.api.util.BlockPosUtil) ArrayList(java.util.ArrayList) DATA_MODEL(com.minecolonies.api.entity.citizen.AbstractEntityCitizen.DATA_MODEL) Nullable(org.jetbrains.annotations.Nullable) AdvancementTriggers(com.minecolonies.api.advancements.AdvancementTriggers) AbstractEntityCitizen(com.minecolonies.api.entity.citizen.AbstractEntityCitizen) AbstractAISkeleton(com.minecolonies.coremod.entity.ai.basic.AbstractAISkeleton) IColony(com.minecolonies.api.colony.IColony) ICitizenJobHandler(com.minecolonies.api.entity.citizen.citizenhandlers.ICitizenJobHandler) AbstractJobGuard(com.minecolonies.coremod.colony.jobs.AbstractJobGuard) NotNull(org.jetbrains.annotations.NotNull) PrioritizedGoal(net.minecraft.entity.ai.goal.PrioritizedGoal) IJob(com.minecolonies.api.colony.jobs.IJob) AbstractJobGuard(com.minecolonies.coremod.colony.jobs.AbstractJobGuard) AbstractAISkeleton(com.minecolonies.coremod.entity.ai.basic.AbstractAISkeleton) ArrayList(java.util.ArrayList) IColony(com.minecolonies.api.colony.IColony) PrioritizedGoal(net.minecraft.entity.ai.goal.PrioritizedGoal) NotNull(org.jetbrains.annotations.NotNull)

Example 12 with AbstractJobGuard

use of com.minecolonies.coremod.colony.jobs.AbstractJobGuard in project minecolonies by Minecolonies.

the class AbstractBuildingGuards method startPatrolNext.

/**
 * Starts the patrol to the next point
 */
private void startPatrolNext() {
    getNextPatrolTarget(true);
    patrolTimer = 5;
    for (final ICitizenData curguard : getAllAssignedCitizen()) {
        if (curguard.getEntity().isPresent()) {
            if (curguard.getEntity().get().getCitizenJobHandler().getColonyJob() instanceof AbstractJobGuard) {
                ((AbstractEntityAIGuard<?, ?>) curguard.getEntity().get().getCitizenJobHandler().getColonyJob().getWorkerAI()).setNextPatrolTarget(lastPatrolPoint);
            }
        }
    }
    arrivedAtPatrol.clear();
}
Also used : AbstractJobGuard(com.minecolonies.coremod.colony.jobs.AbstractJobGuard) AbstractEntityAIGuard(com.minecolonies.coremod.entity.ai.citizen.guard.AbstractEntityAIGuard) ICitizenData(com.minecolonies.api.colony.ICitizenData)

Example 13 with AbstractJobGuard

use of com.minecolonies.coremod.colony.jobs.AbstractJobGuard in project minecolonies by Minecolonies.

the class EventHandler method onEnteringChunkEntity.

/**
 * Event called when the player enters a new chunk.
 *
 * @param event the event.
 */
@SubscribeEvent
public static void onEnteringChunkEntity(@NotNull final EntityEvent.EnteringChunk event) {
    if (MineColonies.getConfig().getServer().pvp_mode.get() && event.getEntity() instanceof EntityCitizen) {
        if (event.getEntity().level == null || !WorldUtil.isEntityChunkLoaded(event.getEntity().level, new ChunkPos(event.getNewChunkX(), event.getNewChunkZ())) || !WorldUtil.isEntityChunkLoaded(event.getEntity().level, new ChunkPos(event.getOldChunkX(), event.getOldChunkZ()))) {
            return;
        }
        final EntityCitizen entityCitizen = (EntityCitizen) event.getEntity();
        if (entityCitizen.getCitizenJobHandler().getColonyJob() instanceof AbstractJobGuard) {
            final World world = entityCitizen.getCommandSenderWorld();
            final Chunk chunk = world.getChunk(event.getNewChunkX(), event.getNewChunkZ());
            final IColonyTagCapability chunkCapability = chunk.getCapability(CLOSE_COLONY_CAP, null).resolve().orElse(null);
            if (chunkCapability != null && chunkCapability.getOwningColony() != 0 && entityCitizen.getCitizenColonyHandler().getColonyId() != chunkCapability.getOwningColony()) {
                final IColony colony = IColonyManager.getInstance().getColonyByWorld(chunkCapability.getOwningColony(), entityCitizen.level);
                if (colony != null) {
                    colony.addGuardToAttackers(entityCitizen, ((IGuardBuilding) entityCitizen.getCitizenColonyHandler().getWorkBuilding()).getPlayerToFollowOrRally());
                }
            }
        }
    }
}
Also used : AbstractJobGuard(com.minecolonies.coremod.colony.jobs.AbstractJobGuard) ChunkPos(net.minecraft.util.math.ChunkPos) AbstractEntityCitizen(com.minecolonies.api.entity.citizen.AbstractEntityCitizen) EntityCitizen(com.minecolonies.coremod.entity.citizen.EntityCitizen) World(net.minecraft.world.World) ClientWorld(net.minecraft.client.world.ClientWorld) ServerWorld(net.minecraft.world.server.ServerWorld) Chunk(net.minecraft.world.chunk.Chunk) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 14 with AbstractJobGuard

use of com.minecolonies.coremod.colony.jobs.AbstractJobGuard in project minecolonies by ldtteam.

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)

Aggregations

AbstractJobGuard (com.minecolonies.coremod.colony.jobs.AbstractJobGuard)14 AbstractEntityCitizen (com.minecolonies.api.entity.citizen.AbstractEntityCitizen)8 ICitizenData (com.minecolonies.api.colony.ICitizenData)6 SubscribeEvent (net.minecraftforge.eventbus.api.SubscribeEvent)6 IColony (com.minecolonies.api.colony.IColony)4 AbstractEntityAIGuard (com.minecolonies.coremod.entity.ai.citizen.guard.AbstractEntityAIGuard)4 EntityCitizen (com.minecolonies.coremod.entity.citizen.EntityCitizen)4 ArrayList (java.util.ArrayList)4 BlockPos (net.minecraft.util.math.BlockPos)4 NotNull (org.jetbrains.annotations.NotNull)4 AdvancementTriggers (com.minecolonies.api.advancements.AdvancementTriggers)2 ModModelTypes (com.minecolonies.api.client.render.modeltype.ModModelTypes)2 IBuilding (com.minecolonies.api.colony.buildings.IBuilding)2 IJob (com.minecolonies.api.colony.jobs.IJob)2 JobEntry (com.minecolonies.api.colony.jobs.registry.JobEntry)2 DesiredActivity (com.minecolonies.api.entity.ai.DesiredActivity)2 AIOneTimeEventTarget (com.minecolonies.api.entity.ai.statemachine.AIOneTimeEventTarget)2 DATA_MODEL (com.minecolonies.api.entity.citizen.AbstractEntityCitizen.DATA_MODEL)2 ICitizenJobHandler (com.minecolonies.api.entity.citizen.citizenhandlers.ICitizenJobHandler)2 BlockPosUtil (com.minecolonies.api.util.BlockPosUtil)2