Fix /condense functionality after 1.12. (#1298)

Prior to this commit `/condense` would return the first best item to condense an ItemStack into, with no consideration of better forms of condensation.

This commit adds a local variable `bestRecipes` which stores all the valid recipes and sorts through them for the best, then picks out the best. The best recipe is defined by a compare(o2, o1) where o1 and o2 are integers of the SimpleRecipe's input ItemStack amount.
This commit is contained in:
Ali Moghnieh 2017-06-22 22:21:42 +01:00
parent 2fe05b4ff4
commit 3831464665
No known key found for this signature in database
GPG key ID: F09D3A1BAF2E6D70

View file

@ -106,6 +106,7 @@ public class Commandcondense extends EssentialsCommand {
} }
final Iterator<Recipe> intr = ess.getServer().recipeIterator(); final Iterator<Recipe> intr = ess.getServer().recipeIterator();
List<SimpleRecipe> bestRecipes = new ArrayList<>();
while (intr.hasNext()) { while (intr.hasNext()) {
final Recipe recipe = intr.next(); final Recipe recipe = intr.next();
final Collection<ItemStack> recipeItems = getStackOnRecipeMatch(recipe, stack); final Collection<ItemStack> recipeItems = getStackOnRecipeMatch(recipe, stack);
@ -114,11 +115,17 @@ public class Commandcondense extends EssentialsCommand {
final ItemStack input = stack.clone(); final ItemStack input = stack.clone();
input.setAmount(recipeItems.size()); input.setAmount(recipeItems.size());
final SimpleRecipe newRecipe = new SimpleRecipe(recipe.getResult(), input); final SimpleRecipe newRecipe = new SimpleRecipe(recipe.getResult(), input);
condenseList.put(stack, newRecipe); bestRecipes.add(newRecipe);
return newRecipe;
} }
} }
if (!bestRecipes.isEmpty()) {
if (bestRecipes.size() > 1) {
Collections.sort(bestRecipes, SimpleRecipeComparator.INSTANCE);
}
SimpleRecipe recipe = bestRecipes.get(0);
condenseList.put(stack, recipe);
return recipe;
}
condenseList.put(stack, null); condenseList.put(stack, null);
return null; return null;
} }
@ -187,4 +194,13 @@ public class Commandcondense extends EssentialsCommand {
return Collections.emptyList(); return Collections.emptyList();
} }
} }
private static class SimpleRecipeComparator implements Comparator<SimpleRecipe> {
private static final SimpleRecipeComparator INSTANCE = new SimpleRecipeComparator();
@Override
public int compare(SimpleRecipe o1, SimpleRecipe o2) {
return Integer.compare(o2.getInput().getAmount(), o1.getInput().getAmount());
}
}
} }