From fe6e0a80887d54dad154dfe849da21438a7169b3 Mon Sep 17 00:00:00 2001 From: MasterCake Date: Sat, 8 May 2021 03:22:08 +0200 Subject: [PATCH] save user_id<->mc_handle instead of chat_id<->mc_handle Should fix #3 --- .../java/de/Linus122/Telegram/Telegram.java | 26 ++++++++++--------- .../java/de/Linus122/TelegramChat/Data.java | 20 ++++++-------- .../Linus122/TelegramChat/TelegramChat.java | 21 ++++++++++----- 3 files changed, 37 insertions(+), 30 deletions(-) diff --git a/src/main/java/de/Linus122/Telegram/Telegram.java b/src/main/java/de/Linus122/Telegram/Telegram.java index a7352c8..c249eb5 100755 --- a/src/main/java/de/Linus122/Telegram/Telegram.java +++ b/src/main/java/de/Linus122/Telegram/Telegram.java @@ -81,11 +81,11 @@ public class Telegram { if (update.getMessage() != null) { Chat chat = update.getMessage().getChat(); - + if (chat.isPrivate()) { // private chat - if (!TelegramChat.getBackend().ids.contains(chat.getId())) - TelegramChat.getBackend().ids.add(chat.getId()); + if (!TelegramChat.getBackend().chat_ids.contains(chat.getId())) + TelegramChat.getBackend().chat_ids.add(chat.getId()); if (update.getMessage().getText() != null) { String text = update.getMessage().getText(); @@ -102,19 +102,19 @@ public class Telegram { } this.sendMsg(chat.getId(), Utils.formatMSG("can-see-but-not-chat")[0]); } else { - handleUserMessage(text, chat); + handleUserMessage(text, update); } } } else if (!chat.isPrivate()) { // group chat int id = chat.getId(); - if (!TelegramChat.getBackend().ids.contains(id)) - TelegramChat.getBackend().ids.add(id); + if (!TelegramChat.getBackend().chat_ids.contains(id)) + TelegramChat.getBackend().chat_ids.add(id); if (update.getMessage().getText() != null) { String text = update.getMessage().getText(); - handleUserMessage(text, chat); + handleUserMessage(text, update); } } } @@ -125,14 +125,16 @@ public class Telegram { return true; } - public void handleUserMessage(String text, Chat chat) { + public void handleUserMessage(String text, Update update) { + Chat chat = update.getMessage().getChat(); + int user_id = update.getMessage().getFrom().getId(); if (TelegramChat.getBackend().getLinkCodes().containsKey(text)) { // LINK - TelegramChat.link(TelegramChat.getBackend().getUUIDFromLinkCode(text), chat.getId()); + TelegramChat.link(TelegramChat.getBackend().getUUIDFromLinkCode(text), user_id); TelegramChat.getBackend().removeLinkCode(text); - } else if (TelegramChat.getBackend().getLinkedChats().containsKey(chat.getId())) { + } else if (TelegramChat.getBackend().getLinkedChats().containsKey(user_id)) { ChatMessageToMc chatMsg = new ChatMessageToMc( - TelegramChat.getBackend().getUUIDFromChatID(chat.getId()), text, chat.getId()); + TelegramChat.getBackend().getUUIDFromUserID(user_id), text, chat.getId()); for (TelegramActionListener actionListener : listeners) { actionListener.onSendToMinecraft(chatMsg); @@ -166,7 +168,7 @@ public class Telegram { public void sendAll(final ChatMessageToTelegram chat) { new Thread(new Runnable() { public void run() { - for (int id : TelegramChat.getBackend().ids) { + for (int id : TelegramChat.getBackend().chat_ids) { chat.chat_id = id; // post("sendMessage", gson.toJson(chat, Chat.class)); sendMsg(chat); diff --git a/src/main/java/de/Linus122/TelegramChat/Data.java b/src/main/java/de/Linus122/TelegramChat/Data.java index 388e38e..d2b0449 100755 --- a/src/main/java/de/Linus122/TelegramChat/Data.java +++ b/src/main/java/de/Linus122/TelegramChat/Data.java @@ -1,25 +1,20 @@ package de.Linus122.TelegramChat; -import java.io.File; -import java.io.FileInputStream; -import java.io.ObjectInputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.UUID; -import com.google.gson.Gson; - public class Data { private String token = ""; - // Player name // ChatID + // User ID : Player ID private HashMap linkedChats = new HashMap(); - // Player name // RandomInt + // Token : Player ID private HashMap linkCodes = new HashMap(); - public List ids = new ArrayList(); + public List chat_ids = new ArrayList(); private boolean firstUse = true; @@ -32,6 +27,7 @@ public class Data { this.token = token; } + // chats public HashMap getLinkedChats() { return linkedChats; } @@ -49,11 +45,11 @@ public class Data { } public List getIds() { - return ids; + return chat_ids; } public void setIds(List ids) { - this.ids = ids; + this.chat_ids = ids; } public boolean isFirstUse() { @@ -80,8 +76,8 @@ public class Data { linkCodes.remove(code); } - public UUID getUUIDFromChatID(int chatID) { - return linkedChats.get(chatID); + public UUID getUUIDFromUserID(int userID) { + return linkedChats.get(userID); } } diff --git a/src/main/java/de/Linus122/TelegramChat/TelegramChat.java b/src/main/java/de/Linus122/TelegramChat/TelegramChat.java index 9584067..85a99f7 100755 --- a/src/main/java/de/Linus122/TelegramChat/TelegramChat.java +++ b/src/main/java/de/Linus122/TelegramChat/TelegramChat.java @@ -30,6 +30,7 @@ import de.Linus122.Handlers.CommandHandler; import de.Linus122.Metrics.Metrics; import de.Linus122.Telegram.Telegram; import de.Linus122.Telegram.Utils; +import de.Linus122.TelegramComponents.Chat; import de.Linus122.TelegramComponents.ChatMessageToMc; import de.Linus122.TelegramComponents.ChatMessageToTelegram; @@ -138,11 +139,11 @@ public class TelegramChat extends JavaPlugin implements Listener { sendToMC(chatMsg.getUuid_sender(), chatMsg.getContent(), chatMsg.getChatID_sender()); } - private static void sendToMC(UUID uuid, String msg, int sender) { + private static void sendToMC(UUID uuid, String msg, int sender_chat) { OfflinePlayer op = Bukkit.getOfflinePlayer(uuid); List recievers = new ArrayList(); - recievers.addAll(TelegramChat.data.ids); - recievers.remove((Object) sender); + recievers.addAll(TelegramChat.data.chat_ids); + recievers.remove((Object) sender_chat); String msgF = Utils.formatMSG("general-message-to-mc", op.getName(), msg)[0]; for (int id : recievers) { telegramHook.sendMsg(id, msgF.replaceAll("ยง.", "")); @@ -151,10 +152,18 @@ public class TelegramChat extends JavaPlugin implements Listener { } - public static void link(UUID player, int chatID) { - TelegramChat.data.addChatPlayerLink(chatID, player); + public static void link(UUID player, int userID) { + TelegramChat.data.addChatPlayerLink(userID, player); OfflinePlayer p = Bukkit.getOfflinePlayer(player); - telegramHook.sendMsg(chatID, "Success! Linked " + p.getName()); + telegramHook.sendMsg(userID, "Success! Linked " + p.getName()); + } + + public boolean isChatLinked(Chat chat) { + if(TelegramChat.getBackend().getLinkedChats().containsKey(chat.getId())) { + return true; + } + + return false; } public static String generateLinkToken() {