Search in sources :

Example 71 with AbstractEntityCitizen

use of com.minecolonies.api.entity.citizen.AbstractEntityCitizen in project minecolonies by ldtteam.

the class RecallCitizenMessage method onExecute.

@Override
protected void onExecute(final NetworkEvent.Context ctxIn, final boolean isLogicalServer, final IColony colony, final IBuilding building) {
    final List<ICitizenData> citizens = new ArrayList<>(building.getAllAssignedCitizen());
    for (int i = 0; i < building.getAllAssignedCitizen().size(); i++) {
        Optional<AbstractEntityCitizen> optionalEntityCitizen = citizens.get(i).getEntity();
        final ICitizenData citizenData = citizens.get(i);
        if (!optionalEntityCitizen.isPresent()) {
            if (citizenData != null) {
                Log.getLogger().warn(String.format("Citizen #%d:%d has gone AWOL, respawning them!", colony.getID(), citizenData.getId()));
                citizenData.setNextRespawnPosition(EntityUtils.getSpawnPoint(colony.getWorld(), building.getPosition()));
                citizenData.updateEntityIfNecessary();
            } else {
                Log.getLogger().warn("Citizen is AWOL and citizenData is null!");
                return;
            }
        } else if (optionalEntityCitizen.get().getTicksExisted() == 0) {
            citizenData.getEntity().ifPresent(Entity::remove);
            citizenData.updateEntityIfNecessary();
        }
        final BlockPos loc = building.getPosition();
        if (optionalEntityCitizen.isPresent() && !TeleportHelper.teleportCitizen(optionalEntityCitizen.get(), colony.getWorld(), loc)) {
            final PlayerEntity player = ctxIn.getSender();
            if (player == null) {
                return;
            }
            LanguageHandler.sendPlayerMessage(player, "com.minecolonies.coremod.workerhuts.recallFail");
        }
    }
}
Also used : ArrayList(java.util.ArrayList) AbstractEntityCitizen(com.minecolonies.api.entity.citizen.AbstractEntityCitizen) ICitizenData(com.minecolonies.api.colony.ICitizenData) BlockPos(net.minecraft.util.math.BlockPos) PlayerEntity(net.minecraft.entity.player.PlayerEntity)

Example 72 with AbstractEntityCitizen

use of com.minecolonies.api.entity.citizen.AbstractEntityCitizen in project minecolonies by ldtteam.

the class BuildingCombatAcademy method getCombatPartner.

/**
 * Get the citizen of the combat partner or null if not existing or available.
 *
 * @param citizen the citizen.
 * @return the citizen or null.
 */
