Search in sources :

Example 41 with AbstractEntityCitizen

use of com.minecolonies.api.entity.citizen.AbstractEntityCitizen in project minecolonies by Minecolonies.

the class EntityAIWorkHealer method decide.

/**
 * Decide what to do next. Check if all patients are up date, else update their states. Then check if there is any patient we can cure or request things for.
 *
 * @return the next state to go to.
 */
private IAIState decide() {
    if (walkToBuilding()) {
        return DECIDE;
    }
    final BuildingHospital hospital = building;
    for (final AbstractEntityCitizen citizen : WorldUtil.getEntitiesWithinBuilding(world, AbstractEntityCitizen.class, building, cit -> cit.getCitizenDiseaseHandler().isSick())) {
        hospital.checkOrCreatePatientFile(citizen.getCivilianID());
    }
    for (final Patient patient : hospital.getPatients()) {
        final ICitizenData data = hospital.getColony().getCitizenManager().getCivilian(patient.getId());
        if (data == null || !data.getEntity().isPresent() || (data.getEntity().isPresent() && !data.getEntity().get().getCitizenDiseaseHandler().isSick())) {
            hospital.removePatientFile(patient);
            continue;
        }
        final EntityCitizen citizen = (EntityCitizen) data.getEntity().get();
        final String diseaseName = citizen.getCitizenDiseaseHandler().getDisease();
        @Nullable final Disease disease = diseaseName.isEmpty() ? null : IColonyManager.getInstance().getCompatibilityManager().getDisease(diseaseName);
        if (patient.getState() == Patient.PatientState.NEW) {
            this.currentPatient = patient;
            return REQUEST_CURE;
        }
        if (patient.getState() == Patient.PatientState.REQUESTED) {
            if (disease == null) {
                this.currentPatient = patient;
                return CURE;
            }
            if (testRandomCureChance()) {
                this.currentPatient = patient;
                return FREE_CURE;
            }
            if (!InventoryUtils.isItemHandlerFull(citizen.getInventoryCitizen())) {
                if (hasCureInInventory(disease, worker.getInventoryCitizen()) || hasCureInInventory(disease, building.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).orElseGet(null))) {
                    this.currentPatient = patient;
                    return CURE;
                }
                final ImmutableList<IRequest<? extends Stack>> list = building.getOpenRequestsOfType(worker.getCitizenData().getId(), TypeToken.of(Stack.class));
                final ImmutableList<IRequest<? extends Stack>> completed = building.getCompletedRequestsOfType(worker.getCitizenData(), TypeToken.of(Stack.class));
                for (final ItemStack cure : IColonyManager.getInstance().getCompatibilityManager().getDisease(diseaseName).getCure()) {
                    if (!InventoryUtils.hasItemInItemHandler(worker.getInventoryCitizen(), cure::sameItem)) {
                        if (InventoryUtils.getItemCountInItemHandler(building.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).orElseGet(null), stack -> stack.sameItem(cure)) >= cure.getCount()) {
                            needsCurrently = new Tuple<>(stack -> stack.sameItem(cure), cure.getCount());
                            return GATHERING_REQUIRED_MATERIALS;
                        }
                        boolean hasCureRequested = false;
                        for (final IRequest<? extends Stack> request : list) {
                            if (request.getRequest().getStack().sameItem(cure)) {
                                hasCureRequested = true;
                                break;
                            }
                        }
                        for (final IRequest<? extends Stack> request : completed) {
                            if (request.getRequest().getStack().sameItem(cure)) {
                                hasCureRequested = true;
                                break;
                            }
                        }
                        if (!hasCureRequested) {
                            patient.setState(Patient.PatientState.NEW);
                            break;
                        }
                    }
                }
            } else {
                data.triggerInteraction(new StandardInteraction(new TranslationTextComponent(PATIENT_FULL_INVENTORY), ChatPriority.BLOCKING));
            }
        }
        if (patient.getState() == Patient.PatientState.TREATED) {
            if (disease == null) {
                this.currentPatient = patient;
                return CURE;
            }
            if (!hasCureInInventory(disease, citizen.getInventoryCitizen())) {
                patient.setState(Patient.PatientState.NEW);
                return DECIDE;
            }
        }
    }
    for (final PlayerEntity player : WorldUtil.getEntitiesWithinBuilding(world, PlayerEntity.class, building, player -> player.getHealth() < player.getMaxHealth() - 10 - (2 * building.getBuildingLevel()))) {
        playerToHeal = player;
        return CURE_PLAYER;
    }
    final ICitizenData data = building.getColony().getCitizenManager().getRandomCitizen();
    if (data.getEntity().isPresent() && data.getEntity().get().getHealth() < 10.0 && BlockPosUtil.getDistance2D(data.getEntity().get().blockPosition(), building.getPosition()) < building.getBuildingLevel() * 40) {
        remotePatient = data;
        return WANDER;
    }
    return DECIDE;
}
Also used : IItemHandler(net.minecraftforge.items.IItemHandler) IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) ICitizenData(com.minecolonies.api.colony.ICitizenData) TypeToken(com.google.common.reflect.TypeToken) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) ItemStack(net.minecraft.item.ItemStack) ImmutableList(com.google.common.collect.ImmutableList) IAIState(com.minecolonies.api.entity.ai.statemachine.states.IAIState) JobHealer(com.minecolonies.coremod.colony.jobs.JobHealer) AITarget(com.minecolonies.api.entity.ai.statemachine.AITarget) com.minecolonies.api.util(com.minecolonies.api.util) Network(com.minecolonies.coremod.Network) PATIENT_FULL_INVENTORY(com.minecolonies.api.util.constant.TranslationConstants.PATIENT_FULL_INVENTORY) StreamParticleEffectMessage(com.minecolonies.coremod.network.messages.client.StreamParticleEffectMessage) Stack(com.minecolonies.api.colony.requestsystem.requestable.Stack) AbstractEntityAIInteract(com.minecolonies.coremod.entity.ai.basic.AbstractEntityAIInteract) PlayerEntity(net.minecraft.entity.player.PlayerEntity) CircleParticleEffectMessage(com.minecolonies.coremod.network.messages.client.CircleParticleEffectMessage) IColonyManager(com.minecolonies.api.colony.IColonyManager) ChatPriority(com.minecolonies.api.colony.interactionhandling.ChatPriority) StandardInteraction(com.minecolonies.coremod.colony.interactionhandling.StandardInteraction) BlockPos(net.minecraft.util.math.BlockPos) AIWorkerState(com.minecolonies.api.entity.ai.statemachine.states.AIWorkerState) ParticleTypes(net.minecraft.particles.ParticleTypes) Nullable(org.jetbrains.annotations.Nullable) AbstractEntityCitizen(com.minecolonies.api.entity.citizen.AbstractEntityCitizen) BuildingHospital(com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingHospital) EntityCitizen(com.minecolonies.coremod.entity.citizen.EntityCitizen) CapabilityItemHandler(net.minecraftforge.items.CapabilityItemHandler) NotNull(org.jetbrains.annotations.NotNull) BuildingHospital(com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingHospital) StandardInteraction(com.minecolonies.coremod.colony.interactionhandling.StandardInteraction) AbstractEntityCitizen(com.minecolonies.api.entity.citizen.AbstractEntityCitizen) ICitizenData(com.minecolonies.api.colony.ICitizenData) ItemStack(net.minecraft.item.ItemStack) Stack(com.minecolonies.api.colony.requestsystem.requestable.Stack) PlayerEntity(net.minecraft.entity.player.PlayerEntity) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) AbstractEntityCitizen(com.minecolonies.api.entity.citizen.AbstractEntityCitizen) EntityCitizen(com.minecolonies.coremod.entity.citizen.EntityCitizen) ItemStack(net.minecraft.item.ItemStack) Nullable(org.jetbrains.annotations.Nullable)

