Skip to content

Commit

Permalink
Add notification messages for different events
Browse files Browse the repository at this point in the history
* Server start and stop
* Discord integration start and stop
* Player join, leave, death
* Advancement messages
  • Loading branch information
sciwhiz12 committed Nov 6, 2020
1 parent 0657cc5 commit 08cffdd
Show file tree
Hide file tree
Showing 8 changed files with 240 additions and 14 deletions.
23 changes: 18 additions & 5 deletions src/main/java/sciwhiz12/concord/ChatBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@
import net.dv8tion.jda.api.events.ReadyEvent;
import net.dv8tion.jda.api.hooks.AnnotatedEventManager;
import net.dv8tion.jda.api.hooks.SubscribeEvent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.common.MinecraftForge;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;
import sciwhiz12.concord.msg.MessageListener;
import sciwhiz12.concord.msg.Messaging;
import sciwhiz12.concord.msg.PlayerListener;
import sciwhiz12.concord.msg.StatusListener;

import java.util.EnumSet;

Expand All @@ -26,15 +30,20 @@ public class ChatBot {
EnumSet.of(Permission.VIEW_CHANNEL, Permission.MESSAGE_READ, Permission.MESSAGE_WRITE);

private final JDA discord;
private final MessageListener message;
private final MessageListener msgListener;
private final PlayerListener playerListener;
private final StatusListener statusListener;

ChatBot(JDA discord) {
this.discord = discord;
discord.setEventManager(new AnnotatedEventManager());
discord.addEventListener(this);
message = new MessageListener(this);
discord.addEventListener(message);
MinecraftForge.EVENT_BUS.register(message);
discord.addEventListener(msgListener = new MessageListener(this));
discord.addEventListener(playerListener = new PlayerListener(this));
discord.addEventListener(statusListener = new StatusListener(this));
MinecraftForge.EVENT_BUS.register(msgListener);
MinecraftForge.EVENT_BUS.register(playerListener);
MinecraftForge.EVENT_BUS.register(statusListener);
}

public JDA getDiscord() {
Expand Down Expand Up @@ -89,11 +98,15 @@ void onReady(ReadyEvent event) {

LOGGER.info(BOT, "Discord bot is ready!");
LOGGER.info(BOT, "Invite URL for bot: {}", discord.getInviteUrl(REQUIRED_PERMISSIONS));

Messaging.sendToChannel(discord, new TranslationTextComponent("message.concord.bot.start").getString());
}

void shutdown() {
LOGGER.info(BOT, "Shutting down Discord bot...");
MinecraftForge.EVENT_BUS.unregister(message);
MinecraftForge.EVENT_BUS.unregister(msgListener);
MinecraftForge.EVENT_BUS.unregister(playerListener);
MinecraftForge.EVENT_BUS.unregister(statusListener);
discord.shutdown();
}
}
17 changes: 13 additions & 4 deletions src/main/java/sciwhiz12/concord/Concord.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.utils.cache.CacheFlag;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.fml.ExtensionPoint;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.config.ModConfig;
import net.minecraftforge.fml.event.server.FMLServerStartingEvent;
import net.minecraftforge.fml.event.server.FMLServerStoppingEvent;
import net.minecraftforge.fml.network.FMLNetworkConstants;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import sciwhiz12.concord.msg.Messaging;

import java.util.EnumSet;
import javax.security.auth.login.LoginException;
Expand All @@ -36,8 +38,8 @@ public Concord() {

ConcordConfig.register();

MinecraftForge.EVENT_BUS.addListener(this::onServerStarting);
MinecraftForge.EVENT_BUS.addListener(this::onServerStopping);
MinecraftForge.EVENT_BUS.addListener(EventPriority.LOWEST, this::onServerStarting);
MinecraftForge.EVENT_BUS.addListener(EventPriority.LOWEST, this::onServerStopping);
}

public void onServerStarting(FMLServerStartingEvent event) {
Expand All @@ -50,7 +52,7 @@ public void onServerStarting(FMLServerStartingEvent event) {

public void onServerStopping(FMLServerStoppingEvent event) {
if (isEnabled()) {
disable();
disable(true);
}
}

Expand All @@ -59,8 +61,15 @@ public static boolean isEnabled() {
}

public static void disable() {
disable(false);
}

public static void disable(boolean suppressMessage) {
if (!isEnabled()) return;
LOGGER.info("Shutting down Discord integration...");
if (!suppressMessage) {
Messaging.sendToChannel(BOT.getDiscord(), new TranslationTextComponent("message.concord.bot.stop").getString());
}
BOT.shutdown();
BOT = null;
}
Expand Down
80 changes: 79 additions & 1 deletion src/main/java/sciwhiz12/concord/ConcordConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ public class ConcordConfig {
public static boolean USE_CUSTOM_FONT = true;
public static boolean LAZY_TRANSLATIONS = true;

public static boolean SERVER_START = true;
public static boolean SERVER_STOP = true;
public static boolean BOT_START = true;
public static boolean BOT_STOP = false;

public static boolean PLAYER_JOIN = true;
public static boolean PLAYER_LEAVE = true;
public static boolean PLAYER_DEATH = true;
public static boolean PLAYER_ADV_TASK = true;
public static boolean PLAYER_ADV_CHALLENGE = true;
public static boolean PLAYER_ADV_GOAL = true;

public static void register() {
ModLoadingContext.get().registerConfig(ModConfig.Type.SERVER, Spec.CONFIG_SPEC);
FMLJavaModLoadingContext.get().getModEventBus().addListener(ConcordConfig::onConfigEvent);
Expand All @@ -29,6 +41,18 @@ static void bakeConfigValues() {

USE_CUSTOM_FONT = Spec.USE_CUSTOM_FONT.get();
LAZY_TRANSLATIONS = Spec.LAZY_TRANSLATIONS.get();

SERVER_START = Spec.SERVER_START.get();
SERVER_STOP = Spec.SERVER_STOP.get();
BOT_START = Spec.BOT_START.get();
BOT_STOP = Spec.BOT_STOP.get();

PLAYER_JOIN = Spec.PLAYER_JOIN.get();
PLAYER_LEAVE = Spec.PLAYER_LEAVE.get();
PLAYER_DEATH = Spec.PLAYER_DEATH.get();
PLAYER_ADV_TASK = Spec.PLAYER_ADV_TASK.get();
PLAYER_ADV_CHALLENGE = Spec.PLAYER_ADV_CHALLENGE.get();
PLAYER_ADV_GOAL = Spec.PLAYER_ADV_GOAL.get();
}

public static void onConfigEvent(ModConfig.ModConfigEvent event) {
Expand All @@ -49,6 +73,19 @@ static class Spec {
public static final ForgeConfigSpec.BooleanValue USE_CUSTOM_FONT;
public static final ForgeConfigSpec.BooleanValue LAZY_TRANSLATIONS;

public static final ForgeConfigSpec.BooleanValue SERVER_START;
public static final ForgeConfigSpec.BooleanValue SERVER_STOP;
public static final ForgeConfigSpec.BooleanValue BOT_START;
public static final ForgeConfigSpec.BooleanValue BOT_STOP;

public static final ForgeConfigSpec.BooleanValue PLAYER_JOIN;
public static final ForgeConfigSpec.BooleanValue PLAYER_LEAVE;
public static final ForgeConfigSpec.BooleanValue PLAYER_DEATH;
public static final ForgeConfigSpec.BooleanValue PLAYER_ADV_TASK;
public static final ForgeConfigSpec.BooleanValue PLAYER_ADV_CHALLENGE;
public static final ForgeConfigSpec.BooleanValue PLAYER_ADV_GOAL;


static {
ForgeConfigSpec.Builder builder = new ForgeConfigSpec.Builder();

Expand Down Expand Up @@ -79,7 +116,7 @@ static class Spec {
"Set to false if you cannot ensure that all clients will have the mod installed.")
.define("use_custom_font", true);

LAZY_TRANSLATIONS = builder.comment("Lazily translate the messages wheb possible.",
LAZY_TRANSLATIONS = builder.comment("Lazily translate the messages when possible.",
"This requires the clients have a resource pack with the messages, else they will render weirdly.",
"If false, all translation keys will be translated on the server.",
"If true, translation keys will translated on the server only if the client does not have the mod " +
Expand All @@ -89,6 +126,47 @@ static class Spec {

builder.pop();
}
{
builder.comment("Game notification settings",
"Each setting controls a specific game to Discord notification message.")
.push("notify");

SERVER_STOP = builder.comment("Complete startup of server",
"Translation key: message.concord.server.start")
.define("server.start", true);
SERVER_START = builder.comment("Stopping of server.",
"Translation key: message.concord.server.stop")
.define("server.stop", true);

BOT_START = builder.comment("Enabling of Discord integration.",
"Translation key: message.concord.bot.start")
.define("bot.start", false);
BOT_STOP = builder.comment("Disabling of Discord integration.",
"Translation key: message.concord.bot.stop")
.define("bot.stop", false);

PLAYER_JOIN = builder.comment("Player joining the game",
"Translation key: message.concord.player.join")
.define("player.join", true);
PLAYER_LEAVE =builder.comment("Player leaving the game",
"Translation key: message.concord.player.leave")
.define("player.leave", true);
PLAYER_DEATH = builder.comment("Player death message",
"Translation key: message.concord.player.death")
.define("player.death", true);

PLAYER_ADV_TASK = builder.comment("Player completed an normal advancement",
"Translation key: message.concord.player.advancement.task")
.define("player.adv.task", true);
PLAYER_ADV_CHALLENGE = builder.comment("Player completed a challenge advancement",
"Translation key: message.concord.player.advancement.challenge")
.define("player.adv.challenge", true);
PLAYER_ADV_GOAL = builder.comment("Player completed a goal advancement",
"Translation key: message.concord.player.advancement.goal")
.define("player.adv.goal", true);

builder.pop();
}

CONFIG_SPEC = builder.build();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/sciwhiz12/concord/msg/MessageListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ void onGuildMessageReceived(GuildMessageReceivedEvent event) {

@net.minecraftforge.eventbus.api.SubscribeEvent(priority = EventPriority.LOWEST)
void onServerChat(ServerChatEvent event) {
Messaging.sendToChannel(bot.getDiscord(), event.getUsername(), event.getComponent());
Messaging.sendToChannel(bot.getDiscord(), event.getComponent().getString());
}
}
4 changes: 2 additions & 2 deletions src/main/java/sciwhiz12/concord/msg/Messaging.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ public static void sendToAllPlayers(MinecraftServer server, Member member, Strin
}
}

public static void sendToChannel(JDA discord, String username, ITextComponent message) {
public static void sendToChannel(JDA discord, CharSequence text) {
final TextChannel channel = discord.getTextChannelById(ConcordConfig.CHANNEL_ID);
if (channel != null) {
channel.sendMessage(message.getString()).queue();
channel.sendMessage(text).queue();
}
}
}
84 changes: 84 additions & 0 deletions src/main/java/sciwhiz12/concord/msg/PlayerListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package sciwhiz12.concord.msg;

import net.minecraft.advancements.DisplayInfo;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.event.entity.living.LivingDeathEvent;
import net.minecraftforge.event.entity.player.AdvancementEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import sciwhiz12.concord.ChatBot;
import sciwhiz12.concord.ConcordConfig;

public class PlayerListener {
private final ChatBot bot;

public PlayerListener(ChatBot bot) {
this.bot = bot;
}

@SubscribeEvent(priority = EventPriority.LOWEST)
void onPlayerLogin(PlayerEvent.PlayerLoggedInEvent event) {
if (event.getEntity().getEntityWorld().isRemote()) return;
if (!ConcordConfig.PLAYER_JOIN) return;

TranslationTextComponent text = new TranslationTextComponent("message.concord.player.join",
event.getPlayer().getDisplayName());

Messaging.sendToChannel(bot.getDiscord(), text.getString());
}

@SubscribeEvent(priority = EventPriority.LOWEST)
void onPlayerLogout(PlayerEvent.PlayerLoggedOutEvent event) {
if (event.getEntity().getEntityWorld().isRemote()) return;
if (!ConcordConfig.PLAYER_LEAVE) return;

TranslationTextComponent text = new TranslationTextComponent("message.concord.player.leave",
event.getPlayer().getDisplayName());

Messaging.sendToChannel(bot.getDiscord(), text.getString());
}

@SubscribeEvent(priority = EventPriority.LOWEST)
void onLivingDeath(LivingDeathEvent event) {
if (event.getEntity().getEntityWorld().isRemote()) return;
if (!ConcordConfig.PLAYER_DEATH) return;

if (event.getEntity() instanceof ServerPlayerEntity) {
ServerPlayerEntity player = (ServerPlayerEntity) event.getEntity();

Messaging.sendToChannel(bot.getDiscord(), player.getCombatTracker().getDeathMessage().getString());
}
}

@SubscribeEvent(priority = EventPriority.LOWEST)
void onAdvancement(AdvancementEvent event) {
if (event.getEntity().getEntityWorld().isRemote()) return;

final DisplayInfo info = event.getAdvancement().getDisplay();
if (info != null && info.shouldAnnounceToChat()) {
switch (info.getFrame()) {
case TASK: {
if (!ConcordConfig.PLAYER_ADV_TASK) return;
break;
}
case CHALLENGE: {
if (!ConcordConfig.PLAYER_ADV_CHALLENGE) return;
break;
}
case GOAL: {
if (!ConcordConfig.PLAYER_ADV_GOAL) return;
break;
}
}
TranslationTextComponent text = new TranslationTextComponent(
"message.concord.player.advancement." + info.getFrame().getName(),
event.getPlayer().getDisplayName(),
info.getTitle(),
info.getDescription());

Messaging.sendToChannel(bot.getDiscord(), text.getString());
}
}
}
33 changes: 33 additions & 0 deletions src/main/java/sciwhiz12/concord/msg/StatusListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package sciwhiz12.concord.msg;

import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.event.server.FMLServerStartedEvent;
import net.minecraftforge.fml.event.server.FMLServerStoppingEvent;
import sciwhiz12.concord.ChatBot;
import sciwhiz12.concord.ConcordConfig;

public class StatusListener {
private final ChatBot bot;

public StatusListener(ChatBot bot) {
this.bot = bot;
}

@SubscribeEvent(priority = EventPriority.LOWEST)
void onServerStarted(FMLServerStartedEvent event) {
if (!ConcordConfig.SERVER_START) return;

Messaging.sendToChannel(bot.getDiscord(),
new TranslationTextComponent("message.concord.server.start").getString());
}

@SubscribeEvent(priority = EventPriority.LOW)
void onServerStopping(FMLServerStoppingEvent event) {
if (!ConcordConfig.SERVER_STOP) return;

Messaging.sendToChannel(bot.getDiscord(),
new TranslationTextComponent("message.concord.server.stop").getString());
}
}
11 changes: 10 additions & 1 deletion src/main/resources/assets/concord/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,14 @@
"command.concord.reload": "Reloading discord integration...",
"command.concord.status": "Discord integration status: %s",
"command.concord.status.enabled": "ENABLED",
"command.concord.status.disabled": "DISABLED"
"command.concord.status.disabled": "DISABLED",
"message.concord.bot.start": "_Discord integration is now active!_",
"message.concord.bot.stop": "_Discord integration is being disabled!_",
"message.concord.server.start": "_Server is now started!_",
"message.concord.server.stop": "_Server is stopping!_",
"message.concord.player.join": "**%s** _joined the game._",
"message.concord.player.leave": "**%s** _left the game._",
"message.concord.player.advancement.task": "**%s** has made the advancement **%s**\n_%s_",
"message.concord.player.advancement.challenge": "**%s** has completed the challenge **%s**\n_%s_",
"message.concord.player.advancement.goal": "**%s** has reached the goal **%s**\n_%s_"
}

0 comments on commit 08cffdd

Please sign in to comment.