connection util: send chat message

This commit is contained in:
Bixilon 2022-02-11 20:21:35 +01:00
parent 54a70e6665
commit 417408b2ea
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
4 changed files with 27 additions and 27 deletions

View File

@ -23,7 +23,6 @@ import de.bixilon.minosoft.gui.rendering.gui.gui.GUIBuilder
import de.bixilon.minosoft.gui.rendering.gui.gui.screen.menu.Menu
import de.bixilon.minosoft.gui.rendering.gui.hud.elements.LayoutedGUIElement
import de.bixilon.minosoft.modding.event.EventInitiators
import de.bixilon.minosoft.protocol.packets.c2s.play.chat.ChatMessageC2SP
import glm_.vec2.Vec2i
import glm_.vec3.Vec3d
@ -33,7 +32,7 @@ class DebugMenu(guiRenderer: GUIRenderer) : Menu(guiRenderer) {
init {
add(TextElement(guiRenderer, "Debug options", HorizontalAlignments.CENTER, false))
add(SpacerElement(guiRenderer, Vec2i(0, 10)))
add(ButtonElement(guiRenderer, "Switch to next gamemode") { connection.sendPacket(ChatMessageC2SP("/gamemode ${connection.player.gamemode.next().name.lowercase()}")) })
add(ButtonElement(guiRenderer, "Switch to next gamemode") { connection.util.sendChatMessage("/gamemode ${connection.player.gamemode.next().name.lowercase()}") })
add(ButtonElement(guiRenderer, "Hack to next gamemode") {
val previous = connection.player.tabListItem.gamemode
val next = previous.next()

View File

@ -37,7 +37,6 @@ import de.bixilon.minosoft.gui.rendering.system.window.KeyChangeTypes
import de.bixilon.minosoft.modding.event.events.ChatMessageReceiveEvent
import de.bixilon.minosoft.modding.event.events.InternalMessageReceiveEvent
import de.bixilon.minosoft.modding.event.invoker.CallbackEventInvoker
import de.bixilon.minosoft.protocol.packets.c2s.play.chat.ChatMessageC2SP
import de.bixilon.minosoft.util.KUtil.toResourceLocation
import glm_.vec2.Vec2d
import glm_.vec2.Vec2i
@ -142,8 +141,9 @@ class ChatElement(guiRenderer: GUIRenderer) : Element(guiRenderer), LayoutedElem
}
private fun submit() {
if (input.value.isNotBlank()) {
connection.sendPacket(ChatMessageC2SP(input.value))
val value = input.value
if (value.isNotBlank()) {
connection.util.sendChatMessage(value)
}
input.value = ""
guiRenderer.gui.pop()

View File

@ -17,7 +17,10 @@ import de.bixilon.minosoft.data.text.BaseComponent
import de.bixilon.minosoft.data.text.ChatColors
import de.bixilon.minosoft.data.text.ChatComponent
import de.bixilon.minosoft.gui.rendering.RenderConstants
import de.bixilon.minosoft.modding.event.events.ChatMessageSendEvent
import de.bixilon.minosoft.modding.event.events.InternalMessageReceiveEvent
import de.bixilon.minosoft.protocol.packets.c2s.play.chat.ChatMessageC2SP
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition
import de.bixilon.minosoft.util.logging.Log
import de.bixilon.minosoft.util.logging.LogLevels
import de.bixilon.minosoft.util.logging.LogMessageType
@ -30,4 +33,23 @@ class ConnectionUtil(
connection.fireEvent(InternalMessageReceiveEvent(connection, BaseComponent(RenderConstants.DEBUG_MESSAGES_PREFIX, ChatComponent.of(message).apply { applyDefaultColor(ChatColors.BLUE) })))
Log.log(LogMessageType.CHAT_IN, LogLevels.INFO) { message }
}
fun sendChatMessage(message: String) {
var toSend = message
// remove prefixed spaces
while (toSend.startsWith(' ')) {
toSend = toSend.removeRange(0, 1)
}
if (message.isBlank()) {
throw IllegalArgumentException("Chat message can not be blank!")
}
if (message.contains(ProtocolDefinition.TEXT_COMPONENT_SPECIAL_PREFIX_CHAR)) {
throw IllegalArgumentException("Chat message can not contain chat formatting (${ProtocolDefinition.TEXT_COMPONENT_SPECIAL_PREFIX_CHAR}): $toSend")
}
if (connection.fireEvent(ChatMessageSendEvent(connection, toSend))) {
return
}
Log.log(LogMessageType.CHAT_OUT) { toSend }
connection.sendPacket(ChatMessageC2SP(toSend))
}
}

View File

@ -19,11 +19,6 @@ import de.bixilon.minosoft.data.commands.CommandNode;
import de.bixilon.minosoft.data.commands.CommandRootNode;
import de.bixilon.minosoft.data.commands.parser.MessageParser;
import de.bixilon.minosoft.data.commands.parser.exceptions.CommandParseException;
import de.bixilon.minosoft.modding.event.events.ChatMessageSendEvent;
import de.bixilon.minosoft.protocol.packets.c2s.play.chat.ChatMessageC2SP;
import de.bixilon.minosoft.util.logging.Log;
import de.bixilon.minosoft.util.logging.LogMessageType;
import org.apache.commons.lang3.StringUtils;
public class CommandSendChat extends Command {
public static final char[] ILLEGAL_CHAT_CHARS = {'§'};
@ -47,23 +42,7 @@ public class CommandSendChat extends Command {
// return;
}
}
if (StringUtils.isBlank(message)) {
// throw new IllegalArgumentException(("Chat message is blank!"));
return;
}
for (char illegalChar : ILLEGAL_CHAT_CHARS) {
if (message.indexOf(illegalChar) != -1) {
// throw new IllegalArgumentException(String.format("%s is not allowed in chat", illegalChar));
return;
}
}
ChatMessageSendEvent event = new ChatMessageSendEvent(connection, message);
if (connection.fireEvent(event)) {
return;
}
Log.log(LogMessageType.CHAT_OUT, message);
connection.sendPacket(new ChatMessageC2SP(event.getMessage()));
connection.getUtil().sendChatMessage(message);
})));
return parent;
}