Skip to content

Commit

Permalink
thodor cherrypick this
Browse files Browse the repository at this point in the history
  • Loading branch information
Raycoms committed Oct 27, 2024
1 parent e9c6463 commit 4015e79
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.minecolonies.api.entity.citizen.citizenhandlers;

import com.minecolonies.api.entity.citizen.AbstractEntityCitizen;
import com.minecolonies.core.colony.buildings.workerbuildings.BuildingHospital;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -67,5 +69,5 @@ public interface ICitizenDiseaseHandler
/**
* Sets a flag that the citizen is now at the hospital.
*/
void setSleepsAtHospital(final boolean isAtHospital);
void setSleepsAtHospital(final BuildingHospital buildingHospital);
}
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ public final class CitizenConstants
*/
public static final String TAG_IMMUNITY = "immunity";

/**
* Hospital pos tag
*/
public static final String TAG_HOSPITAL = "hospital";

/**
* Noon day time.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -654,9 +654,9 @@ public final class TranslationConstants
@NonNls
public static final String FOOD_QUALITY_TOOLTIP = "com.minecolonies.core.gui.restaurant.foodquality";
@NonNls
public static final String FOOD_QUALITY = ".foodquality";
public static final String FOOD_QUALITY = "foodquality";
@NonNls
public static final String FOOD_DIVERSITY = ".fooddiversity";
public static final String FOOD_DIVERSITY = "fooddiversity";
@NonNls
public static final String URGENT = ".urgent";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package com.minecolonies.core.colony.buildings.workerbuildings;

import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.ImmutableList;
import com.minecolonies.api.colony.ICitizenData;
import com.minecolonies.api.colony.IColony;
import com.minecolonies.api.colony.IColonyManager;
import com.minecolonies.api.entity.citizen.AbstractEntityCitizen;
import com.minecolonies.api.util.BlockPosUtil;
import com.minecolonies.api.util.Disease;
import com.minecolonies.api.util.constant.NbtTagConstants;
import com.minecolonies.core.colony.CitizenData;
import com.minecolonies.core.colony.buildings.AbstractBuilding;
import com.minecolonies.core.entity.ai.workers.util.Patient;
import com.minecolonies.core.entity.citizen.EntityCitizen;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
Expand Down Expand Up @@ -49,7 +54,7 @@ public class BuildingHospital extends AbstractBuilding
* Map from beds to patients, 0 is empty.
*/
@NotNull
private final Map<BlockPos, Integer> bedMap = new HashMap<>();
private final BiMap<BlockPos, Integer> bedMap = HashBiMap.create();

/**
* Map of patients of this hospital.
Expand Down Expand Up @@ -323,6 +328,16 @@ else if (entry.getValue() != 0)
}
}

/**
* Check if the citizen is actually in bed.
* @param citizen the entity to check.
* @return true if close to bed.
*/
public boolean isInBed(final AbstractEntityCitizen citizen)
{
return bedMap.inverse().getOrDefault(citizen.getCitizenData().getId(), BlockPos.ZERO).distManhattan(citizen.blockPosition()) < 10;
}

@Override
public boolean canEat(final ItemStack stack)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,16 +182,16 @@ private DiseaseState findEmptyBed()
final IColony colony = citizen.getCitizenColonyHandler().getColony();
final IBuilding hospital = colony.getBuildingManager().getBuilding(hospitalPos);

