Skip to content

Commit

Permalink
save user_id<->mc_handle instead of chat_id<->mc_handle
Browse files Browse the repository at this point in the history
Should fix #3
  • Loading branch information
mastercake10 committed May 8, 2021
1 parent eba7bba commit fe6e0a8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 30 deletions.
26 changes: 14 additions & 12 deletions src/main/java/de/Linus122/Telegram/Telegram.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ public boolean getUpdate() {

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();
Expand All @@ -102,19 +102,19 @@ public boolean getUpdate() {
}
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);
}
}
}
Expand All @@ -125,14 +125,16 @@ public boolean getUpdate() {
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);
Expand Down Expand Up @@ -166,7 +168,7 @@ public void sendMsg(ChatMessageToTelegram chat) {
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);
Expand Down
20 changes: 8 additions & 12 deletions src/main/java/de/Linus122/TelegramChat/Data.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -32,6 +27,7 @@ public void setToken(String token) {
this.token = token;
}

// chats
public HashMap<Integer, UUID> getLinkedChats() {
return linkedChats;
}
Expand All @@ -49,11 +45,11 @@ public void setLinkCodes(HashMap<String, UUID> linkCodes) {
}

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() {
Expand All @@ -80,8 +76,8 @@ public void removeLinkCode(String code) {
linkCodes.remove(code);
}

public UUID getUUIDFromChatID(int chatID) {
return linkedChats.get(chatID);
public UUID getUUIDFromUserID(int userID) {
return linkedChats.get(userID);
}

}
21 changes: 15 additions & 6 deletions src/main/java/de/Linus122/TelegramChat/TelegramChat.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
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;

Expand Down Expand Up @@ -138,11 +139,11 @@ public static void sendToMC(ChatMessageToMc chatMsg) {
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("§.", ""));
Expand All @@ -151,10 +152,18 @@ private static void sendToMC(UUID uuid, String msg, int sender) {

}

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() {
Expand Down

0 comments on commit fe6e0a8

Please sign in to comment.