Example 42 with AbstractEntityCitizen

use of com.minecolonies.api.entity.citizen.AbstractEntityCitizen in project minecolonies by Minecolonies.

the class AbstractCraftingBuildingModule method fullFillRecipe.

@Override
public boolean fullFillRecipe(final IRecipeStorage storage) {
    final List<IItemHandler> handlers = building.getHandlers();
    final ICitizenData data = building.getModuleMatching(WorkerBuildingModule.class, m -> m.getJobEntry() == jobEntry).getFirstCitizen();
    if (data == null || !data.getEntity().isPresent()) {
        // we shouldn't hit this case, but just in case...
        return storage.fullfillRecipe(building.getColony().getWorld(), handlers);
    }
    final AbstractEntityCitizen worker = data.getEntity().get();
    final int primarySkill = worker.getCitizenData().getCitizenSkillHandler().getLevel(building.getModuleMatching(WorkerBuildingModule.class, m -> m.getJobEntry() == jobEntry).getPrimarySkill());
    final int luck = (int) (((primarySkill + 1) * 2) - Math.pow((primarySkill + 1) / 10.0, 2));
    LootContext.Builder builder = (new LootContext.Builder((ServerWorld) building.getColony().getWorld()).withParameter(LootParameters.ORIGIN, worker.position()).withParameter(LootParameters.THIS_ENTITY, worker).withParameter(LootParameters.TOOL, worker.getMainHandItem()).withRandom(worker.getRandom()).withLuck((float) luck));
    return storage.fullfillRecipe(builder.create(RecipeStorage.recipeLootParameters), handlers);
}
Also used : ServerWorld(net.minecraft.world.server.ServerWorld) IItemHandler(net.minecraftforge.items.IItemHandler) IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) SettingKey(com.minecolonies.coremod.colony.buildings.modules.settings.SettingKey) Constants(net.minecraftforge.common.util.Constants) CompoundNBT(net.minecraft.nbt.CompoundNBT) IFormattableTextComponent(net.minecraft.util.text.IFormattableTextComponent) AbstractJobCrafter(com.minecolonies.coremod.colony.jobs.AbstractJobCrafter) MinecoloniesAPIProxy(com.minecolonies.api.MinecoloniesAPIProxy) StandardFactoryController(com.minecolonies.api.colony.requestsystem.StandardFactoryController) RECIPE_IMPROVED(com.minecolonies.api.util.constant.TranslationConstants.RECIPE_IMPROVED) JobEntry(com.minecolonies.api.colony.jobs.registry.JobEntry) CraftingType(com.minecolonies.api.crafting.registry.CraftingType) ListNBT(net.minecraft.nbt.ListNBT) ImmutableSet(com.google.common.collect.ImmutableSet) PublicWorkerCraftingProductionResolver(com.minecolonies.coremod.colony.requestsystem.resolvers.PublicWorkerCraftingProductionResolver) TAG_RECIPES(com.minecolonies.api.util.constant.NbtTagConstants.TAG_RECIPES) Predicate(java.util.function.Predicate) TAG_DISABLED_RECIPES(com.minecolonies.api.util.constant.NbtTagConstants.TAG_DISABLED_RECIPES) LootParameters(net.minecraft.loot.LootParameters) AbstractBuilding(com.minecolonies.coremod.colony.buildings.AbstractBuilding) com.minecolonies.api.crafting(com.minecolonies.api.crafting) Collectors(java.util.stream.Collectors) Nullable(org.jetbrains.annotations.Nullable) AbstractEntityCitizen(com.minecolonies.api.entity.citizen.AbstractEntityCitizen) IRequestResolver(com.minecolonies.api.colony.requestsystem.resolver.IRequestResolver) com.minecolonies.api.colony.buildings.modules(com.minecolonies.api.colony.buildings.modules) NotNull(org.jetbrains.annotations.NotNull) java.util(java.util) PublicWorkerCraftingRequestResolver(com.minecolonies.coremod.colony.requestsystem.resolvers.PublicWorkerCraftingRequestResolver) CRAFTING_REDUCEABLE(com.minecolonies.api.util.constant.TagConstants.CRAFTING_REDUCEABLE) ISettingKey(com.minecolonies.api.colony.buildings.modules.settings.ISettingKey) IToken(com.minecolonies.api.colony.requestsystem.token.IToken) ICitizenData(com.minecolonies.api.colony.ICitizenData) IWareHouse(com.minecolonies.api.colony.buildings.workerbuildings.IWareHouse) ItemStack(net.minecraft.item.ItemStack) LootContext(net.minecraft.loot.LootContext) CustomRecipe(com.minecolonies.coremod.colony.crafting.CustomRecipe) com.minecolonies.api.util(com.minecolonies.api.util) RECIPES(com.minecolonies.api.research.util.ResearchConstants.RECIPES) IDeliverable(com.minecolonies.api.colony.requestsystem.requestable.IDeliverable) ModTags(com.minecolonies.api.items.ModTags) IColonyManager(com.minecolonies.api.colony.IColonyManager) Tuple(net.minecraft.util.Tuple) CrafterRecipeSetting(com.minecolonies.coremod.colony.buildings.modules.settings.CrafterRecipeSetting) Blocks(net.minecraft.block.Blocks) TypeConstants(com.minecolonies.api.util.constant.TypeConstants) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) PublicCrafting(com.minecolonies.api.colony.requestsystem.requestable.crafting.PublicCrafting) ResourceLocation(net.minecraft.util.ResourceLocation) IColony(com.minecolonies.api.colony.IColony) CustomRecipeManager(com.minecolonies.coremod.colony.crafting.CustomRecipeManager) PacketBuffer(net.minecraft.network.PacketBuffer) IJob(com.minecolonies.api.colony.jobs.IJob) IItemHandler(net.minecraftforge.items.IItemHandler) LootContext(net.minecraft.loot.LootContext) AbstractEntityCitizen(com.minecolonies.api.entity.citizen.AbstractEntityCitizen) ICitizenData(com.minecolonies.api.colony.ICitizenData)

