restructure project

This commit is contained in:
MasterCake 2021-03-31 03:21:31 +02:00
parent df457f2be2
commit 61ee137c95
10 changed files with 88 additions and 41 deletions

View File

@ -6,12 +6,18 @@
<version>1.0.15</version> <version>1.0.15</version>
<name>TelegramChat</name> <name>TelegramChat</name>
<url>https://www.spigotmc.org/resources/telegramchat.16576/</url> <url>https://www.spigotmc.org/resources/telegramchat.16576/</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories> <repositories>
<repository> <repository>
<id>spigot-repo</id> <id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/groups/public/</url> <url>https://hub.spigotmc.org/nexus/content/groups/public/</url>
</repository> </repository>
</repositories> </repositories>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.spigotmc</groupId> <groupId>org.spigotmc</groupId>
@ -20,6 +26,7 @@
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>
<!-- Uses the properties in this file for plugin.yml and config.yml --> <!-- Uses the properties in this file for plugin.yml and config.yml -->
<resources> <resources>

View File

@ -0,0 +1,28 @@
package de.Linus122.Handlers;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import de.Linus122.Telegram.TelegramActionListener;
import de.Linus122.TelegramComponents.ChatMessageToMc;
import de.Linus122.TelegramComponents.ChatMessageToTelegram;
public class BanHandler implements TelegramActionListener{
@Override
public void onSendToTelegram(ChatMessageToTelegram chat) {
// TODO Auto-generated method stub
}
@Override
public void onSendToMinecraft(ChatMessageToMc chatMsg) {
OfflinePlayer player = Bukkit.getOfflinePlayer(chatMsg.getUuid_sender());
if(player != null && player.isBanned()) {
chatMsg.setCancelled(true);
}
}
}

View File

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

View File