public AbstractEntityCitizen getCombatPartner(final AbstractEntityCitizen citizen) {
    final ICitizenData data = citizen.getCitizenData();
    if (data != null) {
        final int citizenId;
        if (trainingPartners.containsKey(data.getId())) {
            citizenId = trainingPartners.get(data.getId());
        } else if (trainingPartners.containsValue(data.getId())) {
            citizenId = trainingPartners.inverse().get(data.getId());
        } else {
            return null;
        }
        final ICitizenData citizenData = getFirstModuleOccurance(WorkAtHomeBuildingModule.class).getAssignedCitizen().stream().filter(cit -> cit.getId() != data.getId()).filter(cit -> cit.getId() == citizenId).findFirst().orElse(null);
        if (citizenData != null) {
            return citizenData.getEntity().orElse(null);
        }
    }
    return null;
}
Also used : Window(com.ldtteam.blockout.views.Window) Constants(net.minecraftforge.common.util.Constants) CompoundNBT(net.minecraft.nbt.CompoundNBT) ICitizenData(com.minecolonies.api.colony.ICitizenData) Random(java.util.Random) ArrayList(java.util.ArrayList) Block(net.minecraft.block.Block) WindowHutWorkerModulePlaceholder(com.minecolonies.coremod.client.gui.huts.WindowHutWorkerModulePlaceholder) Map(java.util.Map) ListNBT(net.minecraft.nbt.ListNBT) BiMap(com.google.common.collect.BiMap) IColonyView(com.minecolonies.api.colony.IColonyView) WorkAtHomeBuildingModule(com.minecolonies.coremod.colony.buildings.modules.WorkAtHomeBuildingModule) World(net.minecraft.world.World) IBuildingView(com.minecolonies.api.colony.buildings.views.IBuildingView) JobCombatTraining(com.minecolonies.coremod.colony.jobs.JobCombatTraining) AbstractBuilding(com.minecolonies.coremod.colony.buildings.AbstractBuilding) BlockPos(net.minecraft.util.math.BlockPos) BlockPosUtil(com.minecolonies.api.util.BlockPosUtil) Collectors(java.util.stream.Collectors) CarvedPumpkinBlock(net.minecraft.block.CarvedPumpkinBlock) AbstractBuildingView(com.minecolonies.coremod.colony.buildings.views.AbstractBuildingView) HashBiMap(com.google.common.collect.HashBiMap) AbstractEntityCitizen(com.minecolonies.api.entity.citizen.AbstractEntityCitizen) List(java.util.List) NbtTagConstants(com.minecolonies.api.util.constant.NbtTagConstants) NBTUtils(com.minecolonies.api.util.NBTUtils) IColony(com.minecolonies.api.colony.IColony) HayBlock(net.minecraft.block.HayBlock) NotNull(org.jetbrains.annotations.NotNull) ICitizenData(com.minecolonies.api.colony.ICitizenData)

Example 73 with AbstractEntityCitizen

use of com.minecolonies.api.entity.citizen.AbstractEntityCitizen in project minecolonies by ldtteam.

the class EntityAIWorkEnchanter method gatherAndDrain.

/**
 * Gather experience from a worker. Go to the hut of the worker. Wait for the worker. Drain, and then return to work building.
 *
 * @return next state to go to.
 */
private IAIState gatherAndDrain() {
    if (job.getPosToDrainFrom() == null) {
        return IDLE;
    }
    if (walkToBlock(job.getPosToDrainFrom())) {
        return getState();
    }
    final AbstractBuilding buildingWorker = getOwnBuilding().getColony().getBuildingManager().getBuilding(job.getPosToDrainFrom(), AbstractBuilding.class);
    if (buildingWorker == null) {
        resetDraining();
        getOwnBuilding().getFirstModuleOccurance(EnchanterStationsModule.class).removeWorker(job.getPosToDrainFrom());
        return IDLE;
    }
    if (citizenToGatherFrom == null) {
        final List<AbstractEntityCitizen> workers = new ArrayList<>();
        for (final Optional<AbstractEntityCitizen> citizen : getModuleForJob().getAssignedEntities()) {
            citizen.ifPresent(workers::add);
        }
        final AbstractEntityCitizen citizen;
        if (workers.size() > 1) {
            citizen = workers.get(worker.getRandom().nextInt(workers.size()));
        } else {
            if (workers.isEmpty()) {
                resetDraining();
                return DECIDE;
            }
            citizen = workers.get(0);
        }
        citizenToGatherFrom = citizen.getCitizenData();
        progressTicks = 0;
        return getState();
    }
    if (!citizenToGatherFrom.getEntity().isPresent()) {
        citizenToGatherFrom = null;
        return getState();
    }
    if (progressTicks == 0) {
        // If worker is too far away wait.
        if (BlockPosUtil.getDistance2D(citizenToGatherFrom.getEntity().get().blockPosition(), worker.blockPosition()) > MIN_DISTANCE_TO_DRAIN) {
            if (!job.incrementWaitingTicks()) {
                resetDraining();
                return DECIDE;
            }
            return getState();
        }
    }
    progressTicks++;
    if (progressTicks < MAX_PROGRESS_TICKS) {
        final Vector3d start = worker.position().add(0, 2, 0);
        final Vector3d goal = citizenToGatherFrom.getEntity().get().position().add(0, 2, 0);
        Network.getNetwork().sendToTrackingEntity(new StreamParticleEffectMessage(start, goal, ParticleTypes.ENCHANT, progressTicks % MAX_PROGRESS_TICKS, MAX_PROGRESS_TICKS), worker);
        Network.getNetwork().sendToTrackingEntity(new CircleParticleEffectMessage(start, ParticleTypes.HAPPY_VILLAGER, progressTicks), worker);
        WorkerUtil.faceBlock(new BlockPos(goal), worker);
        if (worker.getRandom().nextBoolean()) {
            worker.swing(Hand.MAIN_HAND);
        } else {
            worker.swing(Hand.OFF_HAND);
        }
        return getState();
    }
    final int bookSlot = InventoryUtils.findFirstSlotInItemHandlerWith(worker.getInventoryCitizen(), Items.BOOK);
    if (bookSlot != -1) {
        final int size = citizenToGatherFrom.getInventory().getSlots();
        final int attempts = (int) (getSecondarySkillLevel() / 5.0);
        for (int i = 0; i < attempts; i++) {
            int randomSlot = worker.getRandom().nextInt(size);
            final ItemStack stack = citizenToGatherFrom.getInventory().getStackInSlot(randomSlot);
            if (!stack.isEmpty() && stack.isEnchantable()) {
                EnchantmentHelper.enchantItem(worker.getRandom(), stack, getSecondarySkillLevel() > 50 ? 2 : 1, false);
                break;
            }
        }
        worker.getInventoryCitizen().extractItem(bookSlot, 1, false);
        worker.getCitizenData().getCitizenSkillHandler().incrementLevel(Skill.Mana, 1);
        worker.getCitizenExperienceHandler().addExperience(XP_PER_DRAIN);
        worker.getCitizenData().markDirty();
    }
    resetDraining();
    return IDLE;
}
Also used : StreamParticleEffectMessage(com.minecolonies.coremod.network.messages.client.StreamParticleEffectMessage) EnchanterStationsModule(com.minecolonies.coremod.colony.buildings.modules.EnchanterStationsModule) AbstractEntityCitizen(com.minecolonies.api.entity.citizen.AbstractEntityCitizen) ArrayList(java.util.ArrayList) Vector3d(net.minecraft.util.math.vector.Vector3d) CircleParticleEffectMessage(com.minecolonies.coremod.network.messages.client.CircleParticleEffectMessage) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack) AbstractBuilding(com.minecolonies.coremod.colony.buildings.AbstractBuilding)

Aggregations

AbstractEntityCitizen (com.minecolonies.api.entity.citizen.AbstractEntityCitizen)73 BlockPos (net.minecraft.util.math.BlockPos)33 ICitizenData (com.minecolonies.api.colony.ICitizenData)32 PlayerEntity (net.minecraft.entity.player.PlayerEntity)22 ItemStack (net.minecraft.item.ItemStack)22 NotNull (org.jetbrains.annotations.NotNull)20 IColony (com.minecolonies.api.colony.IColony)17 AttributeModifier (net.minecraft.entity.ai.attributes.AttributeModifier)14 TranslationTextComponent (net.minecraft.util.text.TranslationTextComponent)14 IBuilding (com.minecolonies.api.colony.buildings.IBuilding)12 Entity (net.minecraft.entity.Entity)12 java.util (java.util)10 Collectors (java.util.stream.Collectors)10 CompoundNBT (net.minecraft.nbt.CompoundNBT)10 IItemHandler (net.minecraftforge.items.IItemHandler)10 com.minecolonies.api.util (com.minecolonies.api.util)9 EntityCitizen (com.minecolonies.coremod.entity.citizen.EntityCitizen)8 ArrayList (java.util.ArrayList)8 ServerPlayerEntity (net.minecraft.entity.player.ServerPlayerEntity)8 World (net.minecraft.world.World)8