diff --git a/src/main/java/org/altart/telegrambridge/bot/feature/MessageListener.java b/src/main/java/org/altart/telegrambridge/bot/feature/MessageListener.java index 165f471..7927694 100644 --- a/src/main/java/org/altart/telegrambridge/bot/feature/MessageListener.java +++ b/src/main/java/org/altart/telegrambridge/bot/feature/MessageListener.java @@ -64,13 +64,7 @@ public void onUpdateReceived(@NotNull Update update) { Message message = update.getMessage(); String text = message.getText(); if (text != null && !text.startsWith("/") && TelegramBridge.config.sendToChat) { - String username = message.getFrom().getUserName(); - if (username == null) { - username = String.format("%s %s", message.getFrom().getFirstName(), message.getFrom().getLastName()); - } - if (username.trim().isEmpty()) { - username = "Anonymous"; - } + String username = getUserName(message); TelegramBridge.log.info("Telegram message received from " + username + ": " + text); for (Player player : Bukkit.getOnlinePlayers()) { if (player.hasPermission(Permissions.RECEIVE.getString())) { @@ -118,6 +112,24 @@ public void onUpdateReceived(@NotNull Update update) { } } + private static @NotNull String getUserName(Message message) { + String username = message.getFrom().getUserName(); + if (username == null) { + String firstName = message.getFrom().getFirstName(); + String lastName = message.getFrom().getLastName(); + username = firstName; + if (lastName != null) { + username += " " + message.getFrom().getLastName(); + } + } + + if (username.trim().isEmpty()) { + username = "Anonymous"; + } + + return username; + } + @SuppressWarnings("deprecation") private static TextComponent replyComponent(String username, String message, @Nullable String lang) { HashMap replyValues = makeMessageMap(username, shrinkReplyText(message)); diff --git a/src/main/java/org/altart/telegrambridge/utils/Format.java b/src/main/java/org/altart/telegrambridge/utils/Format.java index a9f9fef..55f9456 100644 --- a/src/main/java/org/altart/telegrambridge/utils/Format.java +++ b/src/main/java/org/altart/telegrambridge/utils/Format.java @@ -1,21 +1,25 @@ package org.altart.telegrambridge.utils; -import org.altart.telegrambridge.TelegramBridge; +import org.jetbrains.annotations.NotNull; import java.util.HashMap; import java.util.Map; public class Format { - public static String string(String format, HashMap values) { + public static String string(@NotNull String format, @NotNull HashMap values) { String result = format; result = result.replace("\\n", "\n"); for (Map.Entry entry : values.entrySet()) { - result = result.replace("%" + entry.getKey() + "%", entry.getValue()); + String value = entry.getValue(); + if (value == null) { + value = ""; + } + result = result.replace("%" + entry.getKey() + "%", value); } return result; } - public static String string(String format, String key, String value) { + public static String string(@NotNull String format, @NotNull String key, @NotNull String value) { String result = format; result = result.replace("\\n", "\n"); result = result.replace("%" + key + "%", value);