Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Wetterquarz committed Mar 5, 2024
0 parents commit 09d3383
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Eclipse specific files
.settings/
.classpath
.project

# Generated files
target/
dependency-reduced-pom.xml
121 changes: 121 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<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>com.wetterquarz</groupId>
<artifactId>minecraftdiscordsync</artifactId>
<version>0.1.5</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<repository>
<id>sonatype</id>
<url>https://oss.sonatype.org/content/groups/public/</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.20.4-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<!-- Mojang dependencies -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-buffer</artifactId>
<version>4.1.97.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-codec</artifactId>
<version>4.1.97.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-common</artifactId>
<version>4.1.97.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-handler</artifactId>
<version>4.1.97.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-resolver</artifactId>
<version>4.1.97.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-transport</artifactId>
<version>4.1.97.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-transport-classes-epoll</artifactId>
<version>4.1.97.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-transport-native-epoll</artifactId>
<version>4.1.97.Final</version>
<classifier>linux-x86_64</classifier>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-transport-native-epoll</artifactId>
<version>4.1.97.Final</version>
<classifier>linux-aarch_64</classifier>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-transport-native-unix-common</artifactId>
<version>4.1.97.Final</version>
<scope>provided</scope>
</dependency>
<!-- End of Mojang dependencies-->
<dependency>
<groupId>com.discord4j</groupId>
<artifactId>discord4j-core</artifactId>
<version>3.2.6</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.5.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.wetterquarz.minecraftdiscordsync;

import org.bukkit.plugin.java.JavaPlugin;

import discord4j.common.util.Snowflake;
import discord4j.core.DiscordClient;
import discord4j.core.GatewayDiscordClient;
import discord4j.core.object.entity.channel.GuildMessageChannel;
import net.md_5.bungee.api.ChatColor;
import reactor.core.Disposable;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

public class DiscordSyncPlugin extends JavaPlugin {

private Disposable discordAccess = null;

@Override
public void onEnable() {
saveDefaultConfig();
Flux<String> events = Flux.create(new MinecraftEventListener(this))
.map(ChatColor::stripColor);
final String token = getConfig().getString("bot_token");
DiscordClient client = DiscordClient.create(token);
discordAccess = client.login()
.flatMap(gateway -> this.printMessages(gateway, events))
.subscribe();
}

@Override
public void onDisable() {
discordAccess.dispose();
discordAccess = null;
}

private Mono<Void> printMessages(GatewayDiscordClient gateway, Flux<String> messages) {
final String channelId = getConfig().getString("channel_id");
GuildMessageChannel channel = (GuildMessageChannel) gateway.getChannelById(Snowflake.of(channelId)).block();
getLogger().info("Showing join and leave activity in channel " + channel.getName());
return Flux.create(new MinecraftEventListener(this))
.map(ChatColor::stripColor)
.flatMap(channel::createMessage)
.onErrorContinue((t, msg) -> getLogger().severe(t.toString()))
.then();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.wetterquarz.minecraftdiscordsync;

import java.util.function.Consumer;

import org.bukkit.event.*;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.java.JavaPlugin;

import reactor.core.publisher.FluxSink;

public class MinecraftEventListener implements Listener, Consumer<FluxSink<String>> {

private FluxSink<String> eventSink;
private final JavaPlugin plugin;

public MinecraftEventListener(JavaPlugin plugin) {
this.plugin = plugin;
}

@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
eventSink.next(event.getJoinMessage());
}

@EventHandler
public void onPlayerLeave(PlayerQuitEvent event) {
eventSink.next(event.getQuitMessage());
}

@Override
public void accept(FluxSink<String> sink) {
eventSink = sink;
plugin.getServer().getPluginManager().registerEvents(this, plugin);
eventSink.onDispose(() -> HandlerList.unregisterAll(this));
}

}
2 changes: 2 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bot_token: PUT_YOUR_DISCORD_BOT_TOKEN_HERE
channel_id: PUT_THE_CHANNEL_ID_WHERE_MESSAGES_SHOULD_BE_SENT_HERE
5 changes: 5 additions & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: 'DiscordSyncPlugin'
main: 'com.wetterquarz.minecraftdiscordsync.DiscordSyncPlugin'
version: '0.1.5'
api: '1.20.4'
api-version: '1.20'

0 comments on commit 09d3383

Please sign in to comment.