Search in sources :

Example 16 with AbstractEntityCitizen

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

the class EventHandler method onEntityConverted.

/**
 * Gets called when a Hoglin, Pig, Piglin, Villager, or ZombieVillager gets converted to something else.
 *
 * @param event the event to handle.
 */
@SubscribeEvent
public static void onEntityConverted(@NotNull final LivingConversionEvent.Pre event) {
    LivingEntity entity = event.getEntityLiving();
    if (entity instanceof ZombieVillagerEntity && event.getOutcome() == EntityType.VILLAGER) {
        final World world = entity.getCommandSenderWorld();
        final IColony colony = IColonyManager.getInstance().getIColony(world, entity.blockPosition());
        if (colony != null && colony.hasBuilding("tavern", 1, false)) {
            event.setCanceled(true);
            if (ForgeEventFactory.canLivingConvert(entity, ModEntities.VISITOR, null)) {
                IVisitorData visitorData = (IVisitorData) colony.getVisitorManager().createAndRegisterCivilianData();
                BlockPos tavernPos = colony.getBuildingManager().getRandomBuilding(b -> !b.getModules(TavernBuildingModule.class).isEmpty());
                IBuilding tavern = colony.getBuildingManager().getBuilding(tavernPos);
                visitorData.setHomeBuilding(tavern);
                visitorData.setBedPos(tavernPos);
                tavern.getModules(TavernBuildingModule.class).forEach(mod -> mod.getExternalCitizens().add(visitorData.getId()));
                int recruitLevel = world.random.nextInt(10 * tavern.getBuildingLevel()) + 15;
                List<com.minecolonies.api.util.Tuple<Item, Integer>> recruitCosts = IColonyManager.getInstance().getCompatibilityManager().getRecruitmentCostsWeights();
                visitorData.getCitizenSkillHandler().init(recruitLevel);
                colony.getVisitorManager().spawnOrCreateCivilian(visitorData, world, entity.blockPosition(), false);
                colony.getEventDescriptionManager().addEventDescription(new VisitorSpawnedEvent(entity.blockPosition(), visitorData.getName()));
                if (visitorData.getEntity().isPresent()) {
                    AbstractEntityCitizen visitorEntity = visitorData.getEntity().get();
                    for (EquipmentSlotType slotType : EquipmentSlotType.values()) {
                        ItemStack itemstack = entity.getItemBySlot(slotType);
                        if (slotType.getType() == EquipmentSlotType.Group.ARMOR && !itemstack.isEmpty()) {
                            visitorEntity.setItemSlot(slotType, itemstack);
                        }
                    }
                }
                if (!entity.isSilent()) {
                    world.levelEvent((PlayerEntity) null, 1027, entity.blockPosition(), 0);
                }
                entity.remove();
                Tuple<Item, Integer> cost = recruitCosts.get(world.random.nextInt(recruitCosts.size()));
                visitorData.setRecruitCosts(new ItemStack(cost.getA(), (int) (recruitLevel * 3.0 / cost.getB())));
                visitorData.triggerInteraction(new RecruitmentInteraction(new TranslationTextComponent("com.minecolonies.coremod.gui.chat.recruitstorycured", visitorData.getName().split(" ")[0]), ChatPriority.IMPORTANT));
            }
        }
    }
}
Also used : VisitorSpawnedEvent(com.minecolonies.coremod.colony.colonyEvents.citizenEvents.VisitorSpawnedEvent) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) EquipmentSlotType(net.minecraft.inventory.EquipmentSlotType) AbstractEntityCitizen(com.minecolonies.api.entity.citizen.AbstractEntityCitizen) World(net.minecraft.world.World) ClientWorld(net.minecraft.client.world.ClientWorld) ServerWorld(net.minecraft.world.server.ServerWorld) EntryPoint(com.minecolonies.coremod.commands.EntryPoint) LivingEntity(net.minecraft.entity.LivingEntity) Item(net.minecraft.item.Item) BlockItem(net.minecraft.item.BlockItem) RecruitmentInteraction(com.minecolonies.coremod.colony.interactionhandling.RecruitmentInteraction) ZombieVillagerEntity(net.minecraft.entity.monster.ZombieVillagerEntity) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack) TavernBuildingModule(com.minecolonies.coremod.colony.buildings.modules.TavernBuildingModule) Tuple(com.minecolonies.api.util.Tuple) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 17 with AbstractEntityCitizen

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

