Search in sources :

Example 11 with IBuilding

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

the class BuildingManager method cleanUpBuildings.

@Override
public void cleanUpBuildings(@NotNull final IColony colony) {
    @Nullable final List<IBuilding> removedBuildings = new ArrayList<>();
    // Need this list, we may enter here while we add a building in the real world.
    final List<IBuilding> tempBuildings = new ArrayList<>(buildings.values());
    for (@NotNull final IBuilding building : tempBuildings) {
        final BlockPos loc = building.getPosition();
        if (WorldUtil.isBlockLoaded(colony.getWorld(), loc) && !building.isMatchingBlock(colony.getWorld().getBlockState(loc).getBlock())) {
            // Sanity cleanup
            removedBuildings.add(building);
        }
    }
    @NotNull final ArrayList<BlockPos> tempFields = new ArrayList<>(fields);
    for (@NotNull final BlockPos pos : tempFields) {
        if (WorldUtil.isBlockLoaded(colony.getWorld(), pos)) {
            if (colony.getWorld().getBlockEntity(pos) instanceof ScarecrowTileEntity) {
                final ScarecrowTileEntity scarecrow = (ScarecrowTileEntity) colony.getWorld().getBlockEntity(pos);
                if (scarecrow == null) {
                    removeField(pos);
                }
            } else {
                removeField(pos);
            }
        }
    }
    if (!removedBuildings.isEmpty() && removedBuildings.size() >= buildings.values().size()) {
        Log.getLogger().warn("Colony:" + colony.getID() + " is removing all buildings at once. Did you just load a backup? If not there is a chance that colony data got corrupted and you want to restore a backup.");
    }
    removedBuildings.forEach(IBuilding::destroy);
}
Also used : IBuilding(com.minecolonies.api.colony.buildings.IBuilding) BlockPos(net.minecraft.util.math.BlockPos) ScarecrowTileEntity(com.minecolonies.coremod.tileentities.ScarecrowTileEntity) AbstractScarecrowTileEntity(com.minecolonies.api.tileentities.AbstractScarecrowTileEntity) NotNull(org.jetbrains.annotations.NotNull) Nullable(org.jetbrains.annotations.Nullable)

Example 12 with IBuilding

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

the class BuildingManager method addNewBuilding.

@Override
public IBuilding addNewBuilding(@NotNull final AbstractTileEntityColonyBuilding tileEntity, final World world) {
    tileEntity.setColony(colony);
    if (!buildings.containsKey(tileEntity.getPosition())) {
        @Nullable final IBuilding building = IBuildingDataManager.getInstance().createFrom(colony, tileEntity);
        if (building != null) {
            addBuilding(building);
            tileEntity.setBuilding(building);
            building.upgradeBuildingLevelToSchematicData();
            Log.getLogger().info(String.format("Colony %d - new AbstractBuilding for %s at %s", colony.getID(), tileEntity.getBlockState().getClass(), tileEntity.getPosition()));
            building.setIsMirrored(tileEntity.isMirrored());
            if (!tileEntity.getStyle().isEmpty()) {
                building.setStyle(tileEntity.getStyle());
            } else {
                building.setStyle(colony.getStyle());
            }
            if (world != null && !(building instanceof IRSComponent)) {
                building.onPlacement();
                ConstructionTapeHelper.placeConstructionTape(building.getCorners(), world);
            }
            colony.getRequestManager().onProviderAddedToColony(building);
            setMaxChunk(building);
        } else {
            Log.getLogger().error(String.format("Colony %d unable to create AbstractBuilding for %s at %s", colony.getID(), tileEntity.getBlockState().getClass(), tileEntity.getPosition()), new Exception());
        }
        colony.getCitizenManager().calculateMaxCitizens();
        colony.getPackageManager().updateSubscribers();
        return building;
    }
    return null;
}
Also used : IBuilding(com.minecolonies.api.colony.buildings.IBuilding) IRSComponent(com.minecolonies.api.colony.buildings.IRSComponent) Nullable(org.jetbrains.annotations.Nullable)

Example 13 with IBuilding

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

the class RaidManager method getRandomBuilding.

