Skip to content

Commit

Permalink
Move sickness handler to citizendata (#10483)
Browse files Browse the repository at this point in the history
Move sickness handler to citizendata
Child buildings no longer claim
Add torchflower to tier6
  • Loading branch information
Raycoms committed Nov 27, 2024
1 parent c95c340 commit 914b3be
Show file tree
Hide file tree
Showing 24 changed files with 159 additions and 189 deletions.
12 changes: 7 additions & 5 deletions src/main/java/com/minecolonies/api/colony/ICitizenData.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import com.minecolonies.api.colony.jobs.IJob;
import com.minecolonies.api.entity.citizen.AbstractEntityCitizen;
import com.minecolonies.api.entity.citizen.VisibleCitizenStatus;
import com.minecolonies.api.entity.citizen.citizenhandlers.ICitizenFoodHandler;
import com.minecolonies.api.entity.citizen.citizenhandlers.ICitizenHappinessHandler;
import com.minecolonies.api.entity.citizen.citizenhandlers.ICitizenMournHandler;
import com.minecolonies.api.entity.citizen.citizenhandlers.ICitizenSkillHandler;
import com.minecolonies.api.entity.citizen.citizenhandlers.*;
import com.minecolonies.api.quests.IQuestGiver;
import com.minecolonies.api.quests.IQuestParticipant;
import com.minecolonies.api.util.Tuple;
Expand All @@ -18,7 +15,10 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.List;
import java.util.Optional;
import java.util.Random;
import java.util.UUID;

public interface ICitizenData extends ICivilianData, IQuestGiver, IQuestParticipant
{
Expand Down Expand Up @@ -151,6 +151,8 @@ public interface ICitizenData extends ICivilianData, IQuestGiver, IQuestParticip
*/
ICitizenSkillHandler getCitizenSkillHandler();

ICitizenDiseaseHandler getCitizenDiseaseHandler();

/**
* Schedule restart and cleanup.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public interface ICivilianData extends ICitizen, INBTSerializable<CompoundTag>
/**
* Tick the data to update values.
*/
void update();
void update(final int tickRate);

/**
* Trigger a possible interaction.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ default ICitizenData spawnOrCreateCitizen(final ICitizenData data, @NotNull fina
/**
* Tick the citizen data of all active citizens.
*/
boolean tickCitizenData();
boolean tickCitizenData(int tickRate);

/**
* Call this to set all the citizens in the colony to mourn or not.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.minecolonies.core.entity.pathfinding.navigation.AbstractAdvancedPathNavigate;
import com.minecolonies.core.entity.pathfinding.navigation.PathingStuckHandler;
import com.mojang.datafixers.util.Pair;
import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.network.chat.Component;
Expand Down Expand Up @@ -663,15 +662,6 @@ public boolean checkCanDropLoot()
*/
public abstract ICitizenSleepHandler getCitizenSleepHandler();

/**
* The Handler to check if the citizen is sick.
*
* @return the instance of the handler.
*/
public abstract ICitizenDiseaseHandler getCitizenDiseaseHandler();

public abstract void setCitizenDiseaseHandler(ICitizenDiseaseHandler citizenDiseaseHandler);

public abstract float getRotationYaw();

public abstract float getRotationPitch();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.minecolonies.api.entity.citizen.citizenhandlers;

import com.minecolonies.api.entity.citizen.AbstractEntityCitizen;
import com.minecolonies.api.colony.ICitizenData;
import net.minecraft.nbt.CompoundTag;
import org.jetbrains.annotations.NotNull;

/**
* Citizen disease handler interface.
Expand All @@ -12,7 +11,7 @@ public interface ICitizenDiseaseHandler
/**
* To tick the handler.
*/
void tick();
void update(final int tickRate);

/**
* Check if the citizen is sick and must be healed..
Expand Down Expand Up @@ -50,7 +49,7 @@ public interface ICitizenDiseaseHandler
/**
* Called when two citizens collide.
*/
void onCollission(@NotNull final AbstractEntityCitizen citizen);
void onCollission(final ICitizenData citizen);

/**
* True when the citizen needs to go to a hospital because its hurt
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/minecolonies/api/util/SoundUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ else if (v <= 0.3)
{
playSoundAtCitizenWith(worldIn, pos, EventType.GENERAL, citizen);
}
else if (v <= 0.4 && citizen.getEntity().isPresent() && citizen.getEntity().get().getCitizenDiseaseHandler().isSick())
else if (v <= 0.4 && citizen.getEntity().isPresent() && citizen.getCitizenDiseaseHandler().isSick())
{
playSoundAtCitizenWith(worldIn, pos, EventType.SICKNESS, citizen);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ public static void init()
citizen -> citizen.getColony() != null && citizen.getSaturation() <= LOW_SATURATION && citizen.getEntity().isPresent()
&& citizen.getColony().getBuildingManager().getBestBuilding(citizen.getEntity().get(), BuildingCook.class) == null
&& InventoryUtils.findFirstSlotInItemHandlerNotEmptyWith(citizen.getInventory(), ISFOOD) == -1);
InteractionValidatorRegistry.registerStandardPredicate(Component.translatableEscape(NO_HOSPITAL),
citizen -> citizen.getColony() != null && citizen.getEntity().isPresent() && citizen.getEntity().get().getCitizenDiseaseHandler().isSick()
InteractionValidatorRegistry.registerStandardPredicate(Component.translatable(NO_HOSPITAL),
citizen -> citizen.getColony() != null && citizen.getEntity().isPresent() && citizen.getCitizenDiseaseHandler().isSick()
&& citizen.getColony().getBuildingManager().getBestBuilding(citizen.getEntity().get(), BuildingHospital.class) == null);
InteractionValidatorRegistry.registerStandardPredicate(Component.translatableEscape(WAITING_FOR_CURE),
citizen -> citizen.getColony() != null && citizen.getEntity().isPresent() && !citizen.getEntity().get().getCitizenDiseaseHandler().getDisease().isEmpty());
InteractionValidatorRegistry.registerStandardPredicate(Component.translatable(WAITING_FOR_CURE),
citizen -> citizen.getColony() != null && citizen.getEntity().isPresent() && !citizen.getCitizenDiseaseHandler().getDisease().isEmpty());

InteractionValidatorRegistry.registerPosBasedPredicate(Component.translatableEscape(COM_MINECOLONIES_COREMOD_JOB_DELIVERYMAN_CHESTFULL),
(citizen, pos) ->
Expand Down Expand Up @@ -155,8 +155,8 @@ public static void init()

});

InteractionValidatorRegistry.registerStandardPredicate(Component.translatableEscape(PATIENT_FULL_INVENTORY),
citizen -> citizen.getEntity().isPresent() && citizen.getEntity().get().getCitizenDiseaseHandler().isSick()
InteractionValidatorRegistry.registerStandardPredicate(Component.translatable(PATIENT_FULL_INVENTORY),
citizen -> citizen.getEntity().isPresent() && citizen.getCitizenDiseaseHandler().isSick()
&& InventoryUtils.isItemHandlerFull(citizen.getEntity().get().getInventoryCitizen()));

InteractionValidatorRegistry.registerStandardPredicate(Component.translatableEscape(PUPIL_NO_CARPET),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ private ModHappinessFactorTypeInitializer()

HappinessRegistry.housingFunction = DEFERRED_REGISTER_HAPPINESS_FUNCTION.register(HOUSING_FUNCTION.getPath(), () -> new HappinessFunctionEntry(data -> data.getHomeBuilding() == null ? 0.0 : data.getHomeBuilding().getBuildingLevel() / 3.0));
HappinessRegistry.unemploymentFunction = DEFERRED_REGISTER_HAPPINESS_FUNCTION.register(UNEMPLOYMENT_FUNCTION.getPath(), () -> new HappinessFunctionEntry(data -> data.isChild() ? 1.0 : (data.getWorkBuilding() == null ? 0.5 : data.getWorkBuilding().getBuildingLevel() > 3 ? 2.0 : 1.0)));
HappinessRegistry.healthFunction = DEFERRED_REGISTER_HAPPINESS_FUNCTION.register(HEALTH_FUNCTION.getPath(), () -> new HappinessFunctionEntry(data -> data.getEntity().isPresent() ? (data.getEntity().get().getCitizenDiseaseHandler().isSick() ? 0.5 : 1.0) : 1.0));
HappinessRegistry.healthFunction = DEFERRED_REGISTER_HAPPINESS_FUNCTION.register(HEALTH_FUNCTION.getPath(),
() -> new HappinessFunctionEntry(data -> data.getEntity().isPresent() ? (data.getCitizenDiseaseHandler().isSick() ? 0.5 : 1.0) : 1.0));
HappinessRegistry.idleatjobFunction = DEFERRED_REGISTER_HAPPINESS_FUNCTION.register(IDLEATJOB_FUNCTION.getPath(), () -> new HappinessFunctionEntry(data -> data.isIdleAtJob() ? 0.5 : 1.0));

HappinessRegistry.sleptTonightFunction = DEFERRED_REGISTER_HAPPINESS_FUNCTION.register(SLEPTTONIGHT_FUNCTION.getPath(), () -> new HappinessFunctionEntry(data -> data.getJob() instanceof AbstractJobGuard ? 1 : 0.5));
Expand Down
24 changes: 19 additions & 5 deletions src/main/java/com/minecolonies/core/colony/CitizenData.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.minecolonies.api.entity.citizen.AbstractEntityCitizen;
import com.minecolonies.api.entity.citizen.Skill;
import com.minecolonies.api.entity.citizen.VisibleCitizenStatus;
import com.minecolonies.api.entity.citizen.citizenhandlers.ICitizenDiseaseHandler;
import com.minecolonies.api.entity.citizen.citizenhandlers.ICitizenFoodHandler;
import com.minecolonies.api.entity.citizen.citizenhandlers.ICitizenSkillHandler;
import com.minecolonies.api.inventory.InventoryCitizen;
Expand All @@ -33,10 +34,7 @@
import com.minecolonies.core.colony.interactionhandling.ServerCitizenInteraction;
import com.minecolonies.core.colony.interactionhandling.StandardInteraction;
import com.minecolonies.core.entity.citizen.EntityCitizen;
import com.minecolonies.core.entity.citizen.citizenhandlers.CitizenFoodHandler;
import com.minecolonies.core.entity.citizen.citizenhandlers.CitizenHappinessHandler;
import com.minecolonies.core.entity.citizen.citizenhandlers.CitizenMournHandler;
import com.minecolonies.core.entity.citizen.citizenhandlers.CitizenSkillHandler;
import com.minecolonies.core.entity.citizen.citizenhandlers.*;
import com.minecolonies.core.network.messages.client.colony.ColonyViewCitizenViewMessage;
import com.minecolonies.core.util.AttributeModifierUtils;
import net.minecraft.core.BlockPos;
Expand Down Expand Up @@ -228,6 +226,11 @@ public class CitizenData implements ICitizenData
*/
private final ICitizenFoodHandler citizenFoodHandler;

/**
* Disease handler
*/
private final CitizenDiseaseHandler citizenDiseaseHandler;

/**
* The citizen chat options on the server side.
*/
Expand Down Expand Up @@ -353,6 +356,7 @@ public CitizenData(final int id, final IColony colony)
this.citizenMournHandler = new CitizenMournHandler(this);
this.citizenSkillHandler = new CitizenSkillHandler();
this.citizenFoodHandler = new CitizenFoodHandler(this);
citizenDiseaseHandler = new CitizenDiseaseHandler(this);
}

@Override
Expand Down Expand Up @@ -1188,6 +1192,12 @@ public ICitizenFoodHandler getCitizenFoodHandler()
return citizenFoodHandler;
}

