use of com.minecolonies.api.colony.ICitizenData in project minecolonies by Minecolonies.
the class CitizenHappinessHandler method getSocialModifier.
/**
* Get the social modifier for the colony.
*
* @param colony the colony.
* @return true if so.
*/
private double getSocialModifier(final IColony colony) {
final double total = colony.getCitizenManager().getCitizens().size();
double unemployment = 0;
double homelessness = 0;
double sickPeople = 0;
double hungryPeople = 0;
for (final ICitizenData citizen : colony.getCitizenManager().getCitizens()) {
if (!citizen.isChild() && citizen.getJob() == null) {
unemployment++;
}
if (citizen.getHomeBuilding() == null) {
homelessness++;
}
if (citizen.getEntity().isPresent() && citizen.getEntity().get().getCitizenDiseaseHandler().isSick()) {
sickPeople++;
}
if (citizen.getSaturation() <= 1) {
hungryPeople++;
}
}
return (total - (unemployment + homelessness + sickPeople + hungryPeople)) / total;
}
use of com.minecolonies.api.colony.ICitizenData in project minecolonies by Minecolonies.
the class EntityAIWorkUndertaker method tryResurrect.
/**
* Attempt to resurrect buried citizen from its citizen data
* Randomize to see if resurrection successful and resurrect if need be
*
* @return the next IAIState
*/
private IAIState tryResurrect() {
@Nullable final BuildingGraveyard buildingGraveyard = building;
if (checkForToolOrWeapon(ToolType.SHOVEL) || buildingGraveyard.getFirstModuleOccurance(GraveyardManagementModule.class).getLastGraveData() == null || buildingGraveyard.getGraveToWorkOn() == null) {
return IDLE;
}
unequip();
@Nullable final BlockPos gravePos = buildingGraveyard.getGraveToWorkOn();
if (gravePos == null) {
return IDLE;
}
// Still moving to the block
if (walkToBlock(gravePos, 3)) {
return getState();
}
final TileEntity entity = world.getBlockEntity(gravePos);
if (entity instanceof TileEntityGrave) {
if (effortCounter < EFFORT_RESURRECT) {
worker.swing(Hand.MAIN_HAND);
effortCounter += getSecondarySkillLevel();
return getState();
}
effortCounter = 0;
shouldDumpInventory = true;
final double chance = getResurrectChance(buildingGraveyard);
if (getTotemResurrectChance() > 0 && random.nextDouble() <= TOTEM_BREAK_CHANCE) {
worker.getInventoryCitizen().extractItem(InventoryUtils.findFirstSlotInItemHandlerWith(worker.getInventoryCitizen(), Items.TOTEM_OF_UNDYING), 1, false);
worker.playSound(SoundEvents.TOTEM_USE, 1.0f, 1.0f);
}
if (chance >= random.nextDouble()) {
final ICitizenData citizenData = buildingGraveyard.getColony().getCitizenManager().resurrectCivilianData(buildingGraveyard.getFirstModuleOccurance(GraveyardManagementModule.class).getLastGraveData().getCitizenDataNBT(), true, world, gravePos);
MessageUtils.format(MESSAGE_INFO_CITIZEN_UNDERTAKER_RESURRECTED_SUCCESS, citizenData.getName()).sendTo(buildingGraveyard.getColony()).forManagers();
worker.getCitizenColonyHandler().getColony().getCitizenManager().updateCitizenMourn(citizenData, false);
AdvancementUtils.TriggerAdvancementPlayersForColony(worker.getCitizenColonyHandler().getColony(), playerMP -> AdvancementTriggers.CITIZEN_RESURRECT.trigger(playerMP));
buildingGraveyard.getFirstModuleOccurance(GraveyardManagementModule.class).setLastGraveData(null);
world.setBlockAndUpdate(gravePos, Blocks.AIR.defaultBlockState());
return INVENTORY_FULL;
}
}
return DIG_GRAVE;
}
use of com.minecolonies.api.colony.ICitizenData in project minecolonies by Minecolonies.
the class EntityAIEatTask method hasFood.
/**
* Checks if the citizen has food in the inventory and makes a decision based on that.
*
* @return the next state to go to.
*/
private boolean hasFood() {
final int slot = InventoryUtils.findFirstSlotInProviderNotEmptyWith(citizen, stack -> CAN_EAT.test(stack) && canEat(citizen.getCitizenData(), stack));
if (slot != -1) {
foodSlot = slot;
return true;
}
final ICitizenData citizenData = citizen.getCitizenData();
if (InventoryUtils.hasItemInItemHandler(citizen.getInventoryCitizen(), ISCOOKABLE)) {
citizenData.triggerInteraction(new StandardInteraction(new TranslationTextComponent(RAW_FOOD), ChatPriority.PENDING));
} else if (InventoryUtils.hasItemInItemHandler(citizen.getInventoryCitizen(), stack -> CAN_EAT.test(stack) && !canEat(citizenData, stack))) {
if (citizenData.isChild()) {
citizenData.triggerInteraction(new StandardInteraction(new TranslationTextComponent(BETTER_FOOD_CHILDREN), ChatPriority.BLOCKING));
} else {
citizenData.triggerInteraction(new StandardInteraction(new TranslationTextComponent(BETTER_FOOD), ChatPriority.BLOCKING));
}
}
return false;
}
use of com.minecolonies.api.colony.ICitizenData 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;
}
use of com.minecolonies.api.colony.ICitizenData in project minecolonies by Minecolonies.
the class EntityAIWorkHealer method cure.
/**
* Give a citizen the cure.
*
* @return the next state to go to.
*/
private IAIState cure() {
if (currentPatient == null) {
return DECIDE;
}
final ICitizenData data = building.getColony().getCitizenManager().getCivilian(currentPatient.getId());
if (data == null || !data.getEntity().isPresent() || !data.getEntity().get().getCitizenDiseaseHandler().isSick()) {
currentPatient = null;
return DECIDE;
}
final EntityCitizen citizen = (EntityCitizen) data.getEntity().get();
if (walkToBlock(data.getEntity().get().blockPosition())) {
return CURE;
}
final String diseaseName = citizen.getCitizenDiseaseHandler().getDisease();
final Disease disease = IColonyManager.getInstance().getCompatibilityManager().getDisease(diseaseName);
if (diseaseName.isEmpty()) {
currentPatient = null;
citizen.heal(10);
worker.getCitizenExperienceHandler().addExperience(BASE_XP_GAIN);
citizen.markDirty();
return DECIDE;
}
if (!hasCureInInventory(disease, worker.getInventoryCitizen())) {
if (hasCureInInventory(disease, building.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).orElseGet(null))) {
for (final ItemStack cure : disease.getCure()) {
if (InventoryUtils.getItemCountInItemHandler(worker.getInventoryCitizen(), stack -> stack.sameItem(cure)) < cure.getCount()) {
needsCurrently = new Tuple<>(stack -> stack.sameItem(cure), 1);
return GATHERING_REQUIRED_MATERIALS;
}
}
}
currentPatient = null;
return DECIDE;
}
if (!hasCureInInventory(disease, citizen.getInventoryCitizen())) {
for (final ItemStack cure : disease.getCure()) {
if (InventoryUtils.getItemCountInItemHandler(citizen.getInventoryCitizen(), stack -> stack.sameItem(cure)) < cure.getCount()) {
if (InventoryUtils.isItemHandlerFull(citizen.getInventoryCitizen())) {
data.triggerInteraction(new StandardInteraction(new TranslationTextComponent(PATIENT_FULL_INVENTORY), ChatPriority.BLOCKING));
currentPatient = null;
return DECIDE;
}
InventoryUtils.transferXOfFirstSlotInItemHandlerWithIntoNextFreeSlotInItemHandler(worker.getInventoryCitizen(), cure::sameItem, cure.getCount(), citizen.getInventoryCitizen());
}
}
}
worker.getCitizenExperienceHandler().addExperience(BASE_XP_GAIN);
currentPatient.setState(Patient.PatientState.TREATED);
currentPatient = null;
return DECIDE;
}
Aggregations