Search in sources :

Example 21 with CitizenData

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);
    }
}
Also used : CitizenData(com.minecolonies.coremod.colony.CitizenData)

Example 22 with CitizenData

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);
        }
    }
}
Also used : NBTTagList(net.minecraft.nbt.NBTTagList) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) CitizenData(com.minecolonies.coremod.colony.CitizenData) BlockPos(net.minecraft.util.math.BlockPos)

Example 23 with CitizenData

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;
}
Also used : AbstractBuildingGuards(com.minecolonies.coremod.colony.buildings.AbstractBuildingGuards) Task(com.minecolonies.coremod.colony.buildings.AbstractBuildingGuards.Task) Colony(com.minecolonies.coremod.colony.Colony) BlockPos(net.minecraft.util.math.BlockPos) CitizenData(com.minecolonies.coremod.colony.CitizenData) AbstractBuilding(com.minecolonies.coremod.colony.buildings.AbstractBuilding) NotNull(org.jetbrains.annotations.NotNull)

Example 24 with CitizenData

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");
                }
            }
        }
    }
}
Also used : AbstractBuildingWorker(com.minecolonies.coremod.colony.buildings.AbstractBuildingWorker) Colony(com.minecolonies.coremod.colony.Colony) CitizenData(com.minecolonies.coremod.colony.CitizenData) BlockPos(net.minecraft.util.math.BlockPos) EntityCitizen(com.minecolonies.coremod.entity.EntityCitizen) Nullable(org.jetbrains.annotations.Nullable)

Example 25 with CitizenData

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);
        }
    }
}
Also used : BuildingHome(com.minecolonies.coremod.colony.buildings.BuildingHome) Colony(com.minecolonies.coremod.colony.Colony) CitizenData(com.minecolonies.coremod.colony.CitizenData) AbstractBuilding(com.minecolonies.coremod.colony.buildings.AbstractBuilding)

Aggregations

CitizenData (com.minecolonies.coremod.colony.CitizenData)29 BlockPos (net.minecraft.util.math.BlockPos)15 Colony (com.minecolonies.coremod.colony.Colony)14 NotNull (org.jetbrains.annotations.NotNull)9 EntityCitizen (com.minecolonies.coremod.entity.EntityCitizen)7 Nullable (org.jetbrains.annotations.Nullable)7 TextComponentString (net.minecraft.util.text.TextComponentString)6 IToken (com.minecolonies.api.colony.requestsystem.token.IToken)4 InvWrapper (net.minecraftforge.items.wrapper.InvWrapper)4 IRequest (com.minecolonies.api.colony.requestsystem.request.IRequest)3 MineColonies (com.minecolonies.coremod.MineColonies)3 JobDeliveryman (com.minecolonies.coremod.colony.jobs.JobDeliveryman)3 ItemStack (net.minecraft.item.ItemStack)3 Lists (com.google.common.collect.Lists)2 TypeToken (com.google.common.reflect.TypeToken)2 IColony (com.minecolonies.api.colony.IColony)2 ILocation (com.minecolonies.api.colony.requestsystem.location.ILocation)2 IRequestManager (com.minecolonies.api.colony.requestsystem.manager.IRequestManager)2 Delivery (com.minecolonies.api.colony.requestsystem.requestable.Delivery)2 IRecipeStorage (com.minecolonies.api.crafting.IRecipeStorage)2