From a902d66f4250fafbea3acf51a09ac1812d981e76 Mon Sep 17 00:00:00 2001 From: James Peters Date: Wed, 30 Dec 2020 17:18:54 +0000 Subject: [PATCH] Autocrafting Duplication Bug #27 Crafting now removes duplicate inventories from input array --- .../minecraft/chests/crafting/Crafting.java | 37 ++----------------- .../interfaces/VirtualCraftingHolder.java | 13 ++++--- 2 files changed, 11 insertions(+), 39 deletions(-) diff --git a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/crafting/Crafting.java b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/crafting/Crafting.java index 8eb6b9f..6a1244f 100644 --- a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/crafting/Crafting.java +++ b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/crafting/Crafting.java @@ -1,21 +1,16 @@ package com.jamesdpeters.minecraft.chests.crafting; import com.jamesdpeters.minecraft.chests.api.ApiSpecific; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; import org.bukkit.Bukkit; -import org.bukkit.Material; import org.bukkit.NamespacedKey; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.Recipe; -import org.bukkit.inventory.RecipeChoice; import org.bukkit.inventory.ShapedRecipe; import org.bukkit.inventory.ShapelessRecipe; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Optional; -import java.util.Set; - public class Crafting { private static List shapedRecipes; @@ -42,32 +37,6 @@ public class Crafting { return ApiSpecific.getNmsProvider().getCraftingProvider().craft(Bukkit.getWorlds().get(0), recipe); } - private static boolean matchesShapeless(List choice, List items) { - items = new ArrayList<>(items); - for (RecipeChoice c : choice) { - boolean match = false; - for (int i = 0; i < items.size(); i++) { - ItemStack item = items.get(i); - if (item == null || item.getType() == Material.AIR) - continue; - if (c.test(item)) { - match = true; - items.remove(i); - break; - } - } - if (!match) - return false; - } - Set remainingItems = new HashSet<>(items); - return (remainingItems.size() == 1 && (items.contains(new ItemStack(Material.AIR)) || items.contains(null))); - } - - private static boolean matchesShaped(ShapedRecipe shape, List items) { - UserShapedRecipe userShapedRecipe = new UserShapedRecipe(items); - return userShapedRecipe.matchesRecipe(shape); - } - public static Recipe getRecipeByKey(NamespacedKey key) { Optional recipe = shapelessRecipes.stream().filter(s -> s.getKey().equals(key)).findFirst(); if (recipe.isPresent()) return recipe.get(); diff --git a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/interfaces/VirtualCraftingHolder.java b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/interfaces/VirtualCraftingHolder.java index d0e0002..8e51076 100644 --- a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/interfaces/VirtualCraftingHolder.java +++ b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/interfaces/VirtualCraftingHolder.java @@ -8,6 +8,11 @@ import com.jamesdpeters.minecraft.chests.serialize.Config; import com.jamesdpeters.minecraft.chests.serialize.LocationInfo; import com.jamesdpeters.minecraft.chests.storage.autocraft.AutoCraftingStorage; import com.jamesdpeters.minecraft.chests.storage.chestlink.ChestLinkStorage; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.stream.Collectors; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.Sound; @@ -27,11 +32,6 @@ import org.bukkit.inventory.ShapelessRecipe; import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitTask; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; - public class VirtualCraftingHolder implements InventoryHolder { private final Inventory inventory; @@ -394,6 +394,9 @@ public class VirtualCraftingHolder implements InventoryHolder { // Create a copy of the real inventories and decide if any of the inputs match the output. List tempInvs = new ArrayList<>(); + // Remove duplicate inventories from list. + inputs = inputs.stream().distinct().collect(Collectors.toList()); + for (Inventory inv : inputs) { Inventory tempInv = Utils.copyInventory(inv); tempInvs.add(tempInv);