Last active
June 1, 2020 10:44
-
-
Save SebbyLaw/42398701ff19e447a39e2c152d923c84 to your computer and use it in GitHub Desktop.
Compress List of ItemStack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.List; | |
import java.util.ArrayList; | |
import java.util.Map; | |
import java.util.HashMap; | |
import org.bukkit.Material; | |
import org.bukkit.inventory.ItemStack; | |
/* | |
Will return a new ArrayList, without modifying the original List | |
Does not account for tools or nbt data. Too bad! | |
*/ | |
public static ArrayList<ItemStack> compressedList(List<ItemStack> itemStacks) { | |
HashMap<Material, int> materials = new HashMap<Material, int>(){{ | |
for (ItemStack itemStack: itemStacks) { | |
if (itemStack != null) { | |
int i = get(itemStack.getType()); // can be null | |
put(itemStack.getType(), i == null ? itemStack.getAmount() : itemStack.getAmount() + i); | |
} | |
} | |
}}; | |
return new ArrayList<ItemStack>(){{ | |
for (Map.Entry<Material, int> entry: materials.entrySet()) { | |
Material m = entry.getKey(); | |
int count = entry.getValue(); | |
while (count >= 64) { | |
add(new ItemStack(m, 64)); | |
count -= 64; | |
} | |
add(new ItemStack(m, count)); | |
} | |
}}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment