Skip to content

Commit

Permalink
fix: sort
Browse files Browse the repository at this point in the history
  • Loading branch information
makeevrserg committed Aug 27, 2023
1 parent 5de0314 commit c1ffcfd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 61 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ makeevrserg.java.ktarget=17
# Project
makeevrserg.project.name=AspeKt
makeevrserg.project.group=ru.astrainteractive.aspekt
makeevrserg.project.version.string=2.5.2
makeevrserg.project.version.string=2.5.3
makeevrserg.project.description=Essentials plugin for EmpireProjekt
makeevrserg.project.developers=makeevrserg|Makeev Roman|[email protected]
makeevrserg.project.url=https://empireprojekt.ru
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package ru.astrainteractive.aspekt.event.sort

enum class Sort(var desc: Boolean = false) {
TYPE, NAME, WOOL, GLASS, BLOCK, TOOL
enum class Sort(val desc: Boolean) {
TYPE_ASC(false), TYPE_DESC(true),
NAME_ASC(false), NAME_DESC(true),
WOOL_ASC(false), WOOL_DESC(true),
GLASS_ASC(false), GLASS_DESC(true),
BLOCK_ASC(false), BLOCK_DESC(true),
TOOL_ASC(false), TOOLS_DESC(true);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import org.bukkit.entity.Player
import org.bukkit.inventory.Inventory
import org.bukkit.inventory.ItemStack
import ru.astrainteractive.klibs.mikro.core.util.next
import java.util.*
import java.util.UUID

class SortController {

private val sortTypes = mutableMapOf<UUID, Sort>()

fun rememberPlayer(player: Player) {
sortTypes[player.uniqueId] = Sort.TYPE
sortTypes[player.uniqueId] = Sort.values().first()
}

fun removePlayer(player: Player) {
Expand All @@ -19,40 +20,42 @@ class SortController {

fun trySortInventory(clickedInventory: Inventory, player: Player) {
val contents = clickedInventory.storageContents
val prevSortType = sortTypes[player.uniqueId] ?: Sort.TYPE
sortTypes[player.uniqueId] =
if (!prevSortType.desc) prevSortType.apply { desc = !desc } else prevSortType.next(Sort.values())
clickedInventory.storageContents = when (prevSortType) {
Sort.TYPE -> sortByType(contents, prevSortType.desc)
Sort.NAME -> sortByName(contents, prevSortType.desc)
Sort.WOOL -> sortByWool(contents, prevSortType.desc)
Sort.GLASS -> sortByGlass(contents, prevSortType.desc)
Sort.BLOCK -> sortByBlock(contents, prevSortType.desc)
Sort.TOOL -> sortByTool(contents, prevSortType.desc)
}.reversedArray()
val sortType = sortTypes[player.uniqueId]?.next(Sort.values()) ?: Sort.values().first()
sortTypes[player.uniqueId] = sortType
println(sortType)
clickedInventory.storageContents = when (sortType) {
Sort.TYPE_ASC -> sortByType(contents, sortType.desc)
Sort.TYPE_DESC -> sortByType(contents, sortType.desc)
Sort.NAME_ASC -> sortByName(contents, sortType.desc)
Sort.NAME_DESC -> sortByName(contents, sortType.desc)
Sort.WOOL_ASC -> sortByWool(contents, sortType.desc)
Sort.WOOL_DESC -> sortByWool(contents, sortType.desc)
Sort.GLASS_ASC -> sortByGlass(contents, sortType.desc)
Sort.GLASS_DESC -> sortByGlass(contents, sortType.desc)
Sort.BLOCK_ASC -> sortByBlock(contents, sortType.desc)
Sort.BLOCK_DESC -> sortByBlock(contents, sortType.desc)
Sort.TOOL_ASC -> sortByTool(contents, sortType.desc)
Sort.TOOLS_DESC -> sortByTool(contents, sortType.desc)
}
}

private fun sortBy(
content: Array<out ItemStack?>,
desc: Boolean,
algorithm: () -> Comparator<ItemStack?>,
): Array<out ItemStack?> {
return if (desc) {
content.sortedArrayWith(algorithm())
} else {
content.sortedArrayWith(algorithm()).reversedArray()
}
): Array<out ItemStack?> = if (desc) {
content.sortedArrayWith(algorithm())
} else {
content.sortedArrayWith(algorithm()).reversedArray()
}

private fun sortByType(content: Array<out ItemStack?>, desc: Boolean) =
sortBy(content, desc) {
compareBy<ItemStack?> { it?.type }
}
private fun sortByType(content: Array<out ItemStack?>, desc: Boolean) = sortBy(content, desc) {
compareBy { it?.type }
}

private fun sortByName(content: Array<out ItemStack?>, desc: Boolean) =
sortBy(content, desc) {
compareBy { it?.itemMeta?.displayName }
}
private fun sortByName(content: Array<out ItemStack?>, desc: Boolean) = sortBy(content, desc) {
compareBy { it?.itemMeta?.displayName }
}

private fun sortByWool(content: Array<out ItemStack?>, desc: Boolean) = sortBy(content, desc) {
compareBy {
Expand All @@ -76,7 +79,6 @@ class SortController {
{ it?.type?.name?.contains("wood", ignoreCase = true) },
{ it?.type?.name?.contains("ore", ignoreCase = true) },
{ it?.type?.name?.contains("block", ignoreCase = true) },

)
}

Expand All @@ -91,32 +93,4 @@ class SortController {
{ it?.type?.name?.contains("hoe", ignoreCase = true) },
)
}

object ToolsComparable {
private fun ItemStack.typeContains(it: String): Boolean? = this.type.name.contains(it, ignoreCase = true)
val axe: (ItemStack?) -> Comparable<*>?
get() = { it?.typeContains("_axe") }
val pickaxe: (ItemStack?) -> Comparable<*>?
get() = { it?.typeContains("pickaxe") }
val hoe: (ItemStack?) -> Comparable<*>?
get() = { it?.typeContains("_hoe") }
val sword: (ItemStack?) -> Comparable<*>?
get() = { it?.typeContains("_sword") }
val shovel: (ItemStack?) -> Comparable<*>?
get() = { it?.typeContains("_shovel") }
}

object BlocksComparable {
private fun ItemStack.typeContains(it: String): Boolean? = this.type.name.contains(it, ignoreCase = true)
val ore: (ItemStack?) -> Comparable<*>?
get() = { it?.typeContains("_ore") }
val planks: (ItemStack?) -> Comparable<*>?
get() = { it?.typeContains("plank") }
val slab: (ItemStack?) -> Comparable<*>?
get() = { it?.typeContains("_slab") }
val fence: (ItemStack?) -> Comparable<*>?
get() = { it?.typeContains("_fence") }
val glass: (ItemStack?) -> Comparable<*>?
get() = { it?.typeContains("_glass") }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ class SortEvent(
sortController.removePlayer(e.player)
}
val inventoryClick = DSLEvent<InventoryClickEvent>(eventListener, plugin) { e ->
if (e.click != ClickType.MIDDLE) return@DSLEvent
if (!e.isShiftClick) return@DSLEvent
if (e.click != ClickType.SHIFT_RIGHT) return@DSLEvent
val clickedInventory = e.clickedInventory ?: return@DSLEvent
val player = e.whoClicked as? Player ?: return@DSLEvent
e.isCancelled = true
sortController.trySortInventory(clickedInventory, player)
}
}

0 comments on commit c1ffcfd

Please sign in to comment.