Example 43 with AbstractEntityCitizen

use of com.minecolonies.api.entity.citizen.AbstractEntityCitizen in project minecolonies by Minecolonies.

the class CitizenData method initEntityValues.

/**
 * Initializes the entities values from citizen data.
 */
@Override
public void initEntityValues() {
    if (!getEntity().isPresent()) {
        return;
    }
    final AbstractEntityCitizen citizen = getEntity().get();
    citizen.setCitizenId(getId());
    citizen.getCitizenColonyHandler().setColonyId(getColony().getID());
    citizen.setIsChild(isChild());
    citizen.setCustomName(new StringTextComponent(getName()));
    citizen.getAttribute(Attributes.MAX_HEALTH).setBaseValue(BASE_MAX_HEALTH);
    citizen.setFemale(isFemale());
    citizen.setTextureId(getTextureId());
    citizen.getEntityData().set(DATA_COLONY_ID, colony.getID());
    citizen.getEntityData().set(DATA_CITIZEN_ID, citizen.getCivilianID());
    citizen.getEntityData().set(DATA_IS_FEMALE, citizen.isFemale() ? 1 : 0);
    citizen.getEntityData().set(DATA_TEXTURE, citizen.getTextureId());
    citizen.getEntityData().set(DATA_TEXTURE_SUFFIX, getTextureSuffix());
    citizen.getEntityData().set(DATA_IS_ASLEEP, isAsleep());
    citizen.getEntityData().set(DATA_IS_CHILD, isChild());
    citizen.getEntityData().set(DATA_BED_POS, getBedPos());
    citizen.getEntityData().set(DATA_JOB, getJob() == null ? "" : getJob().getJobRegistryEntry().getRegistryName().toString());
    citizen.getEntityData().set(DATA_STYLE, colony.getTextureStyleId());
    citizen.getCitizenExperienceHandler().updateLevel();
    setLastPosition(citizen.blockPosition());
    citizen.getCitizenJobHandler().onJobChanged(citizen.getCitizenJobHandler().getColonyJob());
    applyResearchEffects();
    applyItemModifiers(citizen);
    markDirty();
}
Also used : AbstractEntityCitizen(com.minecolonies.api.entity.citizen.AbstractEntityCitizen) StringTextComponent(net.minecraft.util.text.StringTextComponent)

