diff --git a/build.gradle b/build.gradle index 101d25e..1e8d83c 100644 --- a/build.gradle +++ b/build.gradle @@ -27,6 +27,8 @@ repositories { // Loom adds the essential maven repositories to download Minecraft and libraries from automatically. // See https://docs.gradle.org/current/userguide/declaring_repositories.html // for more information about repositories. + maven { url 'https://maven.wispforest.io/releases/' } + } dependencies { @@ -36,6 +38,13 @@ dependencies { modImplementation "net.fabricmc:fabric-loader:${project.loader_version}" modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" + + annotationProcessor modImplementation("io.wispforest:owo-lib:${project.owo_version}") + include "io.wispforest:owo-sentinel:${project.owo_version}" + + implementation("net.dv8tion:JDA:${project.jda_version}") { + exclude module: 'opus-java' + } } processResources { diff --git a/gradle.properties b/gradle.properties index 834a148..c5a62fe 100644 --- a/gradle.properties +++ b/gradle.properties @@ -12,3 +12,6 @@ archives_base_name=discord # Dependencies # check this on https://modmuss50.me/fabric.html fabric_version=0.107.0+1.21.1 + +owo_version=0.12.15+1.21 +jda_version=5.1.2 \ No newline at end of file diff --git a/src/main/java/sad/ovh/discord/Discord.java b/src/main/java/sad/ovh/discord/Discord.java index 9f20655..ec4f395 100644 --- a/src/main/java/sad/ovh/discord/Discord.java +++ b/src/main/java/sad/ovh/discord/Discord.java @@ -1,10 +1,19 @@ package sad.ovh.discord; import net.fabricmc.api.ModInitializer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class Discord implements ModInitializer { + public static final String MOD_ID = "slop-discord"; + public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID); + + public static final sad.ovh.discord.DiscordConfig CONFIG = sad.ovh.discord.DiscordConfig.createAndLoad(); @Override public void onInitialize() { + LOGGER.info("slop's custom discord plugin"); + DiscordRegistration.createClient(); + } } diff --git a/src/main/java/sad/ovh/discord/DiscordConfigModel.java b/src/main/java/sad/ovh/discord/DiscordConfigModel.java new file mode 100644 index 0000000..1c64a5d --- /dev/null +++ b/src/main/java/sad/ovh/discord/DiscordConfigModel.java @@ -0,0 +1,14 @@ +package sad.ovh.discord; + +import io.wispforest.owo.config.Option; +import io.wispforest.owo.config.annotation.Config; +import io.wispforest.owo.config.annotation.Sync; + +@Sync(Option.SyncMode.NONE) +@Config(name = "slop-discord-config", wrapperName = "DiscordConfig") +public class DiscordConfigModel { + public String token = ""; + public String channelId = ""; + public String consoleId = ""; + public String avatarApiUrl = "https://vzge.me/bust/256/%s"; +} diff --git a/src/main/java/sad/ovh/discord/DiscordRegistration.java b/src/main/java/sad/ovh/discord/DiscordRegistration.java new file mode 100644 index 0000000..b863277 --- /dev/null +++ b/src/main/java/sad/ovh/discord/DiscordRegistration.java @@ -0,0 +1,60 @@ +package sad.ovh.discord; + +import net.dv8tion.jda.api.JDA; +import net.dv8tion.jda.api.JDABuilder; +import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import net.dv8tion.jda.api.events.message.MessageUpdateEvent; +import net.dv8tion.jda.api.events.session.ReadyEvent; +import net.dv8tion.jda.api.hooks.ListenerAdapter; +import net.dv8tion.jda.api.requests.GatewayIntent; +import net.dv8tion.jda.api.utils.cache.CacheFlag; +import org.jetbrains.annotations.NotNull; + +import java.util.EnumSet; + +public class DiscordRegistration { + static public JDA client; + static public TextChannel channel; + static public TextChannel consoleChannel; + + static public Boolean isReady; + + static public void createClient() { + client = JDABuilder + .create(Discord.CONFIG.token(), EnumSet.of(GatewayIntent.GUILD_MESSAGES, GatewayIntent.GUILD_MEMBERS, GatewayIntent.MESSAGE_CONTENT)) + .disableCache(CacheFlag.ACTIVITY, CacheFlag.VOICE_STATE, CacheFlag.EMOJI, CacheFlag.STICKER, CacheFlag.CLIENT_STATUS, CacheFlag.ONLINE_STATUS, CacheFlag.SCHEDULED_EVENTS) + .addEventListeners(new DiscordEvents()) + .build(); + } + + private static class DiscordEvents extends ListenerAdapter { + @Override + public void onReady(@NotNull ReadyEvent event) { + final var self = client.getSelfUser(); + Discord.LOGGER.info("Logged in as {}", self.getAsTag()); + + channel = client.getTextChannelById(Discord.CONFIG.channelId()); + consoleChannel = client.getTextChannelById(Discord.CONFIG.channelId()); + + if (channel == null) { + Discord.LOGGER.error("Channel not found! Set an existing channel ID that I can see!"); + client.shutdown(); + return; + } + if (consoleChannel == null) { + Discord.LOGGER.warn("No console channel was set."); + } + + isReady = true; + } + + @Override + public void onMessageReceived(@NotNull MessageReceivedEvent event) { + } + + @Override + public void onMessageUpdate(@NotNull MessageUpdateEvent event) { + } + } +} diff --git a/src/main/java/sad/ovh/discord/MinecraftRegistration.java b/src/main/java/sad/ovh/discord/MinecraftRegistration.java new file mode 100644 index 0000000..89aeaac --- /dev/null +++ b/src/main/java/sad/ovh/discord/MinecraftRegistration.java @@ -0,0 +1,49 @@ +package sad.ovh.discord; + +import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents; +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; +import net.fabricmc.fabric.api.message.v1.ServerMessageEvents; +import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; +import net.minecraft.server.network.ServerPlayerEntity; + +public class MinecraftRegistration { + public static void register() { + ServerLifecycleEvents.SERVER_STARTING.register(server -> { + if (!DiscordRegistration.isReady) + return; + }); + + ServerLifecycleEvents.SERVER_STARTED.register(server -> { + if (!DiscordRegistration.isReady) + return; + }); + + ServerLifecycleEvents.SERVER_STOPPING.register(server -> { + if (!DiscordRegistration.isReady) + return; + + }); + + ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> { + if (!DiscordRegistration.isReady) + return; + }); + + ServerPlayConnectionEvents.DISCONNECT.register((handler, server) -> { + if (!DiscordRegistration.isReady) + return; + }); + + ServerLivingEntityEvents.AFTER_DEATH.register((entity, damageSource) -> { + if (!DiscordRegistration.isReady) + return; + if (!(entity instanceof ServerPlayerEntity player)) + return; + }); + + ServerMessageEvents.CHAT_MESSAGE.register((message, sender, params) -> { + if (!DiscordRegistration.isReady) + return; + }); + } +} diff --git a/src/main/resources/assets/discord/icon.png b/src/main/resources/assets/discord/icon.png new file mode 100644 index 0000000..baececa Binary files /dev/null and b/src/main/resources/assets/discord/icon.png differ