fixed text transmission MC -> Telegram

This commit is contained in:
mastercake10 2017-12-26 18:48:31 +01:00
parent 1561fcf56f
commit 17a34bd68e
8 changed files with 53 additions and 45 deletions

8
README.md Normal file
View File

@ -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.

View File

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

View File

@ -11,6 +11,6 @@ public class Data {
public HashMap<Integer, UUID> linkedChats = new HashMap<Integer, UUID>(); public HashMap<Integer, UUID> linkedChats = new HashMap<Integer, UUID>();
//Player name // RandomInt //Player name // RandomInt
public HashMap<String, UUID> linkCodes = new HashMap<String, UUID>(); 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; boolean firstUse = true;
} }

View File

@ -21,14 +21,14 @@ public class LinkTelegramCmd implements CommandExecutor {
if(Main.data == null){ if(Main.data == null){
Main.data = new Data(); Main.data = new Data();
} }
if(Telegram.authJson == null){ if(Main.telegramHook.authJson == null){
cs.sendMessage("§cPlease add a bot to your server first! /telegram"); cs.sendMessage("§cPlease add a bot to your server first! /telegram");
return true; return true;
} }
String token = Main.generateLinkToken(); String token = Main.generateLinkToken();
Main.data.linkCodes.put(token, ((Player) cs).getUniqueId()); 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); cs.sendMessage("§c" + token);
return true; return true;

View File