Example 44 with AbstractEntityCitizen

use of com.minecolonies.api.entity.citizen.AbstractEntityCitizen in project minecolonies by Minecolonies.

the class CitizenData method applyResearchEffects.

@Override
public void applyResearchEffects() {
    if (getEntity().isPresent()) {
        final AbstractEntityCitizen citizen = getEntity().get();
        // Applies entity related research effects.
        citizen.getNavigation().getPathingOptions().setCanUseRails(((EntityCitizen) citizen).canPathOnRails());
        citizen.getNavigation().getPathingOptions().setCanClimbVines(((EntityCitizen) citizen).canClimbVines());
        final AttributeModifier speedModifier = new AttributeModifier(RESEARCH_BONUS_MULTIPLIER, colony.getResearchManager().getResearchEffects().getEffectStrength(WALKING), AttributeModifier.Operation.MULTIPLY_TOTAL);
        AttributeModifierUtils.addModifier(citizen, speedModifier, Attributes.MOVEMENT_SPEED);
        final AttributeModifier healthModLevel = new AttributeModifier(HEALTH_BOOST.toString(), colony.getResearchManager().getResearchEffects().getEffectStrength(HEALTH_BOOST), AttributeModifier.Operation.ADDITION);
        AttributeModifierUtils.addHealthModifier(citizen, healthModLevel);
    }
}
Also used : AbstractEntityCitizen(com.minecolonies.api.entity.citizen.AbstractEntityCitizen) AttributeModifier(net.minecraft.entity.ai.attributes.AttributeModifier)

