Skip to content

Commit d85e1b8

Browse files
committed
actual init
1 parent 5dd2c0f commit d85e1b8

File tree

8 files changed

+327
-0
lines changed

8 files changed

+327
-0
lines changed

pom.xml

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>net.sinender</groupId>
8+
<artifactId>BungeeMsg</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<packaging>jar</packaging>
11+
12+
<name>BungeeMsg</name>
13+
14+
<properties>
15+
<java.version>1.8</java.version>
16+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17+
</properties>
18+
19+
<build>
20+
<defaultGoal>clean package</defaultGoal>
21+
<plugins>
22+
<plugin>
23+
<groupId>org.apache.maven.plugins</groupId>
24+
<artifactId>maven-compiler-plugin</artifactId>
25+
<version>3.8.1</version>
26+
<configuration>
27+
<source>8</source>
28+
<target>8</target>
29+
</configuration>
30+
</plugin>
31+
<plugin>
32+
<groupId>org.apache.maven.plugins</groupId>
33+
<artifactId>maven-shade-plugin</artifactId>
34+
<version>3.2.4</version>
35+
<executions>
36+
<execution>
37+
<phase>package</phase>
38+
<goals>
39+
<goal>shade</goal>
40+
</goals>
41+
<configuration>
42+
<createDependencyReducedPom>false</createDependencyReducedPom>
43+
</configuration>
44+
</execution>
45+
</executions>
46+
</plugin>
47+
</plugins>
48+
<resources>
49+
<resource>
50+
<directory>src/main/resources</directory>
51+
<filtering>true</filtering>
52+
</resource>
53+
</resources>
54+
</build>
55+
56+
<repositories>
57+
<repository>
58+
<id>sonatype</id>
59+
<url>https://oss.sonatype.org/content/groups/public/</url>
60+
</repository>
61+
<repository>
62+
<id>jitpack-repo</id>
63+
<url>https://jitpack.io</url>
64+
</repository>
65+
</repositories>
66+
67+
<dependencies>
68+
<dependency>
69+
<groupId>net.md-5</groupId>
70+
<artifactId>bungeecord-api</artifactId>
71+
<version>1.19-R0.1-SNAPSHOT</version>
72+
<type>jar</type>
73+
<scope>provided</scope>
74+
</dependency>
75+
<dependency>
76+
<groupId>org.projectlombok</groupId>
77+
<artifactId>lombok</artifactId>
78+
<version>1.18.22</version>
79+
<scope>provided</scope>
80+
</dependency>
81+
<dependency>
82+
<groupId>org.mongodb</groupId>
83+
<artifactId>mongo-java-driver</artifactId>
84+
<version>3.12.10</version>
85+
<scope>compile</scope>
86+
</dependency>
87+
<dependency>
88+
<groupId>org.apache.commons</groupId>
89+
<artifactId>commons-collections4</artifactId>
90+
<version>4.1</version>
91+
<scope>compile</scope>
92+
</dependency>
93+
</dependencies>
94+
</project>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package net.sinender.bungeemsg;
2+
3+
import net.md_5.bungee.api.ProxyServer;
4+
import net.md_5.bungee.api.connection.ProxiedPlayer;
5+
import net.md_5.bungee.api.plugin.Plugin;
6+
import net.md_5.bungee.config.Configuration;
7+
import net.md_5.bungee.config.ConfigurationProvider;
8+
import net.md_5.bungee.config.YamlConfiguration;
9+
import net.sinender.bungeemsg.commands.MsgCommand;
10+
import net.sinender.bungeemsg.pubsub.PubSub;
11+
12+
import java.io.File;
13+
import java.io.IOException;
14+
import java.util.Arrays;
15+
16+
public class BungeeMsg extends Plugin {
17+
public static BungeeMsg instance;
18+
public static Configuration config;
19+
public static PubSub pubsub;
20+
public static BungeeMsg getInstance() {
21+
return instance;
22+
}
23+
24+
@Override
25+
public void onEnable() {
26+
instance = this;
27+
try {
28+
config = ConfigurationProvider.getProvider(YamlConfiguration.class).load(new File(getDataFolder(), "config.yml"));
29+
new File(getDataFolder(), "config.yml");
30+
pubsub = new PubSub(this);
31+
pubsub.registerListener("msgVerify", args -> {
32+
String sender = args[0];
33+
String target = args[1];
34+
String msg = String.join(" ", Arrays.copyOfRange(args, 2, args.length));
35+
for (ProxiedPlayer player : ProxyServer.getInstance().getPlayers()) {
36+
if (player.getName().equalsIgnoreCase(target)) {
37+
player.sendMessage("§dFrom §e" + sender + "§7: §f" + msg);
38+
pubsub.publish("msgSent", sender, target, msg);
39+
return;
40+
}
41+
}
42+
});
43+
pubsub.registerListener("msgSent", args -> {
44+
String sender = args[0];
45+
String target = args[1];
46+
String msg = args[2];
47+
for (ProxiedPlayer player : ProxyServer.getInstance().getPlayers()) {
48+
if (player.getName().equalsIgnoreCase(sender)) {
49+
player.sendMessage("§dTo §e" + target + "§7: §f" + msg);
50+
return;
51+
}
52+
}
53+
});
54+
ProxyServer.getInstance().getPluginManager().registerCommand(this, new MsgCommand());
55+
56+
} catch (IOException e) {
57+
e.printStackTrace();
58+
}
59+
}
60+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package net.sinender.bungeemsg.commands;
2+
3+
import net.md_5.bungee.api.CommandSender;
4+
import net.md_5.bungee.api.ProxyServer;
5+
import net.md_5.bungee.api.connection.ProxiedPlayer;
6+
import net.md_5.bungee.api.plugin.Command;
7+
import net.sinender.bungeemsg.BungeeMsg;
8+
9+
import java.util.Arrays;
10+
11+
public class MsgCommand extends Command {
12+
public MsgCommand() {
13+
super("msg");
14+
}
15+
@Override
16+
public void execute(CommandSender sender, String[] args) {
17+
String target = args[0];
18+
String msg = String.join(" ", Arrays.copyOfRange(args, 1, args.length));
19+
for (ProxiedPlayer player : ProxyServer.getInstance().getPlayers()) {
20+
if (player.getName().equalsIgnoreCase(target)) {
21+
player.sendMessage("§dFrom §e" + sender.getName() + "§7: §f" + msg);
22+
sender.sendMessage("§dTo §e" + target + "§7: §f" + msg);
23+
return;
24+
}
25+
}
26+
BungeeMsg.pubsub.publish("msgVerify", sender.getName(), target, msg);
27+
}
28+
}
29+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package net.sinender.bungeemsg.pubsub;
2+
3+
/**
4+
* @author Andrew R.
5+
*/
6+
@FunctionalInterface
7+
public interface Callback {
8+
void onMessage(String[] args);
9+
}
10+
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package net.sinender.bungeemsg.pubsub;
2+
3+
import com.mongodb.ConnectionString;
4+
import com.mongodb.MongoClientSettings;
5+
import com.mongodb.client.MongoClient;
6+
import com.mongodb.client.MongoClients;
7+
import com.mongodb.client.MongoCollection;
8+
import com.mongodb.client.MongoDatabase;
9+
import net.sinender.bungeemsg.BungeeMsg;
10+
import org.bson.Document;
11+
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
15+
public class MongoDBPS {
16+
private final MongoCollection<Document> collection;
17+
private static MongoClient client;
18+
private static MongoDatabase database;
19+
20+
public MongoDBPS(String collection) {
21+
this.collection = database.getCollection(collection);
22+
}
23+
24+
public static void connect() {
25+
// connect to mongoDB
26+
BungeeMsg.getInstance().getLogger().info("Connecting to MongoDB...");
27+
MongoClientSettings settings = MongoClientSettings.builder()
28+
.applyConnectionString(new ConnectionString(BungeeMsg.config.getString("mongo.uri")))
29+
.applyToSslSettings(x -> x.enabled(BungeeMsg.config.getBoolean("mongo.useSSL")))
30+
.build();
31+
client = MongoClients.create(settings);
32+
database = client.getDatabase(BungeeMsg.config.getString("mongo.database"));
33+
BungeeMsg.getInstance().getLogger().info("Successfully connected to MongoDB!");
34+
}
35+
36+
public Document getDocument(String uuid) {
37+
// get document from mongoDB
38+
Document query = new Document("key", uuid);
39+
return collection.find(query).first();
40+
}
41+
42+
public List<Document> getDocuments() {
43+
return collection.find().into(new ArrayList<>());
44+
}
45+
46+
public Document getLatestDocument() {
47+
return collection.find().sort(new Document("_id", -1)).first();
48+
}
49+
50+
public void setDocument(String uuid, Document document) {
51+
// set document in mongoDB
52+
collection.insertOne(document);
53+
}
54+
55+
public Document updateDocument(Document document) {
56+
Document query = new Document("key", document.getString("key"));
57+
if (collection.find(query).first() == null) {
58+
collection.insertOne(document);
59+
} else {
60+
collection.replaceOne(query, document);
61+
}
62+
return document;
63+
}
64+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package net.sinender.bungeemsg.pubsub;
2+
3+
import net.sinender.bungeemsg.BungeeMsg;
4+
import org.apache.commons.collections4.CollectionUtils;
5+
import org.bson.Document;
6+
7+
import java.util.*;
8+
9+
public class PubSub {
10+
HashMap<String, Callback> callbacks = new HashMap<>();
11+
ArrayList<String> listened = new ArrayList<>();
12+
private final BungeeMsg plugin;
13+
private final MongoDBPS pubsubDB;
14+
15+
public PubSub(BungeeMsg plugin) {
16+
this.plugin = plugin;
17+
plugin.getLogger().info("Starting PubSub");
18+
MongoDBPS.connect();
19+
pubsubDB = new MongoDBPS(BungeeMsg.config.getString("mongo.pubsub"));
20+
new Timer().scheduleAtFixedRate(new TimerTask() {
21+
@Override
22+
public void run() {
23+
update();
24+
}
25+
}, 0, 200);
26+
}
27+
28+
public void publish(String key, Object... objects) {
29+
String s = "";
30+
for (Object obj : objects) {
31+
s += ";" + obj.toString();
32+
}
33+
publish(key, s.substring(1));
34+
}
35+
36+
public void publish(String key, String msg) {
37+
Document doc = new Document("key", key).append("msg", msg).append("uuid", UUID.randomUUID().toString());
38+
pubsubDB.setDocument(key, doc);
39+
}
40+
41+
public void registerListener(String channel, Callback callback) {
42+
callbacks.putIfAbsent(channel, callback);
43+
System.out.println("Registering a new pubsub listener for " + channel);
44+
}
45+
public void update() {
46+
for (Document doc : pubsubDB.getDocuments()) {
47+
String key = doc.getString("key");
48+
String msg = doc.getString("msg");
49+
String uuid = doc.getString("uuid");
50+
if (CollectionUtils.containsAny(listened, Collections.singletonList(uuid))) {
51+
continue;
52+
}
53+
if (CollectionUtils.containsAny(callbacks.keySet(), Collections.singletonList(key))) {
54+
Callback callback = callbacks.get(key);
55+
listened.add(uuid);
56+
callback.onMessage(msg.split(";"));
57+
}
58+
}
59+
}
60+
}

src/main/resources/bungee.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
name: BungeeMsg
2+
version: '${project.version}'
3+
main: net.sinender.cumbungee.BungeeMsg

src/main/resources/config.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
mongo:
2+
useSSL: false
3+
uri: "localhost"
4+
database: "test"
5+
pubsub: "test"
6+
7+

0 commit comments

Comments
 (0)