Search in sources :

Example 91 with IBuilding

use of com.minecolonies.api.colony.buildings.IBuilding in project minecolonies by Minecolonies.

the class RegisteredStructureManager method guardBuildingChangedAt.

@Override
public void guardBuildingChangedAt(final IBuilding guardBuilding, final int newLevel) {
    final int claimRadius = guardBuilding.getClaimRadius(Math.max(guardBuilding.getBuildingLevel(), newLevel));
    final MutableBoundingBox guardedRegion = BlockPosUtil.getChunkAlignedBB(guardBuilding.getPosition(), claimRadius);
    for (final IBuilding building : getBuildings().values()) {
        if (guardedRegion.isInside(building.getPosition())) {
            building.resetGuardBuildingNear();
        }
    }
}
Also used : MutableBoundingBox(net.minecraft.util.math.MutableBoundingBox) IBuilding(com.minecolonies.api.colony.buildings.IBuilding)

Example 92 with IBuilding

use of com.minecolonies.api.colony.buildings.IBuilding 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 93 with IBuilding

use of com.minecolonies.api.colony.buildings.IBuilding in project minecolonies by Minecolonies.

the class CitizenManager method calculateMaxCitizens.

@Override
public void calculateMaxCitizens() {
    int newMaxCitizens = 0;
    int potentialMax = 0;
    for (final IBuilding b : colony.getBuildingManager().getBuildings().values()) {
        if (b.getBuildingLevel() > 0) {
            if (b.hasModule(BedHandlingModule.class) && b.hasModule(WorkAtHomeBuildingModule.class)) {
                final WorkAtHomeBuildingModule module = b.getFirstModuleOccurance(WorkAtHomeBuildingModule.class);
                newMaxCitizens += b.getAllAssignedCitizen().size();
                potentialMax += module.getModuleMax() - b.getAllAssignedCitizen().size();
            } else if (b.hasModule(LivingBuildingModule.class)) {
                newMaxCitizens += b.getFirstModuleOccurance(LivingBuildingModule.class).getModuleMax();
            }
        }
    }
    if (getMaxCitizens() != newMaxCitizens) {
        setMaxCitizens(newMaxCitizens);
        setPotentialMaxCitizens(potentialMax + newMaxCitizens);
        colony.markDirty();
    }
}
Also used : LivingBuildingModule(com.minecolonies.coremod.colony.buildings.modules.LivingBuildingModule) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) WorkAtHomeBuildingModule(com.minecolonies.coremod.colony.buildings.modules.WorkAtHomeBuildingModule) BedHandlingModule(com.minecolonies.coremod.colony.buildings.modules.BedHandlingModule)

Example 94 with IBuilding

use of com.minecolonies.api.colony.buildings.IBuilding in project minecolonies by Minecolonies.

the class Tree method checkIfInColonyAndNotInBuilding.

/**
 * Calculates with a colony if the position is inside the colony and if it is inside a building.
 *
 * @param pos    the position.
 * @param colony the colony.
 * @param world  the world to use
 * @return return false if not inside the colony or if inside a building.
 */
public static boolean checkIfInColonyAndNotInBuilding(final BlockPos pos, final IColony colony, final IWorldReader world) {
    final IChunk chunk = world.getChunk(pos);
    if (!(chunk instanceof Chunk)) {
        return false;
    }
    final IColonyTagCapability cap = ((Chunk) chunk).getCapability(CLOSE_COLONY_CAP, null).resolve().orElse(null);
    if (cap != null && cap.getOwningColony() != colony.getID()) {
        return false;
    }
    // Dynamic tree's are never part of buildings
    if (Compatibility.isDynamicBlock(world.getBlockState(pos).getBlock())) {
        return true;
    }
    for (final IBuilding building : colony.getBuildingManager().getBuildings().values()) {
        if (building.isInBuilding(pos)) {
            return false;
        }
    }
    return true;
}
Also used : IBuilding(com.minecolonies.api.colony.buildings.IBuilding) IChunk(net.minecraft.world.chunk.IChunk) Chunk(net.minecraft.world.chunk.Chunk) IChunk(net.minecraft.world.chunk.IChunk) IColonyTagCapability(com.minecolonies.api.colony.IColonyTagCapability)

Example 95 with IBuilding

use of com.minecolonies.api.colony.buildings.IBuilding in project minecolonies by Minecolonies.

the class EntityAIQuarrier method startWorkingAtOwnBuilding.

// Miner wants to work but is not at building
@NotNull
private IAIState startWorkingAtOwnBuilding() {
    worker.getCitizenData().setVisibleStatus(VisibleCitizenStatus.WORKING);
    final IBuilding quarry = job.findQuarry();
    if (quarry == null) {
        walkToBuilding();
        worker.getCitizenData().triggerInteraction(new StandardInteraction(new TranslationTextComponent(QUARRY_MINER_NO_QUARRY), ChatPriority.BLOCKING));
        return IDLE;
    }
    if (quarry.getFirstModuleOccurance(QuarryModule.class).isFinished()) {
        walkToBuilding();
        worker.getCitizenData().triggerInteraction(new StandardInteraction(new TranslationTextComponent(QUARRY_MINER_FINISHED_QUARRY), ChatPriority.BLOCKING));
        return IDLE;
    }
    if (walkToBlock(quarry.getPosition())) {
        return getState();
    }
    // Miner is at building
    return LOAD_STRUCTURE;
}
Also used : IBuilding(com.minecolonies.api.colony.buildings.IBuilding) StandardInteraction(com.minecolonies.coremod.colony.interactionhandling.StandardInteraction) QuarryModule(com.minecolonies.coremod.colony.buildings.modules.QuarryModule) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

IBuilding (com.minecolonies.api.colony.buildings.IBuilding)187 BlockPos (net.minecraft.util.math.BlockPos)71 NotNull (org.jetbrains.annotations.NotNull)45 IColony (com.minecolonies.api.colony.IColony)37 Nullable (org.jetbrains.annotations.Nullable)26 ServerPlayerEntity (net.minecraft.entity.player.ServerPlayerEntity)24 ICitizenData (com.minecolonies.api.colony.ICitizenData)22 World (net.minecraft.world.World)20 ItemStack (net.minecraft.item.ItemStack)19 TranslationTextComponent (net.minecraft.util.text.TranslationTextComponent)19 TileEntity (net.minecraft.tileentity.TileEntity)17 CompoundNBT (net.minecraft.nbt.CompoundNBT)15 ArrayList (java.util.ArrayList)14 AbstractBuildingGuards (com.minecolonies.coremod.colony.buildings.AbstractBuildingGuards)10 ItemStorage (com.minecolonies.api.crafting.ItemStorage)9 ResourceLocation (net.minecraft.util.ResourceLocation)9 IItemHandler (net.minecraftforge.items.IItemHandler)9 InventoryUtils (com.minecolonies.api.util.InventoryUtils)8 ItemStackUtils (com.minecolonies.api.util.ItemStackUtils)8 SubscribeEvent (net.minecraftforge.eventbus.api.SubscribeEvent)8