Sfoglia il codice sorgente

converted to maven, added API and issue fix

mastercake10 6 anni fa
parent
commit
6199a119e0

+ 57 - 0
pom.xml

@@ -0,0 +1,57 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+	<modelVersion>4.0.0</modelVersion>
+	<groupId>TelegramChat</groupId>
+	<artifactId>TelegramChat</artifactId>
+	<version>1.0.9.9</version>
+	<name>TelegramChat</name>
+	<url>https://www.spigotmc.org/resources/telegramchat.16576/</url>
+	<repositories>
+		<repository>
+			<id>spigot-repo</id>
+			<url>https://hub.spigotmc.org/nexus/content/groups/public/</url>
+		</repository>
+	</repositories>
+	<dependencies>
+		<dependency>
+			<groupId>org.spigotmc</groupId>
+			<artifactId>spigot-api</artifactId>
+			<version>1.12.2-R0.1-SNAPSHOT</version>
+			<scope>provided</scope>
+		</dependency>
+	</dependencies>
+	<build>
+		<!-- Uses the properties in this file for plugin.yml and config.yml -->
+		<resources>
+			<resource>
+				<directory>${basedir}/src/main/resources</directory>
+				<filtering>true</filtering>
+				<includes>
+					<include>plugin.yml</include>
+					<include>config.yml</include>
+				</includes>
+			</resource>
+		</resources>
+		<plugins>
+			<!-- Sets the Java version to 8 -->
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-compiler-plugin</artifactId>
+				<version>3.1</version>
+				<configuration>
+					<source>1.8</source>
+					<target>1.8</target>
+				</configuration>
+			</plugin>
+			<!-- Sets the custom JARfile name (Project name without spaces is good) -->
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-jar-plugin</artifactId>
+				<version>2.4</version>
+				<configuration>
+					<finalName>${project.name}</finalName>
+				</configuration>
+			</plugin>
+		</plugins>
+	</build>
+</project>

+ 0 - 0
src/de/Linus122/Metrics/Metrics.java → src/main/java/de/Linus122/Metrics/Metrics.java


+ 7 - 0
src/main/java/de/Linus122/TelegramChat/API.java

@@ -0,0 +1,7 @@
+package de.Linus122.TelegramChat;
+
+public class API {
+	public static Telegram getTelegramHook(){
+		return Main.telegramHook;
+	}
+}

+ 0 - 0
src/de/Linus122/TelegramChat/Data.java → src/main/java/de/Linus122/TelegramChat/Data.java


+ 0 - 0
src/de/Linus122/TelegramChat/LinkTelegramCmd.java → src/main/java/de/Linus122/TelegramChat/LinkTelegramCmd.java


+ 9 - 2
src/de/Linus122/TelegramChat/Main.java → src/main/java/de/Linus122/TelegramChat/Main.java

@@ -29,6 +29,7 @@ import com.google.gson.Gson;
 
 import de.Linus122.Metrics.Metrics;
 import de.Linus122.TelegramComponents.Chat;
