Fix hacky fix of 2cf3c6

Only modify inventory array when it's changes will be applied (The cachedInventory is not a deep clone and should not be modified as I did expect because of Player#getInventory()#getContents()
This commit is contained in:
Christian Koop 2020-04-17 16:35:06 +02:00 committed by Brianna
parent 58fa0cbe61
commit e77d89173f

View File

@ -45,30 +45,8 @@ public class ModuleAutoCrafting extends Module {
if (hopper == null || (toCraft = getAutoCrafting(hopper)) == null || toCraft.getType() == Material.AIR) if (hopper == null || (toCraft = getAutoCrafting(hopper)) == null || toCraft.getType() == Material.AIR)
return; return;
// jam check: is this hopper gummed up?
if (crafterEjection) {
// TODO: Recode. Why specifically eject ingredients (allMaterials).
// And why not eject when we checked crafting is possible
final List<Material> allMaterials = getRecipes(toCraft).getAllMaterials();
if (Stream.of(hopperCache.cachedInventory)
.allMatch(item -> item != null && allMaterials.stream().anyMatch(mat -> mat == item.getType()))) {
// Crafter can't function if there's nowhere to put the output
// ¯\_()_/¯
// forcibly open the last slot
ItemStack last = hopperCache.cachedInventory[4];
hopperCache.setItem(4, null);
// and yeet into space!
hopper.getWorld().dropItemNaturally(hopper.getLocation(), last);
}
}
synchronized (hopperCache) { //TODO: Check if this is required synchronized (hopperCache) { //TODO: Check if this is required
ItemStack[] items = new ItemStack[hopperCache.cachedInventory.length]; ItemStack[] items = hopperCache.cachedInventory;
// TODO: Don't use that hacky workaround. Just don't modify it, if not meant to
for (int i = 0; i < hopperCache.cachedInventory.length; i++) {
items[i] = hopperCache.cachedInventory[i].clone();
}
for (SimpleRecipe recipe : getRecipes(toCraft).recipes) { for (SimpleRecipe recipe : getRecipes(toCraft).recipes) {
// key=indexForItemsArray, value=amountAfterCrafting // key=indexForItemsArray, value=amountAfterCrafting
@ -110,6 +88,29 @@ public class ModuleAutoCrafting extends Module {
// Not enough ingredients for this recipe // Not enough ingredients for this recipe
if (amount != 0) continue; if (amount != 0) continue;
boolean freeSlotAfterRemovingIngredients =
slotsToAlter.values().stream().anyMatch(iAmount -> iAmount <= 0) ||
Arrays.stream(items).anyMatch(Objects::isNull);
// jam check: is this hopper gummed up?
if (crafterEjection && !freeSlotAfterRemovingIngredients) {
// TODO: Recode. Why specifically eject ingredients (allMaterials).
// And why not eject when we checked crafting is possible
final List<Material> allMaterials = getRecipes(toCraft).getAllMaterials();
if (Stream.of(hopperCache.cachedInventory)
.allMatch(item -> item != null && allMaterials.stream().anyMatch(mat -> mat == item.getType()))) {
// Crafter can't function if there's nowhere to put the output
// ¯\_()_/¯
// forcibly open the last slot
ItemStack last = items[4];
items[4] = null;
freeSlotAfterRemovingIngredients = true;
// and yeet into space!
hopper.getWorld().dropItemNaturally(hopper.getLocation(), last);
}
}
if (freeSlotAfterRemovingIngredients) {
for (Map.Entry<Integer, Integer> entry : slotsToAlter.entrySet()) { for (Map.Entry<Integer, Integer> entry : slotsToAlter.entrySet()) {
if (entry.getValue() <= 0) { if (entry.getValue() <= 0) {
items[entry.getKey()] = null; items[entry.getKey()] = null;
@ -119,7 +120,6 @@ public class ModuleAutoCrafting extends Module {
} }
// Add the resulting item into the inventory - Just making sure there actually is enough space // Add the resulting item into the inventory - Just making sure there actually is enough space
boolean success = false;
for (int i = 0; i < items.length; i++) { for (int i = 0; i < items.length; i++) {
if (items[i] == null || if (items[i] == null ||
(items[i].isSimilar(recipe.result) (items[i].isSimilar(recipe.result)
@ -130,12 +130,10 @@ public class ModuleAutoCrafting extends Module {
items[i].setAmount(items[i].getAmount() + recipe.result.getAmount()); items[i].setAmount(items[i].getAmount() + recipe.result.getAmount());
} }
success = true;
break; break;
} }
} }
if (success) {
hopperCache.setContents(items); hopperCache.setContents(items);
} }
} }