mirror of
https://github.com/JEFF-Media-GbR/ChestSort.git
synced 2025-01-06 00:17:52 +01:00
Code cleanup
This commit is contained in:
parent
31e4f289d6
commit
69a92244d3
@ -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());
|
||||
}
|
||||
}
|
||||
|
62
src/de/jeffclan/JeffChestSort/JeffChestSortOrganizer.java
Normal file
62
src/de/jeffclan/JeffChestSort/JeffChestSortOrganizer.java
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -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())) {
|
||||
|
Loading…
Reference in New Issue
Block a user