From c4e534710a18f78c3d13c79be16bc9fddd762e53 Mon Sep 17 00:00:00 2001 From: yitzy299 Date: Tue, 15 Jun 2021 18:50:14 +0100 Subject: [PATCH] Big refactor (#14) Key changes: - BlockBot API for handling of events and listeners - Mixins now trigger events - When adding a bot, the registerBot method in BlockBot should be called. This will handle all the listeners needed for the bot - All bots need to implement the Bot interface and call the registerBot method This is not final but makes up the framework for the future layout of BlockBot. Any suggestions please contact me on the discord Signed-off-by: yitzy299 --- .../quiltservertools/blockbot/BlockBot.java | 35 ++++- .../blockbot/BlockBotDiscord.java | 135 ++++++++++++++++++ .../blockbot/BlockBotUtils.java | 13 ++ .../quiltservertools/blockbot/Discord.java | 115 --------------- .../quiltservertools/blockbot/Listeners.java | 11 -- .../quiltservertools/blockbot/Status.java | 15 +- .../quiltservertools/blockbot/api/Bot.java | 31 ++++ .../blockbot/api/event/ChatMessageEvent.java | 15 ++ .../event/PlayerAdvancementGrantEvent.java | 16 +++ .../blockbot/api/event/PlayerDeathEvent.java | 16 +++ .../mixin/MixinPlayerAdvancementTracker.java | 4 +- .../mixin/MixinServerPlayNetworkHandler.java | 4 +- .../mixin/MixinServerPlayerEntity.java | 7 +- 13 files changed, 266 insertions(+), 151 deletions(-) create mode 100644 src/main/java/com/github/quiltservertools/blockbot/BlockBotDiscord.java create mode 100644 src/main/java/com/github/quiltservertools/blockbot/BlockBotUtils.java delete mode 100644 src/main/java/com/github/quiltservertools/blockbot/Discord.java create mode 100644 src/main/java/com/github/quiltservertools/blockbot/api/Bot.java create mode 100644 src/main/java/com/github/quiltservertools/blockbot/api/event/ChatMessageEvent.java create mode 100644 src/main/java/com/github/quiltservertools/blockbot/api/event/PlayerAdvancementGrantEvent.java create mode 100644 src/main/java/com/github/quiltservertools/blockbot/api/event/PlayerDeathEvent.java diff --git a/src/main/java/com/github/quiltservertools/blockbot/BlockBot.java b/src/main/java/com/github/quiltservertools/blockbot/BlockBot.java index 92adad8..e80229e 100644 --- a/src/main/java/com/github/quiltservertools/blockbot/BlockBot.java +++ b/src/main/java/com/github/quiltservertools/blockbot/BlockBot.java @@ -1,34 +1,57 @@ package com.github.quiltservertools.blockbot; +import com.github.quiltservertools.blockbot.api.Bot; +import com.github.quiltservertools.blockbot.api.event.ChatMessageEvent; +import com.github.quiltservertools.blockbot.api.event.PlayerAdvancementGrantEvent; +import com.github.quiltservertools.blockbot.api.event.PlayerDeathEvent; import net.fabricmc.api.DedicatedServerModInitializer; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; +import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; +import net.minecraft.server.MinecraftServer; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import javax.security.auth.login.LoginException; +import java.util.ArrayList; +import java.util.List; public class BlockBot implements DedicatedServerModInitializer { public static Config CONFIG; public static Logger LOG; - public static Discord DISCORD; + public static final List bots = new ArrayList<>(); @Override public void onInitializeServer() { LOG = LogManager.getLogger(); + CONFIG = new Config(); ServerLifecycleEvents.SERVER_STARTING.register(server -> { - CONFIG = new Config(); try { - DISCORD = new Discord(CONFIG, server); - DISCORD.serverStatus(true); + var bot = new BlockBotDiscord(); + registerBot(bot, CONFIG, server); } catch (LoginException e) { e.printStackTrace(); server.stop(false); } }); + ServerLifecycleEvents.SERVER_STARTED.register(server -> bots.forEach(bot -> bot.serverStatus(true))); ServerLifecycleEvents.SERVER_STOPPING.register(server -> { CONFIG.shutdown(); - DISCORD.serverStatus(false); - DISCORD.shutdown(); + bots.forEach(bot -> { + bot.serverStatus(false); + bot.onShutdown(); + }); }); } + + public void registerBot(Bot bot, Config config, MinecraftServer server) throws LoginException { + bots.add(bot); + bot.registerListeners(config, server); + // Events + ChatMessageEvent.EVENT.register(bot::onChatMessage); + PlayerDeathEvent.EVENT.register(bot::onDeathMessage); + PlayerAdvancementGrantEvent.EVENT.register(bot::onAdvancementGrant); + + ServerPlayConnectionEvents.JOIN.register(bot::onPlayerConnect); + ServerPlayConnectionEvents.DISCONNECT.register(bot::onPlayerDisconnect); + } } diff --git a/src/main/java/com/github/quiltservertools/blockbot/BlockBotDiscord.java b/src/main/java/com/github/quiltservertools/blockbot/BlockBotDiscord.java new file mode 100644 index 0000000..63aaf5b --- /dev/null +++ b/src/main/java/com/github/quiltservertools/blockbot/BlockBotDiscord.java @@ -0,0 +1,135 @@ +package com.github.quiltservertools.blockbot; + +import club.minnced.discord.webhook.WebhookClient; +import club.minnced.discord.webhook.WebhookClientBuilder; +import club.minnced.discord.webhook.send.*; +import com.github.quiltservertools.blockbot.api.Bot; +import com.github.quiltservertools.blockbot.api.event.ChatMessageEvent; +import com.github.quiltservertools.blockbot.api.event.PlayerAdvancementGrantEvent; +import com.github.quiltservertools.blockbot.api.event.PlayerDeathEvent; +import net.dv8tion.jda.api.JDA; +import net.dv8tion.jda.api.JDABuilder; +import net.dv8tion.jda.api.entities.Activity; +import net.dv8tion.jda.api.managers.Presence; +import net.fabricmc.fabric.api.networking.v1.PacketSender; +import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; +import net.minecraft.advancement.Advancement; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.network.ServerPlayNetworkHandler; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.text.Text; +import okhttp3.OkHttpClient; +import okhttp3.Protocol; +import org.apache.commons.lang3.StringUtils; + +import javax.security.auth.login.LoginException; +import java.util.Collections; +import java.util.Objects; + +import static com.github.quiltservertools.blockbot.BlockBotUtils.getAvatarUrl; + +public class BlockBotDiscord implements Bot { + private WebhookClient webhook; + private JDA jda; + private Status status; + + @Override + public void registerListeners(Config config, MinecraftServer server) throws LoginException { + jda = JDABuilder.createDefault(config.getIdentifier()).build(); + jda.addEventListener(new Listeners(config, server)); + BlockBot.LOG.info("Setup discord bot with token provided"); + // Init webhook + WebhookClientBuilder builder = new WebhookClientBuilder(config.getWebhook()); + builder.setHttpClient(new OkHttpClient.Builder() + .protocols(Collections.singletonList(Protocol.HTTP_1_1)) + .build()); + builder.setDaemon(true); + builder.setAllowedMentions(AllowedMentions.none().withParseUsers(true)); + this.webhook = builder.build(); + + this.status = new Status(); + } + + public JDA getBot() { + return jda; + } + + @Override + public void onChatMessage(ServerPlayerEntity player, String message) { + webhook.send(prepareChatMessage(player, message)); + } + + private WebhookMessage prepareChatMessage(ServerPlayerEntity player, String message) { + WebhookMessageBuilder builder = new WebhookMessageBuilder(); + builder.setUsername(player.getName().getString()); + builder.setAvatarUrl(getAvatarUrl(player)); + builder.setContent(message); + return builder.build(); + } + + @Override + public void serverStatus(boolean starting) { + System.out.println(BlockBot.CONFIG.sendStatusMessages()); + if (!BlockBot.CONFIG.sendStatusMessages()) return; + WebhookEmbedBuilder builder = new WebhookEmbedBuilder(); + builder.setAuthor(new WebhookEmbed.EmbedAuthor(BlockBot.CONFIG.getName(), BlockBot.CONFIG.getLogo(), null)); + builder.setColor(starting ? 3334259 : 14695980); + builder.setDescription(starting ? "Server Started" : "Server Stopped"); + webhook.send(builder.build()); + } + + @Override + public void onDeathMessage(ServerPlayerEntity player, Text message) { + WebhookEmbedBuilder builder = new WebhookEmbedBuilder(); + builder.setAuthor(new WebhookEmbed.EmbedAuthor(player.getName().asString(), getAvatarUrl(player), null)); + String msg = message.getString().replaceFirst(player.getName().asString() + " ", ""); + msg = StringUtils.capitalize(msg); + builder.setDescription(msg); + builder.setColor(15789375); + webhook.send(builder.build()); + } + + @Override + public void onAdvancementGrant(ServerPlayerEntity player, Advancement advancement) { + if (!BlockBot.CONFIG.sendAdvancementMessages()) return; + WebhookEmbedBuilder builder = new WebhookEmbedBuilder(); + builder.setDescription(player.getName().getString() + " has made the advancement [" + Objects.requireNonNull(advancement.getDisplay()).getTitle().getString() + "]"); + builder.setColor(16771646); + builder.setAuthor(new WebhookEmbed.EmbedAuthor(player.getName().asString(), getAvatarUrl(player), null)); + webhook.send(builder.build()); + } + + @Override + public void onShutdown() { + jda.shutdown(); + webhook.close(); + } + + @Override + public void tickStatus(MinecraftServer server) { + if (!BlockBot.CONFIG.showPresence()) return; + Presence presence = jda.getPresence(); + Activity status = Activity.playing(BlockBot.CONFIG.getName() + " - " + this.status.getPlayerCount() + " online"); + presence.setActivity(status); + } + + @Override + public void onPlayerConnect(ServerPlayNetworkHandler handler, PacketSender packetSender, MinecraftServer server) { + webhook.send(buildConnectMessage(handler.getPlayer(), true)); + tickStatus(server); + } + + @Override + public void onPlayerDisconnect(ServerPlayNetworkHandler handler, MinecraftServer server) { + webhook.send(buildConnectMessage(handler.getPlayer(), false)); + tickStatus(server); + } + + private WebhookEmbed buildConnectMessage(ServerPlayerEntity player, boolean joined) { + WebhookEmbedBuilder builder = new WebhookEmbedBuilder(); + builder.setAuthor(new WebhookEmbed.EmbedAuthor(player.getName().asString(), getAvatarUrl(player), null)); + builder.setDescription(joined ? "Joined the game" : "Left the game"); + builder.setColor(joined ? 3334259 : 14695980); + return builder.build(); + } +} diff --git a/src/main/java/com/github/quiltservertools/blockbot/BlockBotUtils.java b/src/main/java/com/github/quiltservertools/blockbot/BlockBotUtils.java new file mode 100644 index 0000000..c12ac50 --- /dev/null +++ b/src/main/java/com/github/quiltservertools/blockbot/BlockBotUtils.java @@ -0,0 +1,13 @@ +package com.github.quiltservertools.blockbot; + +import net.minecraft.server.network.ServerPlayerEntity; + +import java.time.LocalDateTime; + +public class BlockBotUtils { + public static String getAvatarUrl(ServerPlayerEntity player) { + String url = "https://crafatar.com/avatars/" + player.getUuidAsString() + "?t=" + LocalDateTime.now().getHour(); + if (BlockBot.CONFIG.enableSkinOverlay()) url = url.concat("&overlay"); + return url; + } +} diff --git a/src/main/java/com/github/quiltservertools/blockbot/Discord.java b/src/main/java/com/github/quiltservertools/blockbot/Discord.java deleted file mode 100644 index 43bebd4..0000000 --- a/src/main/java/com/github/quiltservertools/blockbot/Discord.java +++ /dev/null @@ -1,115 +0,0 @@ -package com.github.quiltservertools.blockbot; - -import club.minnced.discord.webhook.WebhookClient; -import club.minnced.discord.webhook.WebhookClientBuilder; -import club.minnced.discord.webhook.send.*; -import net.dv8tion.jda.api.JDA; -import net.dv8tion.jda.api.JDABuilder; -import net.minecraft.advancement.Advancement; -import net.minecraft.network.packet.c2s.play.ChatMessageC2SPacket; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.network.ServerPlayerEntity; -import net.minecraft.text.Text; -import okhttp3.OkHttpClient; -import okhttp3.Protocol; -import org.apache.commons.lang3.StringUtils; - -import javax.security.auth.login.LoginException; -import java.time.LocalDateTime; -import java.util.Collections; -import java.util.Objects; - - -public class Discord { - private final JDA jda; - private final WebhookClient webhook; - private final String name; - private final String logo; - private final Status status; - - public Discord(Config config, MinecraftServer server) throws LoginException { - jda = JDABuilder.createDefault(config.getIdentifier()).build(); - jda.addEventListener(new Listeners(config, server)); - BlockBot.LOG.info("Setup discord bot with token provided"); - - // Init webhook - WebhookClientBuilder builder = new WebhookClientBuilder(config.getWebhook()); - builder.setHttpClient(new OkHttpClient.Builder() - .protocols(Collections.singletonList(Protocol.HTTP_1_1)) - .build()); - builder.setDaemon(true); - builder.setAllowedMentions(AllowedMentions.none().withParseUsers(true)); - this.webhook = builder.build(); - this.name = config.getName(); - this.logo = config.getLogo(); - this.status = new Status(); - } - - public void shutdown() { - jda.shutdown(); - webhook.close(); - } - - public Status getStatus() { - return status; - } - - public JDA getJda() { - return jda; - } - - public void sendMessageToDiscord(ChatMessageC2SPacket packet, ServerPlayerEntity player) { - webhook.send(prepare(player.getName().asString(), player.getUuidAsString(), packet.getChatMessage())); - } - - public void joinLeaveToDiscord(boolean left, ServerPlayerEntity player) { - WebhookEmbedBuilder builder = new WebhookEmbedBuilder(); - builder.setAuthor(new WebhookEmbed.EmbedAuthor(player.getName().asString(), getAvatar(player.getUuidAsString()), null)); - builder.setDescription(left ? "Left the game" : "Joined the game"); - builder.setColor(left ? 14695980 : 3334259); - webhook.send(builder.build()); - } - - private String getAvatar(String UUID) { - String url = "https://crafatar.com/avatars/" + UUID + "?t=" + LocalDateTime.now().getHour(); - if (BlockBot.CONFIG.enableSkinOverlay()) url = url.concat("&overlay"); - return url; - } - - private WebhookMessage prepare(String username, String uuid, String content) { - WebhookMessageBuilder builder = new WebhookMessageBuilder(); - builder.setUsername(username); - builder.setAvatarUrl(getAvatar(uuid)); - builder.setContent(content); - return builder.build(); - } - - public void serverStatus(boolean start) { - if (!BlockBot.CONFIG.sendStatusMessages()) return; - WebhookEmbedBuilder builder = new WebhookEmbedBuilder(); - builder.setAuthor(new WebhookEmbed.EmbedAuthor(this.name, this.logo, null)); - builder.setColor(start ? 3334259 : 14695980); - builder.setDescription(start ? "Server Started" : "Server Stopped"); - webhook.send(builder.build()); - } - - public void sendDeathMessage(ServerPlayerEntity player, Text text) { - WebhookEmbedBuilder builder = new WebhookEmbedBuilder(); - builder.setAuthor(new WebhookEmbed.EmbedAuthor(player.getName().asString(), getAvatar(player.getUuidAsString()), null)); - String message = text.getString(); - message = message.replaceFirst(player.getName().asString() + " ", ""); - message = StringUtils.capitalize(message); - builder.setDescription(message); - builder.setColor(15789375); - webhook.send(builder.build()); - } - - public void sendAdvancementMessage(ServerPlayerEntity player, Advancement advancement) { - if (!BlockBot.CONFIG.sendAdvancementMessages()) return; - WebhookEmbedBuilder builder = new WebhookEmbedBuilder(); - builder.setDescription(player.getName().getString() + " has made the advancement [" + Objects.requireNonNull(advancement.getDisplay()).getTitle().getString() + "]"); - builder.setColor(16771646); - builder.setAuthor(new WebhookEmbed.EmbedAuthor(player.getName().asString(), getAvatar(player.getUuidAsString()), null)); - webhook.send(builder.build()); - } -} diff --git a/src/main/java/com/github/quiltservertools/blockbot/Listeners.java b/src/main/java/com/github/quiltservertools/blockbot/Listeners.java index 5456576..5965cbd 100644 --- a/src/main/java/com/github/quiltservertools/blockbot/Listeners.java +++ b/src/main/java/com/github/quiltservertools/blockbot/Listeners.java @@ -5,7 +5,6 @@ import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; -import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; import net.minecraft.network.MessageType; import net.minecraft.server.MinecraftServer; import net.minecraft.text.*; @@ -21,16 +20,6 @@ public class Listeners extends ListenerAdapter { public Listeners(Config config, MinecraftServer server) { this.config = config; this.server = server; - ServerPlayConnectionEvents.JOIN.register((handler, sender, server1) -> { - BlockBot.DISCORD.joinLeaveToDiscord(false, handler.player); - BlockBot.DISCORD.getStatus().addPlayer(handler.player.getUuid()); - BlockBot.DISCORD.getStatus().update(); - }); - ServerPlayConnectionEvents.DISCONNECT.register((handler, sender) -> { - BlockBot.DISCORD.joinLeaveToDiscord(true, handler.player); - BlockBot.DISCORD.getStatus().removePlayer(handler.player.getUuid()); - BlockBot.DISCORD.getStatus().update(); - }); } @Override diff --git a/src/main/java/com/github/quiltservertools/blockbot/Status.java b/src/main/java/com/github/quiltservertools/blockbot/Status.java index 95c2140..a05e05b 100644 --- a/src/main/java/com/github/quiltservertools/blockbot/Status.java +++ b/src/main/java/com/github/quiltservertools/blockbot/Status.java @@ -1,33 +1,26 @@ package com.github.quiltservertools.blockbot; -import net.dv8tion.jda.api.entities.Activity; -import net.dv8tion.jda.api.managers.Presence; - import java.util.ArrayList; import java.util.List; import java.util.UUID; public class Status { - private List players; + private final List players; + public Status() { players = new ArrayList<>(); } - private int getPlayerCount() { + public int getPlayerCount() { return players.size(); } public void addPlayer(UUID uuid) { players.add(uuid); } + public void removePlayer(UUID uuid) { players.remove(uuid); } - public void update() { - if (!BlockBot.CONFIG.showPresence()) return; - Presence presence = BlockBot.DISCORD.getJda().getPresence(); - Activity status = Activity.playing(BlockBot.CONFIG.getName() + " - " + getPlayerCount() + " online"); - presence.setActivity(status); - } } diff --git a/src/main/java/com/github/quiltservertools/blockbot/api/Bot.java b/src/main/java/com/github/quiltservertools/blockbot/api/Bot.java new file mode 100644 index 0000000..17b8dad --- /dev/null +++ b/src/main/java/com/github/quiltservertools/blockbot/api/Bot.java @@ -0,0 +1,31 @@ +package com.github.quiltservertools.blockbot.api; + +import com.github.quiltservertools.blockbot.Config; +import net.fabricmc.fabric.api.networking.v1.PacketSender; +import net.minecraft.advancement.Advancement; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.network.ServerPlayNetworkHandler; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.text.Text; + +import javax.security.auth.login.LoginException; + +public interface Bot { + void registerListeners(Config config, MinecraftServer server) throws LoginException; + + void onChatMessage(ServerPlayerEntity player, String message); + + void serverStatus(boolean starting); + + void onDeathMessage(ServerPlayerEntity player, Text message); + + void onAdvancementGrant(ServerPlayerEntity player, Advancement advancement); + + void onShutdown(); + + void tickStatus(MinecraftServer server); + + void onPlayerConnect(ServerPlayNetworkHandler handler, PacketSender packetSender, MinecraftServer server); + + void onPlayerDisconnect(ServerPlayNetworkHandler handler, MinecraftServer server); +} diff --git a/src/main/java/com/github/quiltservertools/blockbot/api/event/ChatMessageEvent.java b/src/main/java/com/github/quiltservertools/blockbot/api/event/ChatMessageEvent.java new file mode 100644 index 0000000..2860ee1 --- /dev/null +++ b/src/main/java/com/github/quiltservertools/blockbot/api/event/ChatMessageEvent.java @@ -0,0 +1,15 @@ +package com.github.quiltservertools.blockbot.api.event; + +import net.fabricmc.fabric.api.event.Event; +import net.fabricmc.fabric.api.event.EventFactory; +import net.minecraft.server.network.ServerPlayerEntity; + +public interface ChatMessageEvent { + Event EVENT = EventFactory.createArrayBacked(ChatMessageEvent.class, (listeners) -> (player, message) -> { + for(ChatMessageEvent listener: listeners) { + listener.message(player, message); + } + }); + + void message(ServerPlayerEntity player, String message); +} diff --git a/src/main/java/com/github/quiltservertools/blockbot/api/event/PlayerAdvancementGrantEvent.java b/src/main/java/com/github/quiltservertools/blockbot/api/event/PlayerAdvancementGrantEvent.java new file mode 100644 index 0000000..4baf2ac --- /dev/null +++ b/src/main/java/com/github/quiltservertools/blockbot/api/event/PlayerAdvancementGrantEvent.java @@ -0,0 +1,16 @@ +package com.github.quiltservertools.blockbot.api.event; + +import net.fabricmc.fabric.api.event.Event; +import net.fabricmc.fabric.api.event.EventFactory; +import net.minecraft.advancement.Advancement; +import net.minecraft.server.network.ServerPlayerEntity; + +public interface PlayerAdvancementGrantEvent { + Event EVENT = EventFactory.createArrayBacked(PlayerAdvancementGrantEvent.class, (listeners) -> (player, advancement) -> { + for (PlayerAdvancementGrantEvent listener : listeners) { + listener.onAdvancementGrant(player, advancement); + } + }); + + void onAdvancementGrant(ServerPlayerEntity player, Advancement advancement); +} diff --git a/src/main/java/com/github/quiltservertools/blockbot/api/event/PlayerDeathEvent.java b/src/main/java/com/github/quiltservertools/blockbot/api/event/PlayerDeathEvent.java new file mode 100644 index 0000000..f6d94cb --- /dev/null +++ b/src/main/java/com/github/quiltservertools/blockbot/api/event/PlayerDeathEvent.java @@ -0,0 +1,16 @@ +package com.github.quiltservertools.blockbot.api.event; + +import net.fabricmc.fabric.api.event.Event; +import net.fabricmc.fabric.api.event.EventFactory; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.text.Text; + +public interface PlayerDeathEvent { + Event EVENT = EventFactory.createArrayBacked(PlayerDeathEvent.class, (listeners) -> (player, message) -> { + for (PlayerDeathEvent listener: listeners) { + listener.death(player, message); + } + }); + + void death(ServerPlayerEntity player, Text message); +} diff --git a/src/main/java/com/github/quiltservertools/blockbot/mixin/MixinPlayerAdvancementTracker.java b/src/main/java/com/github/quiltservertools/blockbot/mixin/MixinPlayerAdvancementTracker.java index f661f6f..fc354d3 100644 --- a/src/main/java/com/github/quiltservertools/blockbot/mixin/MixinPlayerAdvancementTracker.java +++ b/src/main/java/com/github/quiltservertools/blockbot/mixin/MixinPlayerAdvancementTracker.java @@ -1,6 +1,6 @@ package com.github.quiltservertools.blockbot.mixin; -import com.github.quiltservertools.blockbot.BlockBot; +import com.github.quiltservertools.blockbot.api.event.PlayerAdvancementGrantEvent; import net.minecraft.advancement.Advancement; import net.minecraft.advancement.PlayerAdvancementTracker; import net.minecraft.server.network.ServerPlayerEntity; @@ -18,6 +18,6 @@ public class MixinPlayerAdvancementTracker { @Inject(method = "grantCriterion(Lnet/minecraft/advancement/Advancement;Ljava/lang/String;)Z", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/PlayerManager;broadcastChatMessage(Lnet/minecraft/text/Text;Lnet/minecraft/network/MessageType;Ljava/util/UUID;)V")) public void announceAdvancement(Advancement advancement, String criterionName, CallbackInfoReturnable cir) { - BlockBot.DISCORD.sendAdvancementMessage(owner, advancement); + PlayerAdvancementGrantEvent.EVENT.invoker().onAdvancementGrant(owner, advancement); } } diff --git a/src/main/java/com/github/quiltservertools/blockbot/mixin/MixinServerPlayNetworkHandler.java b/src/main/java/com/github/quiltservertools/blockbot/mixin/MixinServerPlayNetworkHandler.java index 4dc0df2..7391e19 100644 --- a/src/main/java/com/github/quiltservertools/blockbot/mixin/MixinServerPlayNetworkHandler.java +++ b/src/main/java/com/github/quiltservertools/blockbot/mixin/MixinServerPlayNetworkHandler.java @@ -1,6 +1,6 @@ package com.github.quiltservertools.blockbot.mixin; -import com.github.quiltservertools.blockbot.BlockBot; +import com.github.quiltservertools.blockbot.api.event.ChatMessageEvent; import net.minecraft.network.packet.c2s.play.ChatMessageC2SPacket; import net.minecraft.server.network.ServerPlayNetworkHandler; import org.spongepowered.asm.mixin.Mixin; @@ -12,7 +12,7 @@ public class MixinServerPlayNetworkHandler { @Inject(method = "onGameMessage(Lnet/minecraft/network/packet/c2s/play/ChatMessageC2SPacket;)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/network/ServerPlayNetworkHandler;filterText(Ljava/lang/String;Ljava/util/function/Consumer;)V")) public void sendChatMessageToDiscord(ChatMessageC2SPacket packet, CallbackInfo ci) { - BlockBot.DISCORD.sendMessageToDiscord(packet, ((ServerPlayNetworkHandler)(Object)this).player); + ChatMessageEvent.EVENT.invoker().message(((ServerPlayNetworkHandler)(Object)this).getPlayer(), packet.getChatMessage()); } } diff --git a/src/main/java/com/github/quiltservertools/blockbot/mixin/MixinServerPlayerEntity.java b/src/main/java/com/github/quiltservertools/blockbot/mixin/MixinServerPlayerEntity.java index 465d6ed..47b1cc2 100644 --- a/src/main/java/com/github/quiltservertools/blockbot/mixin/MixinServerPlayerEntity.java +++ b/src/main/java/com/github/quiltservertools/blockbot/mixin/MixinServerPlayerEntity.java @@ -1,9 +1,8 @@ package com.github.quiltservertools.blockbot.mixin; -import com.github.quiltservertools.blockbot.BlockBot; +import com.github.quiltservertools.blockbot.api.event.PlayerDeathEvent; import net.minecraft.entity.damage.DamageSource; import net.minecraft.server.network.ServerPlayerEntity; -import net.minecraft.text.Text; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; @@ -14,7 +13,7 @@ public class MixinServerPlayerEntity { @Inject(method = "onDeath(Lnet/minecraft/entity/damage/DamageSource;)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/PlayerManager;broadcastChatMessage(Lnet/minecraft/text/Text;Lnet/minecraft/network/MessageType;Ljava/util/UUID;)V")) public void sendDeathMessageToDiscord(DamageSource source, CallbackInfo ci) { - Text message = ((ServerPlayerEntity) (Object) this).getDamageTracker().getDeathMessage(); - BlockBot.DISCORD.sendDeathMessage((ServerPlayerEntity) (Object) this, message); + source.getDeathMessage((ServerPlayerEntity) (Object) this).getString(); + PlayerDeathEvent.EVENT.invoker().death((ServerPlayerEntity) (Object) this, source.getDeathMessage((ServerPlayerEntity) (Object) this)); } }