Search in sources :

Example 71 with ICitizenData

use of com.minecolonies.api.colony.ICitizenData in project minecolonies by Minecolonies.

the class ReproductionManager method trySpawnChild.

/**
 * Try to spawn a new citizen as child. Mom / dad entities are required and chosen randomly in this hut. Childs inherit stats from their parents, avergaged +-2 Childs get
 * assigned to a free housing slot in the colony to be raised there, if the house has an adult living there the child takes its name and gets raised by it.
 */
public void trySpawnChild() {
    // Spawn a child when adults are present
    if (colony.canMoveIn() && colony.getCitizenManager().getCurrentCitizenCount() < colony.getCitizenManager().getMaxCitizens() && colony.getCitizenManager().getCurrentCitizenCount() >= Math.min(MIN_SIZE_FOR_REPRO, MinecoloniesAPIProxy.getInstance().getConfig().getServer().initialCitizenAmount.get())) {
        if (!checkForBioParents()) {
            return;
        }
        final IBuilding newHome = colony.getBuildingManager().getHouseWithSpareBed();
        if (newHome == null) {
            return;
        }
        final LivingBuildingModule module = newHome.getFirstModuleOccurance(LivingBuildingModule.class);
        final List<ICitizenData> assignedCitizens = module.getAssignedCitizen();
        assignedCitizens.removeIf(ICitizen::isChild);
        final ICitizenData newCitizen = colony.getCitizenManager().createAndRegisterCivilianData();
        ICitizenData firstParent;
        ICitizenData secondParent;
        if (!assignedCitizens.isEmpty()) {
            firstParent = assignedCitizens.get(random.nextInt(assignedCitizens.size()));
            secondParent = firstParent.getPartner();
            if (secondParent == null) {
                assignedCitizens.removeIf(cit -> cit.getPartner() != null || cit.getName().equals(firstParent.getName()) || cit.isRelatedTo(firstParent));
                if (assignedCitizens.size() > 0 && random.nextBoolean()) {
                    secondParent = assignedCitizens.get(random.nextInt(assignedCitizens.size()));
                } else {
                    final BlockPos altPos = colony.getBuildingManager().getRandomBuilding(b -> b.hasModule(LivingBuildingModule.class) && !b.getPosition().equals(newHome.getPosition()) && BlockPosUtil.getDistance2D(b.getPosition(), newHome.getPosition()) < 50);
                    if (altPos != null) {
                        final IBuilding building = colony.getBuildingManager().getBuilding(altPos);
                        final LivingBuildingModule altModule = building.getFirstModuleOccurance(LivingBuildingModule.class);
                        final List<ICitizenData> newAssignedCitizens = altModule.getAssignedCitizen();
                        newAssignedCitizens.removeIf(cit -> cit.isChild() || cit.getPartner() != null || cit.isRelatedTo(firstParent));
                        if (newAssignedCitizens.size() > 0) {
                            secondParent = newAssignedCitizens.get(random.nextInt(newAssignedCitizens.size()));
                        }
                    }
                }
            }
        } else {
            firstParent = null;
            secondParent = null;
        }
        if (secondParent != null) {
            firstParent.setPartner(secondParent.getId());
            secondParent.setPartner(firstParent.getId());
        }
        newCitizen.getCitizenSkillHandler().init(colony, firstParent, secondParent, random);
        newCitizen.setIsChild(true);
        final List<String> possibleSuffixes = new ArrayList<>();
        if (firstParent != null) {
            newCitizen.addSiblings(firstParent.getChildren().toArray(new Integer[0]));
            firstParent.addChildren(newCitizen.getId());
            possibleSuffixes.add(firstParent.getTextureSuffix());
        }
        if (secondParent != null) {
            newCitizen.addSiblings(secondParent.getChildren().toArray(new Integer[0]));
            secondParent.addChildren(newCitizen.getId());
            possibleSuffixes.add(secondParent.getTextureSuffix());
        }
        newCitizen.setParents(firstParent == null ? "" : firstParent.getName(), secondParent == null ? "" : secondParent.getName());
        newCitizen.generateName(random, firstParent == null ? "" : firstParent.getName(), secondParent == null ? "" : secondParent.getName());
        module.assignCitizen(newCitizen);
        for (final int sibling : newCitizen.getSiblings()) {
            final ICitizenData siblingData = colony.getCitizenManager().getCivilian(sibling);
            if (siblingData != null) {
                siblingData.addSiblings(newCitizen.getId());
            }
        }
        if (possibleSuffixes.contains("_w") && possibleSuffixes.contains("_d")) {
            possibleSuffixes.add("_b");
        }
        if (possibleSuffixes.isEmpty()) {
            possibleSuffixes.addAll(SUFFIXES);
        }
        newCitizen.setSuffix(possibleSuffixes.get(random.nextInt(possibleSuffixes.size())));
        final int populationCount = colony.getCitizenManager().getCurrentCitizenCount();
        AdvancementUtils.TriggerAdvancementPlayersForColony(colony, playerMP -> AdvancementTriggers.COLONY_POPULATION.trigger(playerMP, populationCount));
        MessageUtils.format(MESSAGE_NEW_CHILD_BORN, newCitizen.getName(), colony.getName()).with(TextFormatting.GOLD).sendTo(colony).forManagers();
        colony.getCitizenManager().spawnOrCreateCitizen(newCitizen, colony.getWorld(), newHome.getPosition());
        colony.getEventDescriptionManager().addEventDescription(new CitizenBornEvent(newHome.getPosition(), newCitizen.getName()));
    }
}
Also used : LivingBuildingModule(com.minecolonies.coremod.colony.buildings.modules.LivingBuildingModule) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) ICitizen(com.minecolonies.api.colony.ICitizen) ArrayList(java.util.ArrayList) ICitizenData(com.minecolonies.api.colony.ICitizenData) CitizenBornEvent(com.minecolonies.coremod.colony.colonyEvents.citizenEvents.CitizenBornEvent) BlockPos(net.minecraft.util.math.BlockPos)

