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