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);
}
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;
}
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;
}
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;
}
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;
}
Aggregations