Skip to content

Commit

Permalink
3.0.0 - Added support for bungeecord!
Browse files Browse the repository at this point in the history
  • Loading branch information
LOOHP committed Sep 13, 2020
1 parent 178bbef commit b8b1bd7
Show file tree
Hide file tree
Showing 33 changed files with 2,102 additions and 144 deletions.
96 changes: 64 additions & 32 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>InteractiveChat</groupId>
<artifactId>InteractiveChat</artifactId>
<version>2.7.2</version>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>InteractiveChat</groupId>
<artifactId>InteractiveChat</artifactId>
<version>3.0.0</version>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
Expand All @@ -15,33 +17,33 @@
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>ISO-8859-1</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>ISO-8859-1</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<repositories>
Expand Down Expand Up @@ -69,9 +71,19 @@
<id>dmulloy2-repo</id>
<url>https://repo.dmulloy2.net/nexus/repository/public/</url>
</repository>
<repository>
<id>bungeecord-repo</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.11</version>
<scope>complie</scope>
</dependency>
<dependency>
<groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId>
Expand Down Expand Up @@ -124,5 +136,25 @@
<version>1.0</version>
<systemPath>${basedir}/lib/spigot-1.16.1.jar</systemPath>
</dependency>
<dependency>
<groupId>net.milkbowl.vault</groupId>
<artifactId>VaultAPI</artifactId>
<version>1.7</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.md-5</groupId>
<artifactId>bungeecord-api</artifactId>
<version>1.16-R0.2-SNAPSHOT</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.md-5</groupId>
<artifactId>bungeecord-api</artifactId>
<version>1.16-R0.2-SNAPSHOT</version>
<type>javadoc</type>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
4 changes: 4 additions & 0 deletions src/bungee.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name: InteractiveChatBungee
main: com.loohp.interactivechat.Bungee.InteractiveChatBungee
version: ${project.version}
author: LOOHP
211 changes: 211 additions & 0 deletions src/com/loohp/interactivechat/Bungee/InteractiveChatBungee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
package com.loohp.interactivechat.Bungee;

import java.io.IOException;
import java.net.SocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicLong;

import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import com.loohp.interactivechat.Bungee.Metrics.Charts;
import com.loohp.interactivechat.Bungee.Metrics.Metrics;
import com.loohp.interactivechat.Utils.CompressionUtils;
import com.loohp.interactivechat.Utils.CustomArrayUtils;
import com.loohp.interactivechat.Utils.DataTypeIO;

import net.md_5.bungee.api.Callback;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.ServerPing;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.event.PlayerDisconnectEvent;
import net.md_5.bungee.api.event.PluginMessageEvent;
import net.md_5.bungee.api.event.PostLoginEvent;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.api.plugin.Plugin;
import net.md_5.bungee.event.EventHandler;

public class InteractiveChatBungee extends Plugin implements Listener {

public static Plugin plugin;
public static Metrics metrics;
private static Random random = new Random();
public static AtomicLong pluginMessagesCounter = new AtomicLong(0);

@Override
public void onEnable() {
plugin = this;

getProxy().registerChannel("interchat:main");
getProxy().getPluginManager().registerListener(this, this);
getLogger().info(ChatColor.GREEN + "[InteractiveChat] Registered Plugin Messaging Channels!");

metrics = new Metrics(plugin, 8839);
Charts.setup(metrics);

run();

getLogger().info(ChatColor.GREEN + "[InteractiveChat] InteractiveChatBungee has been enabled!");
}

@Override
public void onDisable() {
getLogger().info(ChatColor.RED + "[InteractiveChat] InteractiveChatBungee has been disabled!");
}

private void run() {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
try {
sendPlayerListData();
sendDelay();
} catch (IOException e) {
e.printStackTrace();
}
}
}, 0, 10000);
}

