Skip to content

Commit

Permalink
1.18.2 port of 1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
yo56789 committed Sep 18, 2024
1 parent d6af2b8 commit 9f88ab4
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 13 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G
loader_version=0.15.7

# Mod Properties
mod_version = 1.1.0
mod_version = 1.2.0
maven_group = io.github.yo56789
archives_base_name = mc-discord-webhook

Expand Down
13 changes: 10 additions & 3 deletions src/main/java/io/github/yo56789/mcdiscwbhk/WebhookHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@ public class WebhookHandler {
public static String assembleMessage(String message, String username, int color) {
if (message.isBlank()) {
return "";
} else if (Config.IS_EMBED_MODE) {
} else if (Config.WEBHOOK_MODE.equalsIgnoreCase("embed")) {
return assembleEmbed(message, username, color);
} else if (Config.WEBHOOK_MODE.equalsIgnoreCase("list")) {
return assembleList(message, username);
}

return String.format("{\"content\":\"%s\", \"username\":\"%s\"}", message, username);
}

public static String assembleMessage(String message, String username, int color, String uuid) {
if (message.isBlank()) {
return "";
} else if (Config.IS_EMBED_MODE) {
} else if (Config.WEBHOOK_MODE.equalsIgnoreCase("embed")) {
return assembleEmbed(message, username, color, uuid);
} else if (Config.WEBHOOK_MODE.equalsIgnoreCase("list")) {
return assembleList(message, username);
}

return String.format("{\"content\":\"%s\", \"username\":\"%s\", \"avatar_url\":\"%s\"}", message, username, String.format(Config.USER_AVATAR_URL, uuid));
Expand All @@ -42,6 +45,10 @@ static String assembleEmbed(String message, String username, int color, String u
return String.format("{ \"content\": null, \"embeds\": [ { \"description\": \"%s\", \"color\": %s } ], \"username\": \"%s\", \"avatar_url\": \"%s\" }", message, color, username, String.format(Config.USER_AVATAR_URL, uuid));
}

static String assembleList(String message, String username) {
return String.format("{\"content\":\"%s\", \"username\":\"%s\"}", String.format(Config.CLASSIC_MESSAGE_FORMAT, username, message), Config.SERVER_NAME);
}

public static void post(String uri, String data) {
if(data.isBlank()) return;

Expand Down
21 changes: 16 additions & 5 deletions src/main/java/io/github/yo56789/mcdiscwbhk/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ public class Config {
public static final String WEBHOOK_URI = config.getOrDefault("webhook-uri", "");
public static final String SERVER_NAME = config.getOrDefault("server-webhook-name", "Server");
public static final String USER_AVATAR_URL = config.getOrDefault("user-avatar-url", "https://mc-heads.net/avatar/%s");
// This should just get the "webhook-mode" key and check if it equals "embed", this bool will be true if passed
// Example: if(IS_EMBED_MODE) /*embed stuff*/ else /*message stuff*/
public static final boolean IS_EMBED_MODE = config.getOrDefault("webhook-mode", "message").equalsIgnoreCase("embed");

public static final String WEBHOOK_MODE = verifyWebhookMode();
public static final String CLASSIC_MESSAGE_FORMAT = config.getOrDefault("list-message-format", "%s > %s");

// Events - Server Lifecycle
public static final boolean EVENT_PLAYER_MESSAGE_ENABLED = config.getOrDefault("event-player-message-enabled", true);

public static final boolean EVENT_SERVER_STARTING_ENABLED = config.getOrDefault("event-server-starting-enabled", false);
public static final String EVENT_SERVER_STARTING = config.getOrDefault("event-server-starting-message", "Server Starting!");

Expand All @@ -35,7 +37,16 @@ public class Config {
public static void init() {
// Protection in-case logs are shared.
// Many log-sharing websites don't recognise links as something that should be filtered.
Main.LOGGER.info("Webhook URL: " + (!WEBHOOK_URI.isEmpty() ? WEBHOOK_URI.substring(0, 60).concat("*************************************") : ""));
Main.LOGGER.info("Launched in " + (IS_EMBED_MODE ? "embed" : "message") + " mode");
Main.LOGGER.info("Webhook URL: " + (!WEBHOOK_URI.isEmpty() ? WEBHOOK_URI.substring(0, 35).concat("***************************************************") : ""));
Main.LOGGER.info("Launched in " + WEBHOOK_MODE + " mode");
}

private static String verifyWebhookMode() {
String mode = config.getOrDefault("webhook-mode", "message");
if (mode.equalsIgnoreCase("message") || mode.equalsIgnoreCase("embed") || mode.equalsIgnoreCase("list")) {
return mode;
}

return "message";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,21 @@ public class DefaultConfig {
user-avatar-url=https://mc-heads.net/avatar/%s
# Webhook mode
# Type "message" or "embed"
# "message" only sends raw text messages. "embed" sends content in embeds
# Options: "message", "embed" or "list"
# "message" sends messages with the bot using the persons mc username. "embed" sends everything in embeds. "list" sends messages like a list.
webhook-mode=message
# Format for sending messages in "list" mode
# First %s is username Second %s is message content
# Default: %s > %s
list-message-format=**%s** > %s
# Events
# Leaving blank will cause event to be disabled
# Player Message
event-player-message-enabled=true
# Server starting
event-server-starting-enabled=false
event-server-starting-message=Server Starting!
Expand All @@ -47,4 +55,5 @@ public class DefaultConfig {
# %s = username of player
event-player-leave-enabled=true
event-player-leave-message=%s left!""";

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ public class ChatMixin {

@Inject(method="handleMessage", at=@At(value = "INVOKE", target = "Lnet/minecraft/server/PlayerManager;broadcast(Lnet/minecraft/text/Text;Ljava/util/function/Function;Lnet/minecraft/network/MessageType;Ljava/util/UUID;)V"))
public void chatMessage(TextStream.Message message, CallbackInfo ci) {
String data = WebhookHandler.assembleMessage(message.getRaw().replaceFirst("<[^>]+>", "").trim(), player.getName().asString(), Colors.BLUE.colorCode, player.getUuidAsString());
WebhookHandler.post(Config.WEBHOOK_URI, data);
if (Config.EVENT_PLAYER_MESSAGE_ENABLED) {
String data = WebhookHandler.assembleMessage(message.getRaw().replaceFirst("<[^>]+>", "").trim(), player.getName().asString(), Colors.BLUE.colorCode, player.getUuidAsString());
WebhookHandler.post(Config.WEBHOOK_URI, data);
}
}
}

0 comments on commit 9f88ab4

Please sign in to comment.