From 4719772e5e296601492f9b477e9fd59bcf6c0d6c Mon Sep 17 00:00:00 2001
From: mfnalex <1122571+mfnalex@users.noreply.github.com>
Date: Sun, 18 Jul 2021 11:25:25 +0200
Subject: [PATCH] 11.1.0 release
---
CHANGELOG.md | 3 +
pom.xml | 2 +-
.../handlers/ChestSortOrganizer.java | 13 ++---
.../chestsort/utils/EnchantmentUtils.java | 56 +++++++++++++++++++
4 files changed, 65 insertions(+), 9 deletions(-)
create mode 100644 src/main/java/de/jeff_media/chestsort/utils/EnchantmentUtils.java
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6fbf506..935fb54 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,8 @@
# Changelog
+## 11.1.0
+- Improved sorting for items enchanted with different types of Enchantments
+
## 11.0.3
- Removed message "You are using the latest version of ChestSort"
- Updated Korean translation
diff --git a/pom.xml b/pom.xml
index b1e0b56..a3f0a8d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -9,7 +9,7 @@
ChestSort
https://www.chestsort.de
Allows automatic chest sorting!
- 11.0.3
+ 11.1.0
jar
diff --git a/src/main/java/de/jeff_media/chestsort/handlers/ChestSortOrganizer.java b/src/main/java/de/jeff_media/chestsort/handlers/ChestSortOrganizer.java
index 00d6fe4..23566b8 100644
--- a/src/main/java/de/jeff_media/chestsort/handlers/ChestSortOrganizer.java
+++ b/src/main/java/de/jeff_media/chestsort/handlers/ChestSortOrganizer.java
@@ -7,6 +7,7 @@ import de.jeff_media.chestsort.hooks.CrackShotHook;
import de.jeff_media.chestsort.hooks.InventoryPagesHook;
import de.jeff_media.chestsort.hooks.SlimeFunHook;
import de.jeff_media.chestsort.data.CategoryLinePair;
+import de.jeff_media.chestsort.utils.EnchantmentUtils;
import de.jeff_media.chestsort.utils.TypeMatchPositionPair;
import de.jeff_media.chestsort.utils.Utils;
import org.bukkit.Bukkit;
@@ -163,7 +164,7 @@ public class ChestSortOrganizer {
return sortableInventories.contains(inv);
}
- static int getNumberOfEnchantments(ItemStack is) {
+ /*static int getNumberOfEnchantments(ItemStack is) {
int totalEnchants = 0;
@@ -182,7 +183,7 @@ public class ChestSortOrganizer {
totalEnchants += level;
}
return totalEnchants;
- }
+ }*/
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
boolean doesInventoryContain(Inventory inv, Material mat) {
@@ -439,7 +440,7 @@ public class ChestSortOrganizer {
}
// Put enchanted items before unenchanted ones
- typeName = typeName + String.format("%05d", 10000 - getNumberOfEnchantments(item));
+ typeName = typeName + EnchantmentUtils.getEnchantmentString(item); //String.format("%05d", 10000 - getNumberOfEnchantments(item));
// Generate the map of string replacements used to generate a sortableString.
// This map can be edited by ChestSortEvent handlers. See ChestSortEvent.getSortableMaps()
@@ -490,11 +491,7 @@ public class ChestSortOrganizer {
chestSortEvent.setLocation(inv.getLocation());
}
}
- } catch (Throwable throwable) {
- //System.out.println("Exception cought");
- // I dont know, but this inv.getLocation() causes a NullPointerException when using EssentialsX's /ec command in 1.12
- // Don't ask me why, but everything still works as expected if we catch that Exception.
- // TODO Auto-generated catch block
+ } catch (Throwable ignored) {
}
diff --git a/src/main/java/de/jeff_media/chestsort/utils/EnchantmentUtils.java b/src/main/java/de/jeff_media/chestsort/utils/EnchantmentUtils.java
new file mode 100644
index 0000000..abe885c
--- /dev/null
+++ b/src/main/java/de/jeff_media/chestsort/utils/EnchantmentUtils.java
@@ -0,0 +1,56 @@
+package de.jeff_media.chestsort.utils;
+
+import org.bukkit.enchantments.Enchantment;
+import org.bukkit.inventory.ItemStack;
+import org.bukkit.inventory.meta.EnchantmentStorageMeta;
+import org.bukkit.inventory.meta.ItemMeta;
+
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+
+public class EnchantmentUtils {
+
+ public static String getEnchantmentString(ItemStack item) {
+ StringBuilder builder = new StringBuilder(",");
+ builder.append(getInversedEnchantmentAmount(item));
+ if(!item.hasItemMeta()) return builder.toString();
+ ItemMeta meta = item.getItemMeta();
+ if(!meta.hasEnchants() && !(meta instanceof EnchantmentStorageMeta)) return builder.toString();
+ List sortedEnchants = new ArrayList<>(meta.getEnchants().keySet());
+ sortedEnchants.sort(Comparator.comparing(o -> o.getKey().getKey()));
+ for(Enchantment enchantment : sortedEnchants) {
+ builder.append(",");
+ builder.append(enchantment.getKey().getKey());
+ builder.append(",");
+ builder.append(Integer.MAX_VALUE - meta.getEnchantLevel(enchantment));
+ }
+ if(meta instanceof EnchantmentStorageMeta) {
+ List sortedStoredEnchants = new ArrayList<>(((EnchantmentStorageMeta)meta).getStoredEnchants().keySet());
+ for(Enchantment enchantment : sortedStoredEnchants) {
+ builder.append(",");
+ builder.append(enchantment.getKey().getKey());
+ builder.append(",");
+ builder.append(Integer.MAX_VALUE - meta.getEnchantLevel(enchantment));
+ }
+ }
+ return builder.toString();
+ }
+
+ public static int getInversedEnchantmentAmount(ItemStack item) {
+ int amount = Integer.MAX_VALUE;
+ ItemMeta meta = item.getItemMeta();
+ if(!meta.hasEnchants() && !(meta instanceof EnchantmentStorageMeta)) return amount;
+ for(int level : meta.getEnchants().values()) {
+ amount -= level;
+ }
+ if(meta instanceof EnchantmentStorageMeta) {
+ for(int level : ((EnchantmentStorageMeta)meta).getStoredEnchants().values()) {
+ amount -= level;
+ }
+ }
+ return amount;
+ }
+
+}