From edea476066bbf7eda9dd2cd195d8065e422de745 Mon Sep 17 00:00:00 2001 From: Drex Date: Tue, 31 Oct 2023 18:23:39 +0100 Subject: [PATCH 1/7] Fix large images kicking players --- .../extensions/BlockBotApiExtension.kt | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/io/github/quiltservertools/blockbotdiscord/extensions/BlockBotApiExtension.kt b/src/main/kotlin/io/github/quiltservertools/blockbotdiscord/extensions/BlockBotApiExtension.kt index dc4c336..a52765d 100644 --- a/src/main/kotlin/io/github/quiltservertools/blockbotdiscord/extensions/BlockBotApiExtension.kt +++ b/src/main/kotlin/io/github/quiltservertools/blockbotdiscord/extensions/BlockBotApiExtension.kt @@ -61,6 +61,7 @@ import org.koin.core.component.inject import java.net.URL import javax.imageio.ImageIO import kotlin.math.ceil +import kotlin.math.max class BlockBotApiExtension : Extension(), Bot { @@ -158,9 +159,17 @@ class BlockBotApiExtension : Extension(), Bot { var hoverEvent: HoverEvent? = null if (appendImages && attachment.isImage && attachment.size < 8 * 1024 * 1024) { + // The entire message (serialized in json) can at most be 65535 bytes, + // see DataOutputStream.writeUTF(String str, DataOutput out) + + // Each pixel adds ~40 characters to the final string + // We want to use 64000 characters for the message lore. So we can have at most 1600 pixels. + // The easiest implementation is to just limit the width and height to 40 pixels. val image = ImageIO.read(URL(attachment.data.proxyUrl)) - val stepSize = (ceil(image.width.toDouble() / 48).toInt()).coerceAtLeast(1) + val stepSizeWidth = (ceil(image.width.toDouble() / 40).toInt()).coerceAtLeast(1) + val stepSizeHeight = (ceil(image.height.toDouble() / 40).toInt()).coerceAtLeast(1) + val stepSize = max(stepSizeWidth, stepSizeHeight) val stepSquared = stepSize * stepSize val width = image.width val height = image.height From e8e4c9ad20c061f75218512ee1177404cffe822e Mon Sep 17 00:00:00 2001 From: Drex Date: Sun, 10 Dec 2023 15:08:45 +0100 Subject: [PATCH 2/7] Improve tellraw selector filter I hope this is the last change that is required to satisfy everyone. This improves the selection filter to compare it to the second last node, which for tellraw should always be the selector! (This commit allow commands like `/execute ... run tellraw @a [{"text":"test"}]` to be broadcast.) --- .../blockbotapi/mixin/TellRawCommandMixin.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/mixin/TellRawCommandMixin.java b/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/mixin/TellRawCommandMixin.java index ecae357..7bf7d0f 100644 --- a/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/mixin/TellRawCommandMixin.java +++ b/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/mixin/TellRawCommandMixin.java @@ -1,12 +1,11 @@ package io.github.quiltservertools.blockbotapi.mixin; import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.context.ParsedCommandNode; import com.mojang.brigadier.exceptions.CommandSyntaxException; import io.github.quiltservertools.blockbotapi.event.ChatMessageEvent; import io.github.quiltservertools.blockbotapi.sender.MessageSender; import io.github.quiltservertools.blockbotapi.sender.PlayerMessageSender; -import net.minecraft.command.EntitySelector; -import net.minecraft.command.argument.EntityArgumentType; import net.minecraft.command.argument.TextArgumentType; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.command.TellRawCommand; @@ -24,9 +23,12 @@ public abstract class TellRawCommandMixin { method = "method_13777", at = @At(value = "HEAD") ) - private static void relayMeToDiscord(CommandContext context, CallbackInfoReturnable cir) throws CommandSyntaxException { - // Check if tellraw is sent to every online player - if (context.getArgument("targets", EntitySelector.class).getLimit() > 1 && EntityArgumentType.getPlayers(context, "targets").size() == context.getSource().getPlayerNames().size()) { + private static void relayTellrawToDiscord(CommandContext context, CallbackInfoReturnable cir) throws CommandSyntaxException { + // We are checking for "@a" to make sure only messages intended for the public are relayed. + // Messages with a selector like @a[distance=..100] should not be relayed. + String input = context.getInput(); + ParsedCommandNode parsedCommandNode = context.getNodes().get(context.getNodes().size() - 2); + if (parsedCommandNode.getRange().get(input).equals("@a")) { var entity = context.getSource().getEntity(); MessageSender sender; if (entity instanceof ServerPlayerEntity player) { From cffdefbf3b64a0484f19ae5e1dcda18c34d2836b Mon Sep 17 00:00:00 2001 From: Drex Date: Sun, 10 Dec 2023 15:23:17 +0100 Subject: [PATCH 3/7] Update to 1.20.4 --- build.gradle.kts | 2 ++ libs.versions.toml | 14 +++++++------- .../extensions/BlockBotApiExtension.kt | 2 +- .../blockbotdiscord/utility/Extensions.kt | 4 ++-- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 9746b0f..1f59365 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -38,6 +38,8 @@ allprojects { } maven("https://api.modrinth.com/maven") maven("https://oss.sonatype.org/content/repositories/snapshots") + // net.kyori:adventure-text-serializer-gson dev builds + maven("https://s01.oss.sonatype.org/content/repositories/snapshots/") } // Declare dependencies diff --git a/libs.versions.toml b/libs.versions.toml index 70cfb0a..61773ca 100644 --- a/libs.versions.toml +++ b/libs.versions.toml @@ -1,9 +1,9 @@ [versions] -minecraft = "1.20.2" -fabric-loader = "0.14.23" -yarn-mappings = "1.20.2+build.1" +minecraft = "1.20.4" +fabric-loader = "0.15.1" +yarn-mappings = "1.20.4+build.1" -fabric-api = "0.90.0+1.20.2" +fabric-api = "0.91.2+1.20.4" # Kotlin fabric-kotlin = "1.9.4+kotlin.1.8.21" @@ -12,11 +12,11 @@ fabric-kotlin = "1.9.4+kotlin.1.8.21" kord-extensions = "1.5.6" mc-discord-reserializer = "4.3.0" emoji-java = "5.1.1" -adventure-gson = "4.11.0" +adventure-gson = "4.15.0-SNAPSHOT" -placeholder-api = "2.2.0+1.20.2" +placeholder-api = "2.3.0+1.20.3" permission-api = "0.3-SNAPSHOT" -vanish-api = "1.4.3+1.20.2" +vanish-api = "1.5.0+1.20.3-rc1" konf = "1.1.2" [libraries] diff --git a/src/main/kotlin/io/github/quiltservertools/blockbotdiscord/extensions/BlockBotApiExtension.kt b/src/main/kotlin/io/github/quiltservertools/blockbotdiscord/extensions/BlockBotApiExtension.kt index a52765d..de38bb9 100644 --- a/src/main/kotlin/io/github/quiltservertools/blockbotdiscord/extensions/BlockBotApiExtension.kt +++ b/src/main/kotlin/io/github/quiltservertools/blockbotdiscord/extensions/BlockBotApiExtension.kt @@ -210,7 +210,7 @@ class BlockBotApiExtension : Extension(), Bot { x += stepSize } - list.add(NbtString.of(Text.Serializer.toJson(text))) + list.add(NbtString.of(Text.Serialization.toJsonString(text))) y += stepSize x = 0 } diff --git a/src/main/kotlin/io/github/quiltservertools/blockbotdiscord/utility/Extensions.kt b/src/main/kotlin/io/github/quiltservertools/blockbotdiscord/utility/Extensions.kt index d8908bd..135be83 100644 --- a/src/main/kotlin/io/github/quiltservertools/blockbotdiscord/utility/Extensions.kt +++ b/src/main/kotlin/io/github/quiltservertools/blockbotdiscord/utility/Extensions.kt @@ -23,8 +23,8 @@ fun Message.summary(): String { fun GameProfile.getTextures() = Iterables.getFirst(this.properties.get("textures"), null)?.value -fun Component.toNative(): MutableText = Text.Serializer.fromJson(GsonComponentSerializer.gson().serialize(this))?: Text.empty() +fun Component.toNative(): MutableText = Text.Serialization.fromJson(GsonComponentSerializer.gson().serialize(this))?: Text.empty() -fun Text.toAdventure() = GsonComponentSerializer.gson().deserialize(Text.Serializer.toJson(this)) +fun Text.toAdventure() = GsonComponentSerializer.gson().deserialize(Text.Serialization.toJsonString(this)) fun ServerPlayerEntity.isVanished() = FabricLoader.getInstance().isModLoaded("melius-vanish") && VanishAPI.isVanished(this) From 694ada741a68fcdee90ef35e627f76954e15b385 Mon Sep 17 00:00:00 2001 From: Drex Date: Thu, 25 Jan 2024 23:44:35 +0100 Subject: [PATCH 4/7] Improve join / leave message events Moves join and leave message events to the actual broadcast to ensure better compatibility --- .../blockbotapi/BlockBotApi.java | 9 +++---- .../quiltservertools/blockbotapi/Bot.java | 4 +-- .../event/PlayerJoinMessageEvent.java | 15 +++++++++++ .../event/PlayerLeaveMessageEvent.java | 15 +++++++++++ .../blockbotapi/mixin/PlayerManagerMixin.java | 25 +++++++++++++++++ .../mixin/ServerPlayNetworkHandlerMixin.java | 27 +++++++++++++++++++ .../main/resources/blockbot-api.mixins.json | 2 ++ build.gradle.kts | 2 +- gradle/wrapper/gradle-wrapper.properties | 2 +- libs.versions.toml | 2 +- .../extensions/BlockBotApiExtension.kt | 8 +++--- 11 files changed, 96 insertions(+), 15 deletions(-) create mode 100644 blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/event/PlayerJoinMessageEvent.java create mode 100644 blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/event/PlayerLeaveMessageEvent.java create mode 100644 blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/mixin/PlayerManagerMixin.java create mode 100644 blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/mixin/ServerPlayNetworkHandlerMixin.java diff --git a/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/BlockBotApi.java b/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/BlockBotApi.java index 6bdceb7..906a222 100644 --- a/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/BlockBotApi.java +++ b/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/BlockBotApi.java @@ -1,15 +1,12 @@ package io.github.quiltservertools.blockbotapi; -import io.github.quiltservertools.blockbotapi.event.ChatMessageEvent; -import io.github.quiltservertools.blockbotapi.event.PlayerAdvancementGrantEvent; -import io.github.quiltservertools.blockbotapi.event.PlayerDeathEvent; +import io.github.quiltservertools.blockbotapi.event.*; import io.github.quiltservertools.blockbotapi.sender.MessageSender; import io.github.quiltservertools.blockbotapi.sender.PlayerMessageSender; import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; import net.fabricmc.fabric.api.message.v1.ServerMessageEvents; -import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -38,8 +35,8 @@ public static void registerBot(Bot bot) { BlockBotApi.bots.add(bot); ChatMessageEvent.EVENT.register(bot::onChatMessage); - ServerPlayConnectionEvents.JOIN.register(bot::onPlayerConnect); - ServerPlayConnectionEvents.DISCONNECT.register(bot::onPlayerDisconnect); + PlayerJoinMessageEvent.EVENT.register(bot::onPlayerJoinMessage); + PlayerLeaveMessageEvent.EVENT.register(bot::onPlayerLeaveMessage); PlayerDeathEvent.EVENT.register(bot::onPlayerDeath); PlayerAdvancementGrantEvent.EVENT.register(bot::onAdvancementGrant); ServerLifecycleEvents.SERVER_STARTED.register(bot::onServerStart); diff --git a/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/Bot.java b/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/Bot.java index 4494c91..35b8301 100644 --- a/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/Bot.java +++ b/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/Bot.java @@ -10,8 +10,8 @@ public interface Bot { void onChatMessage(MessageSender sender, Text message); - void onPlayerConnect(ServerPlayNetworkHandler handler, PacketSender sender, MinecraftServer server); - void onPlayerDisconnect(ServerPlayNetworkHandler handler, MinecraftServer server); + void onPlayerJoinMessage(ServerPlayerEntity player); + void onPlayerLeaveMessage(ServerPlayerEntity player); void onPlayerDeath(ServerPlayerEntity player, Text message); void onAdvancementGrant(ServerPlayerEntity player, Advancement advancement); void onServerStart(MinecraftServer server); diff --git a/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/event/PlayerJoinMessageEvent.java b/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/event/PlayerJoinMessageEvent.java new file mode 100644 index 0000000..9f721d9 --- /dev/null +++ b/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/event/PlayerJoinMessageEvent.java @@ -0,0 +1,15 @@ +package io.github.quiltservertools.blockbotapi.event; + +import net.fabricmc.fabric.api.event.Event; +import net.fabricmc.fabric.api.event.EventFactory; +import net.minecraft.server.network.ServerPlayerEntity; + +public interface PlayerJoinMessageEvent { + Event EVENT = EventFactory.createArrayBacked(PlayerJoinMessageEvent.class, (listeners) -> (player) -> { + for (PlayerJoinMessageEvent listener : listeners) { + listener.onPlayerJoinMessage(player); + } + }); + + void onPlayerJoinMessage(ServerPlayerEntity player); +} diff --git a/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/event/PlayerLeaveMessageEvent.java b/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/event/PlayerLeaveMessageEvent.java new file mode 100644 index 0000000..1bc8638 --- /dev/null +++ b/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/event/PlayerLeaveMessageEvent.java @@ -0,0 +1,15 @@ +package io.github.quiltservertools.blockbotapi.event; + +import net.fabricmc.fabric.api.event.Event; +import net.fabricmc.fabric.api.event.EventFactory; +import net.minecraft.server.network.ServerPlayerEntity; + +public interface PlayerLeaveMessageEvent { + Event EVENT = EventFactory.createArrayBacked(PlayerLeaveMessageEvent.class, (listeners) -> (player) -> { + for (PlayerLeaveMessageEvent listener : listeners) { + listener.onPlayerLeaveMessage(player); + } + }); + + void onPlayerLeaveMessage(ServerPlayerEntity player); +} diff --git a/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/mixin/PlayerManagerMixin.java b/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/mixin/PlayerManagerMixin.java new file mode 100644 index 0000000..75a9fbe --- /dev/null +++ b/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/mixin/PlayerManagerMixin.java @@ -0,0 +1,25 @@ +package io.github.quiltservertools.blockbotapi.mixin; + +import io.github.quiltservertools.blockbotapi.event.PlayerJoinMessageEvent; +import net.minecraft.network.ClientConnection; +import net.minecraft.server.PlayerManager; +import net.minecraft.server.network.ConnectedClientData; +import net.minecraft.server.network.ServerPlayerEntity; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(PlayerManager.class) +public abstract class PlayerManagerMixin { + @Inject( + method = "onPlayerConnect", + at = @At( + value = "INVOKE", + target = "Lnet/minecraft/server/PlayerManager;broadcast(Lnet/minecraft/text/Text;Z)V" + ) + ) + private void relayJoinMessageToDiscord(ClientConnection connection, ServerPlayerEntity player, ConnectedClientData clientData, CallbackInfo ci) { + PlayerJoinMessageEvent.EVENT.invoker().onPlayerJoinMessage(player); + } +} diff --git a/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/mixin/ServerPlayNetworkHandlerMixin.java b/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/mixin/ServerPlayNetworkHandlerMixin.java new file mode 100644 index 0000000..f718cec --- /dev/null +++ b/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/mixin/ServerPlayNetworkHandlerMixin.java @@ -0,0 +1,27 @@ +package io.github.quiltservertools.blockbotapi.mixin; + +import io.github.quiltservertools.blockbotapi.event.PlayerLeaveMessageEvent; +import net.minecraft.server.network.ServerPlayNetworkHandler; +import net.minecraft.server.network.ServerPlayerEntity; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(ServerPlayNetworkHandler.class) +public abstract class ServerPlayNetworkHandlerMixin { + @Shadow + public ServerPlayerEntity player; + + @Inject( + method = "cleanUp", + at = @At( + value = "INVOKE", + target = "Lnet/minecraft/server/PlayerManager;broadcast(Lnet/minecraft/text/Text;Z)V" + ) + ) + private void relayLeaveMessageToDiscord(CallbackInfo ci) { + PlayerLeaveMessageEvent.EVENT.invoker().onPlayerLeaveMessage(this.player); + } +} diff --git a/blockbot-api/src/main/resources/blockbot-api.mixins.json b/blockbot-api/src/main/resources/blockbot-api.mixins.json index f5b579a..440bdac 100644 --- a/blockbot-api/src/main/resources/blockbot-api.mixins.json +++ b/blockbot-api/src/main/resources/blockbot-api.mixins.json @@ -7,7 +7,9 @@ "MeCommandMixin", "MixinPlayerAdvancementTracker", "MixinServerPlayerEntity", + "PlayerManagerMixin", "SayCommandMixin", + "ServerPlayNetworkHandlerMixin", "TellRawCommandMixin" ], "server": [], diff --git a/build.gradle.kts b/build.gradle.kts index 1f59365..c03b416 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,7 +1,7 @@ plugins { java id("maven-publish") - id("fabric-loom") version "1.2.+" + id("fabric-loom") version "1.5.+" kotlin("jvm") version "1.7.10" id("com.github.johnrengelman.shadow") version "7.1.2" kotlin("plugin.serialization") version "1.7.10" diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index fae0804..a595206 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/libs.versions.toml b/libs.versions.toml index 61773ca..cfe4901 100644 --- a/libs.versions.toml +++ b/libs.versions.toml @@ -1,6 +1,6 @@ [versions] minecraft = "1.20.4" -fabric-loader = "0.15.1" +fabric-loader = "0.15.6" yarn-mappings = "1.20.4+build.1" fabric-api = "0.91.2+1.20.4" diff --git a/src/main/kotlin/io/github/quiltservertools/blockbotdiscord/extensions/BlockBotApiExtension.kt b/src/main/kotlin/io/github/quiltservertools/blockbotdiscord/extensions/BlockBotApiExtension.kt index de38bb9..8842da9 100644 --- a/src/main/kotlin/io/github/quiltservertools/blockbotdiscord/extensions/BlockBotApiExtension.kt +++ b/src/main/kotlin/io/github/quiltservertools/blockbotdiscord/extensions/BlockBotApiExtension.kt @@ -320,8 +320,8 @@ class BlockBotApiExtension : Extension(), Bot { } } - override fun onPlayerConnect(handler: ServerPlayNetworkHandler, sender: PacketSender, server: MinecraftServer) { - if (!handler.player.isVanished()) sendPlayerJoinMessage(handler.player) + override fun onPlayerJoinMessage(player: ServerPlayerEntity) { + if (player.isVanished()) sendPlayerJoinMessage(player) } private fun sendPlayerJoinMessage(player: ServerPlayerEntity) { @@ -337,8 +337,8 @@ class BlockBotApiExtension : Extension(), Bot { } } - override fun onPlayerDisconnect(handler: ServerPlayNetworkHandler, server: MinecraftServer) { - if (!handler.player.isVanished()) sendPlayerLeaveMessage(handler.player) + override fun onPlayerLeaveMessage(player: ServerPlayerEntity) { + if (!player.isVanished()) sendPlayerLeaveMessage(player) } private fun sendPlayerLeaveMessage(player: ServerPlayerEntity) { From 0ac50b991091a744706e05262fd19677ca49119c Mon Sep 17 00:00:00 2001 From: Drex Date: Thu, 25 Jan 2024 23:51:56 +0100 Subject: [PATCH 5/7] Fix join message --- .../blockbotdiscord/extensions/BlockBotApiExtension.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/io/github/quiltservertools/blockbotdiscord/extensions/BlockBotApiExtension.kt b/src/main/kotlin/io/github/quiltservertools/blockbotdiscord/extensions/BlockBotApiExtension.kt index 8842da9..6f1b4d8 100644 --- a/src/main/kotlin/io/github/quiltservertools/blockbotdiscord/extensions/BlockBotApiExtension.kt +++ b/src/main/kotlin/io/github/quiltservertools/blockbotdiscord/extensions/BlockBotApiExtension.kt @@ -321,7 +321,7 @@ class BlockBotApiExtension : Extension(), Bot { } override fun onPlayerJoinMessage(player: ServerPlayerEntity) { - if (player.isVanished()) sendPlayerJoinMessage(player) + if (!player.isVanished()) sendPlayerJoinMessage(player) } private fun sendPlayerJoinMessage(player: ServerPlayerEntity) { From cb0e850498962164d625d38203d0affe45c489ab Mon Sep 17 00:00:00 2001 From: Drex Date: Tue, 2 Apr 2024 00:42:26 +0200 Subject: [PATCH 6/7] Implement #67 resolves #67 --- .../blockbotapi/mixin/MeCommandMixin.java | 20 +---------- .../blockbotapi/mixin/RandomCommandMixin.java | 34 +++++++++++++++++++ .../blockbotapi/mixin/SayCommandMixin.java | 20 +---------- .../mixin/TellRawCommandMixin.java | 18 +--------- .../blockbotapi/sender/MessageSender.java | 20 +++++++++++ .../main/resources/blockbot-api.mixins.json | 1 + 6 files changed, 58 insertions(+), 55 deletions(-) create mode 100644 blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/mixin/RandomCommandMixin.java diff --git a/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/mixin/MeCommandMixin.java b/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/mixin/MeCommandMixin.java index 56474a4..5fefabc 100644 --- a/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/mixin/MeCommandMixin.java +++ b/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/mixin/MeCommandMixin.java @@ -3,13 +3,9 @@ import com.mojang.brigadier.context.CommandContext; import io.github.quiltservertools.blockbotapi.event.ChatMessageEvent; import io.github.quiltservertools.blockbotapi.sender.MessageSender; -import io.github.quiltservertools.blockbotapi.sender.PlayerMessageSender; import net.minecraft.network.message.SignedMessage; -import net.minecraft.server.PlayerManager; import net.minecraft.server.command.MeCommand; import net.minecraft.server.command.ServerCommandSource; -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; @@ -22,21 +18,7 @@ public abstract class MeCommandMixin { at = @At(value = "INVOKE", target = "Lnet/minecraft/server/PlayerManager;broadcast(Lnet/minecraft/network/message/SignedMessage;Lnet/minecraft/server/command/ServerCommandSource;Lnet/minecraft/network/message/MessageType$Parameters;)V") ) private static void relayPlayerMeToDiscord(CommandContext ctx, SignedMessage message, CallbackInfo ci) { - var entity = ctx.getSource().getEntity(); - MessageSender sender; - if (entity instanceof ServerPlayerEntity player) { - sender = new PlayerMessageSender( - player, - MessageSender.MessageType.EMOTE - ); - } else { - sender = new MessageSender( - Text.literal(ctx.getSource().getName()), - ctx.getSource().getDisplayName(), - MessageSender.MessageType.EMOTE - ); - } - + MessageSender sender = MessageSender.of(ctx.getSource(), MessageSender.MessageType.EMOTE); ChatMessageEvent.EVENT.invoker().message( sender, message.getContent() diff --git a/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/mixin/RandomCommandMixin.java b/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/mixin/RandomCommandMixin.java new file mode 100644 index 0000000..541786e --- /dev/null +++ b/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/mixin/RandomCommandMixin.java @@ -0,0 +1,34 @@ +package io.github.quiltservertools.blockbotapi.mixin; + +import com.llamalad7.mixinextras.injector.wrapoperation.Operation; +import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; +import io.github.quiltservertools.blockbotapi.event.ChatMessageEvent; +import io.github.quiltservertools.blockbotapi.sender.MessageSender; +import net.minecraft.server.PlayerManager; +import net.minecraft.server.command.RandomCommand; +import net.minecraft.server.command.ServerCommandSource; +import net.minecraft.text.MutableText; +import net.minecraft.text.Text; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; + +@Mixin(RandomCommand.class) +public abstract class RandomCommandMixin { + + @WrapOperation( + method = "execute", + at = @At( + value = "INVOKE", target = "Lnet/minecraft/server/PlayerManager;broadcast(Lnet/minecraft/text/Text;Z)V" + ) + ) + private static void relayRandomRollToDiscord(PlayerManager instance, Text message, boolean overlay, Operation original, ServerCommandSource source) { + original.call(instance, message, overlay); + MessageSender sender = MessageSender.of(source, MessageSender.MessageType.ANNOUNCEMENT); + ChatMessageEvent.EVENT.invoker().message( + sender, + // Change translation to literal + Text.literal(message.getString()) + ); + } + +} diff --git a/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/mixin/SayCommandMixin.java b/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/mixin/SayCommandMixin.java index c8a9aa7..57f15d8 100644 --- a/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/mixin/SayCommandMixin.java +++ b/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/mixin/SayCommandMixin.java @@ -3,13 +3,9 @@ import com.mojang.brigadier.context.CommandContext; import io.github.quiltservertools.blockbotapi.event.ChatMessageEvent; import io.github.quiltservertools.blockbotapi.sender.MessageSender; -import io.github.quiltservertools.blockbotapi.sender.PlayerMessageSender; import net.minecraft.network.message.SignedMessage; -import net.minecraft.server.PlayerManager; import net.minecraft.server.command.SayCommand; import net.minecraft.server.command.ServerCommandSource; -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; @@ -22,21 +18,7 @@ public abstract class SayCommandMixin { at = @At(value = "INVOKE", target = "Lnet/minecraft/server/PlayerManager;broadcast(Lnet/minecraft/network/message/SignedMessage;Lnet/minecraft/server/command/ServerCommandSource;Lnet/minecraft/network/message/MessageType$Parameters;)V") ) private static void relayPlayerSayToDiscord(CommandContext ctx, SignedMessage message, CallbackInfo ci) { - var entity = ctx.getSource().getEntity(); - MessageSender sender; - if (entity instanceof ServerPlayerEntity player) { - sender = new PlayerMessageSender( - player, - MessageSender.MessageType.ANNOUNCEMENT - ); - } else { - sender = new MessageSender( - Text.literal(ctx.getSource().getName()), - ctx.getSource().getDisplayName(), - MessageSender.MessageType.ANNOUNCEMENT - ); - } - + MessageSender sender = MessageSender.of(ctx.getSource(), MessageSender.MessageType.ANNOUNCEMENT); ChatMessageEvent.EVENT.invoker().message( sender, message.getContent() diff --git a/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/mixin/TellRawCommandMixin.java b/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/mixin/TellRawCommandMixin.java index 7bf7d0f..9f33ed0 100644 --- a/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/mixin/TellRawCommandMixin.java +++ b/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/mixin/TellRawCommandMixin.java @@ -5,12 +5,9 @@ import com.mojang.brigadier.exceptions.CommandSyntaxException; import io.github.quiltservertools.blockbotapi.event.ChatMessageEvent; import io.github.quiltservertools.blockbotapi.sender.MessageSender; -import io.github.quiltservertools.blockbotapi.sender.PlayerMessageSender; import net.minecraft.command.argument.TextArgumentType; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.command.TellRawCommand; -import net.minecraft.server.network.ServerPlayerEntity; -import net.minecraft.text.Text; import net.minecraft.text.Texts; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; @@ -30,20 +27,7 @@ private static void relayTellrawToDiscord(CommandContext co ParsedCommandNode parsedCommandNode = context.getNodes().get(context.getNodes().size() - 2); if (parsedCommandNode.getRange().get(input).equals("@a")) { var entity = context.getSource().getEntity(); - MessageSender sender; - if (entity instanceof ServerPlayerEntity player) { - sender = new PlayerMessageSender( - player, - MessageSender.MessageType.REGULAR - ); - } else { - sender = new MessageSender( - Text.literal(context.getSource().getName()), - context.getSource().getDisplayName(), - MessageSender.MessageType.REGULAR - ); - } - + MessageSender sender = MessageSender.of(context.getSource(), MessageSender.MessageType.EMOTE); ChatMessageEvent.EVENT.invoker().message( sender, Texts.parse(context.getSource(), TextArgumentType.getTextArgument(context, "message"), entity, 0) diff --git a/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/sender/MessageSender.java b/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/sender/MessageSender.java index 0a8c23e..d6a0b3c 100644 --- a/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/sender/MessageSender.java +++ b/blockbot-api/src/main/java/io/github/quiltservertools/blockbotapi/sender/MessageSender.java @@ -1,5 +1,7 @@ package io.github.quiltservertools.blockbotapi.sender; +import net.minecraft.server.command.ServerCommandSource; +import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; import org.jetbrains.annotations.NotNull; @@ -14,6 +16,24 @@ public MessageSender(Text name, Text displayName, @NotNull MessageType type) { this.type = type; } + public static MessageSender of(ServerCommandSource commandSource, MessageType type) { + var entity = commandSource.getEntity(); + MessageSender sender; + if (entity instanceof ServerPlayerEntity player) { + sender = new PlayerMessageSender( + player, + type + ); + } else { + sender = new MessageSender( + Text.literal(commandSource.getName()), + commandSource.getDisplayName(), + type + ); + } + return sender; + } + public Text getName() { return name; } diff --git a/blockbot-api/src/main/resources/blockbot-api.mixins.json b/blockbot-api/src/main/resources/blockbot-api.mixins.json index 440bdac..2a0a540 100644 --- a/blockbot-api/src/main/resources/blockbot-api.mixins.json +++ b/blockbot-api/src/main/resources/blockbot-api.mixins.json @@ -8,6 +8,7 @@ "MixinPlayerAdvancementTracker", "MixinServerPlayerEntity", "PlayerManagerMixin", + "RandomCommandMixin", "SayCommandMixin", "ServerPlayNetworkHandlerMixin", "TellRawCommandMixin" From aca7039f3b6a40acd6584304d1807f6fa329e271 Mon Sep 17 00:00:00 2001 From: Drex Date: Wed, 24 Apr 2024 01:29:05 +0200 Subject: [PATCH 7/7] Update dependencies --- build.gradle.kts | 2 +- gradle/wrapper/gradle-wrapper.properties | 2 +- libs.versions.toml | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index c03b416..0d4fa40 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,7 +1,7 @@ plugins { java id("maven-publish") - id("fabric-loom") version "1.5.+" + id("fabric-loom") version "1.6.+" kotlin("jvm") version "1.7.10" id("com.github.johnrengelman.shadow") version "7.1.2" kotlin("plugin.serialization") version "1.7.10" diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index a595206..48c0a02 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/libs.versions.toml b/libs.versions.toml index cfe4901..4d9beb6 100644 --- a/libs.versions.toml +++ b/libs.versions.toml @@ -1,9 +1,9 @@ [versions] minecraft = "1.20.4" -fabric-loader = "0.15.6" -yarn-mappings = "1.20.4+build.1" +fabric-loader = "0.15.10" +yarn-mappings = "1.20.4+build.3" -fabric-api = "0.91.2+1.20.4" +fabric-api = "0.97.0+1.20.4" # Kotlin fabric-kotlin = "1.9.4+kotlin.1.8.21" @@ -15,7 +15,7 @@ emoji-java = "5.1.1" adventure-gson = "4.15.0-SNAPSHOT" placeholder-api = "2.3.0+1.20.3" -permission-api = "0.3-SNAPSHOT" +permission-api = "0.3.1" vanish-api = "1.5.0+1.20.3-rc1" konf = "1.1.2"