-
Notifications
You must be signed in to change notification settings - Fork 12
Set Chat Name Color #662
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Set Chat Name Color #662
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| FROM eclipse-temurin:17-jdk-jammy | ||
|
|
||
| USER root | ||
|
|
||
| # set working directory | ||
| WORKDIR /workspaces/RPKit | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| REM This script is meant to be run from the .devcontainer directory. | ||
|
|
||
| docker build . -t rpk-dev-container | ||
| docker run -it -v %cd%\..\:/workspaces/RPKit rpk-dev-container /bin/bash |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # This script is meant to be run from the .devcontainer directory. | ||
|
|
||
| docker build . -t rpk-dev-container | ||
| docker run -it -v %cd%\..\:/workspaces/RPKit rpk-dev-container /bin/bash |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
...src/main/kotlin/com/rpkit/chat/bukkit/command/setchatnamecolor/SetChatNameColorCommand.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| /* | ||
| * Copyright 2020 Ren Binden | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.rpkit.chat.bukkit.command.setchatnamecolor | ||
|
|
||
| import com.rpkit.chat.bukkit.RPKChatBukkit | ||
| import com.rpkit.chat.bukkit.database.table.RPKChatNameColorTable | ||
| import com.rpkit.core.service.Services | ||
| import com.rpkit.players.bukkit.profile.minecraft.RPKMinecraftProfileId | ||
| import com.rpkit.players.bukkit.profile.minecraft.RPKMinecraftProfileService | ||
| import org.bukkit.command.Command | ||
| import org.bukkit.command.CommandExecutor | ||
| import org.bukkit.command.CommandSender | ||
| import org.bukkit.entity.Player | ||
| import java.util.concurrent.CompletableFuture | ||
| import java.util.concurrent.CompletableFuture.runAsync | ||
|
|
||
| /** | ||
| * SetChatNameColor command. | ||
| * Sets the chat color name for a player. | ||
| */ | ||
| class SetChatNameColorCommand(private val plugin: RPKChatBukkit) : CommandExecutor { | ||
|
|
||
| override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array<out String>): Boolean { | ||
| if (!sender.hasPermission("rpkit.chat.command.setchatnamecolor")) { | ||
| sender.sendMessage(plugin.messages["no-permission-setchatnamecolor"]) | ||
dmccoystephenson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return true | ||
| } | ||
| if (sender !is Player) { | ||
| sender.sendMessage(plugin.messages["not-from-console"]) | ||
| return true | ||
| } | ||
| val minecraftProfileService = Services[RPKMinecraftProfileService::class.java] | ||
| if (minecraftProfileService == null) { | ||
| sender.sendMessage(plugin.messages["no-minecraft-profile-service"]) | ||
| return true | ||
| } | ||
| val minecraftProfile = minecraftProfileService.getPreloadedMinecraftProfile(sender) | ||
| if (minecraftProfile == null) { | ||
| sender.sendMessage(plugin.messages["no-minecraft-profile"]) | ||
| return true | ||
| } | ||
|
|
||
| // if no arguments | ||
| if (args.isEmpty()) { | ||
| sender.sendMessage(plugin.messages["setchatnamecolor-usage"]) | ||
| return true | ||
| } | ||
|
|
||
| // retrieve desired chat name color | ||
| val chatNameColor = args[0] | ||
| if (isInputTooLong(chatNameColor)) { | ||
dmccoystephenson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| sender.sendMessage(plugin.messages["setchatnamecolor-too-long"]) | ||
| return true | ||
| } | ||
|
|
||
| if (!isHexColorCodeValid(chatNameColor)) { | ||
| sender.sendMessage(plugin.messages["setchatnamecolor-invalid-color-code"]) | ||
| return true | ||
| } | ||
|
|
||
| val minecraftProfileId = minecraftProfile.id | ||
| if (minecraftProfileId == null) { | ||
| sender.sendMessage(plugin.messages["no-minecraft-profile"]) | ||
| return true | ||
| } | ||
|
|
||
| setChatNameColorAsync(minecraftProfileId, chatNameColor).thenRun { | ||
| sender.sendMessage(plugin.messages["setchatnamecolor-chat-name-color-has-been-set"]) | ||
| }.exceptionally { exception -> | ||
| plugin.logger.severe("Failed to set chat name color for ${minecraftProfile.name}") | ||
| plugin.logger.severe(exception.message) | ||
dmccoystephenson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| sender.sendMessage(plugin.messages["setchatnamecolor-something-went-wrong"]) | ||
| return@exceptionally null | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| /** | ||
| * Checks if the input is too long. The maximum length for a chat name color is 16 characters. | ||
| * @param name the input to check | ||
| * @return true if the input is too long, false otherwise | ||
| */ | ||
| private fun isInputTooLong(name: String): Boolean { | ||
| return name.length > 16 | ||
dmccoystephenson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * Checks if the input is a valid hex color code. A valid hex color code is a string that starts with a '#' followed by 6 characters that are either digits or letters from a to f. | ||
| * @param colorCode the color code to check | ||
| * @return true if the color code is valid, false otherwise | ||
| */ | ||
| private fun isHexColorCodeValid(colorCode: String): Boolean { | ||
| return colorCode.matches(Regex("#[0-9a-fA-F]{6}")) | ||
| } | ||
|
|
||
| /** | ||
| * Sets the chat name color for a player asynchronously. | ||
| * @param minecraftProfileId the ID of the player's Minecraft profile | ||
| * @param chatNameColor the chat name color to set | ||
| * @return a string indicating the result of the operation | ||
| */ | ||
| private fun setChatNameColorAsync(minecraftProfileId: RPKMinecraftProfileId, chatNameColor: String) : CompletableFuture<Void> { | ||
| return runAsync { | ||
| val recordExists = plugin.database.getTable(RPKChatNameColorTable::class.java)[minecraftProfileId].join() != null | ||
| if (recordExists) { | ||
| plugin.database.getTable(RPKChatNameColorTable::class.java).update(minecraftProfileId, chatNameColor) | ||
dmccoystephenson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } else { | ||
| plugin.database.getTable(RPKChatNameColorTable::class.java).insert(minecraftProfileId, chatNameColor) | ||
| } | ||
| } | ||
| } | ||
| } | ||
92 changes: 92 additions & 0 deletions
92
...chat-bukkit/src/main/kotlin/com/rpkit/chat/bukkit/database/table/RPKChatNameColorTable.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /* | ||
| * Copyright 2021 Ren Binden | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.rpkit.chat.bukkit.database.table | ||
|
|
||
| import com.rpkit.chat.bukkit.RPKChatBukkit | ||
| import com.rpkit.chat.bukkit.database.create | ||
| import com.rpkit.chat.bukkit.database.jooq.Tables.RPKIT_CHAT_NAME_COLOR | ||
| import com.rpkit.core.database.Database | ||
| import com.rpkit.core.database.Table | ||
| import java.util.concurrent.CompletableFuture | ||
| import com.rpkit.chat.bukkit.database.jooq.tables.records.RpkitChatNameColorRecord | ||
| import com.rpkit.players.bukkit.profile.minecraft.RPKMinecraftProfileId | ||
| import java.util.concurrent.CompletableFuture.runAsync | ||
| import java.util.logging.Level.SEVERE | ||
|
|
||
| /** | ||
| * Represents the chat name color table. | ||
| */ | ||
| class RPKChatNameColorTable(private val database: Database, private val plugin: RPKChatBukkit) : Table { | ||
|
|
||
| fun insert(id: RPKMinecraftProfileId?, chatNameColor: String): CompletableFuture<Void> { | ||
| // insert id -> chat name color record into database | ||
| return runAsync { | ||
| database.create | ||
| .insertInto( | ||
| RPKIT_CHAT_NAME_COLOR, | ||
| RPKIT_CHAT_NAME_COLOR.MINECRAFT_PROFILE_ID, | ||
| RPKIT_CHAT_NAME_COLOR.CHAT_NAME_COLOR | ||
| ) | ||
| .values( | ||
| id?.value, | ||
| chatNameColor | ||
| ) | ||
| .execute() | ||
| }.exceptionally { e -> | ||
| plugin.logger.log(SEVERE, "Failed to insert chat name color record for Minecraft profile ${id?.value} and chat name color $chatNameColor", e) | ||
| throw e | ||
| } | ||
| } | ||
|
|
||
| fun update(minecraftProfileId: RPKMinecraftProfileId, chatNameColor: String): CompletableFuture<Void> { | ||
| // update chat name color record in database | ||
| return runAsync { | ||
| database.create | ||
| .update(RPKIT_CHAT_NAME_COLOR) | ||
| .set(RPKIT_CHAT_NAME_COLOR.CHAT_NAME_COLOR, chatNameColor) | ||
| .where(RPKIT_CHAT_NAME_COLOR.MINECRAFT_PROFILE_ID.eq(minecraftProfileId.value)) | ||
| .execute() | ||
| }.exceptionally { e -> | ||
| plugin.logger.log(SEVERE, "Failed to update chat name color record for Minecraft profile ${minecraftProfileId.value} and chat name color $chatNameColor", e) | ||
| throw e | ||
| } | ||
| } | ||
|
|
||
| operator fun get(id: RPKMinecraftProfileId): CompletableFuture<RpkitChatNameColorRecord?> { | ||
| // get chat name color record from database by id | ||
| return CompletableFuture.supplyAsync { | ||
dmccoystephenson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| database.create | ||
| .selectFrom(RPKIT_CHAT_NAME_COLOR) | ||
| .where(RPKIT_CHAT_NAME_COLOR.MINECRAFT_PROFILE_ID.eq(id.value)) | ||
| .fetchOne() | ||
| }.exceptionally { e -> | ||
| plugin.logger.log(SEVERE, "Failed to get chat name color record for Minecraft profile ${id.value}", e) | ||
| null | ||
| } | ||
| } | ||
|
|
||
| fun delete(): CompletableFuture<Void> { | ||
| // delete chat name color record from database | ||
| return runAsync { | ||
| database.create | ||
| .deleteFrom(RPKIT_CHAT_NAME_COLOR) | ||
| .execute() | ||
| }.exceptionally { e -> | ||
| plugin.logger.log(SEVERE, "Failed to delete chat name color records", e) | ||
| throw e | ||
| } | ||
| } | ||
| } | ||
22 changes: 22 additions & 0 deletions
22
...k-chat-bukkit/src/main/resources/com/rpkit/chat/migrations/mysql/V2__Chat_name_colors.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /* | ||
| * Copyright 2020 Ren Binden | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| -- table to keep track of profile id -> chat name color | ||
| CREATE TABLE `rpkit_chat_name_color` | ||
| ( | ||
| `minecraft_profile_id` int NOT NULL, | ||
dmccoystephenson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| `chat_name_color` varchar(256) NOT NULL | ||
| ); | ||
22 changes: 22 additions & 0 deletions
22
...-chat-bukkit/src/main/resources/com/rpkit/chat/migrations/sqlite/V2__Chat_name_colors.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /* | ||
| * Copyright 2020 Ren Binden | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| -- table to keep track of profile id -> chat name color | ||
| CREATE TABLE `rpkit_chat_name_color` | ||
| ( | ||
| `minecraft_profile_id` int NOT NULL, | ||
dmccoystephenson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| `chat_name_color` varchar(256) NOT NULL | ||
| ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.