@@ -4,15 +4,14 @@ import kotlinx.coroutines.CoroutineScope
44import kotlinx.coroutines.launch
55import kotlinx.coroutines.withContext
66import net.kyori.adventure.text.Component
7+ import ru.astrainteractive.astralibs.command.api.brigadier.sender.ConsoleKCommandSender
78import ru.astrainteractive.astralibs.command.api.brigadier.sender.KCommandSender
9+ import ru.astrainteractive.astralibs.command.api.brigadier.sender.KPlayerKCommandSender
810import ru.astrainteractive.astralibs.kyori.KyoriComponentSerializer
911import ru.astrainteractive.astralibs.kyori.unwrap
10- import ru.astrainteractive.astralibs.server.KAudience
11- import ru.astrainteractive.astralibs.server.Locatable
1212import ru.astrainteractive.astralibs.server.location.KLocation
1313import ru.astrainteractive.astralibs.server.location.dist
1414import ru.astrainteractive.astralibs.server.permission.KPermissible
15- import ru.astrainteractive.astralibs.server.player.KPlayer
1615import ru.astrainteractive.astralibs.server.player.OnlineKPlayer
1716import ru.astrainteractive.astralibs.util.clickable
1817import ru.astrainteractive.astralibs.util.isEmpty
@@ -52,27 +51,34 @@ internal class SoulsCommandExecutor(
5251 }
5352
5453 private suspend fun getFilteredSouls (sender : KCommandSender ): List <DatabaseSoul > {
55- return soulsDao.getSouls()
54+ val souls = soulsDao.getSouls()
5655 .getOrNull()
5756 .orEmpty()
58- .filter { soul ->
59- sender.tryCast<Locatable >()
60- ?.getLocation()
61- ?.worldName == soul.location.worldName
62- }
63- .filter { soul ->
64- soul.isFree
65- .or (sender.tryCast<KPermissible >()?.hasPermission(PluginPermission .ViewAllSouls ) == true )
66- .or (sender.tryCast<KPlayer >()?.uuid == soul.ownerUUID)
57+ return when (sender) {
58+ is KPlayerKCommandSender -> {
59+ souls
60+ .filter { soul ->
61+ sender
62+ .instance
63+ .getLocation()
64+ .worldName == soul.location.worldName
65+ }
66+ .filter { soul ->
67+ soul.isFree
68+ .or (sender.instance.hasPermission(PluginPermission .ViewAllSouls ))
69+ .or (sender.instance.uuid == soul.ownerUUID)
70+ }
6771 }
72+
73+ is ConsoleKCommandSender -> souls
74+ }
6875 }
6976
7077 private fun getPageSouls (souls : List <DatabaseSoul >, page : Int ): List <DatabaseSoul > {
7178 val start = page.times(SoulsCommand .PAGE_SIZE ).coerceIn(0 , souls.size)
72- val end =
73- (page * SoulsCommand .PAGE_SIZE + SoulsCommand .PAGE_SIZE ).coerceAtMost(
74- souls.size
75- )
79+ val end = page.times(SoulsCommand .PAGE_SIZE )
80+ .plus(SoulsCommand .PAGE_SIZE )
81+ .coerceAtMost(souls.size)
7682 return if (start == end) {
7783 emptyList()
7884 } else if (end == 0 ) {
@@ -109,7 +115,7 @@ internal class SoulsCommandExecutor(
109115 private fun KCommandSender.canFreeSouls (soul : DatabaseSoul ): Boolean {
110116 val sender = this
111117 val hasPermission = sender.tryCast<KPermissible >()?.hasPermission(PluginPermission .FreeAllSouls ) == true
112- val isOwner = sender.tryCast<KPlayer >()?.uuid == soul.ownerUUID
118+ val isOwner = sender.tryCast<KPlayerKCommandSender >()?.instance ?.uuid == soul.ownerUUID
113119 if (soul.isFree) return false
114120 if (! hasPermission) return false
115121 if (! isOwner) return false
@@ -162,23 +168,25 @@ internal class SoulsCommandExecutor(
162168 is SoulsCommand .Intent .List -> {
163169 ioScope.launch {
164170 val filteredSouls = getFilteredSouls(input.sender)
171+ .also { println () }
165172 val maxPages = filteredSouls.size.div(SoulsCommand .PAGE_SIZE )
166173 val pageSouls = getPageSouls(filteredSouls, input.page)
167174 if (pageSouls.isEmpty()) {
168175 val title = translation.souls.noSoulsOnPage(input.page.plus(1 )).component
169- input.sender.tryCast< KAudience >()?. sendMessage(title)
176+ input.sender.sendMessage(title)
170177 return @launch
171178 }
172179
173- input.sender.tryCast< KAudience >()?. sendMessage(translation.souls.listSoulsTitle.component)
180+ input.sender.sendMessage(translation.souls.listSoulsTitle.component)
174181
175182 pageSouls.forEachIndexed { i, soul ->
176183 val component = createListingItemComponent(
177184 soul = soul,
178185 page = input.page,
179186 i = i,
180187 location = input.sender
181- .tryCast<Locatable >()
188+ .tryCast<KPlayerKCommandSender >()
189+ ?.instance
182190 ?.getLocation()
183191 ).append(
184192 addSpace = true ,
@@ -193,9 +201,9 @@ internal class SoulsCommandExecutor(
193201 soul = soul
194202 )
195203 )
196- input.sender.tryCast< KAudience >()?. sendMessage(component)
204+ input.sender.sendMessage(component)
197205 }
198- input.sender.tryCast< KAudience >()?. sendMessage(createPagingMessage(input, maxPages))
206+ input.sender.sendMessage(createPagingMessage(input, maxPages))
199207 }
200208 }
201209
@@ -205,22 +213,24 @@ internal class SoulsCommandExecutor(
205213 .getOrNull()
206214 ?.copy(isFree = true )
207215 if (newSoul == null ) {
208- input.sender.tryCast< KAudience >()?. sendMessage(translation.souls.soulNotFound.component)
216+ input.sender.sendMessage(translation.souls.soulNotFound.component)
209217 return @launch
210218 }
211219 soulsDao.updateSoul(newSoul)
212220 .onSuccess {
213- input.sender.tryCast< KAudience >()?. sendMessage(translation.souls.soulFreed.component)
221+ input.sender.sendMessage(translation.souls.soulFreed.component)
214222 }
215223 .onFailure {
216- input.sender.tryCast< KAudience >()?. sendMessage(translation.souls.couldNotFreeSoul.component)
224+ input.sender.sendMessage(translation.souls.couldNotFreeSoul.component)
217225 }
218226 }
219227 }
220228
221229 is SoulsCommand .Intent .TeleportToSoul -> {
222230 ioScope.launch {
223- val player = input.sender.tryCast<OnlineKPlayer >() ? : return @launch
231+ val player = input.sender.tryCast<KPlayerKCommandSender >()
232+ ?.instance
233+ ? : return @launch
224234 val location = soulsDao.getSoul(input.soulId)
225235 .getOrNull()
226236 ?.location
0 commit comments