@ -36,6 +36,8 @@ public class Main extends JavaPlugin implements Listener{
public static Data data = new Data(); public static Data data = new Data();
static Plugin pl; static Plugin pl;
public static Telegram telegramHook;
@Override @Override
public void onEnable(){ public void onEnable(){
this.saveDefaultConfig(); this.saveDefaultConfig();
@ -60,11 +62,13 @@ public class Main extends JavaPlugin implements Listener{
e.printStackTrace(); e.printStackTrace();
} }
} }
Telegram.auth(); telegramHook = new Telegram();
telegramHook.auth(data.token);
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){ Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
public void run(){ public void run(){
if(Telegram.connected){ if(telegramHook.connected){
Telegram.getupdate(); telegramHook.getUpdate();
} }
} }
}, 20L, 20L); }, 20L, 20L);
@ -95,7 +99,7 @@ public class Main extends JavaPlugin implements Listener{
recievers.remove((Object) sender); recievers.remove((Object) sender);
String msgF = Main.cfg.getString("chat-format").replace('&', '§').replace("%player%", op.getName()).replace("%message%", msg); String msgF = Main.cfg.getString("chat-format").replace('&', '§').replace("%player%", op.getName()).replace("%message%", msg);
for(int id : recievers){ for(int id : recievers){
Telegram.sendMsg(id, msgF); telegramHook.sendMsg(id, msgF);
} }
Bukkit.broadcastMessage(msgF.replace("&", "§")); Bukkit.broadcastMessage(msgF.replace("&", "§"));
@ -103,7 +107,7 @@ public class Main extends JavaPlugin implements Listener{
public static void link(UUID player, int chatID){ public static void link(UUID player, int chatID){
Main.data.linkedChats.put(chatID, player); Main.data.linkedChats.put(chatID, player);
OfflinePlayer p = Bukkit.getOfflinePlayer(player); OfflinePlayer p = Bukkit.getOfflinePlayer(player);
Telegram.sendMsg(chatID, "Success! Linked " + p.getName()); telegramHook.sendMsg(chatID, "Success! Linked " + p.getName());
} }
public static String generateLinkToken(){ public static String generateLinkToken(){
Random rnd = new Random(); Random rnd = new Random();
@ -126,42 +130,42 @@ public class Main extends JavaPlugin implements Listener{
@EventHandler @EventHandler
public void onJoin(PlayerJoinEvent e){ public void onJoin(PlayerJoinEvent e){
if(!this.getConfig().getBoolean("enable-joinquitmessages")) return; if(!this.getConfig().getBoolean("enable-joinquitmessages")) return;
if(Telegram.connected){ if(telegramHook.connected){
Chat chat = new Chat(); Chat chat = new Chat();
chat.parse_mode = "Markdown"; chat.parse_mode = "Markdown";
chat.text = "`" + e.getPlayer().getName() + " joined the game.`"; chat.text = "`" + e.getPlayer().getName() + " joined the game.`";
Telegram.sendAll(chat); telegramHook.sendAll(chat);
} }
} }
@EventHandler @EventHandler
public void onDeath(PlayerDeathEvent e){ public void onDeath(PlayerDeathEvent e){
if(!this.getConfig().getBoolean("enable-deathmessages")) return; if(!this.getConfig().getBoolean("enable-deathmessages")) return;
if(Telegram.connected){ if(telegramHook.connected){
Chat chat = new Chat(); Chat chat = new Chat();
chat.parse_mode = "Markdown"; chat.parse_mode = "Markdown";
chat.text = "`"+e.getDeathMessage() + "`"; chat.text = "`"+e.getDeathMessage() + "`";
Telegram.sendAll(chat); telegramHook.sendAll(chat);
} }
} }
@EventHandler @EventHandler
public void onQuit(PlayerQuitEvent e){ public void onQuit(PlayerQuitEvent e){
if(!this.getConfig().getBoolean("enable-joinquitmessages")) return; if(!this.getConfig().getBoolean("enable-joinquitmessages")) return;
if(Telegram.connected){ if(telegramHook.connected){
Chat chat = new Chat(); Chat chat = new Chat();
chat.parse_mode = "Markdown"; chat.parse_mode = "Markdown";
chat.text = "`" + e.getPlayer().getName() + " left the game.`"; chat.text = "`" + e.getPlayer().getName() + " left the game.`";
System.out.println(chat.text); System.out.println(chat.text);
Telegram.sendAll(chat); telegramHook.sendAll(chat);
} }
} }
@EventHandler @EventHandler
public void onChat(AsyncPlayerChatEvent e){ public void onChat(AsyncPlayerChatEvent e){
if(!this.getConfig().getBoolean("enable-chatmessages")) return; if(!this.getConfig().getBoolean("enable-chatmessages")) return;
if(Telegram.connected){ if(telegramHook.connected){
Chat chat = new Chat(); Chat chat = new Chat();
chat.parse_mode = "Markdown"; chat.parse_mode = "Markdown";
chat.text = e.getPlayer().getName() + ": _" + e.getMessage().replaceAll("§.", "") + "_"; chat.text = e.getPlayer().getName() + ": _" + e.getMessage().replaceAll("§.", "") + "_";
Telegram.sendAll(chat); telegramHook.sendAll(chat);
} }
} }
} }

View File

@ -19,14 +19,19 @@ import de.Linus122.TelegramComponents.Chat;
public class Telegram { public class Telegram {
public static JsonObject authJson; public JsonObject authJson;
public static boolean connected = false; public boolean connected = false;
static int lastUpdate = 0; 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{ 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; authJson = obj;
connected = true; connected = true;
return true; return true;
@ -36,19 +41,16 @@ public class Telegram {
return false; return false;
} }
} }
public static void getupdate(){ public void getUpdate(){
JsonObject up = null; JsonObject up = null;
try { try {
up = sendGet("https://api.telegram.org/bot" + Main.data.token + "/getUpdates?offset=" + (lastUpdate + 1)); up = sendGet("https://api.telegram.org/bot" + Main.data.token + "/getUpdates?offset=" + (lastUpdate + 1));
} catch (IOException e) { } catch (IOException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
if(up == null){ if(up == null){
auth(); reconnect();
} }
if(up.has("result")){ if(up.has("result")){
for (JsonElement ob : up.getAsJsonArray("result")) { for (JsonElement ob : up.getAsJsonArray("result")) {
@ -67,7 +69,7 @@ public class Telegram {
String text = obj.getAsJsonObject("message").get("text").getAsString(); String text = obj.getAsJsonObject("message").get("text").getAsString();
for(char c : text.toCharArray()){ for(char c : text.toCharArray()){
if((int) c == 55357){ if((int) c == 55357){
Telegram.sendMsg(id, "Emoticons are not allowed, sorry!"); this.sendMsg(id, "Emoticons are not allowed, sorry!");
return; return;
} }
} }
@ -79,9 +81,9 @@ public class Telegram {
chat2.chat_id = id; chat2.chat_id = id;
chat2.parse_mode = "Markdown"; 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)"; 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 }else
if(Main.data.linkCodes.containsKey(text)){ if(Main.data.linkCodes.containsKey(text)){
//LINK //LINK
@ -90,7 +92,7 @@ public class Telegram {
}else if(Main.data.linkedChats.containsKey(id)){ }else if(Main.data.linkedChats.containsKey(id)){
Main.sendToMC(Main.data.linkedChats.get(id), text, id); Main.sendToMC(Main.data.linkedChats.get(id), text, id);
}else{ }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(); Gson gson = new Gson();
Chat chat = new Chat(); Chat chat = new Chat();
chat.chat_id = id; chat.chat_id = id;
@ -114,12 +116,12 @@ public class Telegram {
post("sendMessage", gson.toJson(chat, Chat.class)); post("sendMessage", gson.toJson(chat, Chat.class));
} }
public static void sendMsg(Chat chat){ public void sendMsg(Chat chat){
Gson gson = new Gson(); Gson gson = new Gson();
post("sendMessage", gson.toJson(chat, Chat.class)); post("sendMessage", gson.toJson(chat, Chat.class));
} }
public static void sendAll(final Chat chat){ public void sendAll(final Chat chat){
new Thread(new Runnable(){ new Thread(new Runnable(){
public void run(){ public void run(){
Gson gson = new Gson(); Gson gson = new Gson();
@ -130,7 +132,7 @@ public class Telegram {
} }
}).start(); }).start();
} }
public static void post(String method, String json){ public void post(String method, String json){
try { try {
String body = json; String body = json;
URL url = new URL("https://api.telegram.org/bot" + Main.data.token + "/" + method); URL url = new URL("https://api.telegram.org/bot" + Main.data.token + "/" + method);
@ -162,13 +164,13 @@ public class Telegram {
writer.close(); writer.close();
reader.close(); reader.close();
} catch (Exception e) { } catch (Exception e) {
auth(); reconnect();
System.out.print("[Telegram] Disconnected from Telegram, 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; String a = url;
URL url2 = new URL(a); URL url2 = new URL(a);
URLConnection conn = url2.openConnection(); URLConnection conn = url2.openConnection();

View File

@ -23,10 +23,10 @@ public class TelegramCmd implements CommandExecutor {
Main.save(); Main.save();
boolean success = false; boolean success = false;
success = Telegram.auth(); success = Main.telegramHook.auth(Main.data.token);
if(success){ if(success){
cs.sendMessage("§cSuccessfully connected to Telegram!"); 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{ }else{
cs.sendMessage("§cWrong token. Paste in the whole token!"); cs.sendMessage("§cWrong token. Paste in the whole token!");
} }

View File

@ -1,6 +1,6 @@
name: TelegramChat name: TelegramChat
main: de.Linus122.TelegramChat.Main main: de.Linus122.TelegramChat.Main
version: 1.0 version: 1.0.9.6
authors: [Linus122] authors: [Linus122]
description: Brings minecraft chat to Telegram! description: Brings minecraft chat to Telegram!
commands: commands: