diff --git a/config.yml b/config.yml index 5f33693..fe73ba5 100644 --- a/config.yml +++ b/config.yml @@ -29,6 +29,31 @@ show-message-when-using-chest-and-sorting-is-enabled: false # logs out and back in and then uses a chest again. show-message-again-after-logout: true +########################## +##### Sorting Method ##### +########################## + +# Advanced: how to sort things! There is an example at our spigotmc page +# +# Available variables: +# {type.isBlock} returns 1 if it is a block and 0 if not +# {type.isItem} returns 1 if it is an item and 0 if not +# {type.name} returns the name (e.g. DIRT, GRASS_BLOCK, BIRCH_LOG, DIAMOND_SWORD, ...) +# +# Warning: You must not use spaces and fields have to be separated by commas. +# +# Examples: +# sort by name: +# '{type.name}' +# +# sort by name, but put items before blocks: +# '{type.isBlock},{type.name}' +# +# sort by name, but put blocks before items: +# '{type.isItem},{type.name}' +# +sorting-method: '{type.isBlock},{type.name}' + ######################### ##### localization ###### ######################### @@ -86,4 +111,4 @@ message-error-players-only: "&cError: This command can only be run by players." ######################### # please do not change the following line manually! -config-version: 2 \ No newline at end of file +config-version: 3 \ No newline at end of file diff --git a/plugin.yml b/plugin.yml index 9cb8276..5be59f4 100644 --- a/plugin.yml +++ b/plugin.yml @@ -1,6 +1,6 @@ main: de.jeffclan.JeffChestSort.JeffChestSortPlugin name: ChestSort -version: 1.5.8 +version: 1.6 api-version: 1.13 description: Allows automatic chest sorting author: mfnalex diff --git a/src/de/jeffclan/JeffChestSort/JeffChestSortListener.java b/src/de/jeffclan/JeffChestSort/JeffChestSortListener.java index c0b3a3d..960ca09 100644 --- a/src/de/jeffclan/JeffChestSort/JeffChestSortListener.java +++ b/src/de/jeffclan/JeffChestSort/JeffChestSortListener.java @@ -92,6 +92,6 @@ public class JeffChestSortListener implements Listener { } } - JeffChestSortOrganizer.sortInventory(event.getInventory()); + JeffChestSortOrganizer.sortInventory(event.getInventory(),plugin.sortingMethod); } } diff --git a/src/de/jeffclan/JeffChestSort/JeffChestSortOrganizer.java b/src/de/jeffclan/JeffChestSort/JeffChestSortOrganizer.java index 71207a2..840d852 100644 --- a/src/de/jeffclan/JeffChestSort/JeffChestSortOrganizer.java +++ b/src/de/jeffclan/JeffChestSort/JeffChestSortOrganizer.java @@ -6,57 +6,70 @@ 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++; - } - } + static String getSortableString(ItemStack item,String sortingMethod) { + int typeIsBlock = item.getType().isBlock() ? 1 : 0; + int typeIsItem = item.getType().isItem() ? 1 : 0; + String typeName = item.getType().name(); + String hashCode = String.valueOf(item.hashCode()); + short maxDurability = item.getType().getMaxDurability(); - // count all items that are not null - int count = 0; - for (String s : itemList) { - if (s != null) { - count++; - } - } + String sortableString = sortingMethod.replaceAll("\\{typeIsBlock\\}", "typeIsBlock:"+String.valueOf(typeIsBlock)); + sortableString = sortableString.replaceAll("\\{typeIsItem\\}", "typeIsItem:"+typeIsItem); + sortableString = sortableString.replaceAll("\\{typeName\\}", "typeName:"+typeName); + sortableString = sortableString.replaceAll("\\{typeMaxDurability\\}", "maxDurability:"+String.format("%06d", maxDurability)); + sortableString = sortableString + "," + hashCode; - // create new array with just the size we need - String[] shortenedArray = new String[count]; + return sortableString; - // fill new array with items - for (int j = 0; j < count; j++) { - shortenedArray[j] = itemList[j]; - } + } - // sort array alphabetically - Arrays.sort(shortenedArray); + static void sortInventory(Inventory inv,String sortingMethod) { + ItemStack[] items = inv.getContents(); + inv.clear(); + String[] itemList = new String[inv.getSize()]; - // put everything back in the inventory - for (String s : shortenedArray) { - // System.out.println(s); - for (ItemStack item : items) { - if (item != null && s != null) { - if (item.hashCode() == Integer.parseInt(s.split(",")[1])) { - inv.addItem(item); - item = null; - s = null; - } - } - } - } - } + int i = 0; + for (ItemStack item : items) { + if (item != null) { + itemList[i] = getSortableString(item,sortingMethod); + System.out.println(itemList[i]); + 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) { + // System.out.println(s); + for (ItemStack item : items) { + if (item != null && s != null) { + if (item.hashCode() == Integer.parseInt(s.split(",")[s.split(",").length-1])) { + inv.addItem(item); + item = null; + s = null; + } + } + } + } + } + +} \ No newline at end of file diff --git a/src/de/jeffclan/JeffChestSort/JeffChestSortPlugin.java b/src/de/jeffclan/JeffChestSort/JeffChestSortPlugin.java index e109c17..5e9aeaa 100644 --- a/src/de/jeffclan/JeffChestSort/JeffChestSortPlugin.java +++ b/src/de/jeffclan/JeffChestSort/JeffChestSortPlugin.java @@ -14,17 +14,31 @@ public class JeffChestSortPlugin extends JavaPlugin { Map PerPlayerSettings = new HashMap(); JeffChestSortMessages messages; + String sortingMethod; + int currentConfigVersion = 3; @Override public void onEnable() { createConfig(); messages = new JeffChestSortMessages(this); + sortingMethod = getConfig().getString("sorting-method","{typeIsBlock},{typeName}"); getServer().getPluginManager().registerEvents(new JeffChestSortListener(this), this); JeffChestSortCommandExecutor commandExecutor = new JeffChestSortCommandExecutor(this); this.getCommand("chestsort").setExecutor(commandExecutor); @SuppressWarnings("unused") Metrics metrics = new Metrics(this); + + if(getConfig().getInt("config-version",0) != currentConfigVersion) { + getLogger().warning("======================================================================"); + getLogger().warning("YOU ARE USING AN OLD CONFIG FILE!"); + getLogger().warning("This is not a problem, as ChestSort will just use the default settings"); + getLogger().warning("for unset values. However, if you want to configure the new options,"); + getLogger().warning("please go to https://www.spigotmc.org/resources/1-13-chestsort.59773/"); + getLogger().warning("and replace your config.yml with the new one. You can then insert your"); + getLogger().warning("old changes."); + getLogger().warning("======================================================================"); + } } @Override @@ -48,6 +62,7 @@ public class JeffChestSortPlugin extends JavaPlugin { getConfig().addDefault("show-message-when-using-chest", true); getConfig().addDefault("show-message-when-using-chest-and-sorting-is-enabled", false); getConfig().addDefault("show-message-again-after-logout", true); + getConfig().addDefault("sorting-method", "{typeIsBlock},{typeName}"); } void unregisterPlayer(Player p) {