Browse Source

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

Should fix #3
MasterCake 3 years ago
parent
commit
fe6e0a8088

+ 14 - 12
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);

+ 8 - 12
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<Integer, UUID> linkedChats = new HashMap<Integer, UUID>();
 	
-	// Player name // RandomInt
+	// Token : Player ID
 	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;
 	
@@ -32,6 +27,7 @@ public class Data {
 		this.token = token;
 	}
 
+	// chats 
 	public HashMap<Integer, UUID> getLinkedChats() {
 		return linkedChats;
 	}
@@ -49,11 +45,11 @@ public class Data {
 	}
 
 	public List<Integer> getIds() {
-		return ids;
+		return chat_ids;
 	}
 
 	public void setIds(List<Integer> 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);
 	}
 
 }

+ 15 - 6
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<Integer> recievers = new ArrayList<Integer>();
-		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() {