// Create rules
Iterator<InvTweaksItemTreeItem> it = itemOrder.iterator();
while (it.hasNext()) {
InvTweaksItemTreeItem item = it.next();
// Adapt rule dimensions to fit the amount
int thisSpaceWidth = spaceWidth,
thisSpaceHeight = spaceHeight;
while (stats.get(item) > thisSpaceHeight*thisSpaceWidth) {
if (horizontal) {
if (column + thisSpaceWidth < maxColumn) {
thisSpaceWidth = maxColumn - column + 1;
}
else if (row + thisSpaceHeight < maxRow) {
thisSpaceHeight++;
}
else {
break;
}
}
else {
if (row + thisSpaceHeight < maxRow) {
thisSpaceHeight = maxRow - row + 1;
}
else if (column + thisSpaceWidth < maxColumn) {
thisSpaceWidth++;
}
else {
break;
}
}
}
// Adjust line/column ends to fill empty space
if (horizontal && (column + thisSpaceWidth == maxColumn)) {
thisSpaceWidth++;
}
else if (!horizontal && row + thisSpaceHeight == maxRow) {
thisSpaceHeight++;
}
// Create rule
String constraint = row + "" + column + "-"
+ (char)(row - 1 + thisSpaceHeight)
+ (char)(column - 1 + thisSpaceWidth);
if (!horizontal) {
constraint += 'v';
}
rules.add(new InvTweaksConfigSortingRule(tree, constraint, item.getName(), size, rowSize));
// Check if ther's still room for more rules
availableSlots -= thisSpaceHeight*thisSpaceWidth;
remainingStacks -= stats.get(item);
if (availableSlots >= remainingStacks) {