if (hospital instanceof BuildingHospital)
if (hospital instanceof BuildingHospital buildingHospital)
{
if (usedBed != null && !((BuildingHospital) hospital).getBedList().contains(usedBed))
if (usedBed != null && !buildingHospital.getBedList().contains(usedBed))
{
usedBed = null;
}

if (usedBed == null)
{
for (final BlockPos pos : ((BuildingHospital) hospital).getBedList())
for (final BlockPos pos : buildingHospital.getBedList())
{
final Level world = citizen.level;
BlockState state = world.getBlockState(pos);
Expand All @@ -200,9 +200,9 @@ private DiseaseState findEmptyBed()
&& state.getValue(BedBlock.PART).equals(BedPart.HEAD)
&& world.isEmptyBlock(pos.above()))
{
citizen.getCitizenDiseaseHandler().setSleepsAtHospital(true);
citizen.getCitizenDiseaseHandler().setSleepsAtHospital(buildingHospital);
usedBed = pos;
((BuildingHospital) hospital).registerPatient(usedBed, citizen.getCivilianID());
buildingHospital.registerPatient(usedBed, citizen.getCivilianID());
return FIND_EMPTY_BED;
}
}
Expand Down Expand Up @@ -369,7 +369,7 @@ else if (state == CitizenAIState.IDLE)
private IState goToHut()
{
final IBuilding buildingWorker = citizenData.getWorkBuilding();
citizen.getCitizenDiseaseHandler().setSleepsAtHospital(false);
citizen.getCitizenDiseaseHandler().setSleepsAtHospital(null);

if (buildingWorker == null)
{
Expand All @@ -390,7 +390,7 @@ private IState goToHut()
*/
private IState goToHospital()
{
citizen.getCitizenDiseaseHandler().setSleepsAtHospital(false);
citizen.getCitizenDiseaseHandler().setSleepsAtHospital(null);
if (placeToPath == null)
{
return SEARCH_HOSPITAL;
Expand Down Expand Up @@ -482,7 +482,7 @@ private void reset()
citizen.stopUsingItem();
citizen.setItemInHand(InteractionHand.MAIN_HAND, ItemStack.EMPTY);
placeToPath = null;
citizen.getCitizenDiseaseHandler().setSleepsAtHospital(false);
citizen.getCitizenDiseaseHandler().setSleepsAtHospital(null);
}

// TODO: Citizen AI should set status icons
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import com.minecolonies.api.colony.buildings.IBuilding;
import com.minecolonies.api.entity.citizen.AbstractEntityCitizen;
import com.minecolonies.api.entity.citizen.citizenhandlers.ICitizenDiseaseHandler;
import com.minecolonies.api.util.BlockPosUtil;
import com.minecolonies.core.MineColonies;
import com.minecolonies.core.colony.buildings.workerbuildings.BuildingCook;
import com.minecolonies.core.colony.buildings.workerbuildings.BuildingHospital;
import com.minecolonies.core.colony.jobs.AbstractJobGuard;
import com.minecolonies.core.colony.jobs.JobHealer;
import net.minecraft.core.BlockPos;
Expand Down Expand Up @@ -61,9 +62,9 @@ public class CitizenDiseaseHandler implements ICitizenDiseaseHandler
private int immunityTicks = 0;

/**
* Whether the citizen sleeps at the hostpital
* Pos of the hospital the citizen might be sleeping at (or null if not sleeping at a hospital).
*/
private boolean sleepsAtHospital = false;
private BlockPos hospitalPos = null;

/**
* The initial citizen count
Expand Down Expand Up @@ -152,6 +153,10 @@ public void write(final CompoundTag compound)
{
compound.putString(TAG_DISEASE, disease);
compound.putInt(TAG_IMMUNITY, immunityTicks);
if (hospitalPos != null)
{
BlockPosUtil.write(compound, TAG_HOSPITAL, hospitalPos);
}
}

@Override
Expand All @@ -163,6 +168,10 @@ public void read(final CompoundTag compound)
this.disease = compound.getString(TAG_DISEASE);
}
this.immunityTicks = compound.getInt(TAG_IMMUNITY);
if (compound.contains(TAG_HOSPITAL))
{
hospitalPos = BlockPosUtil.read(compound, TAG_HOSPITAL);
}
}

@Override
Expand All @@ -175,11 +184,9 @@ public String getDisease()
public void cure()
{
this.disease = "";
sleepsAtHospital = false;
if (citizen.getCitizenSleepHandler().isAsleep())
{
citizen.stopSleeping();
final BlockPos hospitalPos = citizen.getCitizenColonyHandler().getColony().getBuildingManager().getBestBuilding(citizen, BuildingCook.class);
final IColony colony = citizen.getCitizenColonyHandler().getColony();
final IBuilding hospital = colony.getBuildingManager().getBuilding(hospitalPos);
if (hospital != null)
Expand All @@ -198,19 +205,39 @@ public void cure()

citizen.getCitizenColonyHandler().getColony().getStatisticsManager().increment(CITIZENS_HEALED, citizen.getCitizenColonyHandler().getColony().getDay());
}

hospitalPos = null;
citizen.markDirty(0);
}

@Override
public boolean sleepsAtHospital()
{
return sleepsAtHospital;
if (hospitalPos != null)
{
final IColony colony = citizen.getCitizenColonyHandler().getColony();
if (colony != null)
{
final BuildingHospital hospital = colony.getBuildingManager().getBuilding(hospitalPos, BuildingHospital.class);
if (hospital != null)
{
return hospital.isInBed(citizen);
}
hospitalPos = null;
}
}
return hospitalPos != null;
}

@Override
public void setSleepsAtHospital(final boolean isAtHospital)
public void setSleepsAtHospital(final BuildingHospital buildingHospital)
{
sleepsAtHospital = isAtHospital;
if (buildingHospital == null)
{
hospitalPos = null;
}
else
{
hospitalPos = buildingHospital.getPosition();
}
}
}

0 comments on commit 4015e79

Please sign in to comment.