the class MinecoloniesAdvancedPathNavigate method moveToTree.

@Override
public TreePathResult moveToTree(final BlockPos startRestriction, final BlockPos endRestriction, final double speed, final List<ItemStorage> excludedTrees, final int dyntreesize, final IColony colony) {
    @NotNull final BlockPos start = AbstractPathJob.prepareStart(ourEntity);
    final BlockPos buildingPos = ((AbstractEntityCitizen) mob).getCitizenColonyHandler().getWorkBuilding().getPosition();
    final BlockPos furthestRestriction = BlockPosUtil.getFurthestCorner(start, startRestriction, endRestriction);
    final PathJobFindTree job = new PathJobFindTree(CompatibilityUtils.getWorldFromEntity(mob), start, buildingPos, startRestriction, endRestriction, furthestRestriction, excludedTrees, dyntreesize, colony, ourEntity);
    return (TreePathResult) setPathJob(job, null, speed, true);
}
Also used : AbstractEntityCitizen(com.minecolonies.api.entity.citizen.AbstractEntityCitizen) BlockPos(net.minecraft.util.math.BlockPos) NotNull(org.jetbrains.annotations.NotNull)

Example 18 with AbstractEntityCitizen

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

the class CitizenSkillHandler method levelUp.

@Override
public void levelUp(final ICitizenData data) {
    // Show level-up particles
    if (data.getEntity().isPresent()) {
        final AbstractEntityCitizen citizen = data.getEntity().get();
        citizen.playSound(SoundEvents.PLAYER_LEVELUP, 1.0f, (float) SoundUtils.getRandomPitch(citizen.getRandom()));
        Network.getNetwork().sendToTrackingEntity(new VanillaParticleMessage(citizen.getX(), citizen.getY(), citizen.getZ(), ParticleTypes.HAPPY_VILLAGER), data.getEntity().get());
    }
    if (data.getJob() != null) {
        data.getJob().onLevelUp();
    }
}
Also used : VanillaParticleMessage(com.minecolonies.coremod.network.messages.client.VanillaParticleMessage) AbstractEntityCitizen(com.minecolonies.api.entity.citizen.AbstractEntityCitizen)

Example 19 with AbstractEntityCitizen

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

the class RecipeStorage method fullfillRecipeAndCopy.

/**
 * Check for space, remove items, and insert crafted items, returning a copy of the crafted items.
 *
 * @param context loot context
 * @param handlers the handlers to use
 * @return copy of the crafted items if successful, null on failure
 */
