I was wondering if there was a way to get the contents of an inventory without duplication. By duplication, I mean when you're an inventory possesses two itemstacks of a particular item with different quantities such as one slot has an itemstack of 5 diamonds and another slot with 10 diamonds. Is there a way to make it so that when getting the contents, it returns a single itemstack of 15 diamonds rather than 2 itemstacks of diamonds? I tried to do something involving a hashmap of materials and such but I worry that my method might cause some instances of tools, etc. to lose their data or create malformed itemstacks.
Code (Text): public Map<ItemStack, Integer> countItems(Inventory i) { private Map<ItemStack, Integer> back = new HashMap<>(); //mapping a single item version of the stack to a count for (ItemStack is : i.getContents()) { is = is.clone(); //clone so we don't modify the actual inventory int amount = is.getAmount(); //get our old amount is.setAmount(1); //set our current stack to 1 for comparison reasons Integer current = back.get(is); //get our current count if (current == null) { //if it's null... current = 0; //initialize it to 0 } current += amount; //increment the amount by our current stack back.put(is, current); //put our new count back } return back; //return our findings }