Skip to content

Commit cafd996

Browse files
committed
sendListAsMessage implemented in MessagingAPI.kt.
Version updated to RC.3. Signed-off-by: Pavel Erokhin (MairwunNx) <MairwunNx@gmail.com>
1 parent 9f5537d commit cafd996

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ kotlin.code.style=official
1616
kotlin_version=1.3.72
1717
kotlinx_serialization_version=0.20.0
1818
# Module informatation.
19-
module_version=2.0.0-RC.2+MC-1.14.4
19+
module_version=2.0.0-RC.3+MC-1.14.4
2020
module_name=Project Essentials Core
2121
module_id=project_essentials_core
2222
module_vendor=MairwunNx (Pavel Erokhin)

src/main/kotlin/com/mairwunnx/projectessentials/core/api/v1/messaging/MessagingAPI.kt

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@
22

33
package com.mairwunnx.projectessentials.core.api.v1.messaging
44

5+
import com.mairwunnx.projectessentials.core.api.v1.SETTING_LIST_MAX_ELEMENTS_IN_PAGE
56
import com.mairwunnx.projectessentials.core.api.v1.SETTING_LOC_ENABLED
7+
import com.mairwunnx.projectessentials.core.api.v1.commands.CommandAPI
68
import com.mairwunnx.projectessentials.core.api.v1.configuration.ConfigurationAPI.getConfigurationByName
9+
import com.mairwunnx.projectessentials.core.api.v1.extensions.getPlayer
10+
import com.mairwunnx.projectessentials.core.api.v1.extensions.isPlayerSender
711
import com.mairwunnx.projectessentials.core.api.v1.localization.LocalizationAPI
812
import com.mairwunnx.projectessentials.core.impl.configurations.GeneralConfiguration
13+
import com.mojang.brigadier.context.CommandContext
14+
import net.minecraft.command.CommandSource
915
import net.minecraft.entity.player.ServerPlayerEntity
1016
import net.minecraft.server.MinecraftServer
17+
import net.minecraft.util.text.StringTextComponent
1118
import net.minecraft.util.text.TextComponentUtils
1219
import net.minecraft.util.text.TranslationTextComponent
1320
import net.minecraft.util.text.event.ClickEvent
@@ -64,6 +71,74 @@ object MessagingAPI {
6471
}
6572
)
6673

74+
/**
75+
* Send list like response to player in chat.
76+
* With pages (passed as argument in [context])
77+
* with name `page`.
78+
*
79+
* Max displayed lines per page defined in
80+
* setting `list-max-elements-in-page`.
81+
*
82+
* @param context command context.
83+
* @param list list to display in server console.
84+
* @param title list title, list name or something like that.
85+
* @since 2.0.0-RC.3.
86+
*/
87+
fun sendListAsMessage(
88+
context: CommandContext<CommandSource>,
89+
list: List<String>,
90+
title: () -> String
91+
) {
92+
require(context.isPlayerSender()) {
93+
"Command sender is no player, use [ServerMessagingAPI.listAsResponse] for server"
94+
}
95+
val linesPerPage = generalConfiguration.getIntOrDefault(
96+
SETTING_LIST_MAX_ELEMENTS_IN_PAGE, 8
97+
)
98+
val pages = list.count() / linesPerPage + 1
99+
val page = when {
100+
CommandAPI.getIntExisting(context, "page") -> {
101+
CommandAPI.getInt(context, "page")
102+
}
103+
else -> 1
104+
}
105+
sendListAsMessage(context.getPlayer()!!, list, page, pages, linesPerPage, title)
106+
}
107+
108+
/**
109+
* Send list like response to player in chat.
110+
*
111+
* Max displayed lines per page defined in
112+
* setting `list-max-elements-in-page`.
113+
*
114+
* @param player player to send list.
115+
* @param list list to display in server console.
116+
* @param page list page. (unchecked page!)
117+
* @param maxPage pages total to can be displayed.
118+
* @param linesPerPage lines per page.
119+
* @param title list title, list name or something like that.
120+
* @since 2.0.0-RC.3.
121+
*/
122+
fun sendListAsMessage(
123+
player: ServerPlayerEntity,
124+
list: List<String>,
125+
page: Int,
126+
maxPage: Int,
127+
linesPerPage: Int,
128+
title: () -> String
129+
) {
130+
val displayedLines = page * linesPerPage
131+
val droppedLines = displayedLines - linesPerPage
132+
val values = list.take(displayedLines).drop(droppedLines)
133+
val message =
134+
"""
135+
§7$title page §c$page §7of §c$maxPage
136+
137+
§7${values.joinToString(separator = "\n") { " §c> §7$it" }}
138+
"""
139+
player.commandSource.sendFeedback(StringTextComponent(message), false)
140+
}
141+
67142
/**
68143
* Send message to all player on server with localized
69144
* string or simple message.

0 commit comments

Comments
 (0)