Search in sources :

Example 56 with IBuilding

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

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

the class CitizenSkillHandler method addXpToSkill.

@Override
public void addXpToSkill(final Skill skill, final double xp, final ICitizenData data) {
    final Tuple<Integer, Double> tuple = skillMap.getOrDefault(skill, new Tuple<>(0, 0.0D));
    int level = tuple.getA();
    final double currentXp = tuple.getB();
    final IBuilding home = data.getHomeBuilding();
    final double citizenHutLevel = home == null ? 0 : home.getBuildingLevel();
    final double citizenHutMaxLevel = home == null ? MAX_BUILDING_LEVEL : home.getMaxBuildingLevel();
    if (((citizenHutLevel < citizenHutMaxLevel || citizenHutMaxLevel < MAX_BUILDING_LEVEL) && (citizenHutLevel + 1) * 10 <= level) || level >= MAX_CITIZEN_LEVEL) {
        return;
    }
    double xpToLevelUp = Math.min(Double.MAX_VALUE, currentXp + xp);
    while (xpToLevelUp > 0) {
        final double nextLevel = ExperienceUtils.getXPNeededForNextLevel(level);
        if (nextLevel > xpToLevelUp) {
            skillMap.put(skill, new Tuple<>(Math.min(MAX_CITIZEN_LEVEL, level), xpToLevelUp));
            xpToLevelUp = 0;
        } else {
            xpToLevelUp = xpToLevelUp - nextLevel;
            level++;
        }
    }
    if (level > tuple.getA()) {
        levelUp(data);
        data.markDirty();
    }
}
Also used : IBuilding(com.minecolonies.api.colony.buildings.IBuilding)

Example 58 with IBuilding

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

the class CitizenSleepHandler method notifyCitizenHandlersOfWakeUp.

private void notifyCitizenHandlersOfWakeUp() {
    if (citizen.getCitizenColonyHandler().getWorkBuilding() != null) {
        citizen.getCitizenStatusHandler().setLatestStatus(new TranslationTextComponent("com.minecolonies.coremod.status.working"));
        citizen.getCitizenColonyHandler().getWorkBuilding().onWakeUp();
    }
    if (citizen.getCitizenJobHandler().getColonyJob() != null) {
        citizen.getCitizenJobHandler().getColonyJob().onWakeUp();
    }
    final IBuilding homeBuilding = citizen.getCitizenColonyHandler().getHomeBuilding();
    if (homeBuilding != null) {
        homeBuilding.onWakeUp();
    }
}
Also used : IBuilding(com.minecolonies.api.colony.buildings.IBuilding) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent)

Example 59 with IBuilding

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

the class EntityMercenaryAI method patrol.

/**
 * Patrols the buildings and random points. Attempts to steal from buildings.
 *
 * @return true
 */
private boolean patrol() {
    if (currentPatrolPos == null || entity.getProxy().walkToBlock(currentPatrolPos, 3, true)) {
        if (currentPatrolPos != null && movingToBuilding) {
            // Attempt to steal!
            final IBuilding building = entity.getColony().getBuildingManager().getBuilding(currentPatrolPos);
            if (building != null) {
                final List<IItemHandler> handlers = new ArrayList<>(InventoryUtils.getItemHandlersFromProvider(building.getTileEntity()));
                final IItemHandler handler = handlers.get(rand.nextInt(handlers.size()));
                final ItemStack stack = handler.extractItem(rand.nextInt(handler.getSlots()), 5, false);
                if (!ItemStackUtils.isEmpty(stack)) {
                    entity.swing(Hand.OFF_HAND);
                    MessageUtils.format(MESSAGE_INFO_COLONY_MERCENARY_STEAL_BUILDING, stack.getHoverName().getString()).sendTo(entity.getColony()).forAllPlayers();
                }
            }
        }
        if (rand.nextInt(4) == 0 && !patrolPoints.isEmpty()) {
            movingToBuilding = true;
            currentPatrolPos = patrolPoints.get(rand.nextInt(patrolPoints.size()));
        } else {
            movingToBuilding = false;
            currentPatrolPos = BlockPosUtil.getRandomPosition(entity.getCommandSenderWorld(), entity.blockPosition(), entity.blockPosition(), 10, 27);
        }
    }
    if (entity.blockPosition().equals(lastWorkerPos)) {
        stuckTimer++;
    } else {
        stuckTimer = 0;
    }
    if (stuckTimer > MAX_STUCK_TIME) {
        stuckTimer = 0;
        currentPatrolPos = null;
        entity.getNavigation().stop();
    }
    lastWorkerPos = entity.blockPosition();
    return true;
}
Also used : IItemHandler(net.minecraftforge.items.IItemHandler) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) ArrayList(java.util.ArrayList) ItemStack(net.minecraft.item.ItemStack)

Example 60 with IBuilding

use of com.minecolonies.api.colony.buildings.IBuilding 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 ActionResultType handleItemUsage(final World worldIn, final BlockPos pos, final CompoundNBT compound, final PlayerEntity playerIn) {
    if (!compound.getAllKeys().contains(TAG_ID)) {
        return ActionResultType.FAIL;
    }
    final IColony colony = IColonyManager.getInstance().getColonyByWorld(compound.getInt(TAG_ID), worldIn);
    if (colony == null) {
        return ActionResultType.FAIL;
    }
    final BlockPos guardTower = BlockPosUtil.read(compound, TAG_POS);
    final IBuilding hut = colony.getBuildingManager().getBuilding(guardTower);
    if (!(hut instanceof AbstractBuildingGuards)) {
        return ActionResultType.FAIL;
    }
    final IGuardBuilding tower = (IGuardBuilding) hut;
    if (BlockPosUtil.getDistance2D(pos, guardTower) > tower.getPatrolDistance()) {
        MessageUtils.format(TOOL_GUARD_SCEPTER_TOWER_TOO_FAR).sendTo(playerIn);
        return ActionResultType.FAIL;
    }
    if (hut.getSetting(AbstractBuildingGuards.GUARD_TASK).getValue().equals(GuardTaskSetting.GUARD)) {
        MessageUtils.format(TOOL_GUARD_SCEPTER_ADD_GUARD_TARGET, pos).sendTo(playerIn);
        tower.setGuardPos(pos);
        playerIn.inventory.removeItemNoUpdate(playerIn.inventory.selected);
    } else {
        if (!compound.getAllKeys().contains(TAG_LAST_POS)) {
            tower.resetPatrolTargets();
        }
        tower.addPatrolTargets(pos);
        MessageUtils.format(TOOL_GUARD_SCEPTER_ADD_PATROL_TARGET, pos).sendTo(playerIn);
    }
    BlockPosUtil.write(compound, TAG_LAST_POS, pos);
    return ActionResultType.SUCCESS;
}
Also used : IGuardBuilding(com.minecolonies.api.colony.buildings.IGuardBuilding) AbstractBuildingGuards(com.minecolonies.coremod.colony.buildings.AbstractBuildingGuards) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) IColony(com.minecolonies.api.colony.IColony) BlockPos(net.minecraft.util.math.BlockPos) 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