@Override
public ICitizenDiseaseHandler getCitizenDiseaseHandler()
{
return citizenDiseaseHandler;
}

@Override
public void scheduleRestart(final ServerPlayer player)
{
Expand Down Expand Up @@ -1271,6 +1281,7 @@ public CompoundTag serializeNBT(@NotNull final HolderLookup.Provider provider)
citizenHappinessHandler.write(provider, nbtTagCompound, true);
citizenMournHandler.write(nbtTagCompound);
citizenFoodHandler.write(nbtTagCompound);
citizenDiseaseHandler.write(nbtTagCompound);

inventory.write(provider, nbtTagCompound);
nbtTagCompound.putInt(TAG_HELD_ITEM_SLOT, inventory.getHeldItemSlot(InteractionHand.MAIN_HAND));
Expand Down Expand Up @@ -1436,6 +1447,7 @@ public void deserializeNBT(@NotNull final HolderLookup.Provider provider, final
this.citizenHappinessHandler.read(provider, nbtTagCompound, true);
this.citizenMournHandler.read(nbtTagCompound);
this.citizenFoodHandler.read(nbtTagCompound);
citizenDiseaseHandler.read(nbtTagCompound);

if (nbtTagCompound.contains(TAG_LEVEL_MAP) && !nbtTagCompound.contains(TAG_NEW_SKILLS))
{
Expand Down Expand Up @@ -1555,7 +1567,7 @@ public void setInteractedRecently(final UUID player)
}

@Override
public void update()
public void update(final int tickRate)
{
if (!getEntity().isPresent() || !getEntity().get().isAlive())
{
Expand Down Expand Up @@ -1611,6 +1623,8 @@ public void update()
}
}
}

citizenDiseaseHandler.update(tickRate);
}

@Override
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/minecolonies/core/colony/Colony.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.*;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerChunkCache;
Expand Down Expand Up @@ -384,7 +383,10 @@ public class Colony implements IColony
colonyStateMachine.addTransition(new TickingTransition<>(INACTIVE, () -> true, this::updateState, UPDATE_STATE_INTERVAL));
colonyStateMachine.addTransition(new TickingTransition<>(UNLOADED, () -> true, this::updateState, UPDATE_STATE_INTERVAL));
colonyStateMachine.addTransition(new TickingTransition<>(ACTIVE, () -> true, this::updateState, UPDATE_STATE_INTERVAL));
colonyStateMachine.addTransition(new TickingTransition<>(ACTIVE, citizenManager::tickCitizenData, () -> ACTIVE, TICKS_SECOND * 3));
colonyStateMachine.addTransition(new TickingTransition<>(ACTIVE, () -> {
citizenManager.tickCitizenData(TICKS_SECOND * 3);
return false;
}, () -> ACTIVE, TICKS_SECOND * 3));

colonyStateMachine.addTransition(new TickingTransition<>(ACTIVE, this::updateSubscribers, () -> ACTIVE, UPDATE_SUBSCRIBERS_INTERVAL));
colonyStateMachine.addTransition(new TickingTransition<>(ACTIVE, this::tickRequests, () -> ACTIVE, UPDATE_RS_INTERVAL));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ public void onPlayerEnterNearby(final Player player)
@Override
public void onPlacement()
{
if (getBuildingLevel() == 0)
if (getBuildingLevel() == 0 && !hasParent())
{
ChunkDataHelper.claimBuildingChunks((Colony) colony, true, getPosition(), getClaimRadius(getBuildingLevel()), getCorners());
}
Expand Down Expand Up @@ -965,7 +965,10 @@ && getPosition() != null
@Override
public void onUpgradeComplete(final int newLevel)
{
ChunkDataHelper.claimBuildingChunks((Colony) colony, true, this.getID(), this.getClaimRadius(newLevel), getCorners());
if (!hasParent())
{
ChunkDataHelper.claimBuildingChunks((Colony) colony, true, this.getID(), this.getClaimRadius(newLevel), getCorners());
}
recheckGuardBuildingNear = true;

ConstructionTapeHelper.removeConstructionTape(getCorners(), colony.getWorld());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.minecolonies.core.colony.Colony;
import com.minecolonies.core.colony.buildings.AbstractBuildingGuards;
import com.minecolonies.core.util.AdvancementUtils;
import com.minecolonies.core.util.ChunkDataHelper;
import net.minecraft.core.BlockPos;
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
Expand Down Expand Up @@ -109,7 +108,6 @@ public void onUpgradeComplete(final int newLevel)
{
return;
}
ChunkDataHelper.claimBuildingChunks((Colony) colony, true, barracks, barrack.getClaimRadius(newLevel), barrack.getCorners());

if (newLevel == barrack.getMaxBuildingLevel())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
import net.minecraft.world.level.block.BedBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.BedPart;

import org.jetbrains.annotations.NotNull;

import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

import static com.minecolonies.api.util.constant.NbtTagConstants.*;
Expand Down Expand Up @@ -210,9 +212,9 @@ private boolean doesAnyPatientRequireStack(final ItemStack stack)
for (final Patient patient : patients.values())
{
final ICitizenData data = colony.getCitizenManager().getCivilian(patient.getId());
if (data != null && data.getEntity().isPresent() && data.getEntity().get().getCitizenDiseaseHandler().isSick())
if (data != null && data.getEntity().isPresent() && data.getCitizenDiseaseHandler().isSick())
{
final String diseaseName = data.getEntity().get().getCitizenDiseaseHandler().getDisease();
final String diseaseName = data.getCitizenDiseaseHandler().getDisease();
if (!diseaseName.isEmpty())
{
final Disease disease = IColonyManager.getInstance().getCompatibilityManager().getDisease(diseaseName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -562,11 +562,11 @@ public void checkCitizensForHappiness()
}

@Override
public boolean tickCitizenData()
public boolean tickCitizenData(final int tickRate)
{
for (ICitizenData iCitizenData : this.getCitizens())
{
iCitizenData.update();
iCitizenData.update(tickRate);
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ public BlockPos getBestBuilding(final BlockPos citizen, final Class<? extends IB
BlockPos goodCook = null;
for (final IBuilding building : buildings.values())
{
if (clazz.isInstance(building) && building.getBuildingLevel() > 0)
if (clazz.isInstance(building) && building.getBuildingLevel() > 0 && WorldUtil.isBlockLoaded(colony.getWorld(), building.getPosition()))
{
final double localDistance = building.getPosition().distSqr(citizen);
if (localDistance < distance)
Expand Down Expand Up @@ -721,7 +721,7 @@ public boolean hasGuardBuildingNear(final IBuilding building)

for (final IBuilding colonyBuilding : getBuildings().values())
{
if (colonyBuilding instanceof IGuardBuilding || colonyBuilding instanceof BuildingBarracks)
if (colonyBuilding.getBuildingLevel() > 0 && (colonyBuilding instanceof IGuardBuilding || colonyBuilding instanceof BuildingBarracks))
{
final BoundingBox guardedRegion = BlockPosUtil.getChunkAlignedBB(colonyBuilding.getPosition(), colonyBuilding.getClaimRadius(colonyBuilding.getBuildingLevel()));
if (guardedRegion.isInside(building.getPosition()))
Expand Down
Loading

0 comments on commit 914b3be

Please sign in to comment.