boolean refillBeforeBreak = config.getProperty(InvTweaksConfig.PROP_AUTO_REFILL_BEFORE_BREAK)
.equals(InvTweaksConfig.VALUE_TRUE);
List<InvTweaksConfigSortingRule> matchingRules = new ArrayList<InvTweaksConfigSortingRule>();
List<InvTweaksConfigSortingRule> rules = config.getRules();
InvTweaksItemTree tree = config.getTree();
// Check that the item is in the tree
if (!tree.isItemUnknown(wantedId, wantedDamage)) {
//// Search replacement
List<InvTweaksItemTreeItem> items = tree.getItems(wantedId, wantedDamage);
// Find rules that match the slot
for (InvTweaksItemTreeItem item : items) {
// Since we search a matching item using rules,
// create a fake one that matches the exact item first
matchingRules.add(new InvTweaksConfigSortingRule(
tree, "D"+(slot-27), item.getName(),
InvTweaksConst.INVENTORY_SIZE, InvTweaksConst.INVENTORY_ROW_SIZE));
}
for (InvTweaksConfigSortingRule rule : rules) {
if (rule.getType() == InvTweaksConfigSortingRuleType.SLOT
|| rule.getType() == InvTweaksConfigSortingRuleType.COLUMN) {
for (int preferredSlot : rule.getPreferredSlots()) {
if (slot == preferredSlot) {
matchingRules.add(rule);
break;
}
}
}
}
// Look only for a matching stack
// First, look for the same item,
// else one that matches the slot's rules
for (InvTweaksConfigSortingRule rule : matchingRules) {
for (int i = 0; i < InvTweaksConst.INVENTORY_SIZE; i++) {
candidateStack = container.getItemStack(i);
if (candidateStack != null) {
List<InvTweaksItemTreeItem> candidateItems = tree.getItems(
getItemID(candidateStack),
getItemDamage(candidateStack));
if (tree.matches(candidateItems, rule.getKeyword())) {
// Choose tool of highest damage value
if (getMaxStackSize(candidateStack) == 1) {
if ((replacementStack == null || getItemDamage(candidateStack) > getItemDamage(replacementStack)) &&
(!refillBeforeBreak || getMaxDamage(getItem(candidateStack)) - getItemDamage(candidateStack)
> InvTweaksConst.AUTO_REFILL_DAMAGE_TRESHOLD)) {