save user_id<->mc_handle instead of chat_id<->mc_handle

Should fix #3
This commit is contained in:
MasterCake 2021-05-08 03:22:08 +02:00
parent eba7bba7fe
commit fe6e0a8088
3 changed files with 37 additions and 30 deletions

View File

@ -81,11 +81,11 @@ public class Telegram {
if (update.getMessage() != null) { if (update.getMessage() != null) {
Chat chat = update.getMessage().getChat(); Chat chat = update.getMessage().getChat();
if (chat.isPrivate()) { if (chat.isPrivate()) {
// private chat // private chat
if (!TelegramChat.getBackend().ids.contains(chat.getId())) if (!TelegramChat.getBackend().chat_ids.contains(chat.getId()))
TelegramChat.getBackend().ids.add(chat.getId()); TelegramChat.getBackend().chat_ids.add(chat.getId());
if (update.getMessage().getText() != null) { if (update.getMessage().getText() != null) {
String text = update.getMessage().getText(); String text = update.getMessage().getText();
@ -102,19 +102,19 @@ public class Telegram {
} }
this.sendMsg(chat.getId(), Utils.formatMSG("can-see-but-not-chat")[0]); this.sendMsg(chat.getId(), Utils.formatMSG("can-see-but-not-chat")[0]);
} else { } else {
handleUserMessage(text, chat); handleUserMessage(text, update);
} }
} }
} else if (!chat.isPrivate()) { } else if (!chat.isPrivate()) {
// group chat // group chat
int id = chat.getId(); int id = chat.getId();
if (!TelegramChat.getBackend().ids.contains(id)) if (!TelegramChat.getBackend().chat_ids.contains(id))
TelegramChat.getBackend().ids.add(id); TelegramChat.getBackend().chat_ids.add(id);
if (update.getMessage().getText() != null) { if (update.getMessage().getText() != null) {
String text = update.getMessage().getText(); String text = update.getMessage().getText();
handleUserMessage(text, chat); handleUserMessage(text, update);
} }
} }
} }
@ -125,14 +125,16 @@ public class Telegram {
return true; 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)) { if (TelegramChat.getBackend().getLinkCodes().containsKey(text)) {
// LINK // LINK
TelegramChat.link(TelegramChat.getBackend().getUUIDFromLinkCode(text), chat.getId()); TelegramChat.link(TelegramChat.getBackend().getUUIDFromLinkCode(text), user_id);
TelegramChat.getBackend().removeLinkCode(text); TelegramChat.getBackend().removeLinkCode(text);
} else if (TelegramChat.getBackend().getLinkedChats().containsKey(chat.getId())) { } else if (TelegramChat.getBackend().getLinkedChats().containsKey(user_id)) {
ChatMessageToMc chatMsg = new ChatMessageToMc( ChatMessageToMc chatMsg = new ChatMessageToMc(
TelegramChat.getBackend().getUUIDFromChatID(chat.getId()), text, chat.getId()); TelegramChat.getBackend().getUUIDFromUserID(user_id), text, chat.getId());
for (TelegramActionListener actionListener : listeners) { for (TelegramActionListener actionListener : listeners) {
actionListener.onSendToMinecraft(chatMsg); actionListener.onSendToMinecraft(chatMsg);
@ -166,7 +168,7 @@ public class Telegram {
public void sendAll(final ChatMessageToTelegram chat) { public void sendAll(final ChatMessageToTelegram chat) {
new Thread(new Runnable() { new Thread(new Runnable() {
public void run() { public void run() {
for (int id : TelegramChat.getBackend().ids) { for (int id : TelegramChat.getBackend().chat_ids) {
chat.chat_id = id; chat.chat_id = id;
// post("sendMessage", gson.toJson(chat, Chat.class)); // post("sendMessage", gson.toJson(chat, Chat.class));
sendMsg(chat); sendMsg(chat);

View File

@ -1,25 +1,20 @@
package de.Linus122.TelegramChat; package de.Linus122.TelegramChat;
import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import com.google.gson.Gson;
public class Data { public class Data {
private String token = ""; private String token = "";
// Player name // ChatID // User ID : Player ID
private HashMap<Integer, UUID> linkedChats = new HashMap<Integer, UUID>(); private HashMap<Integer, UUID> linkedChats = new HashMap<Integer, UUID>();
// Player name // RandomInt // Token : Player ID
private HashMap<String, UUID> linkCodes = new HashMap<String, UUID>(); private HashMap<String, UUID> linkCodes = new HashMap<String, UUID>();
public List<Integer> ids = new ArrayList<Integer>(); public List<Integer> chat_ids = new ArrayList<Integer>();
private boolean firstUse = true; private boolean firstUse = true;
@ -32,6 +27,7 @@ public class Data {
this.token = token; this.token = token;
} }
// chats
public HashMap<Integer, UUID> getLinkedChats() { public HashMap<Integer, UUID> getLinkedChats() {
return linkedChats; return linkedChats;
} }
@ -49,11 +45,11 @@ public class Data {
} }
public List<Integer> getIds() { public List<Integer> getIds() {
return ids; return chat_ids;
} }
public void setIds(List<Integer> ids) { public void setIds(List<Integer> ids) {
this.ids = ids; this.chat_ids = ids;
} }
public boolean isFirstUse() { public boolean isFirstUse() {
@ -80,8 +76,8 @@ public class Data {
linkCodes.remove(code); linkCodes.remove(code);
} }
public UUID getUUIDFromChatID(int chatID) { public UUID getUUIDFromUserID(int userID) {
return linkedChats.get(chatID); return linkedChats.get(userID);
} }
} }

View File

@ -30,6 +30,7 @@ import de.Linus122.Handlers.CommandHandler;
import de.Linus122.Metrics.Metrics; import de.Linus122.Metrics.Metrics;
import de.Linus122.Telegram.Telegram; import de.Linus122.Telegram.Telegram;
import de.Linus122.Telegram.Utils; import de.Linus122.Telegram.Utils;
import de.Linus122.TelegramComponents.Chat;
import de.Linus122.TelegramComponents.ChatMessageToMc; import de.Linus122.TelegramComponents.ChatMessageToMc;
import de.Linus122.TelegramComponents.ChatMessageToTelegram; 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()); 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); OfflinePlayer op = Bukkit.getOfflinePlayer(uuid);
List<Integer> recievers = new ArrayList<Integer>(); List<Integer> recievers = new ArrayList<Integer>();
recievers.addAll(TelegramChat.data.ids); recievers.addAll(TelegramChat.data.chat_ids);
recievers.remove((Object) sender); recievers.remove((Object) sender_chat);
String msgF = Utils.formatMSG("general-message-to-mc", op.getName(), msg)[0]; String msgF = Utils.formatMSG("general-message-to-mc", op.getName(), msg)[0];
for (int id : recievers) { for (int id : recievers) {
telegramHook.sendMsg(id, msgF.replaceAll("§.", "")); telegramHook.sendMsg(id, msgF.replaceAll("§.", ""));
@ -151,10 +152,18 @@ public class TelegramChat extends JavaPlugin implements Listener {
} }
public static void link(UUID player, int chatID) { public static void link(UUID player, int userID) {
TelegramChat.data.addChatPlayerLink(chatID, player); TelegramChat.data.addChatPlayerLink(userID, player);
OfflinePlayer p = Bukkit.getOfflinePlayer(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() { public static String generateLinkToken() {