From 69a92244d3c6aac011a2e8cad29a2d6f63d2c542 Mon Sep 17 00:00:00 2001 From: mfnalex <1122571+mfnalex@users.noreply.github.com> Date: Mon, 13 Aug 2018 12:13:06 +0200 Subject: [PATCH] Code cleanup --- .../JeffChestSort/JeffChestSortListener.java | 55 +--------------- .../JeffChestSort/JeffChestSortOrganizer.java | 62 +++++++++++++++++++ .../JeffChestSort/JeffChestSortPlugin.java | 6 -- 3 files changed, 63 insertions(+), 60 deletions(-) create mode 100644 src/de/jeffclan/JeffChestSort/JeffChestSortOrganizer.java diff --git a/src/de/jeffclan/JeffChestSort/JeffChestSortListener.java b/src/de/jeffclan/JeffChestSort/JeffChestSortListener.java index af5e72a..38310a2 100644 --- a/src/de/jeffclan/JeffChestSort/JeffChestSortListener.java +++ b/src/de/jeffclan/JeffChestSort/JeffChestSortListener.java @@ -89,60 +89,7 @@ public class JeffChestSortListener implements Listener { } } } - - /* - * Alright, what the f**k is going on here? Well... - * - * First of all, we get the inventory's contents in a nice ItemStack-array. - * Altering this array does NOT change the inventory! We just use it to - * generate a sorted list of our items including a string (which happens - * to be the item's type) and a unique identifier, so that we will not - * confuse two items with the same name but different attributes. - */ - Inventory inv = event.getInventory(); - ItemStack[] items = inv.getContents(); - event.getInventory().clear(); - String[] itemList = new String[inv.getSize()]; - - int i = 0; - for (ItemStack item : items) { - if (item != null) { - itemList[i] = plugin.getSortableString(item); - i++; - } - } - - // count all items that are not null - 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]; - - // fill new array with items - for (int j = 0; j < count; j++) { - shortenedArray[j] = itemList[j]; - } - - // sort array alphabetically - Arrays.sort(shortenedArray); - - // put everything back in the inventory - for (String s : shortenedArray) { - for (ItemStack item : items) { - if (item != null && s != null) { - if (item.hashCode() == Integer.parseInt(s.split(",")[1])) { - inv.addItem(item); - item = null; - s = null; - } - } - } - } + JeffChestSortOrganizer.sortInventory(event.getInventory()); } } diff --git a/src/de/jeffclan/JeffChestSort/JeffChestSortOrganizer.java b/src/de/jeffclan/JeffChestSort/JeffChestSortOrganizer.java new file mode 100644 index 0000000..1ae45f2 --- /dev/null +++ b/src/de/jeffclan/JeffChestSort/JeffChestSortOrganizer.java @@ -0,0 +1,62 @@ +package de.jeffclan.JeffChestSort; + +import java.util.Arrays; + +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; + +public class JeffChestSortOrganizer { + + static String getSortableString(ItemStack item) { + return item.getType().name() + + "," + + String.valueOf(item.hashCode()); + } + + static void sortInventory(Inventory inv) { + ItemStack[] items = inv.getContents(); + inv.clear(); + String[] itemList = new String[inv.getSize()]; + + int i = 0; + for (ItemStack item : items) { + if (item != null) { + itemList[i] = getSortableString(item); + i++; + } + } + + // count all items that are not null + 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]; + + // fill new array with items + for (int j = 0; j < count; j++) { + shortenedArray[j] = itemList[j]; + } + + // sort array alphabetically + Arrays.sort(shortenedArray); + + // put everything back in the inventory + for (String s : shortenedArray) { + for (ItemStack item : items) { + if (item != null && s != null) { + if (item.hashCode() == Integer.parseInt(s.split(",")[1])) { + inv.addItem(item); + item = null; + s = null; + } + } + } + } + } + +} diff --git a/src/de/jeffclan/JeffChestSort/JeffChestSortPlugin.java b/src/de/jeffclan/JeffChestSort/JeffChestSortPlugin.java index eae2c65..b1b340f 100644 --- a/src/de/jeffclan/JeffChestSort/JeffChestSortPlugin.java +++ b/src/de/jeffclan/JeffChestSort/JeffChestSortPlugin.java @@ -57,12 +57,6 @@ public class JeffChestSortPlugin extends JavaPlugin { getConfig().addDefault("message-error-players-only","&cError: This command can only be run by players.&r"); } - String getSortableString(ItemStack item) { - return item.getType().name() - + "," - + String.valueOf(item.hashCode()); - } - void unregisterPlayer(Player p) { UUID uniqueId = p.getUniqueId(); if (PerPlayerSettings.containsKey(uniqueId.toString())) {