@Override
public List<ItemStack> fullfillRecipeAndCopy(final LootContext context, final List<IItemHandler> handlers, boolean doInsert) {
    if (!checkForFreeSpace(handlers) || !canFullFillRecipe(1, Collections.emptyMap(), handlers.toArray(new IItemHandler[0]))) {
        return null;
    }
    final AbstractEntityCitizen citizen = (AbstractEntityCitizen) context.getParamOrNull(LootParameters.THIS_ENTITY);
    for (final ItemStorage storage : getCleanedInput()) {
        final ItemStack stack = storage.getItemStack();
        int amountNeeded = storage.getAmount();
        if (amountNeeded == 0) {
            break;
        }
        for (final IItemHandler handler : handlers) {
            int slotOfStack = InventoryUtils.findFirstSlotInItemHandlerNotEmptyWith(handler, itemStack -> !ItemStackUtils.isEmpty(itemStack) && ItemStackUtils.compareItemStacksIgnoreStackSize(itemStack, stack, false, !storage.ignoreNBT()));
            while (slotOfStack != -1 && amountNeeded > 0) {
                if (citizen != null && ItemStackUtils.compareItemStackListIgnoreStackSize(tools, stack, false, !storage.ignoreNBT()) && ItemStackUtils.getDurability(handler.getStackInSlot(slotOfStack)) > 0) {
                    ItemStack toDamage = handler.extractItem(slotOfStack, 1, false);
                    if (!ItemStackUtils.isEmpty(toDamage)) {
                        // The 4 parameter inner call from forge is for adding a callback to alter the damage caused,
                        // but unlike its description does not actually damage the item(despite the same function name). So used to just calculate the damage.
                        toDamage.hurtAndBreak(toDamage.getItem().damageItem(stack, 1, citizen, item -> item.broadcastBreakEvent(Hand.MAIN_HAND)), citizen, item -> item.broadcastBreakEvent(Hand.MAIN_HAND));
                    }
                    if (!ItemStackUtils.isEmpty(toDamage)) {
                        handler.insertItem(slotOfStack, toDamage, false);
                    }
                    amountNeeded -= stack.getCount();
                } else {
                    final int count = ItemStackUtils.getSize(handler.getStackInSlot(slotOfStack));
                    final ItemStack extractedStack = handler.extractItem(slotOfStack, amountNeeded, false).copy();
                    // Deletes some items, but hey.
                    if (ItemStackUtils.isEmpty(extractedStack)) {
                        handler.insertItem(slotOfStack, extractedStack, false);
                        return null;
                    }
                    amountNeeded -= count;
                    if (amountNeeded > 0) {
                        slotOfStack = InventoryUtils.findFirstSlotInItemHandlerNotEmptyWith(handler, itemStack -> !ItemStackUtils.isEmpty(itemStack) && ItemStackUtils.compareItemStacksIgnoreStackSize(itemStack, stack, false, !storage.ignoreNBT()));
                    }
                }
            }
            // stop looping handlers if we have what we need
            if (amountNeeded <= 0) {
                break;
            }
        }
        if (amountNeeded > 0) {
            return null;
        }
    }
    return insertCraftedItems(handlers, getPrimaryOutput(), context, doInsert);
}
Also used : IItemHandler(net.minecraftforge.items.IItemHandler) java.util(java.util) LootTable(net.minecraft.loot.LootTable) ItemStackUtils(com.minecolonies.api.util.ItemStackUtils) Item(net.minecraft.item.Item) IToken(com.minecolonies.api.colony.requestsystem.token.IToken) RecipeTypeEntry(com.minecolonies.api.crafting.registry.RecipeTypeEntry) MinecoloniesAPIProxy(com.minecolonies.api.MinecoloniesAPIProxy) StandardFactoryController(com.minecolonies.api.colony.requestsystem.StandardFactoryController) ItemStack(net.minecraft.item.ItemStack) LootContext(net.minecraft.loot.LootContext) ImmutableList(com.google.common.collect.ImmutableList) Block(net.minecraft.block.Block) Hand(net.minecraft.util.Hand) LootParameterSet(net.minecraft.loot.LootParameterSet) Predicate(java.util.function.Predicate) ModItems(com.ldtteam.structurize.items.ModItems) LootParameters(net.minecraft.loot.LootParameters) IForgeRegistry(net.minecraftforge.registries.IForgeRegistry) Collectors(java.util.stream.Collectors) Blocks(net.minecraft.block.Blocks) AbstractEntityCitizen(com.minecolonies.api.entity.citizen.AbstractEntityCitizen) TypeConstants(com.minecolonies.api.util.constant.TypeConstants) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) InventoryUtils(com.minecolonies.api.util.InventoryUtils) ResourceLocation(net.minecraft.util.ResourceLocation) NotNull(org.jetbrains.annotations.NotNull) IItemHandler(net.minecraftforge.items.IItemHandler) AbstractEntityCitizen(com.minecolonies.api.entity.citizen.AbstractEntityCitizen) ItemStack(net.minecraft.item.ItemStack)

Example 20 with AbstractEntityCitizen

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

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)

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