From 18e46e199d69abae82061ee71f735f9b8abee70f Mon Sep 17 00:00:00 2001 From: mfnalex <1122571+mfnalex@users.noreply.github.com> Date: Sun, 11 Nov 2018 14:14:59 +0100 Subject: [PATCH] optimized sorting algorith --- plugin.yml | 2 +- .../JeffChestSort/JeffChestSortOrganizer.java | 96 ++++++++++++++----- 2 files changed, 72 insertions(+), 26 deletions(-) diff --git a/plugin.yml b/plugin.yml index 3aae3ac..85b5968 100644 --- a/plugin.yml +++ b/plugin.yml @@ -1,6 +1,6 @@ main: de.jeffclan.JeffChestSort.JeffChestSortPlugin name: ChestSort -version: 3.1 +version: 3.2 api-version: 1.13 description: Allows automatic chest sorting author: mfnalex diff --git a/src/de/jeffclan/JeffChestSort/JeffChestSortOrganizer.java b/src/de/jeffclan/JeffChestSort/JeffChestSortOrganizer.java index 3731bbe..c37d5b3 100644 --- a/src/de/jeffclan/JeffChestSort/JeffChestSortOrganizer.java +++ b/src/de/jeffclan/JeffChestSort/JeffChestSortOrganizer.java @@ -4,15 +4,22 @@ import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; import java.util.List; import java.util.Scanner; +import org.bukkit.Material; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.SkullMeta; +import org.bukkit.material.Skull; public class JeffChestSortOrganizer { /* + * DEPRECATED: THE FOLLOWING INFOS ARE OUTDATED + * I HAVE REPLACED THE UUID CONNECTION WITH AN ARRAY THAT REFERS TO THE ACTUAL ITEMSTACK * Thoughts before implementing: * We create a string from each item that can be sorted. * We will omit certain parts of the name and put them behind the main name for sorting reasons. @@ -179,7 +186,7 @@ public class JeffChestSortOrganizer { String color = typeAndColor[1]; String category = getCategory(item.getType().name()); - String hashCode = String.valueOf(item.hashCode()); + String hashCode = String.valueOf(getBetterHash(item)); String sortableString = plugin.sortingMethod.replaceAll("\\{itemsFirst\\}", String.valueOf(itemsFirst)); sortableString = sortableString.replaceAll("\\{blocksFirst\\}", String.valueOf(blocksFirst)); @@ -197,12 +204,19 @@ public class JeffChestSortOrganizer { } void sortInventory(Inventory inv,int startSlot, int endSlot) { + + // This has been optimized as of ChestSort 3.2. + // The hashCode is just kept for legacy reasons, it is actually not needed. + if(plugin.debug) { System.out.println(" "); System.out.println(" "); } + + // We copy the complete inventory into an array ItemStack[] items = inv.getContents(); + // Get rid of all stuff before startSlot and after endSlot for(int i = 0; i nonNullItemsList = new ArrayList(); + for(ItemStack item : items) { + if(item!=null) { + nonNullItemsList.add(item); } } + + + //String[] itemList = new String[inv.getSize()]; + +// int i = 0; +// for (ItemStack item : items) { +// if (item != null) { +// itemList[i] = getSortableString(item); +// i++; +// } +// } + + items=null; + + // We need the list as array + ItemStack[] nonNullItems = nonNullItemsList.toArray(new ItemStack[nonNullItemsList.size()]); + + // Sort the array with ItemStacks according to our sortable String + Arrays.sort(nonNullItems,new Comparator(){ + public int compare(ItemStack s1,ItemStack s2){ + return(getSortableString(s1).compareTo(getSortableString(s2))); + }}); // count all items that are not null - int count = 0; - for (String s : itemList) { - if (s != null) { - count++; - } - } +// int count = 0; +// for (String s : itemList) { +// if (s != null) { +// count++; +// } +// } // create new array with just the size we need - String[] shortenedArray = new String[count]; + //String[] shortenedArray = new String[count]; // fill new array with items - for (int j = 0; j < count; j++) { - shortenedArray[j] = itemList[j]; - } +// for (int j = 0; j < count; j++) { +// shortenedArray[j] = itemList[j]; +// } // sort array alphabetically - Arrays.sort(shortenedArray); + //Arrays.sort(shortenedArray); // put everything back in the inventory - for (String s : shortenedArray) { + + for(ItemStack item : nonNullItems) { + if(plugin.debug) System.out.println(getSortableString(item)); + inv.addItem(item); + } + + /*for (String s : shortenedArray) { + System.out.println(s); // System.out.println(s); for (ItemStack item : items) { if (item != null && s != null) { - if (item.hashCode() == Integer.parseInt(s.split(",")[s.split(",").length - 1])) { + if (getBetterHash(item) == Integer.parseInt(s.split(",")[s.split(",").length - 1])) { inv.addItem(item); item = null; s = null; } } } - } + }*/ + } + + private static int getBetterHash(ItemStack item) { + // I wanted to fix the skull problems here. Instead, I ended up not using the hashCode at all. + // I still left this here because it is nice to see the hashcodes when debug is enabled + return item.hashCode(); } }