@EventHandler
public void onReceive(PluginMessageEvent event) {
if (!event.getTag().equals("interchat:main")) {
return;
}

SocketAddress senderServer = event.getSender().getSocketAddress();

for (ServerInfo server : getProxy().getServers().values()) {
if (!server.getSocketAddress().equals(senderServer) && server.getPlayers().size() > 0) {
server.sendData("interchat:main", event.getData());
}
}
}

@EventHandler
public void onJoin(PostLoginEvent event) {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
try {
sendPlayerListData();
} catch (IOException e) {
e.printStackTrace();
}
}
}, 1000);
}

@EventHandler
public void onLeave(PlayerDisconnectEvent event) {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
try {
sendPlayerListData();
} catch (IOException e) {
e.printStackTrace();
}
}
}, 1000);
}

private void sendPlayerListData() throws IOException {
ByteArrayDataOutput output = ByteStreams.newDataOutput();
Collection<ProxiedPlayer> players = ProxyServer.getInstance().getPlayers();
output.writeInt(players.size());
for (ProxiedPlayer player : players) {
DataTypeIO.writeUUID(output, player.getUniqueId());
DataTypeIO.writeString(output, player.getDisplayName(), StandardCharsets.UTF_8);
}

int packetNumber = random.nextInt();
int packetId = 0x00;
byte[] data = output.toByteArray();

byte[][] dataArray = CustomArrayUtils.divideArray(CompressionUtils.compress(data), 32700);

for (int i = 0; i < dataArray.length; i++) {
byte[] chunk = dataArray[i];

ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeInt(packetNumber);

out.writeShort(packetId);
out.writeBoolean(i == (dataArray.length - 1));

out.write(chunk);

for (ServerInfo server : getProxy().getServers().values()) {
server.sendData("interchat:main", out.toByteArray());
}
}
}

private void sendDelay() throws IOException {
ByteArrayDataOutput output = ByteStreams.newDataOutput();

List<CompletableFuture<Integer>> futures = new LinkedList<>();

for (ServerInfo server : getProxy().getServers().values()) {
futures.add(getPing(server));
}
int highestPing = futures.stream().mapToInt(each -> {
try {
return each.get();
} catch (InterruptedException | ExecutionException e) {
return 0;
}
}).max().orElse(0);

output.writeInt(highestPing * 2 + 200);

int packetNumber = random.nextInt();
int packetId = 0x01;
byte[] data = output.toByteArray();

byte[][] dataArray = CustomArrayUtils.divideArray(CompressionUtils.compress(data), 32700);

for (int i = 0; i < dataArray.length; i++) {
byte[] chunk = dataArray[i];

ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeInt(packetNumber);

out.writeShort(packetId);
out.writeBoolean(i == (dataArray.length - 1));

out.write(chunk);

for (ServerInfo server : getProxy().getServers().values()) {
server.sendData("interchat:main", out.toByteArray());
}
}
}

private CompletableFuture<Integer> getPing(ServerInfo server) {
CompletableFuture<Integer> future = new CompletableFuture<>();
long start = System.currentTimeMillis();
Callback<ServerPing> callback = new Callback<ServerPing>() {
@Override
public void done(ServerPing result, Throwable error) {
if (error == null) {
future.complete((int) (System.currentTimeMillis() - start));
} else {
future.complete(0);
}
}
};
server.ping(callback);
return future;
}
}
21 changes: 21 additions & 0 deletions src/com/loohp/interactivechat/Bungee/Metrics/Charts.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.loohp.interactivechat.Bungee.Metrics;

import java.util.concurrent.Callable;

import com.loohp.interactivechat.Bungee.InteractiveChatBungee;

public class Charts {

public static void setup(Metrics metrics) {

metrics.addCustomChart(new Metrics.SingleLineChart("total_plugin_messages_relayed_per_interval)", new Callable<Integer>() {
@Override
public Integer call() throws Exception {
long amount = InteractiveChatBungee.pluginMessagesCounter.getAndSet(0);
return amount > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) amount;
}
}));

}

}
Loading

0 comments on commit b8b1bd7

Please sign in to comment.