@ -1,4 +1,4 @@
package de.Linus122.TelegramChat; package de.Linus122.Telegram;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.BufferedWriter; import java.io.BufferedWriter;
@ -18,6 +18,7 @@ import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import de.Linus122.TelegramComponents.ChatMessageToTelegram; import de.Linus122.TelegramComponents.ChatMessageToTelegram;
import de.Linus122.TelegramChat.TelegramChat;
import de.Linus122.TelegramComponents.Chat; import de.Linus122.TelegramComponents.Chat;
import de.Linus122.TelegramComponents.ChatMessageToMc; import de.Linus122.TelegramComponents.ChatMessageToMc;
import de.Linus122.TelegramComponents.Update; import de.Linus122.TelegramComponents.Update;
@ -63,7 +64,7 @@ public class Telegram {
public boolean getUpdate() { public boolean getUpdate() {
JsonObject up = null; JsonObject up = null;
try { try {
up = sendGet(String.format(API_URL_GETUPDATES, Main.getBackend().getToken(), lastUpdate + 1)); up = sendGet(String.format(API_URL_GETUPDATES, TelegramChat.getBackend().getToken(), lastUpdate + 1));
} catch (IOException e) { } catch (IOException e) {
return false; return false;
} }
@ -81,16 +82,16 @@ public class Telegram {
if (update.getMessage() != null) { if (update.getMessage() != null) {
Chat chat = update.getMessage().getChat(); Chat chat = update.getMessage().getChat();
if (chat.isPrivate()) { if (chat.isPrivate()) {
if (!Main.getBackend().ids.contains(chat.getId())) if (!TelegramChat.getBackend().ids.contains(chat.getId()))
Main.getBackend().ids.add(chat.getId()); TelegramChat.getBackend().ids.add(chat.getId());
if (update.getMessage().getText() != null) { if (update.getMessage().getText() != null) {
String text = update.getMessage().getText(); String text = update.getMessage().getText();
if (text.length() == 0) if (text.length() == 0)
return true; return true;
if (text.equals("/start")) { if (text.equals("/start")) {
if (Main.getBackend().isFirstUse()) { if (TelegramChat.getBackend().isFirstUse()) {
Main.getBackend().setFirstUse(false); TelegramChat.getBackend().setFirstUse(false);
ChatMessageToTelegram chat2 = new ChatMessageToTelegram(); ChatMessageToTelegram chat2 = new ChatMessageToTelegram();
chat2.chat_id = chat.getId(); chat2.chat_id = chat.getId();
chat2.parse_mode = "Markdown"; chat2.parse_mode = "Markdown";
@ -98,18 +99,20 @@ public class Telegram {
this.sendMsg(chat2); this.sendMsg(chat2);
} }
this.sendMsg(chat.getId(), Utils.formatMSG("can-see-but-not-chat")[0]); this.sendMsg(chat.getId(), Utils.formatMSG("can-see-but-not-chat")[0]);
} else if (Main.getBackend().getLinkCodes().containsKey(text)) { } else if (TelegramChat.getBackend().getLinkCodes().containsKey(text)) {
// LINK // LINK
Main.link(Main.getBackend().getUUIDFromLinkCode(text), chat.getId()); TelegramChat.link(TelegramChat.getBackend().getUUIDFromLinkCode(text), chat.getId());
Main.getBackend().removeLinkCode(text); TelegramChat.getBackend().removeLinkCode(text);
} else if (Main.getBackend().getLinkedChats().containsKey(chat.getId())) { } else if (TelegramChat.getBackend().getLinkedChats().containsKey(chat.getId())) {
ChatMessageToMc chatMsg = new ChatMessageToMc( ChatMessageToMc chatMsg = new ChatMessageToMc(
Main.getBackend().getUUIDFromChatID(chat.getId()), text, chat.getId()); TelegramChat.getBackend().getUUIDFromChatID(chat.getId()), text, chat.getId());
for (TelegramActionListener actionListener : listeners) { for (TelegramActionListener actionListener : listeners) {
actionListener.onSendToMinecraft(chatMsg); actionListener.onSendToMinecraft(chatMsg);
} }
if(!chatMsg.isCancelled()){ if(!chatMsg.isCancelled()){
Main.sendToMC(chatMsg); TelegramChat.sendToMC(chatMsg);
} }
} else { } else {
this.sendMsg(chat.getId(), Utils.formatMSG("need-to-link")[0]); this.sendMsg(chat.getId(), Utils.formatMSG("need-to-link")[0]);
@ -118,8 +121,8 @@ public class Telegram {
} else if (!chat.isPrivate()) { } else if (!chat.isPrivate()) {
int id = chat.getId(); int id = chat.getId();
if (!Main.getBackend().ids.contains(id)) if (!TelegramChat.getBackend().ids.contains(id))
Main.getBackend().ids.add(id); TelegramChat.getBackend().ids.add(id);
} }
} }
@ -149,7 +152,7 @@ public class Telegram {
public void sendAll(final ChatMessageToTelegram chat) { public void sendAll(final ChatMessageToTelegram chat) {
new Thread(new Runnable() { new Thread(new Runnable() {
public void run() { public void run() {
for (int id : Main.getBackend().ids) { for (int id : TelegramChat.getBackend().ids) {
chat.chat_id = id; chat.chat_id = id;
// post("sendMessage", gson.toJson(chat, Chat.class)); // post("sendMessage", gson.toJson(chat, Chat.class));
sendMsg(chat); sendMsg(chat);
@ -161,7 +164,7 @@ public class Telegram {
public 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(String.format(API_URL_GENERAL, Main.getBackend().getToken(), method)); URL url = new URL(String.format(API_URL_GENERAL, TelegramChat.getBackend().getToken(), method));
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST"); connection.setRequestMethod("POST");
connection.setDoInput(true); connection.setDoInput(true);

View File

@ -1,4 +1,4 @@
package de.Linus122.TelegramChat; package de.Linus122.Telegram;
import de.Linus122.TelegramComponents.ChatMessageToTelegram; import de.Linus122.TelegramComponents.ChatMessageToTelegram;
import de.Linus122.TelegramComponents.ChatMessageToMc; import de.Linus122.TelegramComponents.ChatMessageToMc;

View File

@ -1,4 +1,4 @@
package de.Linus122.TelegramChat; package de.Linus122.Telegram;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;

View File

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

View File

@ -7,6 +7,8 @@ import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import de.Linus122.Telegram.Utils;
public class LinkTelegramCmd implements CommandExecutor { public class LinkTelegramCmd implements CommandExecutor {
@Override @Override
@ -18,19 +20,19 @@ public class LinkTelegramCmd implements CommandExecutor {
cs.sendMessage(Utils.formatMSG("no-permissions")[0]); cs.sendMessage(Utils.formatMSG("no-permissions")[0]);
return true; return true;
} }
if (Main.getBackend() == null) { if (TelegramChat.getBackend() == null) {
Main.initBackend(); TelegramChat.initBackend();
} }
if (Main.telegramHook.authJson == null) { if (TelegramChat.telegramHook.authJson == null) {
cs.sendMessage(Utils.formatMSG("need-to-add-bot-first")[0]); cs.sendMessage(Utils.formatMSG("need-to-add-bot-first")[0]);
return true; return true;
} }
String token = Main.generateLinkToken(); String token = TelegramChat.generateLinkToken();
Main.getBackend().addLinkCode(token, ((Player) cs).getUniqueId()); TelegramChat.getBackend().addLinkCode(token, ((Player) cs).getUniqueId());
cs.sendMessage(Utils.formatMSG("get-token", cs.sendMessage(Utils.formatMSG("get-token",
Main.telegramHook.authJson.getAsJsonObject("result").get("username").getAsString(), TelegramChat.telegramHook.authJson.getAsJsonObject("result").get("username").getAsString(),
Main.telegramHook.authJson.getAsJsonObject("result").get("username").getAsString(), token)); TelegramChat.telegramHook.authJson.getAsJsonObject("result").get("username").getAsString(), token));
return true; return true;
} }

View File

@ -25,11 +25,14 @@ import org.bukkit.plugin.java.JavaPlugin;
import com.google.gson.Gson; import com.google.gson.Gson;
import de.Linus122.Handlers.BanHandler;
import de.Linus122.Metrics.Metrics; import de.Linus122.Metrics.Metrics;
import de.Linus122.Telegram.Telegram;
import de.Linus122.Telegram.Utils;
import de.Linus122.TelegramComponents.ChatMessageToMc; import de.Linus122.TelegramComponents.ChatMessageToMc;
import de.Linus122.TelegramComponents.ChatMessageToTelegram; import de.Linus122.TelegramComponents.ChatMessageToTelegram;
public class Main extends JavaPlugin implements Listener { public class TelegramChat extends JavaPlugin implements Listener {
private static File datad = new File("plugins/TelegramChat/data.json"); private static File datad = new File("plugins/TelegramChat/data.json");
private static FileConfiguration cfg; private static FileConfiguration cfg;
@ -81,6 +84,8 @@ public class Main extends JavaPlugin implements Listener {
telegramHook = new Telegram(); telegramHook = new Telegram();
telegramHook.auth(data.getToken()); telegramHook.auth(data.getToken());
telegramHook.addListener(new BanHandler());
Bukkit.getScheduler().runTaskTimerAsynchronously(this, () -> { Bukkit.getScheduler().runTaskTimerAsynchronously(this, () -> {
boolean connectionLost = false; boolean connectionLost = false;
@ -131,7 +136,7 @@ public class Main extends JavaPlugin implements Listener {
private static void sendToMC(UUID uuid, String msg, int sender) { private static void sendToMC(UUID uuid, String msg, int sender) {
OfflinePlayer op = Bukkit.getOfflinePlayer(uuid); OfflinePlayer op = Bukkit.getOfflinePlayer(uuid);
List<Integer> recievers = new ArrayList<Integer>(); List<Integer> recievers = new ArrayList<Integer>();
recievers.addAll(Main.data.ids); recievers.addAll(TelegramChat.data.ids);
recievers.remove((Object) sender); recievers.remove((Object) sender);
String msgF = Utils.formatMSG("general-message-to-mc", op.getName(), msg)[0]; String msgF = Utils.formatMSG("general-message-to-mc", op.getName(), msg)[0];
for (int id : recievers) { for (int id : recievers) {
@ -142,7 +147,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.addChatPlayerLink(chatID, player); TelegramChat.data.addChatPlayerLink(chatID, player);
OfflinePlayer p = Bukkit.getOfflinePlayer(player); OfflinePlayer p = Bukkit.getOfflinePlayer(player);
telegramHook.sendMsg(chatID, "Success! Linked " + p.getName()); telegramHook.sendMsg(chatID, "Success! Linked " + p.getName());
} }

View File

@ -16,17 +16,17 @@ public class TelegramCmd implements CommandExecutor {
cs.sendMessage("§c/telegram [token]"); cs.sendMessage("§c/telegram [token]");
return true; return true;
} }
if (Main.getBackend() == null) { if (TelegramChat.getBackend() == null) {
Main.initBackend(); TelegramChat.initBackend();
} }
Main.getBackend().setToken(args[0]); TelegramChat.getBackend().setToken(args[0]);
Main.save(); TelegramChat.save();
boolean success = false; boolean success = false;
success = Main.telegramHook.auth(Main.getBackend().getToken()); success = TelegramChat.telegramHook.auth(TelegramChat.getBackend().getToken());
if (success) { if (success) {
cs.sendMessage("§cSuccessfully connected to Telegram!"); cs.sendMessage("§cSuccessfully connected to Telegram!");
cs.sendMessage("§aAdd " + Main.telegramHook.authJson.getAsJsonObject("result").get("username").getAsString() cs.sendMessage("§aAdd " + TelegramChat.telegramHook.authJson.getAsJsonObject("result").get("username").getAsString()
+ " to Telegram!"); + " to Telegram!");
} else { } else {
cs.sendMessage("§cWrong token. Paste in the whole token!"); cs.sendMessage("§cWrong token. Paste in the whole token!");