Skip to content

Commit

Permalink
!!! WORKS !!!
Browse files Browse the repository at this point in the history
  • Loading branch information
not-coded committed Feb 26, 2024
1 parent 696883f commit d32b470
Show file tree
Hide file tree
Showing 30 changed files with 780 additions and 109 deletions.
19 changes: 13 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,21 @@ dependencies {

implementation include("club.minnced:discord-webhooks:${project.webhook_version}")

include 'org.apache.commons:commons-collections4:4.4'

include "org.slf4j:slf4j-api:1.8.0-beta4"

include 'org.apache.commons:commons-collections4:4.1'
include 'com.neovisionaries:nv-websocket-client:2.14'
include 'com.squareup.okhttp3:okhttp:4.10.0'
include 'com.squareup.okio:okio-jvm:3.7.0'
include 'com.fasterxml.jackson.core:jackson-databind:2.14.1'
include 'com.fasterxml.jackson.core:jackson-core:2.14.1'
include 'com.squareup.okhttp3:okhttp:3.14.9'
include 'com.squareup.okio:okio:1.17.2'
//include 'com.squareup.okio:okio-jvm:3.7.0'
include 'com.fasterxml.jackson.core:jackson-databind:2.10.1'
include 'com.fasterxml.jackson.core:jackson-annotations:2.10.1'
include 'com.fasterxml.jackson.core:jackson-core:2.10.1'
include "net.sf.trove4j:trove4j:3.0.3"

include 'org.json:json:20231013'

// Uncomment the following line to enable the deprecated Fabric API modules.
// These are included in the Fabric API production distribution and allow you to update your mod to the latest modules at a later more convenient time.

Expand All @@ -60,7 +67,7 @@ processResources {

tasks.withType(JavaCompile).configureEach {
// Minecraft 1.18 (1.18-pre2) upwards uses Java 17.
it.options.release = 8
it.options.release = 17
}

java {
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ loader_version=0.14.21
# Mod Properties
mod_version = 1.0.0
maven_group = fr.catcore
archives_base_name = fabric-discord-link
archives_base_name = Fabric-Discord-Link

# Dependencies
fabric_version=0.25.0+build.415-1.16
jda_version=5.0.0-beta.20
jda_version=5.0.0-beta.12
emoji_version=5.1.1
webhook_version=0.8.4
70 changes: 69 additions & 1 deletion src/main/java/fr/catcore/fdlink/FDLink.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
package fr.catcore.fdlink;

import fr.catcore.fdlink.api.discord.MessageSender;
import fr.catcore.fdlink.api.minecraft.Message;
import fr.catcore.fdlink.api.minecraft.MessagePacket;
import fr.catcore.fdlink.api.minecraft.VersionHelper;
import fr.catcore.fdlink.api.minecraft.compat.MessageCompat;
import fr.catcore.fdlink.api.minecraft.compat.MessagePacketCompat;
import fr.catcore.fdlink.api.minecraft.compat.MinecraftServerCompat;
import fr.catcore.fdlink.config.ConfigHandler;
import fr.catcore.fdlink.discord.bot.DiscordBot;
import fr.catcore.fdlink.discord.webhook.DiscordWebhook;
import net.fabricmc.api.DedicatedServerModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.minecraft.text.BaseText;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText;

import java.util.List;
import java.util.UUID;
import java.util.logging.Logger;

public class FDLink implements DedicatedServerModInitializer {
Expand All @@ -21,6 +34,40 @@ public void onInitializeServer() {
initialize();
}

public static void handleText(Text text, UUID uUID) {
if (text instanceof BaseText baseText) {
FDLink.getMessageSender().sendMessage(getMessageFromText(baseText).setAuthorUUID(uUID));
}
else {
FDLink.getMessageSender().sendMessage(new MessageCompat(text.getString()).setAuthorUUID(uUID));
}
}

private static Message getMessageFromText(BaseText text) {
List<Text> siblings = text.getSiblings();
Message message;
if (text instanceof TranslatableText translatableText) {
Object[] args = translatableText.getArgs();
Object[] argsList = new Object[args.length];
for (int i = 0; i < args.length; i++) {
Object arg = args[i];
if (arg instanceof BaseText) {
argsList[i] = getMessageFromText((BaseText) arg);
} else {
argsList[i] = arg;
}
}
message = new MessageCompat(translatableText.getKey(), text.getString(), argsList);
}
else message = new MessageCompat(text.getString());
for (Text sib : siblings) {
if (sib instanceof BaseText baseText) message.addSibbling(getMessageFromText(baseText));
else message.addSibbling(new MessageCompat(sib.getString()));
}

return message;
}

private static void initialize() {
ConfigHandler.ConfigHolder configHolder = ConfigHandler.getConfig();
messageReceiver = new DiscordBot(configHolder.getToken(), configHolder.getConfig());
Expand All @@ -29,13 +76,34 @@ private static void initialize() {
} else {
LOGGER.info("Found a webhook URL, using Webhook instead of Bot to send message.");
if (configHolder.getConfig().mainConfig.chatChannels.isEmpty() && configHolder.getConfig().mainConfig.logChannels.isEmpty()) {
LOGGER.warn("Unable to find any channel id, only Minecraft->Discord will work, add a channel id to the config if this wasn't intended.");
LOGGER.warning("Unable to find any channel id, only Minecraft->Discord will work, add a channel id to the config if this wasn't intended.");
}
messageSender = new DiscordWebhook(configHolder.getConfig().mainConfig.webhook.url, configHolder.getConfig(), messageReceiver);
}

initializeVersion();

loaded = true;
}

private static void initializeVersion() {
ServerTickEvents.START_SERVER_TICK.register((server -> FDLink.getMessageReceiver().serverTick(new MinecraftServerCompat(server))));

VersionHelper.registerMessageSender((server, message, style) -> {
Message literalText = new MessageCompat(message);
if (style != null) {
literalText = literalText.setStyle(style);
}
server.sendMessageToAll(new MessagePacketCompat(literalText, MessagePacket.MessageType.CHAT, UUID.randomUUID()));
});

ServerLifecycleEvents.SERVER_STARTING.register(minecraftServer -> FDLink.getMessageSender().serverStarting());
ServerLifecycleEvents.SERVER_STARTED.register((server -> FDLink.getMessageSender().serverStarted()));
ServerLifecycleEvents.SERVER_STOPPING.register(minecraftServer -> FDLink.getMessageSender().serverStopping());
ServerLifecycleEvents.SERVER_STOPPED.register((server -> FDLink.getMessageSender().serverStopped()));

}

public static void regenConfig() {
ConfigHandler.ConfigHolder configHolder = ConfigHandler.getConfig();
messageReceiver = new DiscordBot(configHolder.getToken(), configHolder.getConfig());
Expand Down
23 changes: 18 additions & 5 deletions src/main/java/fr/catcore/fdlink/api/discord/MessageHandler.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package fr.catcore.fdlink.api.discord;

import net.minecraft.text.TranslatableText;
import fr.catcore.fdlink.api.minecraft.CompatText;
import fr.catcore.fdlink.api.minecraft.Message;

import java.util.ArrayList;
import java.util.List;

public interface MessageHandler {

static final List<MessageHandler> TEXT_HANDLERS = new ArrayList<>();
static final List<fr.catcore.fdlink.api.discord.handlers.MessageHandler> TEXT_HANDLERS = new ArrayList<>();

MinecraftMessage handleText(TranslatableText text);
MinecraftMessage handleText(Message text);

static void registerHandler(MessageHandler messageHandler) {
static void registerHandler(fr.catcore.fdlink.api.discord.handlers.MessageHandler messageHandler) {
TEXT_HANDLERS.add(messageHandler);
}

default String adaptUsernameToDiscord(String username) {
return adaptUsername(username);
}
Expand All @@ -23,4 +23,17 @@ static String adaptUsername(String username) {
return username.replaceAll("§[b0931825467adcfeklmnor]", "")
.replaceAll("([_`~*>])", "\\\\$1");
}

default String getArgAsString(Object arg) {
return getAsString(arg);
}

static String getAsString(Object arg) {
if (arg instanceof CompatText) {
return ((CompatText) arg).fabric_Discord_Link$getMessage();
} else if (arg instanceof Message) {
return ((Message) arg).getMessage();
}
return (String) arg;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package fr.catcore.fdlink.api.discord;

import com.mojang.brigadier.Message;
import fr.catcore.fdlink.api.config.Config;
import fr.catcore.fdlink.api.minecraft.Message;

public interface MinecraftToDiscordFunction {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.catcore.fdlink.api.discord.handlers;

import fr.catcore.fdlink.api.discord.MinecraftToDiscordFunction;
import fr.catcore.fdlink.api.minecraft.Message;

public class CommandHandler extends MessageHandler {
private final String commandName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import fr.catcore.fdlink.api.config.Config;
import fr.catcore.fdlink.api.discord.MinecraftMessage;
import fr.catcore.fdlink.api.discord.MinecraftToDiscordFunction;
import net.minecraft.text.TranslatableText;
import fr.catcore.fdlink.api.minecraft.Message;

public abstract class MessageHandler {
private final MinecraftToDiscordFunction minecraftToDiscordFunction;
Expand All @@ -12,9 +12,9 @@ public MessageHandler(MinecraftToDiscordFunction minecraftToDiscordFunction) {
this.minecraftToDiscordFunction = minecraftToDiscordFunction;
}

public MinecraftMessage handle(TranslatableText text, Config config) {
public MinecraftMessage handle(Message text, Config config) {
return this.minecraftToDiscordFunction.handleText(text, config);
}

public abstract boolean match(TranslatableText message);
public abstract boolean match(Message message);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package fr.catcore.fdlink.api.discord.handlers;

import fr.arthurbambou.fdlink.api.discord.MinecraftToDiscordFunction;
import fr.arthurbambou.fdlink.api.minecraft.Message;
import fr.catcore.fdlink.api.discord.MinecraftToDiscordFunction;
import fr.catcore.fdlink.api.minecraft.Message;

public class StringHandler extends MessageHandler {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package fr.catcore.fdlink.api.discord.handlers;

import fr.catcore.fdlink.api.discord.MinecraftToDiscordFunction;
import net.minecraft.text.TranslatableText;
import fr.catcore.fdlink.api.minecraft.Message;

public class TextHandler extends MessageHandler {
private final String key;
Expand All @@ -10,7 +10,10 @@ public TextHandler(String key, MinecraftToDiscordFunction minecraftToDiscordFunc
this.key = key;
}

public boolean match(TranslatableText text) {
return text.getKey().startsWith(this.key);
public boolean match(Message text) {
if (text.getTextType() == Message.TextType.TRANSLATABLE) {
return text.getKey().startsWith(this.key);
}
return false;
}
}
20 changes: 20 additions & 0 deletions src/main/java/fr/catcore/fdlink/api/minecraft/CompatText.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package fr.catcore.fdlink.api.minecraft;

import com.google.common.collect.Lists;

import java.util.List;

public interface CompatText {

String fabric_Discord_Link$getMessage();

default String getTranslationKey() {
return "";
}

default List getArgs() {
return Lists.newArrayList();
}


}
18 changes: 18 additions & 0 deletions src/main/java/fr/catcore/fdlink/api/minecraft/MessagePacket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package fr.catcore.fdlink.api.minecraft;

import java.util.UUID;

public interface MessagePacket {

Message getMessage();

MessageType getMessageType();

UUID getUUID();

enum MessageType {
CHAT,
SYSTEM,
INFO;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package fr.catcore.fdlink.api.minecraft;

import fr.catcore.fdlink.api.minecraft.compat.MinecraftServerCompat;
import fr.catcore.fdlink.api.minecraft.style.Style;

public interface MessageSender {

void sendMessageToChat(MinecraftServerCompat server, String message, Style style);
}
26 changes: 26 additions & 0 deletions src/main/java/fr/catcore/fdlink/api/minecraft/MinecraftServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package fr.catcore.fdlink.api.minecraft;

import java.io.File;
import java.util.List;
import java.util.UUID;

public interface MinecraftServer {

String getMotd();

int getPlayerCount();

int getMaxPlayerCount();

List<PlayerEntity> getPlayers();

void sendMessageToAll(MessagePacket messagePacket);

String getIp();

File getIcon();

PlayerEntity getPlayerFromUsername(String username);

String getUsernameFromUUID(UUID uuid);
}
10 changes: 10 additions & 0 deletions src/main/java/fr/catcore/fdlink/api/minecraft/PlayerEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package fr.catcore.fdlink.api.minecraft;

import java.util.UUID;

public interface PlayerEntity {

String getPlayerName();

UUID getUUID();
}
21 changes: 21 additions & 0 deletions src/main/java/fr/catcore/fdlink/api/minecraft/VersionHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package fr.catcore.fdlink.api.minecraft;

import fr.catcore.fdlink.api.minecraft.compat.MinecraftServerCompat;
import fr.catcore.fdlink.api.minecraft.style.Style;
import net.minecraft.server.MinecraftServer;

import java.util.ArrayList;
import java.util.List;

public class VersionHelper {
private static final List<MessageSender> MESSAGE_SENDERS = new ArrayList<>();

public static void registerMessageSender(MessageSender messageSender) {
MESSAGE_SENDERS.add(messageSender);
}

public static void sendMessageToChat(MinecraftServerCompat server, String message, Style style) {
MessageSender messageSender = MESSAGE_SENDERS.get(0);
messageSender.sendMessageToChat(server, message, style);
}
}
Loading

0 comments on commit d32b470

Please sign in to comment.