Example 72 with ICitizenData

use of com.minecolonies.api.colony.ICitizenData in project minecolonies by Minecolonies.

the class ReproductionManager method checkForBioParents.

/**
 * Check if there are potential biological parents in the colony.
 * (At least one male/female citizen).
 * @return true if so.
 */
private boolean checkForBioParents() {
    boolean hasMale = false;
    boolean hasFemale = false;
    for (final ICitizenData data : colony.getCitizenManager().getCitizens()) {
        if (data.isFemale()) {
            hasFemale = true;
        } else {
            hasMale = true;
        }
        if (hasFemale && hasMale) {
            return true;
        }
    }
    for (final ICivilianData data : colony.getVisitorManager().getCivilianDataMap().values()) {
        if (data.isFemale()) {
            hasFemale = true;
        } else {
            hasMale = true;
        }
        if (hasFemale && hasMale) {
            return true;
        }
    }
    return false;
}
Also used : ICivilianData(com.minecolonies.api.colony.ICivilianData) ICitizenData(com.minecolonies.api.colony.ICitizenData)

Example 73 with ICitizenData

use of com.minecolonies.api.colony.ICitizenData in project minecolonies by Minecolonies.

the class VisitorManager method registerCivilian.

@Override
public void registerCivilian(final AbstractCivilianEntity visitor) {
    if (visitor.getCivilianID() == 0 || visitorMap.get(visitor.getCivilianID()) == null) {
        visitor.remove();
        return;
    }
    final ICitizenData data = visitorMap.get(visitor.getCivilianID());
    final Optional<AbstractEntityCitizen> existingCitizen = data.getEntity();
    if (!existingCitizen.isPresent()) {
        data.setEntity(visitor);
        visitor.setCivilianData(data);
        return;
    }
    if (existingCitizen.get() == visitor) {
        return;
    }
    if (visitor.isAlive()) {
        existingCitizen.get().remove();
        data.setEntity(visitor);
        visitor.setCivilianData(data);
        return;
    }
    visitor.remove();
}
Also used : AbstractEntityCitizen(com.minecolonies.api.entity.citizen.AbstractEntityCitizen) ICitizenData(com.minecolonies.api.colony.ICitizenData)

