Merge pull request #97 from NickNackGus/feature/sortable_key_api

Feature/sortable key api
This commit is contained in:
mfnalex 2020-12-09 02:15:11 +01:00 committed by GitHub
commit e6db59324b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 23 deletions

View File

@ -25,6 +25,10 @@ import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import java.util.HashMap;
import java.util.Map;
public class ChestSortListener implements Listener {
@ -561,6 +565,12 @@ public class ChestSortListener implements Listener {
ChestSortEvent chestSortEvent = new ChestSortEvent(e.getInventory());
chestSortEvent.setLocation(e.getWhoClicked().getLocation());
chestSortEvent.setSortableMaps(new HashMap<ItemStack, Map<String, String>>());
for (ItemStack item : e.getInventory().getContents()) {
chestSortEvent.getSortableMaps().put(item, plugin.organizer.getSortableMap(item));
}
Bukkit.getPluginManager().callEvent(chestSortEvent);
if (chestSortEvent.isCancelled()) {
return;

View File

@ -342,9 +342,13 @@ public class ChestSortOrganizer {
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) {
// Generate a map of "{placeholder}", "sortString" pairs for an ItemStack
Map<String, String> getSortableMap(ItemStack item) {
if (item == null) {
// Empty map for non-item
return new HashMap<String, String>();
}
char blocksFirst;
char itemsFirst;
if (item.getType().isBlock()) {
@ -417,20 +421,33 @@ public class ChestSortOrganizer {
// Put enchanted items before unenchanted ones
typeName = typeName + String.format("%05d", 10000 - getNumberOfEnchantments(item));
// Generate the strings that finally are used for sorting.
// They are generated according to the config.yml's sorting-method option
// Generate the map of string replacements used to generate a sortableString.
// This map can be edited by ChestSortEvent handlers. See ChestSortEvent.getSortableMaps()
Map<String, String> sortableMap = new HashMap<String, String>();
sortableMap.put("{itemsFirst}", String.valueOf(itemsFirst));
sortableMap.put("{blocksFirst}", String.valueOf(blocksFirst));
sortableMap.put("{name}", typeName + potionEffect);
sortableMap.put("{color}", color);
sortableMap.put("{category}", categorySticky);
sortableMap.put("{keepCategoryOrder}", lineNumber);
sortableMap.put("{customName}", customName);
sortableMap.put("{lore}", lore);
return sortableMap;
}
// 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, Map<String, String> sortableMap) {
String sortableString = plugin.sortingMethod.replaceAll(",", "|");
sortableString = sortableString.replace("{itemsFirst}", String.valueOf(itemsFirst));
sortableString = sortableString.replace("{blocksFirst}", String.valueOf(blocksFirst));
sortableString = sortableString.replace("{name}", typeName + potionEffect);
sortableString = sortableString.replace("{color}", color);
sortableString = sortableString.replace("{category}", categorySticky);
sortableString = sortableString.replace("{keepCategoryOrder}", lineNumber);
sortableString = sortableString.replace("{customName}", customName);
sortableString = sortableString.replace("{lore}", lore);
for (Map.Entry<String, String> entry : sortableMap.entrySet()) {
String placeholder = entry.getKey();
String sortableValue = entry.getValue();
sortableString = sortableString.replace(placeholder, sortableValue);
}
return sortableString;
}
// Sort a complete inventory
@ -442,16 +459,12 @@ public class ChestSortOrganizer {
void sortInventory(@NotNull Inventory inv, int startSlot, int endSlot) {
if(inv==null) return;
Class<? extends Inventory> invClass = inv.getClass();
de.jeff_media.ChestSortAPI.ChestSortEvent chestSortEvent = new de.jeff_media.ChestSortAPI.ChestSortEvent(inv);
try {
if (invClass.getMethod("getLocation", null) != null) {
// This whole try/catch fixes MethodNotFoundException when using inv.getLocation in Spigot 1.8.
}
if (inv.getLocation() != null) {
de.jeff_media.ChestSortAPI.ChestSortEvent chestSortEvent = new de.jeff_media.ChestSortAPI.ChestSortEvent(inv);
chestSortEvent.setLocation(inv.getLocation());
Bukkit.getPluginManager().callEvent(chestSortEvent);
if (chestSortEvent.isCancelled()) {
return;
if (inv.getLocation() != null) {
chestSortEvent.setLocation(inv.getLocation());
}
}
} catch (Throwable throwable) {
@ -462,6 +475,16 @@ public class ChestSortOrganizer {
}
chestSortEvent.setSortableMaps(new HashMap<ItemStack, Map<String, String>>());
for (ItemStack item : inv.getContents()) {
chestSortEvent.getSortableMaps().put(item, getSortableMap(item));
}
Bukkit.getPluginManager().callEvent(chestSortEvent);
if (chestSortEvent.isCancelled()) {
return;
}
if (plugin.debug) {
System.out.println(" ");
@ -526,7 +549,9 @@ public class ChestSortOrganizer {
ItemStack[] nonNullItems = nonNullItemsList.toArray(new ItemStack[0]);
// Sort the array with ItemStacks according to each ItemStacks' sortable String
Arrays.sort(nonNullItems, Comparator.comparing(this::getSortableString));
Arrays.sort(nonNullItems, Comparator.comparing((ItemStack item) -> {
// lambda expression used to pass extra parameter
return this.getSortableString(item, chestSortEvent.getSortableMaps().get(item));}));
// Now, we put everything back in a temporary inventory to combine ItemStacks
// even when using strict slot sorting
@ -542,7 +567,7 @@ public class ChestSortOrganizer {
for (ItemStack item : nonNullItems) {
if (plugin.debug)
System.out.println(getSortableString(item));
System.out.println(getSortableString(item, chestSortEvent.getSortableMaps().get(item)));
// Add the item to the temporary inventory
tempInventory.addItem(item);
}