added lore and customName placeholder to sorting-method

This commit is contained in:
mfnalex 2019-05-03 01:25:02 +02:00
parent 803342178b
commit 768b2786bb
8 changed files with 106 additions and 43 deletions

View File

@ -6,7 +6,7 @@
<groupId>de.jeffclan</groupId> <groupId>de.jeffclan</groupId>
<artifactId>JeffChestSort</artifactId> <artifactId>JeffChestSort</artifactId>
<version>4.2</version> <version>4.3.1-dev</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>JeffChestSort</name> <name>JeffChestSort</name>

View File

@ -1,5 +1,7 @@
package de.jeffclan.JeffChestSort; package de.jeffclan.JeffChestSort;
import de.jeffclan.utils.TypeMatchPositionPair;
public class JeffChestSortCategory { public class JeffChestSortCategory {
// Represents a sorting category // Represents a sorting category
@ -11,21 +13,22 @@ public class JeffChestSortCategory {
// "COARSE_DIRT" will match the typeMatch "*dirt" // "COARSE_DIRT" will match the typeMatch "*dirt"
String name; String name;
String[] typeMatches; TypeMatchPositionPair[] typeMatches;
JeffChestSortCategory(String name, String[] typeMatches) { JeffChestSortCategory(String name, TypeMatchPositionPair[] typeMatchPositionPairs) {
this.name = name; this.name = name;
this.typeMatches = typeMatches; this.typeMatches = typeMatchPositionPairs;
} }
// Checks whether a the given itemname fits into this category // Checks whether a the given itemname fits into this category and returns the line number. 0 means not found
boolean matches(String itemname) { short matches(String itemname) {
boolean asteriskBefore = false; boolean asteriskBefore = false;
boolean asteriskAfter = false; boolean asteriskAfter = false;
// Very, very simple wildcard checks // Very, very simple wildcard checks
for (String typeMatch : typeMatches) { for (TypeMatchPositionPair typeMatchPositionPair : typeMatches) {
String typeMatch = typeMatchPositionPair.getTypeMatch();
if (typeMatch.startsWith("*")) { if (typeMatch.startsWith("*")) {
asteriskBefore = true; asteriskBefore = true;
typeMatch = typeMatch.substring(1); typeMatch = typeMatch.substring(1);
@ -37,24 +40,24 @@ public class JeffChestSortCategory {
if (asteriskBefore == false && asteriskAfter == false) { if (asteriskBefore == false && asteriskAfter == false) {
if (itemname.equalsIgnoreCase(typeMatch)) { if (itemname.equalsIgnoreCase(typeMatch)) {
return true; return typeMatchPositionPair.getPosition();
} }
} else if (asteriskBefore == true && asteriskAfter == true) { } else if (asteriskBefore == true && asteriskAfter == true) {
if (itemname.contains(typeMatch)) { if (itemname.contains(typeMatch)) {
return true; return typeMatchPositionPair.getPosition();
} }
} else if (asteriskBefore == true && asteriskAfter == false) { } else if (asteriskBefore == true && asteriskAfter == false) {
if (itemname.endsWith(typeMatch)) { if (itemname.endsWith(typeMatch)) {
return true; return typeMatchPositionPair.getPosition();
} }
} else { } else {
if (itemname.startsWith(typeMatch)) { if (itemname.startsWith(typeMatch)) {
return true; return typeMatchPositionPair.getPosition();
} }
} }
} }
return false; return 0;
} }
} }

View File

@ -12,6 +12,9 @@ import org.bukkit.Bukkit;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import de.jeffclan.utils.CategoryLinePair;
import de.jeffclan.utils.TypeMatchPositionPair;
public class JeffChestSortOrganizer { public class JeffChestSortOrganizer {
// This is the heart of ChestSort! // 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 // 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" }; static final String[] woodNames = { "acacia", "birch", "jungle", "oak", "spruce", "dark_oak" };
private static final String emptyPlaceholderString= "~";
// We store a list of all Category objects // We store a list of all Category objects
ArrayList<JeffChestSortCategory> categories = new ArrayList<JeffChestSortCategory>(); ArrayList<JeffChestSortCategory> categories = new ArrayList<JeffChestSortCategory>();
@ -62,17 +67,17 @@ public class JeffChestSortOrganizer {
} }
// Returns an array with all typematches listed in the category file // 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); Scanner sc = new Scanner(file);
List<String> lines = new ArrayList<String>(); List<TypeMatchPositionPair> lines = new ArrayList<TypeMatchPositionPair>();
short currentLine=1;
while (sc.hasNextLine()) { while (sc.hasNextLine()) {
//if(!sc.nextLine().startsWith("#")) { lines.add(new TypeMatchPositionPair(sc.nextLine(),currentLine));
lines.add(sc.nextLine()); currentLine++;
//}
} }
String[] arr = lines.toArray(new String[0]); TypeMatchPositionPair[] result = lines.toArray(new TypeMatchPositionPair[0]);
sc.close(); sc.close();
return arr; return result;
} }
@ -85,7 +90,7 @@ public class JeffChestSortOrganizer {
// [0] = Sortable Item name // [0] = Sortable Item name
// [1] = Color/Wood // [1] = Color/Wood
String myColor = "<none>"; String myColor = (plugin.debug) ? "~color~" : emptyPlaceholderString;
// Only work with lowercase // Only work with lowercase
typeName = typeName.toLowerCase(); typeName = typeName.toLowerCase();
@ -168,16 +173,18 @@ public class JeffChestSortOrganizer {
// This method takes a sortable item name and checks all categories for a match // This method takes a sortable item name and checks all categories for a match
// If none, matches, return "<none>" (it will be put behind all categorized items when sorting by category) // If none, matches, return "<none>" (it will be put behind all categorized items when sorting by category)
String getCategory(String typeName) { CategoryLinePair getCategory(String typeName) {
typeName = typeName.toLowerCase(); typeName = typeName.toLowerCase();
for (JeffChestSortCategory cat : categories) { for (JeffChestSortCategory cat : categories) {
if (cat.matches(typeName)) { short matchingLineNumber = cat.matches(typeName);
return cat.name; if (matchingLineNumber!=0) {
return new CategoryLinePair(cat.name,matchingLineNumber);
} }
} }
return "<none>"; 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" // 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) { String getSortableString(ItemStack item) {
char blocksFirst; char blocksFirst;
@ -193,24 +200,36 @@ public class JeffChestSortOrganizer {
String[] typeAndColor = getTypeAndColor(item.getType().name()); String[] typeAndColor = getTypeAndColor(item.getType().name());
String typeName = typeAndColor[0]; String typeName = typeAndColor[0];
String color = typeAndColor[1]; 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. // Generate the strings that finally are used for sorting.
// They are generated according to the config.yml's sorting-method option // 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("\\{blocksFirst\\}", String.valueOf(blocksFirst));
sortableString = sortableString.replaceAll("\\{name\\}", typeName); sortableString = sortableString.replaceAll("\\{name\\}", typeName);
sortableString = sortableString.replaceAll("\\{color\\}", color); sortableString = sortableString.replaceAll("\\{color\\}", color);
sortableString = sortableString.replaceAll("\\{category\\}", category); 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; return sortableString;
} }
// Sort a complete inventory // Sort a complete inventory
void sortInventory(Inventory inv) { void sortInventory(Inventory inv) {
sortInventory(inv,0,inv.getSize()-1); 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();
}
} }

View File

@ -197,6 +197,8 @@ public class JeffChestSortPlugin extends JavaPlugin {
// Create the config file, including checks for old config versions, and load // Create the config file, including checks for old config versions, and load
// the default values for unset options // the default values for unset options
createConfig(); createConfig();
debug = getConfig().getBoolean("debug");
// Save default sorting category files when enabled in the config (default=true) // Save default sorting category files when enabled in the config (default=true)
saveDefaultCategories(); saveDefaultCategories();

View File

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

View File

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

View File

@ -90,11 +90,14 @@ disabled-worlds:
# Only change this if you know what you are doing. # Only change this if you know what you are doing.
# #
# Available variables: # Available variables:
# {category} order stuff by category as defined in plugins/ChestSort/categories/<category>.txt # {category} order stuff by category as defined in plugins/ChestSort/categories/<category>.txt
# {itemsFirst} put items before blocks # {keepCategoryOrder} orders stuff in the same category according to their line numbers in the category file
# {blocksFirst} put blocks before items # {itemsFirst} put items before blocks
# {name} returns the name (e.g. DIRT, GRASS_BLOCK, BIRCH_LOG, DIAMOND_SWORD, ...) # {blocksFirst} put blocks before items
# {color} returns the color, e.g. light_blue for wool. Empty if block/item is not dyeable # {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. # 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 # sort by category, then put items before blocks and sort by name and color
# '{category},{itemsFirst},{name},{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}' sorting-method: '{category},{itemsFirst},{name},{color}'
######################### #########################

View File

@ -1,6 +1,6 @@
main: de.jeffclan.JeffChestSort.JeffChestSortPlugin main: de.jeffclan.JeffChestSort.JeffChestSortPlugin
name: ChestSort name: ChestSort
version: 4.2 version: 4.3.1-dev
api-version: 1.13 api-version: 1.13
description: Allows automatic chest sorting description: Allows automatic chest sorting
author: mfnalex author: mfnalex