Skip to content

Commit

Permalink
Add: message builder
Browse files Browse the repository at this point in the history
  • Loading branch information
vulcandragi committed Sep 9, 2024
1 parent 8c89052 commit c2f7cdb
Show file tree
Hide file tree
Showing 12 changed files with 226 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.fardragi.nyaruko.auth.commands

import com.fardragi.nyaruko.auth.messages.LoginCheckFailMessage
import com.fardragi.nyaruko.auth.messages.LoginCheckSuccessMessage
import com.fardragi.nyaruko.enums.PermissionLevel
import com.fardragi.nyaruko.services.UserService
import com.fardragi.nyaruko.shared.commands.NyarukoCommandBase
import com.fardragi.nyaruko.shared.messages.FailCommandMessage
import com.fardragi.nyaruko.shared.messages.DefaultMessage
import net.minecraft.command.ICommandSender
import net.minecraft.entity.player.EntityPlayerMP
import net.minecraft.util.ChatComponentText
Expand All @@ -16,22 +15,22 @@ class LoginCommand(private val userService: UserService) : NyarukoCommandBase()
}

override fun getCommandUsage(sender: ICommandSender?): String {
return "/login <password>"
return "/login <senha>"
}

override suspend fun processCommandPlayer(player: EntityPlayerMP, args: Array<out String>): ChatComponentText {
override suspend fun processCommandPlayer(player: EntityPlayerMP, args: Array<out String>): Array<ChatComponentText> {
if (args.isEmpty()) {
return FailCommandMessage.create()
return arrayOf(DefaultMessage.usage(getCommandUsage(player), commandName))
}

val userId = player.uniqueID.toString()
val password = args[0]

if (!userService.checkPassword(userId, password)) {
return LoginCheckFailMessage.create()
return arrayOf(DefaultMessage.error("Senha incorreta"))
}

return LoginCheckSuccessMessage.create()
return arrayOf(LoginCheckSuccessMessage.create())
}

override fun getRequiredPermissionLevel(): Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.fardragi.nyaruko.auth.messages.LoginMessage
import com.fardragi.nyaruko.enums.PermissionLevel
import com.fardragi.nyaruko.services.UserService
import com.fardragi.nyaruko.shared.commands.NyarukoCommandBase
import com.fardragi.nyaruko.shared.messages.FailCommandMessage
import com.fardragi.nyaruko.shared.messages.DefaultMessage
import net.minecraft.command.ICommandSender
import net.minecraft.entity.player.EntityPlayerMP
import net.minecraft.util.ChatComponentText
Expand All @@ -16,12 +16,12 @@ class RegisterCommand(private val userService: UserService) : NyarukoCommandBase
}

override fun getCommandUsage(sender: ICommandSender?): String {
return "/register <password> <repeat password>"
return "/register <senha> <repetir senha>"
}

