Improve rolling machine item balancing, it will no longer move items around when there isnt a better distribution

This commit is contained in:
modmuss50 2019-02-01 14:46:18 +00:00
parent 72a09d2368
commit 9301cfbc90

View file

@ -48,9 +48,8 @@ import techreborn.init.ModBlocks;
import techreborn.lib.ModInfo; import techreborn.lib.ModInfo;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.util.ArrayList; import java.util.*;
import java.util.List; import java.util.stream.Collectors;
import java.util.Optional;
//TODO add tick and power bars. //TODO add tick and power bars.
@ -212,6 +211,41 @@ public class TileRollingMachine extends TilePowerAcceptor
} }
} }
int totalItems = possibleSlots.stream()
.mapToInt(value -> inventory.getStackInSlot(value).getCount()).sum();
int slots = possibleSlots.size();
//This makes an array of ints with the best possible slot distribution
int[] split = new int[slots];
int remainder = totalItems % slots;
Arrays.fill(split, totalItems / slots);
while (remainder > 0){
for (int i = 0; i < split.length; i++) {
if(remainder > 0){
split[i] +=1;
remainder --;
}
}
}
List<Integer> slotDistrubution = possibleSlots.stream()
.mapToInt(value -> inventory.getStackInSlot(value).getCount())
.boxed().collect(Collectors.toList());
boolean needsBalance = false;
for (int i = 0; i < split.length; i++) {
int required = split[i];
if(slotDistrubution.contains(required)){
//We need to remove the int, not at the int, this seems to work around that
slotDistrubution.remove(new Integer(required));
} else {
needsBalance = true;
}
}
if (!needsBalance) {
return Optional.empty();
}
//Slot, count //Slot, count
Pair<Integer, Integer> bestSlot = null; Pair<Integer, Integer> bestSlot = null;
for (Integer slot : possibleSlots) { for (Integer slot : possibleSlots) {