Search in sources :

Example 76 with IBuilding

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

the class AbstractBuilding method removeWorkOrder.

/**
 * Remove the work order for the building.
 * <p>
 * Remove either the upgrade or repair work order
 */
@Override
public void removeWorkOrder() {
    for (@NotNull final WorkOrderBuilding o : colony.getWorkManager().getWorkOrdersOfType(WorkOrderBuilding.class)) {
        if (o.getLocation().equals(getID())) {
            colony.getWorkManager().removeWorkOrder(o.getID());
            markDirty();
            final BlockPos buildingPos = o.getClaimedBy();
            final IBuilding building = colony.getBuildingManager().getBuilding(buildingPos);
            if (building != null) {
                for (final AbstractAssignedCitizenModule module : building.getModules(AbstractAssignedCitizenModule.class)) {
                    for (final ICitizenData citizen : module.getAssignedCitizen()) {
                        building.cancelAllRequestsOfCitizen(citizen);
                    }
                }
            }
            return;
        }
    }
}
Also used : IBuilding(com.minecolonies.api.colony.buildings.IBuilding) ICitizenData(com.minecolonies.api.colony.ICitizenData) BlockPos(net.minecraft.util.math.BlockPos) AbstractAssignedCitizenModule(com.minecolonies.coremod.colony.buildings.modules.AbstractAssignedCitizenModule) WorkOrderBuilding(com.minecolonies.coremod.colony.workorders.WorkOrderBuilding) NotNull(org.jetbrains.annotations.NotNull)

Example 77 with IBuilding

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

the class ColonyManager method deleteColony.

/**
 * Delete a colony and purge all buildings and citizens.
 *
 * @param iColony    the colony to destroy.
 * @param canDestroy if the building outlines should be destroyed as well.
 */
private void deleteColony(@Nullable final IColony iColony, final boolean canDestroy) {
    if (!(iColony instanceof Colony)) {
        return;
    }
    final Colony colony = (Colony) iColony;
    final int id = colony.getID();
    final World world = colony.getWorld();
    if (world == null) {
        Log.getLogger().warn("Deleting Colony " + id + " errored: World is Null");
        return;
    }
    try {
        ChunkDataHelper.claimColonyChunks(world, false, id, colony.getCenter());
        Log.getLogger().info("Removing citizens for " + id);
        for (final ICitizenData citizenData : new ArrayList<>(colony.getCitizenManager().getCitizens())) {
            Log.getLogger().info("Kill Citizen " + citizenData.getName());
            citizenData.getEntity().ifPresent(entityCitizen -> entityCitizen.die(CONSOLE_DAMAGE_SOURCE));
        }
        Log.getLogger().info("Removing buildings for " + id);
        for (final IBuilding building : new ArrayList<>(colony.getBuildingManager().getBuildings().values())) {
            try {
                final BlockPos location = building.getPosition();
                Log.getLogger().info("Delete Building at " + location);
                if (canDestroy) {
                    building.deconstruct();
                }
                building.destroy();
                if (world.getBlockState(location).getBlock() instanceof AbstractBlockHut) {
                    Log.getLogger().info("Found Block, deleting " + world.getBlockState(location).getBlock());
                    world.removeBlock(location, false);
                }
            } catch (final Exception ex) {
                Log.getLogger().warn("Something went wrong deleting a building while deleting the colony!", ex);
            }
        }
        try {
            MinecraftForge.EVENT_BUS.unregister(colony.getEventHandler());
        } catch (final NullPointerException e) {
            Log.getLogger().warn("Can't unregister the event handler twice");
        }
        Log.getLogger().info("Deleting colony: " + colony.getID());
        final IColonyManagerCapability cap = world.getCapability(COLONY_MANAGER_CAP, null).resolve().orElse(null);
        if (cap == null) {
            Log.getLogger().warn(MISSING_WORLD_CAP_MESSAGE);
            return;
        }
        cap.deleteColony(id);
        BackUpHelper.markColonyDeleted(colony.getID(), colony.getDimension());
        colony.getImportantMessageEntityPlayers().forEach(player -> Network.getNetwork().sendToPlayer(new ColonyViewRemoveMessage(colony.getID(), colony.getDimension()), (ServerPlayerEntity) player));
        Log.getLogger().info("Successfully deleted colony: " + id);
    } catch (final RuntimeException e) {
        Log.getLogger().warn("Deleting Colony " + id + " errored:", e);
    }
}
Also used : IBuilding(com.minecolonies.api.colony.buildings.IBuilding) ColonyViewRemoveMessage(com.minecolonies.coremod.network.messages.client.colony.ColonyViewRemoveMessage) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) World(net.minecraft.world.World) AbstractBlockHut(com.minecolonies.api.blocks.AbstractBlockHut) BlockPos(net.minecraft.util.math.BlockPos)

Example 78 with IBuilding

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

the class BuildingResourcesModule method updateAvailableResources.

/**
 * Update the available resources.
 * <p>
 * which are needed for the build and in the structureBuilder's chest or inventory
 */
private void updateAvailableResources() {
    final Set<ICitizenData> set = building.getAllAssignedCitizen();
    final ICitizenData data = set.isEmpty() ? null : set.iterator().next();
    if (data == null) {
        return;
    }
    data.getEntity().ifPresent(structureBuilder -> {
        final InventoryCitizen structureBuilderInventory = data.getInventory();
        if (structureBuilderInventory == null) {
            return;
        }
        for (@NotNull final Map.Entry<String, BuildingBuilderResource> entry : neededResources.entrySet()) {
            final BuildingBuilderResource resource = entry.getValue();
            resource.setAvailable(0);
            if (structureBuilderInventory != null) {
                resource.addAvailable(InventoryUtils.getItemCountInItemHandler(structureBuilderInventory, stack -> ItemStackUtils.compareItemStacksIgnoreStackSize(stack, resource.getItemStack(), true, true)));
            }
            if (building.getTileEntity() != null) {
                resource.addAvailable(InventoryUtils.getItemCountInItemHandler(building.getCapability(ITEM_HANDLER_CAPABILITY, null).orElseGet(null), stack -> ItemStackUtils.compareItemStacksIgnoreStackSize(stack, resource.getItemStack(), true, true)));
            }
            if (data.getJob() instanceof IJobWithExternalWorkStations) {
                for (final IBuilding station : ((IJobWithExternalWorkStations) data.getJob()).getWorkStations()) {
                    resource.addAvailable(InventoryUtils.getItemCountInItemHandler(station.getCapability(ITEM_HANDLER_CAPABILITY, null).orElseGet(null), stack -> ItemStackUtils.compareItemStacksIgnoreStackSize(stack, resource.getItemStack(), true, true)));
                }
            }
        }
    });
}
Also used : IWorkOrder(com.minecolonies.api.colony.workorders.IWorkOrder) AbstractBuildingModule(com.minecolonies.api.colony.buildings.modules.AbstractBuildingModule) IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) BuilderBucket(com.minecolonies.coremod.colony.buildings.utils.BuilderBucket) java.util(java.util) ItemStackUtils(com.minecolonies.api.util.ItemStackUtils) CompoundNBT(net.minecraft.nbt.CompoundNBT) ICitizenData(com.minecolonies.api.colony.ICitizenData) TypeToken(com.google.common.reflect.TypeToken) ItemStack(net.minecraft.item.ItemStack) BuildingBuilderResource(com.minecolonies.coremod.colony.buildings.utils.BuildingBuilderResource) ImmutableList(com.google.common.collect.ImmutableList) TAG_CURR_STAGE(com.minecolonies.api.util.constant.NbtTagConstants.TAG_CURR_STAGE) AbstractBuildingStructureBuilder(com.minecolonies.coremod.colony.buildings.AbstractBuildingStructureBuilder) StringTextComponent(net.minecraft.util.text.StringTextComponent) ITEM_HANDLER_CAPABILITY(net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) TAG_TOTAL_STAGES(com.minecolonies.api.util.constant.NbtTagConstants.TAG_TOTAL_STAGES) Stack(com.minecolonies.api.colony.requestsystem.requestable.Stack) BuildingStructureHandler(com.minecolonies.coremod.entity.ai.util.BuildingStructureHandler) IPersistentModule(com.minecolonies.api.colony.buildings.modules.IPersistentModule) Nullable(org.jetbrains.annotations.Nullable) IJobWithExternalWorkStations(com.minecolonies.api.colony.jobs.IJobWithExternalWorkStations) InventoryCitizen(com.minecolonies.api.inventory.InventoryCitizen) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) InventoryUtils(com.minecolonies.api.util.InventoryUtils) CapabilityItemHandler(net.minecraftforge.items.CapabilityItemHandler) AbstractJobStructure(com.minecolonies.coremod.colony.jobs.AbstractJobStructure) ItemStorage(com.minecolonies.api.crafting.ItemStorage) NotNull(org.jetbrains.annotations.NotNull) PacketBuffer(net.minecraft.network.PacketBuffer) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) InventoryCitizen(com.minecolonies.api.inventory.InventoryCitizen) ICitizenData(com.minecolonies.api.colony.ICitizenData) IJobWithExternalWorkStations(com.minecolonies.api.colony.jobs.IJobWithExternalWorkStations) BuildingBuilderResource(com.minecolonies.coremod.colony.buildings.utils.BuildingBuilderResource) NotNull(org.jetbrains.annotations.NotNull)

Example 79 with IBuilding

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

the class ItemScrollGuardHelp method useOn.

@Override
@NotNull
public ActionResultType useOn(ItemUseContext ctx) {
    final ActionResultType result = super.useOn(ctx);
    if (ctx.getLevel().isClientSide) {
        return result;
    }
    final TileEntity te = ctx.getLevel().getBlockEntity(ctx.getClickedPos());
    if (te instanceof TileEntityColonyBuilding && ctx.getPlayer() != null) {
        final IBuilding building = ((TileEntityColonyBuilding) te).getColony().getBuildingManager().getBuilding(ctx.getClickedPos());
        if (!(building instanceof AbstractBuildingGuards)) {
            MessageUtils.format(TOOL_GUARD_SCROLL_NO_GUARD_BUILDING).sendTo(ctx.getPlayer());
        }
    }
    return result;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) AbstractBuildingGuards(com.minecolonies.coremod.colony.buildings.AbstractBuildingGuards) ActionResultType(net.minecraft.util.ActionResultType) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) TileEntityColonyBuilding(com.minecolonies.api.tileentities.TileEntityColonyBuilding) NotNull(org.jetbrains.annotations.NotNull)

Example 80 with IBuilding

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

the class ItemScrollGuardHelp method onItemUseSuccess.

@Override
protected ItemStack onItemUseSuccess(final ItemStack itemStack, final World world, final ServerPlayerEntity player) {
    final IColony colony = getColony(itemStack);
    final BlockPos buildingPos = BlockPosUtil.read(itemStack.getTag(), TAG_BUILDING_POS);
    final IBuilding building = colony.getBuildingManager().getBuilding(buildingPos);
    if (!(building instanceof AbstractBuildingGuards)) {
        MessageUtils.format(TOOL_GUARD_SCROLL_NO_GUARD_BUILDING).sendTo(player);
        return itemStack;
    }
    itemStack.shrink(1);
    final List<ICitizenData> guards = new ArrayList<>(building.getAllAssignedCitizen());
    if (world.random.nextInt(10) == 0 || colony.getWorld() != world) {
        // Fail
        final LlamaEntity entity = EntityType.LLAMA.create(world);
        entity.setPos(player.getX(), player.getY(), player.getZ());
        world.addFreshEntity(entity);
        player.displayClientMessage(new TranslationTextComponent("minecolonies.scroll.failed" + (world.random.nextInt(FAIL_RESPONSES_TOTAL) + 1)).setStyle(Style.EMPTY.withColor(TextFormatting.GOLD)), true);
        SoundUtils.playSoundForPlayer(player, SoundEvents.EVOKER_CAST_SPELL, 0.5f, 1.0f);
        return itemStack;
    } else {
        for (final ICitizenData citizenData : guards) {
            final AbstractJobGuard job = citizenData.getJob(AbstractJobGuard.class);
            if (job != null && job.getWorkerAI() != null && !((AbstractEntityAIGuard) job.getWorkerAI()).hasTool()) {
                continue;
            }
            if (citizenData.getEntity().isPresent()) {
                if (citizenData.getEntity().get().getCitizenDiseaseHandler().isSick()) {
                    continue;
                }
                citizenData.getEntity().get().remove();
            }
            colony.getCitizenManager().spawnOrCreateCivilian(citizenData, world, player.blockPosition(), true);
            citizenData.setNextRespawnPosition(buildingPos);
            ((AbstractBuildingGuards) building).getSetting(AbstractBuildingGuards.GUARD_TASK).set(GuardTaskSetting.FOLLOW);
            ((AbstractBuildingGuards) building).setPlayerToFollow(player);
            final FollowModeSetting grouping = ((AbstractBuildingGuards) building).getSetting(AbstractBuildingGuards.FOLLOW_MODE);
            if (grouping.getValue().equals(FollowModeSetting.LOOSE)) {
                grouping.trigger();
            }
            citizenData.setSaturation(100);
            colony.getPackageManager().addCloseSubscriber(player);
            if (job != null && job.getWorkerAI() != null) {
                final long spawnTime = world.getGameTime() + TICKS_SECOND * 900;
                // Timed despawn
                job.getWorkerAI().registerTarget(new AIOneTimeEventTarget(() -> {
                    if (world.getGameTime() - spawnTime > 0) {
                        ((AbstractBuildingGuards) building).getSetting(AbstractBuildingGuards.GUARD_TASK).set(GuardTaskSetting.PATROL);
                        citizenData.getEntity().ifPresent(Entity::remove);
                        colony.getPackageManager().removeCloseSubscriber(player);
                        return true;
                    }
                    return false;
                }, AIWorkerState.DECIDE));
            }
        }
        SoundUtils.playSoundForPlayer(player, SoundEvents.UI_TOAST_CHALLENGE_COMPLETE, 0.3f, 1.0f);
    }
    return itemStack;
}
Also used : AbstractBuildingGuards(com.minecolonies.coremod.colony.buildings.AbstractBuildingGuards) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) ArrayList(java.util.ArrayList) AIOneTimeEventTarget(com.minecolonies.api.entity.ai.statemachine.AIOneTimeEventTarget) IColony(com.minecolonies.api.colony.IColony) ICitizenData(com.minecolonies.api.colony.ICitizenData) AbstractJobGuard(com.minecolonies.coremod.colony.jobs.AbstractJobGuard) LlamaEntity(net.minecraft.entity.passive.horse.LlamaEntity) AbstractEntityAIGuard(com.minecolonies.coremod.entity.ai.citizen.guard.AbstractEntityAIGuard) BlockPos(net.minecraft.util.math.BlockPos) FollowModeSetting(com.minecolonies.coremod.colony.buildings.modules.settings.FollowModeSetting)

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