override suspend fun processCommandPlayer(player: EntityPlayerMP, args: Array<out String>): ChatComponentText {
override suspend fun processCommandPlayer(player: EntityPlayerMP, args: Array<out String>): Array<ChatComponentText> {
if (args.isEmpty() || args.size < 2) {
return FailCommandMessage.create()
return arrayOf(DefaultMessage.usage(getCommandUsage(player), commandName))
}

val userId = player.uniqueID.toString()
Expand All @@ -31,16 +31,16 @@ class RegisterCommand(private val userService: UserService) : NyarukoCommandBase
val user = userService.getById(userId)

if (user.isRegistered) {
return AlreadyRegisteredMessage.create()
return arrayOf(AlreadyRegisteredMessage.create())
}

if (password != repeatPassword) {
FailCommandMessage.create("Password not match")
return arrayOf(DefaultMessage.error("As senhas não são iguais"))
}

userService.setPassword(userId, repeatPassword)

return LoginMessage.create()
return arrayOf(LoginMessage.create())
}

override fun getRequiredPermissionLevel(): Int {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.fardragi.nyaruko.auth.handlers

import com.fardragi.nyaruko.auth.messages.LoginMessage
import com.fardragi.nyaruko.auth.messages.RegisterMessage
import com.fardragi.nyaruko.auth.messages.WelcomeMessage
import com.fardragi.nyaruko.extensions.addChatMessages
import com.fardragi.nyaruko.services.UserService
import cpw.mods.fml.common.eventhandler.EventPriority
import cpw.mods.fml.common.eventhandler.SubscribeEvent
Expand All @@ -23,12 +23,8 @@ class LoginHandler(private val userService: UserService) {

delay(2000)

val message = if (user.isRegistered)
LoginMessage.create()
else
RegisterMessage.create()

player.addChatMessage(message)
val welcome = WelcomeMessage.create(userName, user.isRegistered)
player.addChatMessages(welcome)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package com.fardragi.nyaruko.auth.messages

import net.minecraft.event.ClickEvent
import net.minecraft.event.HoverEvent
import net.minecraft.util.ChatComponentText
import net.minecraft.util.ChatStyle
import net.minecraft.util.EnumChatFormatting

object LoginMessage {
fun create(): ChatComponentText {
val text = ChatComponentText("use /login <password>");
val command = ChatComponentText("/login <password>")
command.chatStyle = ChatStyle()
.setChatClickEvent(ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/login"))
.setChatHoverEvent(HoverEvent(HoverEvent.Action.SHOW_TEXT, ChatComponentText("/login")))

val text = ChatComponentText("use ")
text.chatStyle = ChatStyle().setColor(EnumChatFormatting.YELLOW)
text.appendSibling(command)

return text
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.fardragi.nyaruko.auth.messages

import com.fardragi.nyaruko.shared.messages.MessageBuilder
import net.minecraft.event.ClickEvent
import net.minecraft.util.ChatComponentText
import net.minecraft.util.EnumChatFormatting

object WelcomeMessage {
fun create(name: String, isRegistered: Boolean): Array<ChatComponentText> {
val messageBuilder = MessageBuilder()
.addDividerLine()
.add { builder ->
builder.append(" ")
builder.append("123", EnumChatFormatting.AQUA) {
it.obfuscated()
}
builder.append(" Seja bem vindo ao servidor ")
builder.append("123", EnumChatFormatting.AQUA) {
it.obfuscated()
}
}
.addEmptyLine()
.add { builder ->
if (isRegistered) {
builder.append("Logue usando /login <senha>", EnumChatFormatting.YELLOW)
} else {
builder.append("Registre-se usando /register <senha> <repetir senha>", EnumChatFormatting.YELLOW)
}
}
.addEmptyLine()
.add { builder ->
builder.append("Discord: ", EnumChatFormatting.DARK_PURPLE)
builder.append("https://discord.gg/8KxJNzTJzq", EnumChatFormatting.BLUE) {
it.clickEvent(ClickEvent.Action.OPEN_URL, "https://discord.gg/8KxJNzTJzq")
}
}
.addDividerLine()

return messageBuilder.build()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.fardragi.nyaruko.extensions

import net.minecraft.command.ICommandSender
import net.minecraft.entity.player.EntityPlayer
import net.minecraft.util.ChatComponentText

fun EntityPlayer.addChatMessages(messages: Array<ChatComponentText>) {
messages.forEach {
this.addChatMessage(it)
}
}

fun ICommandSender.addChatMessages(messages: Array<ChatComponentText>) {
messages.forEach {
this.addChatMessage(it)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.fardragi.nyaruko.shared.commands

import com.fardragi.nyaruko.NyarukoLog
import com.fardragi.nyaruko.shared.messages.FailCommandMessage
import com.fardragi.nyaruko.extensions.addChatMessages
import com.fardragi.nyaruko.shared.messages.DefaultMessage
import com.fardragi.nyaruko.shared.messages.NotImplementedCommandMessage
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand All @@ -22,23 +23,23 @@ abstract class NyarukoCommandBase : CommandBase() {
else -> processCommandConsole(sender, args)
}

sender.addChatMessage(message)
sender.addChatMessages(message)
} catch (e: Exception) {
e.message?.let { NyarukoLog.error(it) }
sender.addChatMessage(FailCommandMessage.create())
sender.addChatMessage(DefaultMessage.error("Falha em executar o comando"))
}
}
}

open suspend fun processCommandPlayer(player: EntityPlayerMP, args: Array<out String>): ChatComponentText {
return NotImplementedCommandMessage.create()
open suspend fun processCommandPlayer(player: EntityPlayerMP, args: Array<out String>): Array<ChatComponentText> {
return arrayOf(NotImplementedCommandMessage.create())
}

open suspend fun processCommandBlock(commandBlock: CommandBlockLogic, args: Array<out String>): ChatComponentText {
return NotImplementedCommandMessage.create()
open suspend fun processCommandBlock(commandBlock: CommandBlockLogic, args: Array<out String>): Array<ChatComponentText> {
return arrayOf(NotImplementedCommandMessage.create())
}

open suspend fun processCommandConsole(sender: ICommandSender, args: Array<out String>): ChatComponentText {
return NotImplementedCommandMessage.create()
open suspend fun processCommandConsole(sender: ICommandSender, args: Array<out String>): Array<ChatComponentText> {
return arrayOf(NotImplementedCommandMessage.create())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.fardragi.nyaruko.shared.messages

import net.minecraft.event.ClickEvent
import net.minecraft.util.ChatComponentText
import net.minecraft.util.ChatStyle
import net.minecraft.util.EnumChatFormatting

object DefaultMessage {
fun usage(usage: String, command: String): ChatComponentText {
val textComponent = ChatComponentText("Falha em executar o comando")
textComponent.chatStyle = ChatStyle().setColor(EnumChatFormatting.RED)

val usageComponent = ChatComponentText("[$usage]")
usageComponent.chatStyle = ChatStyle().setColor(EnumChatFormatting.GOLD)
.setChatClickEvent(ClickEvent(ClickEvent.Action.RUN_COMMAND, "/help $command"))

textComponent.appendSibling(usageComponent)

return textComponent
}

fun error(message: String): ChatComponentText {
val textComponent = ChatComponentText(message)
textComponent.chatStyle = ChatStyle().setColor(EnumChatFormatting.RED)

return textComponent
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.fardragi.nyaruko.shared.messages

import net.minecraft.util.ChatComponentText
import net.minecraft.util.EnumChatFormatting

class MessageBuilder {
private val messages = mutableListOf<ChatComponentText>()

fun build(): Array<ChatComponentText> {
return messages.toTypedArray()
}

fun add(block: (TextBuilder) -> Unit): MessageBuilder {
val textBuilder = TextBuilder()
block(textBuilder)
messages.add(textBuilder.build())

return this
}

fun addEmptyLine(): MessageBuilder {
add { builder ->
builder.append("")
}

return this
}

fun addDividerLine(): MessageBuilder {
add { builder ->
builder.append("-----------------------------------------------------", EnumChatFormatting.GOLD)
.strikethrough()
}

return this
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.fardragi.nyaruko.shared.messages

import net.minecraft.event.ClickEvent
import net.minecraft.util.ChatComponentText
import net.minecraft.util.ChatStyle
import net.minecraft.util.EnumChatFormatting

class TextBuilder {
private val componentText = ChatComponentText("").apply {
chatStyle = ChatStyle()
}

fun build(): ChatComponentText {
return componentText
}

fun append(
text: String,
color: EnumChatFormatting = EnumChatFormatting.RESET
): TextBuilder {
val textBuilder = TextBuilder()
textBuilder.text(text, color)

componentText.appendSibling(textBuilder.build())

return this
}

fun append(
text: String,
color: EnumChatFormatting = EnumChatFormatting.RESET,
block: (TextBuilder) -> Unit
): TextBuilder {
val textBuilder = TextBuilder()
textBuilder.text(text, color)
block(textBuilder)

componentText.appendSibling(textBuilder.build())

return this
}

private fun text(text: String, color: EnumChatFormatting = EnumChatFormatting.RESET): TextBuilder {
componentText.appendText(text)
componentText.chatStyle.color = color

return this
}

fun obfuscated(): TextBuilder {
componentText.chatStyle.obfuscated = true

return this
}

fun strikethrough(): TextBuilder {
componentText.chatStyle.strikethrough = true

return this
}

fun clickEvent(action: ClickEvent.Action, value: String): TextBuilder {
componentText.chatStyle.chatClickEvent = ClickEvent(action, value)

return this
}
}
1 change: 0 additions & 1 deletion src/main/kotlin/com/fardragi/nyaruko/utils/Minecraft.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@ object Minecraft {
return File(baseDir, "/DragiUtils")
}
}

0 comments on commit c2f7cdb

Please sign in to comment.