@Override
@NotNull
public BlockPos getRandomBuilding() {
    buildingPosUsage++;
    if (buildingPosUsage > 3 || lastBuilding == null) {
        buildingPosUsage = 0;
        final Collection<IBuilding> buildingList = colony.getBuildingManager().getBuildings().values();
        final Object[] buildingArray = buildingList.toArray();
        if (buildingArray.length != 0) {
            final int rand = colony.getWorld().random.nextInt(buildingArray.length);
            final IBuilding building = (IBuilding) buildingArray[rand];
            lastBuilding = building.getPosition();
        } else {
            lastBuilding = colony.getCenter();
        }
    }
    return lastBuilding;
}
Also used : IBuilding(com.minecolonies.api.colony.buildings.IBuilding) NotNull(org.jetbrains.annotations.NotNull)

Example 14 with IBuilding

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

the class RaidManager method findSpawnPointInDirections.

/**
 * Finds a spawnpoint randomly in a circular shape around the center Advances
 *
 * @param start      the center of the area to search for a spawn point
 * @param advancePos The position we advance towards
 * @return the calculated position
 */
private BlockPos findSpawnPointInDirections(final BlockPos start, final BlockPos advancePos) {
    BlockPos spawnPos = new BlockPos(start);
    BlockPos tempPos = new BlockPos(spawnPos);
    final Collection<IBuilding> buildings = colony.getBuildingManager().getBuildings().values();
    final int xDiff = start.getX() - advancePos.getX();
    final int zDiff = start.getZ() - advancePos.getZ();
    Vector3d rates = new Vector3d(1, 1, 1);
    if (xDiff > zDiff) {
        rates = new Vector3d((xDiff) / ((double) zDiff), 1, start.getZ() < advancePos.getZ() ? 1 : -1);
    } else {
        rates = new Vector3d(start.getX() < advancePos.getX() ? 1 : -1, 1, (zDiff) / ((double) xDiff));
    }
    int validChunkCount = 0;
    for (int i = 0; i < 10; i++) {
        if (WorldUtil.isEntityBlockLoaded(colony.getWorld(), tempPos)) {
            tempPos = tempPos.offset(16 * rates.x, 0, 16 * rates.z);
            if (WorldUtil.isEntityBlockLoaded(colony.getWorld(), tempPos)) {
                if (isValidSpawnPoint(buildings, tempPos)) {
                    spawnPos = tempPos;
                    validChunkCount++;
                    if (validChunkCount > 5) {
                        return spawnPos;
                    }
                }
            } else {
                break;
            }
        } else {
            break;
        }
    }
    if (!spawnPos.equals(start)) {
        return spawnPos;
    }
    return null;
}
Also used : IBuilding(com.minecolonies.api.colony.buildings.IBuilding) Vector3d(net.minecraft.util.math.vector.Vector3d) BlockPos(net.minecraft.util.math.BlockPos)

Example 15 with IBuilding

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

the class AbstractWarehouseRequestResolver method getWareHousesInColony.

/**
 * Use to get the ordered list of all warehouses.
 * @param colony the colony in question.
 * @return the ordered list.
 */
protected static List<TileEntityWareHouse> getWareHousesInColony(final Colony colony, final BlockPos requesterPosition) {
    final List<TileEntityWareHouse> wareHouses = new ArrayList<>();
    for (final IBuilding building : colony.getBuildingManager().getBuildings().values()) {
        if (building instanceof BuildingWareHouse && building.getTileEntity() != null) {
            wareHouses.add((TileEntityWareHouse) building.getTileEntity());
        }
    }
    wareHouses.sort((w1, w2) -> {
        final double dist1 = w1.getPosition().distSqr(requesterPosition);
        final double dist2 = w2.getPosition().distSqr(requesterPosition);
        return Double.compare(dist1, dist2);
    });
    return wareHouses;
}
Also used : IBuilding(com.minecolonies.api.colony.buildings.IBuilding) TileEntityWareHouse(com.minecolonies.coremod.tileentities.TileEntityWareHouse) BuildingWareHouse(com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingWareHouse)

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