Example 74 with ICitizenData

use of com.minecolonies.api.colony.ICitizenData in project minecolonies by Minecolonies.

the class CitizenManager method createAndRegisterCivilianData.

@Override
public ICitizenData createAndRegisterCivilianData() {
    // That's needed to prevent bugs when calling IDs that are not used.
    for (int i = 1; i <= this.getCurrentCitizenCount() + 1; i++) {
        if (this.getCivilian(i) == null) {
            topCitizenId = i;
            break;
        }
    }
    final CitizenData citizenData = new CitizenData(topCitizenId, colony);
    citizenData.initForNewCivilian();
    citizens.put(citizenData.getId(), citizenData);
    return citizenData;
}
Also used : ICitizenData(com.minecolonies.api.colony.ICitizenData) CitizenData(com.minecolonies.coremod.colony.CitizenData)

Example 75 with ICitizenData

use of com.minecolonies.api.colony.ICitizenData in project minecolonies by Minecolonies.

the class CitizenManager method resurrectCivilianData.

@Override
public ICitizenData resurrectCivilianData(@NotNull final CompoundNBT compoundNBT, final boolean resetId, @NotNull final World world, final BlockPos spawnPos) {
    // That's needed to prevent bugs when calling IDs that are not used.
    for (int i = 1; i <= this.getCurrentCitizenCount() + 1; i++) {
        if (this.getCivilian(i) == null) {
            topCitizenId = i;
            break;
        }
    }
    if (resetId) {
        compoundNBT.putInt(TAG_ID, topCitizenId);
    }
    final ICitizenData citizenData = deserializeCitizen(compoundNBT);
    citizenData.onResurrect();
    citizens.put(citizenData.getId(), citizenData);
    spawnOrCreateCitizen(citizenData, world, spawnPos);
    return citizenData;
}
Also used : ICitizenData(com.minecolonies.api.colony.ICitizenData)

Aggregations

ICitizenData (com.minecolonies.api.colony.ICitizenData)180 NotNull (org.jetbrains.annotations.NotNull)49 BlockPos (net.minecraft.util.math.BlockPos)47 IColony (com.minecolonies.api.colony.IColony)46 AbstractEntityCitizen (com.minecolonies.api.entity.citizen.AbstractEntityCitizen)40 IBuilding (com.minecolonies.api.colony.buildings.IBuilding)33 TranslationTextComponent (net.minecraft.util.text.TranslationTextComponent)31 ItemStack (net.minecraft.item.ItemStack)28 WorkerBuildingModule (com.minecolonies.coremod.colony.buildings.modules.WorkerBuildingModule)23 PlayerEntity (net.minecraft.entity.player.PlayerEntity)23 IRequest (com.minecolonies.api.colony.requestsystem.request.IRequest)22 Nullable (org.jetbrains.annotations.Nullable)22 CompoundNBT (net.minecraft.nbt.CompoundNBT)18 Colony (com.minecolonies.coremod.colony.Colony)16 ArrayList (java.util.ArrayList)16 AbstractJobCrafter (com.minecolonies.coremod.colony.jobs.AbstractJobCrafter)14 EntityCitizen (com.minecolonies.coremod.entity.citizen.EntityCitizen)14 ServerPlayerEntity (net.minecraft.entity.player.ServerPlayerEntity)13 AbstractAssignedCitizenModule (com.minecolonies.coremod.colony.buildings.modules.AbstractAssignedCitizenModule)12 java.util (java.util)12