|
2 | 2 |
|
3 | 3 | package com.mairwunnx.projectessentials.core.api.v1.messaging |
4 | 4 |
|
| 5 | +import com.mairwunnx.projectessentials.core.api.v1.SETTING_LIST_MAX_ELEMENTS_IN_PAGE |
5 | 6 | import com.mairwunnx.projectessentials.core.api.v1.SETTING_LOC_ENABLED |
| 7 | +import com.mairwunnx.projectessentials.core.api.v1.commands.CommandAPI |
6 | 8 | 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 |
7 | 11 | import com.mairwunnx.projectessentials.core.api.v1.localization.LocalizationAPI |
8 | 12 | import com.mairwunnx.projectessentials.core.impl.configurations.GeneralConfiguration |
| 13 | +import com.mojang.brigadier.context.CommandContext |
| 14 | +import net.minecraft.command.CommandSource |
9 | 15 | import net.minecraft.entity.player.ServerPlayerEntity |
10 | 16 | import net.minecraft.server.MinecraftServer |
| 17 | +import net.minecraft.util.text.StringTextComponent |
11 | 18 | import net.minecraft.util.text.TextComponentUtils |
12 | 19 | import net.minecraft.util.text.TranslationTextComponent |
13 | 20 | import net.minecraft.util.text.event.ClickEvent |
@@ -64,6 +71,74 @@ object MessagingAPI { |
64 | 71 | } |
65 | 72 | ) |
66 | 73 |
|
| 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 | + |
67 | 142 | /** |
68 | 143 | * Send message to all player on server with localized |
69 | 144 | * string or simple message. |
|
0 commit comments