Code cleanup

This commit is contained in:
mfnalex 2018-08-13 12:13:06 +02:00
parent 31e4f289d6
commit 69a92244d3
3 changed files with 63 additions and 60 deletions

View File

@ -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());
}
}

View File

@ -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;
}
}
}
}
}
}

View File

@ -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())) {