Browse Source

fixed text transmission MC -> Telegram

mastercake10 7 years ago
parent
commit
17a34bd68e

+ 8 - 0
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.

+ 0 - 6
src/README.md

@@ -1,6 +0,0 @@
-# TelegramChat
-Spigot 1.7 - 1.11
-
-Brings Telegram to Minecraft!
-
-Spigot site: https://www.spigotmc.org/resources/telegramchat.16576/

+ 1 - 1
src/de/Linus122/TelegramChat/Data.java

@@ -11,6 +11,6 @@ public class Data {
 	public HashMap<Integer, UUID> linkedChats = new HashMap<Integer, UUID>();
 	//Player name // RandomInt
 	public HashMap<String, UUID> linkCodes = new HashMap<String, UUID>();
-	public static List<Integer> ids = new ArrayList<Integer>();
+	public List<Integer> ids = new ArrayList<Integer>();
 	boolean firstUse = true;
 }

+ 2 - 2
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;

+ 17 - 13
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);
 		}
 	}
 }

+ 22 - 20
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();

+ 2 - 2
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!");
 		}

+ 1 - 1
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: