Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. @NotNull
  2. private Comparator<ItemStack> itemStackComparator() {
  3. return (o1, o2) -> {
  4. //Sort first by their order in storableItems (left to right)
  5. ItemStack o1BaseItem = getBaseItem(o1);
  6. ItemStack o2BaseItem = getBaseItem(o2);
  7.  
  8. List<ItemStack> storablesList = new ArrayList<>(storableItems.keySet());
  9. int compare = Integer.compare(storablesList.indexOf(o1BaseItem), storablesList.indexOf(o2BaseItem));
  10. if (compare != 0) return compare;
  11.  
  12. if (o1BaseItem.isSimilar(o2BaseItem)) {
  13. //If the two base items are the same...
  14.  
  15. if (o1.getType() == Material.ENCHANTED_BOOK) {
  16. Map.Entry<Enchantment, Integer> enchantment = getFirstEnchantment(o1, true);
  17. Map.Entry<Enchantment, Integer> enchantment2 = getFirstEnchantment(o2, true);
  18. if (enchantment != null && enchantment2 != null) {
  19. Enchantment key = enchantment.getKey();
  20. Enchantment key2 = enchantment2.getKey();
  21.  
  22. if (key.getKey().equals(key2.getKey())) {
  23. //If they share the same first enchantment, compare the enchant level
  24. compare = Integer.compare(enchantment2.getValue(), enchantment.getValue());
  25. if (compare != 0) return compare;
  26. } else {
  27. //Compare just based on key name? Which I suppose would be alphabetical
  28. compare = key.getKey().getKey().compareTo(key2.getKey().getKey());
  29. if (compare != 0) return compare;
  30. }
  31. }
  32. } else if (o1.getType() == Material.FIREWORK_ROCKET) {
  33. FireworkMeta fireworkMeta = (FireworkMeta) o1.getItemMeta();
  34. FireworkMeta fireworkMeta2 = (FireworkMeta) o2.getItemMeta();
  35. compare = Integer.compare(fireworkMeta2.getPower(), fireworkMeta.getPower());
  36. if (compare != 0) return compare;
  37. }
  38.  
  39. //Compare by durability
  40. if (o1.getItemMeta() instanceof Damageable) {
  41. Damageable damageable = (Damageable) o1.getItemMeta();
  42. if (damageable.getDamage() > 0) {
  43. compare = Integer.compare(damageable.getDamage(), ((Damageable) o2.getItemMeta()).getDamage());
  44. if (compare != 0) return compare;
  45. }
  46. }
  47. }
  48.  
  49. compare = Integer.compare(o1.hashCode(), o2.hashCode());
  50. if (compare != 0) return compare;
  51.  
  52. //At this point, these are identical items based on item hashcode. Player heads are a good example of
  53. //things like this. Does it have a display name?
  54. String displayName = getDisplayName(o1);
  55. String displayName2 = getDisplayName(o2);
  56. if (displayName != null && displayName2 != null) {
  57. compare = displayName.compareTo(displayName2);
  58. if (compare != 0) return compare;
  59. }
  60.  
  61. //Ok how's about comparing itemstack size.
  62. compare = Integer.compare(o2.getAmount(), o1.getAmount());
  63. if (compare != 0) return compare;
  64.  
  65. TheArtifact.log.warning(this.toString() + " Comparison between " + o1 + " and " + o2 + " is 0!");
  66. return 1;
  67. };
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement