Merge remote-tracking branch 'origin/master' into master

This commit is contained in:
mfnalex 2020-12-17 01:08:16 +01:00
commit ee0358abd3
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.event.player.PlayerQuitEvent;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import java.util.HashMap;
import java.util.Map;
public class ChestSortListener implements Listener { public class ChestSortListener implements Listener {
@ -561,6 +565,12 @@ public class ChestSortListener implements Listener {
ChestSortEvent chestSortEvent = new ChestSortEvent(e.getInventory()); ChestSortEvent chestSortEvent = new ChestSortEvent(e.getInventory());
chestSortEvent.setLocation(e.getWhoClicked().getLocation()); 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); Bukkit.getPluginManager().callEvent(chestSortEvent);
if (chestSortEvent.isCancelled()) { if (chestSortEvent.isCancelled()) {
return; return;

View File

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