Search in sources :

Example 6 with AbstractJobGuard

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

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 7 with AbstractJobGuard

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

the class ColonyPermissionEventHandler method on.

/**
 * AttackEntityEvent handler.
 * <p>
 * Check, if a player tries to attack an entity.. Deny if: - If the attacking happens in the colony - Player is less than officer to the colony.
 *
 * @param event ItemEntityPickupEvent
 */
@SubscribeEvent
public void on(final AttackEntityEvent event) {
    if (event.getTarget() instanceof MonsterEntity) {
        return;
    }
    @NotNull final PlayerEntity player = EntityUtils.getPlayerOfFakePlayer(event.getPlayer(), event.getPlayer().getCommandSenderWorld());
    if (MineColonies.getConfig().getServer().enableColonyProtection.get() && colony.isCoordInColony(player.getCommandSenderWorld(), new BlockPos(player.position()))) {
        final Permissions perms = colony.getPermissions();
        if (event.getTarget() instanceof EntityCitizen) {
            final AbstractEntityCitizen citizen = (AbstractEntityCitizen) event.getTarget();
            if (citizen.getCitizenJobHandler().getColonyJob() instanceof AbstractJobGuard && perms.hasPermission(event.getPlayer(), Action.GUARDS_ATTACK)) {
                return;
            }
            if (perms.hasPermission(event.getPlayer(), Action.ATTACK_CITIZEN)) {
                return;
            }
            cancelEvent(event, event.getPlayer(), colony, Action.ATTACK_CITIZEN, new BlockPos(event.getTarget().position()));
            return;
        }
        if (!(event.getTarget() instanceof MobEntity) && !perms.hasPermission(event.getPlayer(), Action.ATTACK_ENTITY)) {
            cancelEvent(event, event.getPlayer(), colony, Action.ATTACK_ENTITY, new BlockPos(event.getTarget().position()));
        }
    }
}
Also used : AbstractJobGuard(com.minecolonies.coremod.colony.jobs.AbstractJobGuard) Permissions(com.minecolonies.coremod.colony.permissions.Permissions) AbstractEntityCitizen(com.minecolonies.api.entity.citizen.AbstractEntityCitizen) MonsterEntity(net.minecraft.entity.monster.MonsterEntity) BlockPos(net.minecraft.util.math.BlockPos) AbstractEntityCitizen(com.minecolonies.api.entity.citizen.AbstractEntityCitizen) EntityCitizen(com.minecolonies.coremod.entity.citizen.EntityCitizen) NotNull(org.jetbrains.annotations.NotNull) MobEntity(net.minecraft.entity.MobEntity) PlayerEntity(net.minecraft.entity.player.PlayerEntity) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 8 with AbstractJobGuard

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

the class EventListener method onUpdateEntityRadar.

@SubscribeEvent
public void onUpdateEntityRadar(@NotNull final EntityRadarUpdateEvent event) {
    final WrappedEntity wrapper = event.getWrappedEntity();
    final LivingEntity entity = wrapper.getEntityLivingRef().get();
    if (entity instanceof AbstractEntityCitizen) {
        final boolean isVisitor = entity instanceof VisitorCitizen;
        TextComponent jobName;
        if (isVisitor) {
            if (!JourneymapOptions.getShowVisitors(this.jmap.getOptions())) {
                wrapper.setDisable(true);
                return;
            }
            jobName = new TranslationTextComponent(PARTIAL_JOURNEY_MAP_INFO + "visitor");
        } else {
            final String jobId = entity.getEntityData().get(DATA_JOB);
            final JobEntry jobEntry = IJobRegistry.getInstance().getValue(new ResourceLocation(jobId));
            final IJob<?> job = jobEntry == null ? null : jobEntry.produceJob(null);
            if (job instanceof AbstractJobGuard ? !JourneymapOptions.getShowGuards(this.jmap.getOptions()) : !JourneymapOptions.getShowCitizens(this.jmap.getOptions())) {
                wrapper.setDisable(true);
                return;
            }
            jobName = new TranslationTextComponent(jobEntry == null ? PARTIAL_JOURNEY_MAP_INFO + "unemployed" : jobEntry.getTranslationKey());
        }
        if (JourneymapOptions.getShowColonistTooltip(this.jmap.getOptions())) {
            ITextComponent name = entity.getCustomName();
            if (name != null) {
                wrapper.setEntityToolTips(Arrays.asList(name, jobName.setStyle(JOB_TOOLTIP)));
            }
        }
        final boolean showName = event.getActiveUiState().ui.equals(Context.UI.Minimap) ? JourneymapOptions.getShowColonistNameMinimap(this.jmap.getOptions()) : JourneymapOptions.getShowColonistNameFullscreen(this.jmap.getOptions());
        if (!showName) {
            wrapper.setCustomName("");
        }
        if (!isVisitor && JourneymapOptions.getShowColonistTeamColour(this.jmap.getOptions())) {
            wrapper.setColor(entity.getTeamColor());
        }
    } else if (entity instanceof AbstractEntityMinecoloniesMob) {
        final JourneymapOptions.RaiderColor color = JourneymapOptions.getRaiderColor(this.jmap.getOptions());
        if (JourneymapOptions.RaiderColor.NONE.equals(color)) {
            wrapper.setDisable(true);
        } else if (!JourneymapOptions.RaiderColor.HOSTILE.equals(color)) {
            wrapper.setColor(color.getColor().getValue());
        }
    }
}
Also used : AbstractEntityCitizen(com.minecolonies.api.entity.citizen.AbstractEntityCitizen) WrappedEntity(journeymap.client.api.model.WrappedEntity) VisitorCitizen(com.minecolonies.coremod.entity.citizen.VisitorCitizen) LivingEntity(net.minecraft.entity.LivingEntity) JobEntry(com.minecolonies.api.colony.jobs.registry.JobEntry) AbstractJobGuard(com.minecolonies.coremod.colony.jobs.AbstractJobGuard) ResourceLocation(net.minecraft.util.ResourceLocation) AbstractEntityMinecoloniesMob(com.minecolonies.api.entity.mobs.AbstractEntityMinecoloniesMob) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 9 with AbstractJobGuard

use of com.minecolonies.coremod.colony.jobs.AbstractJobGuard 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)) {
        MessageUtils.format(TOOL_GUARD_SCROLL_NO_GUARD_BUILDING).sendTo(player);
        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 10 with AbstractJobGuard

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

the class CitizenHappinessHandler method getGuardFactor.

/**
 * Get the guard security happiness modifier from the colony.
 *
 * @param colony the colony.
 * @return true if so.
 */
private double getGuardFactor(final IColony colony) {
    double guards = 1;
    double workers = 1;
    for (final ICitizenData citizen : colony.getCitizenManager().getCitizens()) {
        if (citizen.getJob() instanceof AbstractJobGuard) {
            guards++;
        } else {
            workers++;
        }
    }
    return Math.min(guards / (workers * 2 / 3), 2);
}
Also used : AbstractJobGuard(com.minecolonies.coremod.colony.jobs.AbstractJobGuard) ICitizenData(com.minecolonies.api.colony.ICitizenData)

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