Skip to content

Commit

Permalink
version 2.2
Browse files Browse the repository at this point in the history
add worldname (mc) behind username in tg
each worldname can be replaced in config
[media] message now work even if no caption
each media type now have own text
change game version to 1.16
smol renaming
  • Loading branch information
SvineruS committed Aug 29, 2020
1 parent 80829c3 commit 1c0ecdc
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 21 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>ua.kpi.mc</groupId>
<artifactId>mctotg</artifactId>
<version>2.1</version>
<version>2.2</version>
<packaging>jar</packaging>

<name>Mc2TgBridge</name>
Expand Down Expand Up @@ -68,7 +68,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.15.2-R0.1-SNAPSHOT</version>
<version>1.16.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/ua/kpi/mc/mctotg/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ public void send_msg(String text, Integer replyTo) {
public void onResponse(SendMessage sendMessage, SendResponse sendResponse) {}

@Override
public void onFailure(SendMessage sendMessage, IOException e) { e.printStackTrace();
}
public void onFailure(SendMessage sendMessage, IOException e) {e.printStackTrace();}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import java.util.Arrays;

public class CommandTg implements CommandExecutor {
public class CommandListener implements CommandExecutor {

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
Expand All @@ -22,19 +22,23 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
return true;
}
}

if (args.length >= 2 && sender instanceof Player) {
String name = ((Player) sender).getName();
String name = sender.getName();
try {
int replyTo = Integer.parseInt(args[0]);
String text = String.join(" ", Arrays.copyOfRange(args, 1, args.length));
Core.McToTg(name, text, replyTo);
String world = ((Player) sender).getWorld().getName();

Core.McToTg(name, text, replyTo, world);
return true;
}
catch (NumberFormatException e)
{
return false;
}
}

return false;
}
}
30 changes: 21 additions & 9 deletions src/main/java/ua/kpi/mc/mctotg/Core.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ua.kpi.mc.mctotg;

import com.pengrad.telegrambot.Callback;
import com.pengrad.telegrambot.model.Update;
import com.pengrad.telegrambot.model.*;
import com.pengrad.telegrambot.request.GetChat;
import com.pengrad.telegrambot.request.SetChatDescription;
import com.pengrad.telegrambot.response.BaseResponse;
Expand All @@ -21,13 +21,23 @@ static void TgToMc(Update update) {
name += " " + update.message().from().lastName();
name = name.replaceAll("[^\\x00-\\x7Fа-яА-ЯёЁіІїЇ]", "✭");

String message_text;
String message_text = "";
String media = null;
if (update.message().text() != null)
message_text = update.message().text();
else if (update.message().caption() != null)
message_text = update.message().caption();
else
return;
else {
if (update.message().audio() != null) media = "аудио";
else if (update.message().sticker() != null) media = "стикер";
else if (update.message().animation() != null) media = "гиф";
else if (update.message().photo() != null) media = "фото";
else if (update.message().video() != null) media = "видео";
else if (update.message().videoNote() != null) media = "кругляш";
else if (update.message().voice() != null) media = "войс";
else if (update.message().document() != null) media = "документ";
else media = "медиа";

if (update.message().caption() != null) message_text = update.message().caption();
}

message_text = message_text.replaceAll("[^\\x00-\\x7Fа-яА-ЯёЁіІїЇ]", "✭");

Expand All @@ -36,7 +46,7 @@ else if (update.message().caption() != null)

String cmd =
"{\"text\":\"[" + name + "] \",\"color\":\"aqua\",\"clickEvent\":{\"action\":\"suggest_command\",\"value\":\"/tg " + msg_id + " \"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":\"Нажми, что бы ответить\"}}," +
(update.message().caption() == null ? "" : "{\"text\":\"[медиа] \",\"color\":\"gold\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"" + link + "\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":\"Нажми, что бы открыть телегу\"}},") +
(media == null ? "" : "{\"text\":\"[" + media + "] \",\"color\":\"gold\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"" + link + "\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":\"Нажми, что бы открыть телегу\"}},") +
"{\"text\":\"" + message_text + "\",\"color\":\"white\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"" + link + "\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":\"Нажми, что бы открыть телегу\"}}]";

new BukkitRunnable() {
Expand All @@ -45,10 +55,12 @@ else if (update.message().caption() != null)
}.runTask(Main.instance);
}

static void McToTg(String name, String text, Integer replyTo) {
static void McToTg(String name, String text, Integer replyTo, String world) {
if (text.equals(""))
return;
String msg = "<b>" + name + "</b>: " + text;
world = Main.config.worldNamesDict.getOrDefault(world, world);

String msg = world + "<b>" + name + "</b>" + ": " + text;
Main.bot.send_msg(msg, replyTo);
}

Expand Down
7 changes: 5 additions & 2 deletions src/main/java/ua/kpi/mc/mctotg/EventListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ public class EventListener implements Listener {
@EventHandler(priority = EventPriority.LOW)
public void onPlayerMessageEvent(AsyncPlayerChatEvent event) {
String name = event.getPlayer().getName();
String world = event.getPlayer().getWorld().getName();
String text = event.getMessage();
if (!text.startsWith(Main.config.msgStartWith))
return;
text = text.substring(Main.config.msgStartWith.length());
Core.McToTg(name, text, null);


Core.McToTg(name, text, null, world);
}

@EventHandler
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerJoin(PlayerJoinEvent event) {
int online = Bukkit.getServer().getOnlinePlayers().size();
Core.UpdateDescription(Integer.toString(online));
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/ua/kpi/mc/mctotg/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.HashMap;

public final class Main extends JavaPlugin {
static Myconfig config;
static Bot bot;
Expand All @@ -22,7 +24,7 @@ public void onEnable() {

Core.UpdateDescription("да");

getCommand("tg").setExecutor(new CommandTg());
getCommand("tg").setExecutor(new CommandListener());
getServer().getPluginManager().registerEvents(new EventListener(), this);
}

Expand Down Expand Up @@ -56,10 +58,13 @@ class Myconfig {
String token;
Long chatId;
String msgStartWith;
HashMap<String, String> worldNamesDict = new HashMap<>();

public Myconfig(FileConfiguration config) {
this.token = config.getString("token");
this.chatId = config.getLong("chatId");
this.msgStartWith = config.getString("msgStartWith");
if (config.getConfigurationSection("worldNamesDict") != null)
this.worldNamesDict = (HashMap) config.getConfigurationSection("worldNamesDict").getValues(false);
}
}
6 changes: 5 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
token: "your token"
chatId: 228
# for example use '!' to send only global chat, if corresponding plugin installed
msgStartWith: ''
msgStartWith: ''
worldNamesDict:
world: ''
world_nether: ' 🌋'
world_the_end: ' 🔚'
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Mc2TgBridge
version: ${project.version}
main: ua.kpi.mc.mctotg.Main
api-version: "1.15"
api-version: "1.16"
authors: [svinerus, hatomist]
commands:
tg:
Expand Down

0 comments on commit 1c0ecdc

Please sign in to comment.