use of com.minecolonies.coremod.colony.CitizenData in project minecolonies by Minecolonies.
the class CitizenManager method checkCitizensForHappiness.
@Override
public void checkCitizensForHappiness() {
int guards = 1;
int housing = 0;
int workers = 1;
double saturation = 0;
for (final CitizenData citizen : getCitizens()) {
final AbstractBuildingWorker buildingWorker = citizen.getWorkBuilding();
if (buildingWorker != null) {
if (buildingWorker instanceof AbstractBuildingGuards) {
guards += buildingWorker.getBuildingLevel();
} else {
workers += buildingWorker.getBuildingLevel();
}
}
final AbstractBuilding home = citizen.getHomeBuilding();
if (home != null) {
housing += home.getBuildingLevel();
}
saturation += citizen.getSaturation();
}
final int averageHousing = housing / Math.max(1, getCitizens().size());
if (averageHousing > 1) {
colony.increaseOverallHappiness(averageHousing * HAPPINESS_FACTOR);
}
final int averageSaturation = (int) (saturation / getCitizens().size());
if (averageSaturation < WELL_SATURATED_LIMIT) {
colony.decreaseOverallHappiness((averageSaturation - WELL_SATURATED_LIMIT) * -HAPPINESS_FACTOR);
} else if (averageSaturation > WELL_SATURATED_LIMIT) {
colony.increaseOverallHappiness((averageSaturation - WELL_SATURATED_LIMIT) * HAPPINESS_FACTOR);
}
final int relation = workers / guards;
if (relation > 1) {
colony.decreaseOverallHappiness(relation * HAPPINESS_FACTOR);
}
}
use of com.minecolonies.coremod.colony.CitizenData in project minecolonies by Minecolonies.
the class BuildingHome method readFromNBT.
@Override
public void readFromNBT(@NotNull final NBTTagCompound compound) {
super.readFromNBT(compound);
residents.clear();
final int[] residentIds = compound.getIntArray(TAG_RESIDENTS);
for (final int citizenId : residentIds) {
final CitizenData citizen = getColony().getCitizenManager().getCitizen(citizenId);
if (citizen != null) {
// Bypass addResident (which marks dirty)
residents.add(citizen);
citizen.setHomeBuilding(this);
}
}
final NBTTagList bedTagList = compound.getTagList(TAG_BEDS, Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < bedTagList.tagCount(); ++i) {
final NBTTagCompound bedCompound = bedTagList.getCompoundTagAt(i);
final BlockPos bedPos = NBTUtil.getPosFromTag(bedCompound);
if (!bedList.contains(bedPos)) {
bedList.add(bedPos);
}
}
}
use of com.minecolonies.coremod.colony.CitizenData in project minecolonies by Minecolonies.
the class ItemScepterGuard method handleItemUsage.
/**
* Handles the usage of the item.
*
* @param worldIn the world it is used in.
* @param pos the position.
* @param compound the compound.
* @param playerIn the player using it.
* @return if it has been successful.
*/
@NotNull
private static EnumActionResult handleItemUsage(final World worldIn, final BlockPos pos, final NBTTagCompound compound, final EntityPlayer playerIn) {
final Colony colony = ColonyManager.getClosestColony(worldIn, pos);
if (colony == null) {
return EnumActionResult.FAIL;
}
final BlockPos guardTower = BlockPosUtil.readFromNBT(compound, "pos");
final AbstractBuilding hut = colony.getBuildingManager().getBuilding(guardTower);
if (!(hut instanceof AbstractBuildingGuards)) {
return EnumActionResult.FAIL;
}
final AbstractBuildingGuards tower = (AbstractBuildingGuards) hut;
if (BlockPosUtil.getDistance2D(pos, guardTower) > tower.getPatrolDistance()) {
LanguageHandler.sendPlayerMessage(playerIn, "com.minecolonies.coremod.job.guard.toolClickGuardTooFar");
return EnumActionResult.FAIL;
}
final Task task = Task.values()[compound.getInteger("task")];
final CitizenData citizen = tower.getMainWorker();
String name = "";
if (citizen != null) {
name = " " + citizen.getName();
}
if (task.equals(Task.GUARD)) {
LanguageHandler.sendPlayerMessage(playerIn, "com.minecolonies.coremod.job.guard.toolClickGuard", pos, name);
tower.setGuardTarget(pos);
playerIn.inventory.removeStackFromSlot(playerIn.inventory.currentItem);
} else {
if (!compound.hasKey(TAG_LAST_POS)) {
tower.resetPatrolTargets();
}
tower.addPatrolTargets(pos);
LanguageHandler.sendPlayerMessage(playerIn, "com.minecolonies.coremod.job.guard.toolClickPatrol", pos, name);
}
BlockPosUtil.writeToNBT(compound, TAG_LAST_POS, pos);
return EnumActionResult.SUCCESS;
}
use of com.minecolonies.coremod.colony.CitizenData in project minecolonies by Minecolonies.
the class RecallCitizenMessage method messageOnServerThread.
@Override
public void messageOnServerThread(final RecallCitizenMessage message, final EntityPlayerMP player) {
final Colony colony = ColonyManager.getColony(message.colonyId);
if (colony != null) {
// Verify player has permission to change this huts settings
if (!colony.getPermissions().hasPermission(player, Action.MANAGE_HUTS)) {
return;
}
@Nullable final AbstractBuildingWorker building = colony.getBuildingManager().getBuilding(message.buildingId, AbstractBuildingWorker.class);
if (building != null) {
for (int i = 0; i < building.getWorkerEntities().size(); i++) {
Optional<EntityCitizen> optionalEntityCitizen = building.getWorkerEntities().get(i);
if (!optionalEntityCitizen.isPresent()) {
final CitizenData citizenData = building.getWorker().get(i);
if (citizenData != null) {
Log.getLogger().warn(String.format("Citizen #%d:%d has gone AWOL, respawning them!", colony.getID(), citizenData.getId()));
citizenData.updateCitizenEntityIfNecessary();
optionalEntityCitizen = citizenData.getCitizenEntity();
} else {
Log.getLogger().warn("Citizen is AWOL and citizenData is null!");
return;
}
}
final BlockPos loc = building.getLocation();
if (optionalEntityCitizen.isPresent() && !TeleportHelper.teleportCitizen(optionalEntityCitizen.get(), colony.getWorld(), loc)) {
LanguageHandler.sendPlayerMessage(player, "com.minecolonies.coremod.workerHuts.recallFail");
}
}
}
}
}
use of com.minecolonies.coremod.colony.CitizenData in project minecolonies by Minecolonies.
the class AssignUnassignMessage method messageOnServerThread.
@Override
public void messageOnServerThread(final AssignUnassignMessage message, final EntityPlayerMP player) {
final Colony colony = ColonyManager.getColony(message.colonyId);
if (colony != null) {
// Verify player has permission to change this huts settings
if (!colony.getPermissions().hasPermission(player, Action.MANAGE_HUTS)) {
return;
}
final AbstractBuilding building = colony.getBuildingManager().getBuilding(message.buildingId);
if (!(building instanceof BuildingHome)) {
return;
}
final CitizenData citizen = colony.getCitizenManager().getCitizen(message.citizenID);
if (message.assign && !((BuildingHome) building).isFull() && citizen.getHomeBuilding() == null) {
((BuildingHome) building).addResident(citizen);
} else if (((BuildingHome) building).hasResident(citizen)) {
building.removeCitizen(citizen);
}
}
}
Aggregations