+import de.Linus122.TelegramComponents.ChatMessageToMc;
 
 
 public class Main extends JavaPlugin implements Listener{
@@ -94,7 +95,10 @@ public class Main extends JavaPlugin implements Listener{
 	public void onDisable(){
 		save();
 	}
-	public static void sendToMC(UUID uuid, String msg, int sender){
+	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){
 		OfflinePlayer op = Bukkit.getOfflinePlayer(uuid);
 		List<Integer> recievers = new ArrayList<Integer>();
 		recievers.addAll(Main.data.ids);
@@ -166,8 +170,11 @@ public class Main extends JavaPlugin implements Listener{
 		if(telegramHook.connected){
 			Chat chat = new Chat();
 			chat.parse_mode = "Markdown";
-			chat.text = e.getPlayer().getName() + ": _" + e.getMessage().replaceAll("§.", "") + "_";
+			chat.text = escape(e.getPlayer().getName()) + ": _" + e.getMessage().replaceAll("§.", "") + "_";
 			telegramHook.sendAll(chat);
 		}
 	}
+	public String escape(String str){
+		return str.replace("_", "\\_");
+	}
 }

+ 20 - 5
src/de/Linus122/TelegramChat/Telegram.java → src/main/java/de/Linus122/TelegramChat/Telegram.java

@@ -9,6 +9,8 @@ import java.io.OutputStreamWriter;
 import java.net.HttpURLConnection;
 import java.net.URL;
 import java.net.URLConnection;
+import java.util.ArrayList;
+import java.util.List;
 
 import com.google.gson.Gson;
 import com.google.gson.JsonElement;
@@ -16,6 +18,7 @@ import com.google.gson.JsonObject;
 import com.google.gson.JsonParser;
 
 import de.Linus122.TelegramComponents.Chat;
+import de.Linus122.TelegramComponents.ChatMessageToMc;
 
 
 public class Telegram {
@@ -25,6 +28,12 @@ public class Telegram {
 	static int lastUpdate = 0;
 	public String token;
 
+	private List<TelegramActionListener> listeners = new ArrayList<TelegramActionListener>();
+	
+	public void addListener(TelegramActionListener actionListener){
+		listeners.add(actionListener);
+	}
+	
 	public boolean auth(String token){
 		this.token = token;
 		return reconnect();
@@ -90,7 +99,11 @@ public class Telegram {
 									Main.link(Main.data.linkCodes.get(text), id);
 									Main.data.linkCodes.remove(text);
 								}else if(Main.data.linkedChats.containsKey(id)){
-									Main.sendToMC(Main.data.linkedChats.get(id), text, id);
+									ChatMessageToMc chatMsg = new ChatMessageToMc(Main.data.linkedChats.get(id), text, id);
+									for(TelegramActionListener actionListener : listeners){
+										actionListener.onSendToMinecraft(chatMsg);
+									}
+									Main.sendToMC(chatMsg);
 								}else{
 									this.sendMsg(id, "Sorry, please link your account with /linktelegram ingame to use the chat!");
 								}
@@ -109,14 +122,15 @@ public class Telegram {
 	}
 	
 	public void sendMsg(int id, String msg){
-		Gson gson = new Gson();
 		Chat chat = new Chat();
 		chat.chat_id = id;
 		chat.text = msg;
-		post("sendMessage", gson.toJson(chat, Chat.class));
-		
+		sendMsg(chat);
 	}
 	public void sendMsg(Chat chat){
+		for(TelegramActionListener actionListener : listeners){
+			actionListener.onSendToTelegram(chat);
+		}
 		Gson gson = new Gson();
 		post("sendMessage", gson.toJson(chat, Chat.class));
 		
@@ -127,7 +141,8 @@ public class Telegram {
 				Gson gson = new Gson();
 				for(int id : Main.data.ids){
 					chat.chat_id = id;
-					post("sendMessage", gson.toJson(chat, Chat.class));
+					//post("sendMessage", gson.toJson(chat, Chat.class));
+					sendMsg(chat);
 				}
 			}
 		}).start();

+ 9 - 0
src/main/java/de/Linus122/TelegramChat/TelegramActionListener.java

@@ -0,0 +1,9 @@
+package de.Linus122.TelegramChat;
+
+import de.Linus122.TelegramComponents.Chat;
+import de.Linus122.TelegramComponents.ChatMessageToMc;
+
+public interface TelegramActionListener {
+	public void onSendToTelegram(Chat chat);
+	public void onSendToMinecraft(ChatMessageToMc chatMsg);
+}

+ 0 - 0
src/de/Linus122/TelegramChat/TelegramCmd.java → src/main/java/de/Linus122/TelegramChat/TelegramCmd.java


+ 0 - 0
src/de/Linus122/TelegramComponents/Chat.java → src/main/java/de/Linus122/TelegramComponents/Chat.java


+ 39 - 0
src/main/java/de/Linus122/TelegramComponents/ChatMessageToMc.java

@@ -0,0 +1,39 @@
+package de.Linus122.TelegramComponents;
+
+import java.util.UUID;
+
+public class ChatMessageToMc {
+	UUID uuid_sender;
+	String content;
+	int chatID_sender;
+	
+	public ChatMessageToMc(UUID uuid_sender, String content, int chatID_sender){
+		this.uuid_sender = uuid_sender;
+		this.content = content;
+		this.chatID_sender = chatID_sender;
+	}
+
+	public UUID getUuid_sender() {
+		return uuid_sender;
+	}
+
+	public void setUuid_sender(UUID uuid_sender) {
+		this.uuid_sender = uuid_sender;
+	}
+
+	public String getContent() {
+		return content;
+	}
+
+	public void setContent(String content) {
+		this.content = content;
+	}
+
+	public int getChatID_sender() {
+		return chatID_sender;
+	}
+
+	public void setChatID_sender(int chatID_sender) {
+		this.chatID_sender = chatID_sender;
+	}
+}

+ 0 - 0
src/config.yml → src/main/resources/config.yml


+ 1 - 1
src/plugin.yml → src/main/resources/plugin.yml

@@ -1,6 +1,6 @@
 name: TelegramChat
 main: de.Linus122.TelegramChat.Main
-version: 1.0.9.7
+version: ${project.version}
 authors: [Linus122]
 description: Brings minecraft chat to Telegram!
 commands: