Search in sources :

Example 36 with IBuilding

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

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(), colony.getDimension());
        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 37 with IBuilding

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

the class ChunkDataHelper method claimChunksInRange.

/**
 * Claim a number of chunks in a certain range around a position. Prevents the initial chunkradius from beeing unclaimed, unless forced.
 *
 * @param colony the colony to claim for
 * @param add    if claim or unclaim.
 * @param range  the range.
 * @param center the center position to be claimed.
 * @param force  whether to ignore restrictions.
 */
public static void claimChunksInRange(final IColony colony, final boolean add, final int range, final BlockPos center, final boolean force) {
    final World world = colony.getWorld();
    final int colonyId = colony.getID();
    final RegistryKey<World> dimension = colony.getDimension();
    boolean areAllChunksAdded = true;
    final IChunkmanagerCapability chunkManager = world.getCapability(CHUNK_STORAGE_UPDATE_CAP, null).resolve().orElse(null);
    if (chunkManager == null) {
        Log.getLogger().error(UNABLE_TO_FIND_WORLD_CAP_TEXT, new Exception());
        return;
    }
    final int chunkColonyCenterX = colony.getCenter().getX() >> 4;
    final int chunkColonyCenterZ = colony.getCenter().getZ() >> 4;
    final BlockPos colonyCenterCompare = new BlockPos(colony.getCenter().getX(), 0, colony.getCenter().getZ());
    final int chunkX = center.getX() >> 4;
    final int chunkZ = center.getZ() >> 4;
    final int initialColonySize = getConfig().getServer().initialColonySize.get();
    final int maxColonySize = getConfig().getServer().maxColonySize.get();
    for (int i = chunkX - range; i <= chunkX + range; i++) {
        for (int j = chunkZ - range; j <= chunkZ + range; j++) {
            // TODO: move out from for loops
            if (!force && !add && (Math.abs(chunkColonyCenterX - i) <= initialColonySize + 1 && Math.abs(chunkColonyCenterZ - j) <= initialColonySize + 1)) {
                Log.getLogger().debug("Unclaim of initial chunk prevented");
                continue;
            }
            final BlockPos pos = new BlockPos(i * BLOCKS_PER_CHUNK, 0, j * BLOCKS_PER_CHUNK);
            if (!force && maxColonySize != 0 && pos.distSqr(colonyCenterCompare) > Math.pow(maxColonySize * BLOCKS_PER_CHUNK, 2)) {
                Log.getLogger().debug("Tried to claim chunk at pos X:" + pos.getX() + " Z:" + pos.getZ() + " too far away from the colony:" + colony.getID() + " center:" + colony.getCenter() + " max is config workingRangeTownHall ^2");
                continue;
            }
            if (loadChunkAndAddData(world, pos, add, colonyId, center, chunkManager)) {
                continue;
            }
            areAllChunksAdded = false;
            @NotNull final ChunkLoadStorage newStorage = new ChunkLoadStorage(colonyId, ChunkPos.asLong(i, j), dimension.location(), center);
            chunkManager.addChunkStorage(i, j, newStorage);
        }
    }
    if (areAllChunksAdded && add && range > 0) {
        final IBuilding building = colony.getBuildingManager().getBuilding(center);
        sendPlayersMessage(colony.getImportantMessageEntityPlayers(), COLONY_SIZE_CHANGE, range, building.getSchematicName());
    }
}
Also used : IBuilding(com.minecolonies.api.colony.buildings.IBuilding) IChunkmanagerCapability(com.minecolonies.api.colony.IChunkmanagerCapability) BlockPos(net.minecraft.util.math.BlockPos) ChunkLoadStorage(com.minecolonies.api.util.ChunkLoadStorage) World(net.minecraft.world.World) NotNull(org.jetbrains.annotations.NotNull)

Example 38 with IBuilding

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

the class RecipeStorage method canFullFillRecipe.

@Override
public boolean canFullFillRecipe(final int qty, final Map<ItemStorage, Integer> existingRequirements, @NotNull final List<IItemHandler> citizen, @NotNull final IBuilding building) {
    final List<ItemStorage> items = getCleanedInput();
    for (final ItemStorage storage : items) {
        final ItemStack stack = storage.getItemStack();
        final int availableCount = InventoryUtils.getItemCountInItemHandlers(citizen, itemStack -> !ItemStackUtils.isEmpty(itemStack) && ItemStackUtils.compareItemStacksIgnoreStackSize(itemStack, stack, false, !storage.ignoreNBT())) + InventoryUtils.getCountFromBuilding(building, storage);
        ;
        if (!canFulfillItemStorage(qty, existingRequirements, availableCount, storage)) {
            return false;
        }
    }
    return true;
}
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) ItemStack(net.minecraft.item.ItemStack)

Example 39 with IBuilding

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

the class AbstractJobGuard method initEntityValues.

@Override
public void initEntityValues(AbstractEntityCitizen citizen) {
    super.initEntityValues(citizen);
    final IBuilding workBuilding = citizen.getCitizenData().getWorkBuilding();
    if (workBuilding instanceof AbstractBuildingGuards) {
        AttributeModifierUtils.addHealthModifier(citizen, new AttributeModifier(GUARD_HEALTH_MOD_BUILDING_NAME, ((AbstractBuildingGuards) workBuilding).getBonusHealth(), AttributeModifier.Operation.ADDITION));
        AttributeModifierUtils.addHealthModifier(citizen, new AttributeModifier(GUARD_HEALTH_MOD_CONFIG_NAME, MineColonies.getConfig().getServer().guardHealthMult.get() - 1.0, AttributeModifier.Operation.MULTIPLY_TOTAL));
    }
}
Also used : AbstractBuildingGuards(com.minecolonies.coremod.colony.buildings.AbstractBuildingGuards) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) AttributeModifier(net.minecraft.entity.ai.attributes.AttributeModifier)

Example 40 with IBuilding

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

the class VisitorCitizen method hurt.

@Override
public boolean hurt(@NotNull final DamageSource damageSource, final float damage) {
    if (!(damageSource.getEntity() instanceof EntityCitizen) && super.hurt(damageSource, damage)) {
        if (damageSource.getEntity() instanceof LivingEntity && damage > 1.01f) {
            final IBuilding home = getCitizenData().getHomeBuilding();
            if (home.hasModule(TavernBuildingModule.class)) {
                final TavernBuildingModule module = home.getFirstModuleOccurance(TavernBuildingModule.class);
                for (final Integer id : module.getExternalCitizens()) {
                    ICitizenData data = citizenColonyHandler.getColony().getVisitorManager().getCivilian(id);
                    if (data != null && data.getEntity().isPresent() && data.getEntity().get().getLastHurtByMob() == null) {
                        data.getEntity().get().setLastHurtByMob((LivingEntity) damageSource.getEntity());
                    }
                }
            }
            final Entity sourceEntity = damageSource.getEntity();
            if (sourceEntity instanceof PlayerEntity) {
                if (sourceEntity instanceof ServerPlayerEntity) {
                    return damage <= 1 || getCitizenColonyHandler().getColony().getPermissions().hasPermission((PlayerEntity) sourceEntity, Action.HURT_VISITOR);
                } else {
                    final IColonyView colonyView = IColonyManager.getInstance().getColonyView(getCitizenColonyHandler().getColonyId(), level.dimension());
                    return damage <= 1 || colonyView == null || colonyView.getPermissions().hasPermission((PlayerEntity) sourceEntity, Action.HURT_VISITOR);
                }
            }
        }
        return true;
    }
    return false;
}
Also used : PlayerEntity(net.minecraft.entity.player.PlayerEntity) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) AbstractEntityCitizen(com.minecolonies.api.entity.citizen.AbstractEntityCitizen) TavernBuildingModule(com.minecolonies.coremod.colony.buildings.modules.TavernBuildingModule) PlayerEntity(net.minecraft.entity.player.PlayerEntity) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity)

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