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);
}
}
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();
}
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());
}
}
}
}
}
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;
}
Aggregations