From 768b2786bbdf78d83bbe60e81d2ee6039ac98ef3 Mon Sep 17 00:00:00 2001 From: mfnalex <1122571+mfnalex@users.noreply.github.com> Date: Fri, 3 May 2019 01:25:02 +0200 Subject: [PATCH] added lore and customName placeholder to sorting-method --- pom.xml | 2 +- .../JeffChestSort/JeffChestSortCategory.java | 25 ++++---- .../JeffChestSort/JeffChestSortOrganizer.java | 61 +++++++++++-------- .../JeffChestSort/JeffChestSortPlugin.java | 2 + .../de/jeffclan/utils/CategoryLinePair.java | 20 ++++++ .../jeffclan/utils/TypeMatchPositionPair.java | 21 +++++++ src/main/resources/config.yml | 16 +++-- src/main/resources/plugin.yml | 2 +- 8 files changed, 106 insertions(+), 43 deletions(-) create mode 100644 src/main/java/de/jeffclan/utils/CategoryLinePair.java create mode 100644 src/main/java/de/jeffclan/utils/TypeMatchPositionPair.java diff --git a/pom.xml b/pom.xml index 0687f68..fe126c8 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ de.jeffclan JeffChestSort - 4.2 + 4.3.1-dev jar JeffChestSort diff --git a/src/main/java/de/jeffclan/JeffChestSort/JeffChestSortCategory.java b/src/main/java/de/jeffclan/JeffChestSort/JeffChestSortCategory.java index 1fcdf3e..e6fc810 100644 --- a/src/main/java/de/jeffclan/JeffChestSort/JeffChestSortCategory.java +++ b/src/main/java/de/jeffclan/JeffChestSort/JeffChestSortCategory.java @@ -1,5 +1,7 @@ package de.jeffclan.JeffChestSort; +import de.jeffclan.utils.TypeMatchPositionPair; + public class JeffChestSortCategory { // Represents a sorting category @@ -11,21 +13,22 @@ public class JeffChestSortCategory { // "COARSE_DIRT" will match the typeMatch "*dirt" String name; - String[] typeMatches; + TypeMatchPositionPair[] typeMatches; - JeffChestSortCategory(String name, String[] typeMatches) { + JeffChestSortCategory(String name, TypeMatchPositionPair[] typeMatchPositionPairs) { this.name = name; - this.typeMatches = typeMatches; + this.typeMatches = typeMatchPositionPairs; } - // Checks whether a the given itemname fits into this category - boolean matches(String itemname) { + // Checks whether a the given itemname fits into this category and returns the line number. 0 means not found + short matches(String itemname) { boolean asteriskBefore = false; boolean asteriskAfter = false; // Very, very simple wildcard checks - for (String typeMatch : typeMatches) { + for (TypeMatchPositionPair typeMatchPositionPair : typeMatches) { + String typeMatch = typeMatchPositionPair.getTypeMatch(); if (typeMatch.startsWith("*")) { asteriskBefore = true; typeMatch = typeMatch.substring(1); @@ -37,24 +40,24 @@ public class JeffChestSortCategory { if (asteriskBefore == false && asteriskAfter == false) { if (itemname.equalsIgnoreCase(typeMatch)) { - return true; + return typeMatchPositionPair.getPosition(); } } else if (asteriskBefore == true && asteriskAfter == true) { if (itemname.contains(typeMatch)) { - return true; + return typeMatchPositionPair.getPosition(); } } else if (asteriskBefore == true && asteriskAfter == false) { if (itemname.endsWith(typeMatch)) { - return true; + return typeMatchPositionPair.getPosition(); } } else { if (itemname.startsWith(typeMatch)) { - return true; + return typeMatchPositionPair.getPosition(); } } } - return false; + return 0; } } diff --git a/src/main/java/de/jeffclan/JeffChestSort/JeffChestSortOrganizer.java b/src/main/java/de/jeffclan/JeffChestSort/JeffChestSortOrganizer.java index 12082e0..278eec3 100644 --- a/src/main/java/de/jeffclan/JeffChestSort/JeffChestSortOrganizer.java +++ b/src/main/java/de/jeffclan/JeffChestSort/JeffChestSortOrganizer.java @@ -12,6 +12,9 @@ import org.bukkit.Bukkit; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; +import de.jeffclan.utils.CategoryLinePair; +import de.jeffclan.utils.TypeMatchPositionPair; + public class JeffChestSortOrganizer { // This is the heart of ChestSort! @@ -34,6 +37,8 @@ public class JeffChestSortOrganizer { // The same applies for wood. We strip the wood name from the item name and keep it in the above mentioned color variable static final String[] woodNames = { "acacia", "birch", "jungle", "oak", "spruce", "dark_oak" }; + private static final String emptyPlaceholderString= "~"; + // We store a list of all Category objects ArrayList categories = new ArrayList(); @@ -62,17 +67,17 @@ public class JeffChestSortOrganizer { } // Returns an array with all typematches listed in the category file - String[] getArrayFromCategoryFile(File file) throws FileNotFoundException { + TypeMatchPositionPair[] getArrayFromCategoryFile(File file) throws FileNotFoundException { Scanner sc = new Scanner(file); - List lines = new ArrayList(); + List lines = new ArrayList(); + short currentLine=1; while (sc.hasNextLine()) { - //if(!sc.nextLine().startsWith("#")) { - lines.add(sc.nextLine()); - //} + lines.add(new TypeMatchPositionPair(sc.nextLine(),currentLine)); + currentLine++; } - String[] arr = lines.toArray(new String[0]); + TypeMatchPositionPair[] result = lines.toArray(new TypeMatchPositionPair[0]); sc.close(); - return arr; + return result; } @@ -85,7 +90,7 @@ public class JeffChestSortOrganizer { // [0] = Sortable Item name // [1] = Color/Wood - String myColor = ""; + String myColor = (plugin.debug) ? "~color~" : emptyPlaceholderString; // Only work with lowercase typeName = typeName.toLowerCase(); @@ -168,16 +173,18 @@ public class JeffChestSortOrganizer { // This method takes a sortable item name and checks all categories for a match // If none, matches, return "" (it will be put behind all categorized items when sorting by category) - String getCategory(String typeName) { + CategoryLinePair getCategory(String typeName) { typeName = typeName.toLowerCase(); for (JeffChestSortCategory cat : categories) { - if (cat.matches(typeName)) { - return cat.name; + short matchingLineNumber = cat.matches(typeName); + if (matchingLineNumber!=0) { + return new CategoryLinePair(cat.name,matchingLineNumber); } } - return ""; + return new CategoryLinePair((plugin.debug) ? "~category~" : emptyPlaceholderString,(short) 0); } + // This puts together the sortable item name, the category, the color, and whether the item is a block or a "regular item" String getSortableString(ItemStack item) { char blocksFirst; @@ -193,24 +200,36 @@ public class JeffChestSortOrganizer { String[] typeAndColor = getTypeAndColor(item.getType().name()); String typeName = typeAndColor[0]; String color = typeAndColor[1]; - String category = getCategory(item.getType().name()); + String category = getCategory(item.getType().name()).getCategoryName(); + String customName = (plugin.debug) ? "~customName~" : emptyPlaceholderString; + if(item.getItemMeta().hasDisplayName() && item.getItemMeta().getDisplayName()!=null) { + customName=item.getItemMeta().getDisplayName(); + } + String lore =(plugin.debug)? "~lore~" : emptyPlaceholderString; + if(item.getItemMeta().hasLore() && item.getItemMeta().getLore() != null && item.getItemMeta().getLore().size()!=0) { + String[] loreArray=item.getItemMeta().getLore().toArray(new String[0]); + lore = String.join(",", loreArray); + } + short lineNumber = getCategory(item.getType().name()).getPosition(); - // The hashcode actually not needed anymore, but I kept it for debugging purposes - String hashCode = String.valueOf(getBetterHash(item)); // Generate the strings that finally are used for sorting. // They are generated according to the config.yml's sorting-method option - String sortableString = plugin.sortingMethod.replaceAll("\\{itemsFirst\\}", String.valueOf(itemsFirst)); + String sortableString = plugin.sortingMethod.replaceAll(",", "|"); + sortableString = sortableString.replaceAll("\\{itemsFirst\\}", String.valueOf(itemsFirst)); sortableString = sortableString.replaceAll("\\{blocksFirst\\}", String.valueOf(blocksFirst)); sortableString = sortableString.replaceAll("\\{name\\}", typeName); sortableString = sortableString.replaceAll("\\{color\\}", color); sortableString = sortableString.replaceAll("\\{category\\}", category); - sortableString = sortableString + "," + hashCode; + sortableString = sortableString.replaceAll("\\{line\\}", String.valueOf(lineNumber)); + sortableString = sortableString.replaceAll("\\{customName\\}", customName); + sortableString = sortableString.replaceAll("\\{lore\\}", lore); return sortableString; } + // Sort a complete inventory void sortInventory(Inventory inv) { sortInventory(inv,0,inv.getSize()-1); @@ -296,13 +315,5 @@ public class JeffChestSortOrganizer { } } - // I wanted to fix the skull problems here. Instead, I ended up not using the hashCode at all. - // I still left this here for nostalgic reasons. Also it is nice to see the hashcodes when debug is enabled - // TODO: feel free to remove this and all references, it is really not needed anymore. - private static int getBetterHash(ItemStack item) { - // I used to add some metadata here, but it has been removed since a long time - // as I said: feel free to remove this completely - return item.hashCode(); - } } diff --git a/src/main/java/de/jeffclan/JeffChestSort/JeffChestSortPlugin.java b/src/main/java/de/jeffclan/JeffChestSort/JeffChestSortPlugin.java index 3197e9a..40c0f29 100644 --- a/src/main/java/de/jeffclan/JeffChestSort/JeffChestSortPlugin.java +++ b/src/main/java/de/jeffclan/JeffChestSort/JeffChestSortPlugin.java @@ -197,6 +197,8 @@ public class JeffChestSortPlugin extends JavaPlugin { // Create the config file, including checks for old config versions, and load // the default values for unset options createConfig(); + + debug = getConfig().getBoolean("debug"); // Save default sorting category files when enabled in the config (default=true) saveDefaultCategories(); diff --git a/src/main/java/de/jeffclan/utils/CategoryLinePair.java b/src/main/java/de/jeffclan/utils/CategoryLinePair.java new file mode 100644 index 0000000..ca5758b --- /dev/null +++ b/src/main/java/de/jeffclan/utils/CategoryLinePair.java @@ -0,0 +1,20 @@ +package de.jeffclan.utils; + + +public class CategoryLinePair { + String categoryName; + short position; + + public CategoryLinePair(String categoryName,short position) { + this.categoryName=categoryName; + this.position=position; + } + + public String getCategoryName() { + return categoryName; + } + + public short getPosition() { + return position; + } +} diff --git a/src/main/java/de/jeffclan/utils/TypeMatchPositionPair.java b/src/main/java/de/jeffclan/utils/TypeMatchPositionPair.java new file mode 100644 index 0000000..8dbf4c9 --- /dev/null +++ b/src/main/java/de/jeffclan/utils/TypeMatchPositionPair.java @@ -0,0 +1,21 @@ +package de.jeffclan.utils; + +public class TypeMatchPositionPair { + + String typeMatch; + public String getTypeMatch() { + return typeMatch; + } + + public short getPosition() { + return position; + } + + short position; + + public TypeMatchPositionPair(String typeMatch,short position) { + this.typeMatch=typeMatch; + this.position=position; + } + +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 348662f..dd3f15c 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -90,11 +90,14 @@ disabled-worlds: # Only change this if you know what you are doing. # # Available variables: -# {category} order stuff by category as defined in plugins/ChestSort/categories/.txt -# {itemsFirst} put items before blocks -# {blocksFirst} put blocks before items -# {name} returns the name (e.g. DIRT, GRASS_BLOCK, BIRCH_LOG, DIAMOND_SWORD, ...) -# {color} returns the color, e.g. light_blue for wool. Empty if block/item is not dyeable +# {category} order stuff by category as defined in plugins/ChestSort/categories/.txt +# {keepCategoryOrder} orders stuff in the same category according to their line numbers in the category file +# {itemsFirst} put items before blocks +# {blocksFirst} put blocks before items +# {name} returns the name (e.g. DIRT, GRASS_BLOCK, BIRCH_LOG, DIAMOND_SWORD, ...) +# {color} returns the color, e.g. light_blue for wool. Empty if block/item is not dyeable +# {customName} returns the display name if set (e.g. with an anvil) +# {lore} returns the lore if set # # Warning: You must not use spaces and fields have to be separated by commas. # @@ -111,6 +114,9 @@ disabled-worlds: # sort by category, then put items before blocks and sort by name and color # '{category},{itemsFirst},{name},{color}' # +# sort by category, but keep exactly the same order as defined in each category file, then sort any undefined items by name and color +# '{category},{keepCategoryOrder},{name},{color} +# sorting-method: '{category},{itemsFirst},{name},{color}' ######################### diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 41af7ae..f920fee 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,6 +1,6 @@ main: de.jeffclan.JeffChestSort.JeffChestSortPlugin name: ChestSort -version: 4.2 +version: 4.3.1-dev api-version: 1.13 description: Allows automatic chest sorting author: mfnalex