Example 45 with AbstractEntityCitizen

use of com.minecolonies.api.entity.citizen.AbstractEntityCitizen in project minecolonies by Minecolonies.

the class MinecoloniesAdvancedPathNavigate method moveToTree.

@Override
public TreePathResult moveToTree(final int range, final double speed, final List<ItemStorage> excludedTrees, final int dyntreesize, final IColony colony) {
    @NotNull BlockPos start = AbstractPathJob.prepareStart(ourEntity);
    final BlockPos buildingPos = ((AbstractEntityCitizen) mob).getCitizenColonyHandler().getWorkBuilding().getPosition();
    if (BlockPosUtil.getDistance2D(buildingPos, ((AbstractEntityCitizen) mob).blockPosition()) > range * 4) {
        start = buildingPos;
    }
    return (TreePathResult) setPathJob(new PathJobFindTree(CompatibilityUtils.getWorldFromEntity(mob), start, buildingPos, range, excludedTrees, dyntreesize, colony, ourEntity), null, speed, true);
}
Also used : AbstractEntityCitizen(com.minecolonies.api.entity.citizen.AbstractEntityCitizen) BlockPos(net.minecraft.util.math.BlockPos) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

AbstractEntityCitizen (com.minecolonies.api.entity.citizen.AbstractEntityCitizen)73 BlockPos (net.minecraft.util.math.BlockPos)33 ICitizenData (com.minecolonies.api.colony.ICitizenData)32 PlayerEntity (net.minecraft.entity.player.PlayerEntity)22 ItemStack (net.minecraft.item.ItemStack)22 NotNull (org.jetbrains.annotations.NotNull)20 IColony (com.minecolonies.api.colony.IColony)17 AttributeModifier (net.minecraft.entity.ai.attributes.AttributeModifier)14 TranslationTextComponent (net.minecraft.util.text.TranslationTextComponent)14 IBuilding (com.minecolonies.api.colony.buildings.IBuilding)12 Entity (net.minecraft.entity.Entity)12 java.util (java.util)10 Collectors (java.util.stream.Collectors)10 CompoundNBT (net.minecraft.nbt.CompoundNBT)10 IItemHandler (net.minecraftforge.items.IItemHandler)10 com.minecolonies.api.util (com.minecolonies.api.util)9 EntityCitizen (com.minecolonies.coremod.entity.citizen.EntityCitizen)8 ArrayList (java.util.ArrayList)8 ServerPlayerEntity (net.minecraft.entity.player.ServerPlayerEntity)8 World (net.minecraft.world.World)8