Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,24 @@ TelegramChat is a Bukkit plugin compatible with Paper/Spigot versions 1.7 throug
</dependency>
</dependencies>
```

## Developers
* [Jenkins](https://ci.spaceio.xyz/job/TelegramChat/lastBuild/xyz.spaceio$telegramchat/) latest builds, including unstable ones
* [Repository](https://repo.spaceio.xyz/#browse/browse:maven-snapshots:xyz%2Fspaceio%2Ftelegramchat)

### API
```xml
<repository>
<id>spaceio-repo</id>
<url>https://repo.spaceio.xyz/repository/maven-public/</url>
</repository>

<dependencies>
<dependency>
<groupId>xyz.spaceio</groupId>
<artifactId>telegramchat</artifactId>
<version>VERSION_HERE-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
```
Binary file added lib/Chatty.jar
Binary file not shown.
77 changes: 77 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,33 @@
</repositories>

<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.22.0-GA</version>
</dependency>
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>2.3.2</version>
</dependency>

<!-- Runtime, com.sun.xml.bind module -->
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.6.7.Final</version>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
Expand All @@ -49,6 +76,13 @@
<version>1.7</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.roufid.tutorials</groupId>
<artifactId>example-app</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/Chatty.jar</systemPath>
</dependency>
</dependencies>
<distributionManagement>
<repository>
Expand All @@ -71,6 +105,8 @@
<includes>
<include>plugin.yml</include>
<include>config.yml</include>
<include>hikaricp.properties</include>
<include>META-INF/persistence.xml</include>
</includes>
</resource>
</resources>
Expand Down Expand Up @@ -98,6 +134,47 @@
<finalName>${project.build.finalName}</finalName>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>

<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>

<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<groupId>com.roufid.tutorials</groupId>
<artifactId>example-app</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<file>${basedir}/lib/Chatty.jar</file>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
9 changes: 0 additions & 9 deletions src/main/java/de/Linus122/Telegram/API.java

This file was deleted.

79 changes: 31 additions & 48 deletions src/main/java/de/Linus122/Telegram/Telegram.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
package de.Linus122.Telegram;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import de.Linus122.TelegramChat.TelegramChat;
import de.Linus122.TelegramComponents.Chat;
import de.Linus122.TelegramComponents.ChatMessageToMc;
import de.Linus122.TelegramComponents.ChatMessageToTelegram;
import de.Linus122.TelegramComponents.Update;
import org.bukkit.Bukkit;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
Expand All @@ -11,17 +22,8 @@
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import de.Linus122.TelegramComponents.ChatMessageToTelegram;
import de.Linus122.TelegramChat.TelegramChat;
import de.Linus122.TelegramComponents.Chat;
import de.Linus122.TelegramComponents.ChatMessageToMc;
import de.Linus122.TelegramComponents.Update;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class Telegram {
public JsonObject authJson;
Expand All @@ -38,6 +40,8 @@ public class Telegram {

private Gson gson = new Gson();

private Set<Long> sentWarnings = ConcurrentHashMap.newKeySet();

public void addListener(TelegramActionListener actionListener) {
listeners.add(actionListener);
}
Expand All @@ -62,7 +66,7 @@ public boolean reconnect() {
}

public boolean getUpdate() {
JsonObject up = null;
JsonObject up;
try {
up = sendGet(String.format(API_URL_GETUPDATES, TelegramChat.getBackend().getToken(), lastUpdate + 1));
} catch (IOException e) {
Expand All @@ -83,33 +87,12 @@ public boolean getUpdate() {
Chat chat = update.getMessage().getChat();

if (chat.isPrivate()) {
// private chat
if (!TelegramChat.getBackend().chat_ids.contains(chat.getId()))
TelegramChat.getBackend().chat_ids.add(chat.getId());

if (update.getMessage().getText() != null) {
String text = update.getMessage().getText();
if (text.length() == 0)
return true;
if (text.equals("/start")) {
if (TelegramChat.getBackend().isFirstUse()) {
TelegramChat.getBackend().setFirstUse(false);
ChatMessageToTelegram chat2 = new ChatMessageToTelegram();
chat2.chat_id = chat.getId();
chat2.parse_mode = "Markdown";
chat2.text = Utils.formatMSG("setup-msg")[0];
this.sendMsg(chat2);
}
this.sendMsg(chat.getId(), Utils.formatMSG("can-see-but-not-chat")[0]);
} else {
handleUserMessage(text, update);
}
}
// skip

} else if (!chat.isPrivate()) {
// group chat
long id = chat.getId();
if (!TelegramChat.getBackend().chat_ids.contains(id))
if (!TelegramChat.getBackend().chat_ids.contains(id) && TelegramChat.getBackend().chat_ids.size() < 1)
TelegramChat.getBackend().chat_ids.add(id);

if (update.getMessage().getText() != null) {
Expand All @@ -127,14 +110,14 @@ public boolean getUpdate() {

public void handleUserMessage(String text, Update update) {
Chat chat = update.getMessage().getChat();
long user_id = update.getMessage().getFrom().getId();
long chatId = update.getMessage().getFrom().getId();
if (TelegramChat.getBackend().getLinkCodes().containsKey(text)) {
// LINK
TelegramChat.link(TelegramChat.getBackend().getUUIDFromLinkCode(text), user_id);
TelegramChat.link(TelegramChat.getBackend().getUUIDFromLinkCode(text), chatId);
TelegramChat.getBackend().removeLinkCode(text);
} else if (TelegramChat.getBackend().getLinkedChats().containsKey(user_id)) {
} else if (TelegramChat.getBackend().containsChatId(chatId)) {
ChatMessageToMc chatMsg = new ChatMessageToMc(
TelegramChat.getBackend().getUUIDFromUserID(user_id), text, chat.getId());
TelegramChat.getBackend().getUUIDFromUserID(chatId), text, chat.getId());

for (TelegramActionListener actionListener : listeners) {
actionListener.onSendToMinecraft(chatMsg);
Expand All @@ -144,7 +127,10 @@ public void handleUserMessage(String text, Update update) {
TelegramChat.sendToMC(chatMsg);
}
} else {
this.sendMsg(chat.getId(), Utils.formatMSG("need-to-link")[0]);
if (!sentWarnings.contains(chat.getId())) {
this.sendMsg(chat.getId(), Utils.formatMSG("need-to-link")[0]);
sentWarnings.add(chat.getId());
}
}
}

Expand All @@ -166,15 +152,12 @@ public void sendMsg(ChatMessageToTelegram chat) {
}

public void sendAll(final ChatMessageToTelegram chat) {
new Thread(new Runnable() {
public void run() {
for (long id : TelegramChat.getBackend().chat_ids) {
chat.chat_id = id;
// post("sendMessage", gson.toJson(chat, Chat.class));
sendMsg(chat);
}
Bukkit.getScheduler().runTaskAsynchronously(TelegramChat.getInstance(), () -> {
for (long id : TelegramChat.getBackend().chat_ids) {
chat.chat_id = id;
sendMsg(chat);
}
}).start();
});
}

public void post(String method, String json) {
Expand Down
33 changes: 28 additions & 5 deletions src/main/java/de/Linus122/TelegramChat/Data.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package de.Linus122.TelegramChat;

import java.util.ArrayList;
import de.Linus122.entity.User;
import de.Linus122.repository.UserRepository;

import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;

public class Data {
private String token = "";
Expand All @@ -14,10 +17,10 @@ public class Data {
// Token : Player ID
private HashMap<String, UUID> linkCodes = new HashMap<String, UUID>();

public List<Long> chat_ids = new ArrayList<Long>();
public List<Long> chat_ids = new CopyOnWriteArrayList<>();

private boolean firstUse = true;


public String getToken() {
return token;
Expand Down Expand Up @@ -61,7 +64,21 @@ public void setFirstUse(boolean firstUse) {
}

public void addChatPlayerLink(long chatID, UUID player) {
linkedChats.put(chatID, player);
final User toUpdate = UserRepository.getInstance().readByPlayerId(player.toString())
.orElseGet(() -> {
final User user = new User();
user.setChatId(chatID);
user.setPlayerId(player.toString());
return user;
});
toUpdate.setChatId(chatID);
UserRepository.getInstance().update(toUpdate);
}

public boolean containsChatId(long chatId) {
return UserRepository.getInstance()
.readByChatId(chatId)
.isPresent();
}

public void addLinkCode(String code, UUID player) {
Expand All @@ -77,7 +94,13 @@ public void removeLinkCode(String code) {
}

public UUID getUUIDFromUserID(long userID) {
return linkedChats.get(userID);
return UserRepository.getInstance()
.readByChatId(userID)
.map(User::getPlayerId)
.map(UUID::fromString)
.orElseThrow(() -> new IllegalArgumentException(String.format("Chat with id %s not found", userID)));
}



}
9 changes: 2 additions & 7 deletions src/main/java/de/Linus122/TelegramChat/LinkTelegramCmd.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package de.Linus122.TelegramChat;

import java.io.IOException;

import de.Linus122.Telegram.Utils;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import de.Linus122.Telegram.Utils;

public class LinkTelegramCmd implements CommandExecutor {

@Override
Expand All @@ -30,9 +27,7 @@ public boolean onCommand(CommandSender cs, Command arg1, String arg2, String[] a

String token = TelegramChat.generateLinkToken();
TelegramChat.getBackend().addLinkCode(token, ((Player) cs).getUniqueId());
cs.sendMessage(Utils.formatMSG("get-token",
TelegramChat.telegramHook.authJson.getAsJsonObject("result").get("username").getAsString(),
TelegramChat.telegramHook.authJson.getAsJsonObject("result").get("username").getAsString(), token));
cs.sendMessage(Utils.formatMSG("get-token", token));

return true;
}
Expand Down
Loading