From 17a34bd68eae74b7ed8479f6a3b7894c9fccd7f4 Mon Sep 17 00:00:00 2001 From: mastercake10 Date: Tue, 26 Dec 2017 18:48:31 +0100 Subject: [PATCH] fixed text transmission MC -> Telegram --- README.md | 8 ++++ src/README.md | 6 --- src/de/Linus122/TelegramChat/Data.java | 2 +- .../TelegramChat/LinkTelegramCmd.java | 4 +- src/de/Linus122/TelegramChat/Main.java | 30 +++++++------ src/de/Linus122/TelegramChat/Telegram.java | 42 ++++++++++--------- src/de/Linus122/TelegramChat/TelegramCmd.java | 4 +- src/plugin.yml | 2 +- 8 files changed, 53 insertions(+), 45 deletions(-) create mode 100644 README.md delete mode 100755 src/README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..e828b68 --- /dev/null +++ b/README.md @@ -0,0 +1,8 @@ +[![GitHub last commit](https://img.shields.io/github/last-commit/mastercake10/TelegramChat.svg)](https://github.com/mastercake10/TelegramChat/commits/master) +[![discord](https://discordapp.com/api/guilds/330725294749122561/widget.png)](https://discord.gg/3xgsPh8) +[![view on SpigotMC](https://img.shields.io/badge/view%20on-spigotmc-orange.svg)](https://www.spigotmc.org/resources/telegramchat.16576/) + +![resource icon](https://www.spigotmc.org/data/resource_icons/16/16576.jpg?1476392100) + +## Welcome to the TelegramChat GitHub repository! +TelegramChat is a Spigot plugin compatible with Spigot versions 1.7 through 1.12.2, that connects Telegram with Minecraft. \ No newline at end of file diff --git a/src/README.md b/src/README.md deleted file mode 100755 index b29f08b..0000000 --- a/src/README.md +++ /dev/null @@ -1,6 +0,0 @@ -# TelegramChat -Spigot 1.7 - 1.11 - -Brings Telegram to Minecraft! - -Spigot site: https://www.spigotmc.org/resources/telegramchat.16576/ diff --git a/src/de/Linus122/TelegramChat/Data.java b/src/de/Linus122/TelegramChat/Data.java index a4c4951..bfa5d9b 100755 --- a/src/de/Linus122/TelegramChat/Data.java +++ b/src/de/Linus122/TelegramChat/Data.java @@ -11,6 +11,6 @@ public class Data { public HashMap linkedChats = new HashMap(); //Player name // RandomInt public HashMap linkCodes = new HashMap(); - public static List ids = new ArrayList(); + public List ids = new ArrayList(); boolean firstUse = true; } diff --git a/src/de/Linus122/TelegramChat/LinkTelegramCmd.java b/src/de/Linus122/TelegramChat/LinkTelegramCmd.java index 5e7fe27..777d69d 100755 --- a/src/de/Linus122/TelegramChat/LinkTelegramCmd.java +++ b/src/de/Linus122/TelegramChat/LinkTelegramCmd.java @@ -21,14 +21,14 @@ public class LinkTelegramCmd implements CommandExecutor { if(Main.data == null){ Main.data = new Data(); } - if(Telegram.authJson == null){ + if(Main.telegramHook.authJson == null){ cs.sendMessage("§cPlease add a bot to your server first! /telegram"); return true; } String token = Main.generateLinkToken(); Main.data.linkCodes.put(token, ((Player) cs).getUniqueId()); - cs.sendMessage("§aAdd " + Telegram.authJson.getAsJsonObject("result").get("username").getAsString() + " to Telegram and send this message to " + Telegram.authJson.getAsJsonObject("result").get("username").getAsString() + ":"); + cs.sendMessage("§aAdd " + Main.telegramHook.authJson.getAsJsonObject("result").get("username").getAsString() + " to Telegram and send this message to " + Main.telegramHook.authJson.getAsJsonObject("result").get("username").getAsString() + ":"); cs.sendMessage("§c" + token); return true; diff --git a/src/de/Linus122/TelegramChat/Main.java b/src/de/Linus122/TelegramChat/Main.java index b914366..613f662 100755 --- a/src/de/Linus122/TelegramChat/Main.java +++ b/src/de/Linus122/TelegramChat/Main.java @@ -36,6 +36,8 @@ public class Main extends JavaPlugin implements Listener{ public static Data data = new Data(); static Plugin pl; + public static Telegram telegramHook; + @Override public void onEnable(){ this.saveDefaultConfig(); @@ -60,11 +62,13 @@ public class Main extends JavaPlugin implements Listener{ e.printStackTrace(); } } - Telegram.auth(); + telegramHook = new Telegram(); + telegramHook.auth(data.token); + Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){ public void run(){ - if(Telegram.connected){ - Telegram.getupdate(); + if(telegramHook.connected){ + telegramHook.getUpdate(); } } }, 20L, 20L); @@ -95,7 +99,7 @@ public class Main extends JavaPlugin implements Listener{ recievers.remove((Object) sender); String msgF = Main.cfg.getString("chat-format").replace('&', '§').replace("%player%", op.getName()).replace("%message%", msg); for(int id : recievers){ - Telegram.sendMsg(id, msgF); + telegramHook.sendMsg(id, msgF); } Bukkit.broadcastMessage(msgF.replace("&", "§")); @@ -103,7 +107,7 @@ public class Main extends JavaPlugin implements Listener{ public static void link(UUID player, int chatID){ Main.data.linkedChats.put(chatID, player); OfflinePlayer p = Bukkit.getOfflinePlayer(player); - Telegram.sendMsg(chatID, "Success! Linked " + p.getName()); + telegramHook.sendMsg(chatID, "Success! Linked " + p.getName()); } public static String generateLinkToken(){ Random rnd = new Random(); @@ -126,42 +130,42 @@ public class Main extends JavaPlugin implements Listener{ @EventHandler public void onJoin(PlayerJoinEvent e){ if(!this.getConfig().getBoolean("enable-joinquitmessages")) return; - if(Telegram.connected){ + if(telegramHook.connected){ Chat chat = new Chat(); chat.parse_mode = "Markdown"; chat.text = "`" + e.getPlayer().getName() + " joined the game.`"; - Telegram.sendAll(chat); + telegramHook.sendAll(chat); } } @EventHandler public void onDeath(PlayerDeathEvent e){ if(!this.getConfig().getBoolean("enable-deathmessages")) return; - if(Telegram.connected){ + if(telegramHook.connected){ Chat chat = new Chat(); chat.parse_mode = "Markdown"; chat.text = "`"+e.getDeathMessage() + "`"; - Telegram.sendAll(chat); + telegramHook.sendAll(chat); } } @EventHandler public void onQuit(PlayerQuitEvent e){ if(!this.getConfig().getBoolean("enable-joinquitmessages")) return; - if(Telegram.connected){ + if(telegramHook.connected){ Chat chat = new Chat(); chat.parse_mode = "Markdown"; chat.text = "`" + e.getPlayer().getName() + " left the game.`"; System.out.println(chat.text); - Telegram.sendAll(chat); + telegramHook.sendAll(chat); } } @EventHandler public void onChat(AsyncPlayerChatEvent e){ if(!this.getConfig().getBoolean("enable-chatmessages")) return; - if(Telegram.connected){ + if(telegramHook.connected){ Chat chat = new Chat(); chat.parse_mode = "Markdown"; chat.text = e.getPlayer().getName() + ": _" + e.getMessage().replaceAll("§.", "") + "_"; - Telegram.sendAll(chat); + telegramHook.sendAll(chat); } } } diff --git a/src/de/Linus122/TelegramChat/Telegram.java b/src/de/Linus122/TelegramChat/Telegram.java index cf4991f..7c831c7 100755 --- a/src/de/Linus122/TelegramChat/Telegram.java +++ b/src/de/Linus122/TelegramChat/Telegram.java @@ -19,14 +19,19 @@ import de.Linus122.TelegramComponents.Chat; public class Telegram { - public static JsonObject authJson; - public static boolean connected = false; - + public JsonObject authJson; + public boolean connected = false; static int lastUpdate = 0; - public static boolean auth(){ + public String token; + + public boolean auth(String token){ + this.token = token; + return reconnect(); + } + public boolean reconnect(){ try{ - JsonObject obj = sendGet("https://api.telegram.org/bot" + Main.data.token + "/getMe"); + JsonObject obj = sendGet("https://api.telegram.org/bot" + token + "/getMe"); authJson = obj; connected = true; return true; @@ -36,19 +41,16 @@ public class Telegram { return false; } } - public static void getupdate(){ - + public void getUpdate(){ JsonObject up = null; try { - up = sendGet("https://api.telegram.org/bot" + Main.data.token + "/getUpdates?offset=" + (lastUpdate + 1)); - } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(up == null){ - auth(); + reconnect(); } if(up.has("result")){ for (JsonElement ob : up.getAsJsonArray("result")) { @@ -67,7 +69,7 @@ public class Telegram { String text = obj.getAsJsonObject("message").get("text").getAsString(); for(char c : text.toCharArray()){ if((int) c == 55357){ - Telegram.sendMsg(id, "Emoticons are not allowed, sorry!"); + this.sendMsg(id, "Emoticons are not allowed, sorry!"); return; } } @@ -79,9 +81,9 @@ public class Telegram { chat2.chat_id = id; chat2.parse_mode = "Markdown"; chat2.text = "Congratulations, your bot is working! Have fun with this Plugin. Feel free to donate via *PayPal* to me if you like TelegramChat! [PayPal Donation URL](https://goo.gl/I02XGH)"; - Telegram.sendMsg(chat2); + this.sendMsg(chat2); } - Telegram.sendMsg(id, "You can see the chat but you can't chat at the moment. Type /linktelegram ingame to chat!"); + this.sendMsg(id, "You can see the chat but you can't chat at the moment. Type /linktelegram ingame to chat!"); }else if(Main.data.linkCodes.containsKey(text)){ //LINK @@ -90,7 +92,7 @@ public class Telegram { }else if(Main.data.linkedChats.containsKey(id)){ Main.sendToMC(Main.data.linkedChats.get(id), text, id); }else{ - Telegram.sendMsg(id, "Sorry, please link your account with /linktelegram ingame to use the chat!"); + this.sendMsg(id, "Sorry, please link your account with /linktelegram ingame to use the chat!"); } } @@ -106,7 +108,7 @@ public class Telegram { } } - public static void sendMsg(int id, String msg){ + public void sendMsg(int id, String msg){ Gson gson = new Gson(); Chat chat = new Chat(); chat.chat_id = id; @@ -114,12 +116,12 @@ public class Telegram { post("sendMessage", gson.toJson(chat, Chat.class)); } - public static void sendMsg(Chat chat){ + public void sendMsg(Chat chat){ Gson gson = new Gson(); post("sendMessage", gson.toJson(chat, Chat.class)); } - public static void sendAll(final Chat chat){ + public void sendAll(final Chat chat){ new Thread(new Runnable(){ public void run(){ Gson gson = new Gson(); @@ -130,7 +132,7 @@ public class Telegram { } }).start(); } - public static void post(String method, String json){ + public void post(String method, String json){ try { String body = json; URL url = new URL("https://api.telegram.org/bot" + Main.data.token + "/" + method); @@ -162,13 +164,13 @@ public class Telegram { writer.close(); reader.close(); } catch (Exception e) { - auth(); + reconnect(); System.out.print("[Telegram] Disconnected from Telegram, reconnect..."); } } - public static JsonObject sendGet(String url) throws IOException { + public JsonObject sendGet(String url) throws IOException { String a = url; URL url2 = new URL(a); URLConnection conn = url2.openConnection(); diff --git a/src/de/Linus122/TelegramChat/TelegramCmd.java b/src/de/Linus122/TelegramChat/TelegramCmd.java index 7514add..0db0e31 100755 --- a/src/de/Linus122/TelegramChat/TelegramCmd.java +++ b/src/de/Linus122/TelegramChat/TelegramCmd.java @@ -23,10 +23,10 @@ public class TelegramCmd implements CommandExecutor { Main.save(); boolean success = false; - success = Telegram.auth(); + success = Main.telegramHook.auth(Main.data.token); if(success){ cs.sendMessage("§cSuccessfully connected to Telegram!"); - cs.sendMessage("§aAdd " + Telegram.authJson.getAsJsonObject("result").get("username").getAsString() + " to Telegram!"); + cs.sendMessage("§aAdd " + Main.telegramHook.authJson.getAsJsonObject("result").get("username").getAsString() + " to Telegram!"); }else{ cs.sendMessage("§cWrong token. Paste in the whole token!"); } diff --git a/src/plugin.yml b/src/plugin.yml index e0f507b..ab79e4a 100755 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -1,6 +1,6 @@ name: TelegramChat main: de.Linus122.TelegramChat.Main -version: 1.0 +version: 1.0.9.6 authors: [Linus122] description: Brings minecraft chat to Telegram! commands: