diff --git a/backend/fabric/build.gradle.kts b/backend/fabric/build.gradle.kts index fd15813..39e205c 100644 --- a/backend/fabric/build.gradle.kts +++ b/backend/fabric/build.gradle.kts @@ -10,9 +10,9 @@ dependencies { mappings(loom.officialMojangMappings()) modImplementation(libs.fabric.loader) modImplementation(libs.fabric.api) - include(libs.mixinsextras) - implementation(libs.mixinsextras) - annotationProcessor(libs.mixinsextras) +// include(libs.mixinsextras) +// implementation(libs.mixinsextras) +// annotationProcessor(libs.mixinsextras) shadeModule(projects.signedvelocityBackendCommon) } diff --git a/backend/fabric/src/main/java/io/github/_4drian3d/signedvelocity/fabric/SignedVelocity.java b/backend/fabric/src/main/java/io/github/_4drian3d/signedvelocity/fabric/SignedVelocity.java index 97b0133..b3cdfdc 100644 --- a/backend/fabric/src/main/java/io/github/_4drian3d/signedvelocity/fabric/SignedVelocity.java +++ b/backend/fabric/src/main/java/io/github/_4drian3d/signedvelocity/fabric/SignedVelocity.java @@ -3,13 +3,17 @@ import io.github._4drian3d.signedvelocity.common.SignedQueue; import net.fabricmc.api.DedicatedServerModInitializer; import net.minecraft.resources.ResourceLocation; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public final class SignedVelocity implements DedicatedServerModInitializer { + public static final Logger LOGGER = LoggerFactory.getLogger("SignedVelocity"); public static final ResourceLocation CHANNEL = new ResourceLocation("signedvelocity", "main"); public static final SignedQueue CHAT_QUEUE = new SignedQueue(); public static final SignedQueue COMMAND_QUEUE = new SignedQueue(); @Override public void onInitializeServer() { + LOGGER.info("Started SignedVelocity"); } } \ No newline at end of file diff --git a/backend/fabric/src/main/java/io/github/_4drian3d/signedvelocity/fabric/mixins/ChatCommandPacketMixin.java b/backend/fabric/src/main/java/io/github/_4drian3d/signedvelocity/fabric/mixins/ChatCommandPacketMixin.java new file mode 100644 index 0000000..c02a34a --- /dev/null +++ b/backend/fabric/src/main/java/io/github/_4drian3d/signedvelocity/fabric/mixins/ChatCommandPacketMixin.java @@ -0,0 +1,22 @@ +package io.github._4drian3d.signedvelocity.fabric.mixins; + +import io.github._4drian3d.signedvelocity.fabric.model.SignedChatCommandPacket; +import net.minecraft.network.protocol.game.ServerboundChatCommandPacket; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; + +@Mixin(ServerboundChatCommandPacket.class) +public class ChatCommandPacketMixin implements SignedChatCommandPacket { + @Unique + private boolean signedVelocity$handled = false; + + @Override + public boolean signedVelocity$handled() { + return this.signedVelocity$handled; + } + + @Override + public void signedVelocity$handled(boolean handled) { + this.signedVelocity$handled = handled; + } +} diff --git a/backend/fabric/src/main/java/io/github/_4drian3d/signedvelocity/fabric/mixins/PlayerChatMixin.java b/backend/fabric/src/main/java/io/github/_4drian3d/signedvelocity/fabric/mixins/PlayerChatMixin.java new file mode 100644 index 0000000..892caf0 --- /dev/null +++ b/backend/fabric/src/main/java/io/github/_4drian3d/signedvelocity/fabric/mixins/PlayerChatMixin.java @@ -0,0 +1,22 @@ +package io.github._4drian3d.signedvelocity.fabric.mixins; + +import io.github._4drian3d.signedvelocity.fabric.model.SignedPlayerChatMessage; +import net.minecraft.network.chat.PlayerChatMessage; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; + +@Mixin(PlayerChatMessage.class) +public class PlayerChatMixin implements SignedPlayerChatMessage { + @Unique + public boolean signedVelocity$handled; + + @Override + public boolean signedVelocity$handled() { + return signedVelocity$handled; + } + + @Override + public void signedVelocity$handled(boolean handled) { + signedVelocity$handled = handled; + } +} diff --git a/backend/fabric/src/main/java/io/github/_4drian3d/signedvelocity/fabric/mixins/PlayerListCancellableMixin.java b/backend/fabric/src/main/java/io/github/_4drian3d/signedvelocity/fabric/mixins/PlayerListCancellableMixin.java deleted file mode 100644 index 8f0e43d..0000000 --- a/backend/fabric/src/main/java/io/github/_4drian3d/signedvelocity/fabric/mixins/PlayerListCancellableMixin.java +++ /dev/null @@ -1,30 +0,0 @@ -package io.github._4drian3d.signedvelocity.fabric.mixins; - -import io.github._4drian3d.signedvelocity.common.SignedResult; -import io.github._4drian3d.signedvelocity.fabric.SignedVelocity; -import net.minecraft.network.chat.ChatType; -import net.minecraft.network.chat.PlayerChatMessage; -import net.minecraft.server.level.ServerPlayer; -import net.minecraft.server.players.PlayerList; -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(value = PlayerList.class, priority = 101) -public abstract class PlayerListCancellableMixin { - @Inject( - method = "broadcastChatMessage(Lnet/minecraft/network/chat/PlayerChatMessage;Lnet/minecraft/server/level/ServerPlayer;Lnet/minecraft/network/chat/ChatType$Bound;)V", - at = @At("HEAD"), - cancellable = true) - private void signedVelocity$handleCancellableChat(PlayerChatMessage playerChatMessage, ServerPlayer serverPlayer, ChatType.Bound bound, CallbackInfo ci) { - if (serverPlayer != null) { - System.out.println("Entry to Modify"); - final SignedResult result = SignedVelocity.CHAT_QUEUE.dataFrom(serverPlayer.getUUID()).nextResult().join(); - if (result.cancelled()) { - System.out.println("Modify"); - ci.cancel(); - } - } - } -} diff --git a/backend/fabric/src/main/java/io/github/_4drian3d/signedvelocity/fabric/mixins/PlayerListMixin.java b/backend/fabric/src/main/java/io/github/_4drian3d/signedvelocity/fabric/mixins/PlayerListMixin.java new file mode 100644 index 0000000..3d9d787 --- /dev/null +++ b/backend/fabric/src/main/java/io/github/_4drian3d/signedvelocity/fabric/mixins/PlayerListMixin.java @@ -0,0 +1,50 @@ +package io.github._4drian3d.signedvelocity.fabric.mixins; + +import io.github._4drian3d.signedvelocity.common.SignedResult; +import io.github._4drian3d.signedvelocity.fabric.SignedVelocity; +import io.github._4drian3d.signedvelocity.fabric.model.SignedPlayerChatMessage; +import net.minecraft.network.chat.ChatType; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.PlayerChatMessage; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.server.players.PlayerList; +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; + +import static java.util.Objects.requireNonNull; + +@Mixin(value = PlayerList.class, priority = 1) +public abstract class PlayerListMixin { + @Shadow + public abstract void broadcastChatMessage(PlayerChatMessage playerChatMessage, ServerPlayer serverPlayer, ChatType.Bound bound); + + @Inject( + method = "broadcastChatMessage(Lnet/minecraft/network/chat/PlayerChatMessage;Lnet/minecraft/server/level/ServerPlayer;Lnet/minecraft/network/chat/ChatType$Bound;)V", + at = @At("HEAD"), cancellable = true) + public void signedVelocity$handleChat( + PlayerChatMessage playerChatMessage, + ServerPlayer serverPlayer, + ChatType.Bound bound, + CallbackInfo ci + ) { + requireNonNull(serverPlayer); + if (!((SignedPlayerChatMessage)(Object)playerChatMessage).signedVelocity$handled()) { + ((SignedPlayerChatMessage)(Object)playerChatMessage).signedVelocity$handled(true); + final SignedResult result = SignedVelocity.CHAT_QUEUE.dataFrom(serverPlayer.getUUID()) + .nextResult().join(); + // Cancelled Result + if (result.cancelled()) { + ci.cancel(); + return; + } + final String modified = result.message(); + // Modified Result + if (modified != null) { + this.broadcastChatMessage(playerChatMessage.withUnsignedContent(Component.literal(modified)), serverPlayer, bound); + } + } + } +} diff --git a/backend/fabric/src/main/java/io/github/_4drian3d/signedvelocity/fabric/mixins/PlayerListModifiableMixin.java b/backend/fabric/src/main/java/io/github/_4drian3d/signedvelocity/fabric/mixins/PlayerListModifiableMixin.java deleted file mode 100644 index 3a78b7d..0000000 --- a/backend/fabric/src/main/java/io/github/_4drian3d/signedvelocity/fabric/mixins/PlayerListModifiableMixin.java +++ /dev/null @@ -1,36 +0,0 @@ -package io.github._4drian3d.signedvelocity.fabric.mixins; - -import com.llamalad7.mixinextras.sugar.Local; -import io.github._4drian3d.signedvelocity.common.SignedResult; -import io.github._4drian3d.signedvelocity.fabric.SignedVelocity; -import net.minecraft.network.chat.Component; -import net.minecraft.network.chat.PlayerChatMessage; -import net.minecraft.server.level.ServerPlayer; -import net.minecraft.server.players.PlayerList; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.ModifyVariable; - -@Mixin(value = PlayerList.class, priority = 100) -public abstract class PlayerListModifiableMixin { - @ModifyVariable( - at = @At(value = "HEAD"), - method = "broadcastChatMessage(Lnet/minecraft/network/chat/PlayerChatMessage;Lnet/minecraft/server/level/ServerPlayer;Lnet/minecraft/network/chat/ChatType$Bound;)V", - argsOnly = true) - private PlayerChatMessage signedVelocity$onPlayerChat( - PlayerChatMessage playerChatMessage, - @Local ServerPlayer serverPlayer - ) { - if (serverPlayer == null) { - return playerChatMessage; - } - System.out.println("Entry to Modify"); - final SignedResult result = SignedVelocity.CHAT_QUEUE.dataFrom(serverPlayer.getUUID()).nextResultWithoutAdvance().join(); - final String modifiedChat = result.toModify(); - if (modifiedChat != null) { - System.out.println("Modified"); - return playerChatMessage.withUnsignedContent(Component.literal(modifiedChat)); - } - return playerChatMessage; - } -} diff --git a/backend/fabric/src/main/java/io/github/_4drian3d/signedvelocity/fabric/mixins/ServerCommonPacketListenerMixin.java b/backend/fabric/src/main/java/io/github/_4drian3d/signedvelocity/fabric/mixins/ServerCommonPacketListenerMixin.java index d5c8a74..90f1897 100644 --- a/backend/fabric/src/main/java/io/github/_4drian3d/signedvelocity/fabric/mixins/ServerCommonPacketListenerMixin.java +++ b/backend/fabric/src/main/java/io/github/_4drian3d/signedvelocity/fabric/mixins/ServerCommonPacketListenerMixin.java @@ -1,10 +1,11 @@ package io.github._4drian3d.signedvelocity.fabric.mixins; +import com.google.common.io.ByteArrayDataInput; +import com.google.common.io.ByteStreams; import io.github._4drian3d.signedvelocity.common.SignedQueue; import io.github._4drian3d.signedvelocity.common.SignedResult; import io.github._4drian3d.signedvelocity.fabric.SignedVelocity; import net.fabricmc.fabric.impl.networking.payload.PacketByteBufPayload; -import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.protocol.common.ServerboundCustomPayloadPacket; import org.spongepowered.asm.mixin.Mixin; import net.minecraft.server.network.ServerCommonPacketListenerImpl; @@ -19,10 +20,10 @@ public abstract class ServerCommonPacketListenerMixin { @Inject(at = @At("HEAD"), method = "handleCustomPayload", cancellable = true) private void signedVelocity$onPluginMessage(ServerboundCustomPayloadPacket packet, CallbackInfo ci) { if (packet.payload() instanceof PacketByteBufPayload payload && payload.id().equals(SignedVelocity.CHANNEL)) { - final FriendlyByteBuf input = payload.data(); - final UUID playerId = UUID.fromString(input.readUtf()); - final String source = input.readUtf(); - final String result = input.readUtf(); + final ByteArrayDataInput input = ByteStreams.newDataInput(payload.data().unwrap().array()); + final UUID playerId = UUID.fromString(input.readUTF()); + final String source = input.readUTF(); + final String result = input.readUTF(); final SignedQueue queue = switch (source) { case "COMMAND_RESULT" -> SignedVelocity.COMMAND_QUEUE; @@ -31,7 +32,7 @@ public abstract class ServerCommonPacketListenerMixin { }; final SignedResult resulted = switch (result) { case "CANCEL" -> SignedResult.cancel(); - case "MODIFY" -> SignedResult.modify(input.readUtf()); + case "MODIFY" -> SignedResult.modify(input.readUTF()); case "ALLOWED" -> SignedResult.allowed(); default -> throw new IllegalArgumentException("Invalid result " + result); }; diff --git a/backend/fabric/src/main/java/io/github/_4drian3d/signedvelocity/fabric/mixins/ServerGamePacketListenerCancellableMixin.java b/backend/fabric/src/main/java/io/github/_4drian3d/signedvelocity/fabric/mixins/ServerGamePacketListenerCancellableMixin.java deleted file mode 100644 index dc91671..0000000 --- a/backend/fabric/src/main/java/io/github/_4drian3d/signedvelocity/fabric/mixins/ServerGamePacketListenerCancellableMixin.java +++ /dev/null @@ -1,35 +0,0 @@ -package io.github._4drian3d.signedvelocity.fabric.mixins; - -import io.github._4drian3d.signedvelocity.common.SignedResult; -import io.github._4drian3d.signedvelocity.fabric.SignedVelocity; -import net.minecraft.network.chat.LastSeenMessages; -import net.minecraft.network.protocol.game.ServerboundChatCommandPacket; -import net.minecraft.server.level.ServerPlayer; -import net.minecraft.server.network.ServerGamePacketListenerImpl; -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(value = ServerGamePacketListenerImpl.class, priority = 101) -public abstract class ServerGamePacketListenerCancellableMixin { - @Shadow - public ServerPlayer player; - - @Inject( - method = "performChatCommand", - at = @At("HEAD"), - cancellable = true) - private void signedVelocity$performCancellableCommand( - ServerboundChatCommandPacket serverboundChatCommandPacket, - LastSeenMessages lastSeenMessages, - CallbackInfo ci - ) { - final SignedResult result = SignedVelocity.COMMAND_QUEUE.dataFrom(player.getUUID()) - .nextResult().join(); - if (result.cancelled()) { - ci.cancel(); - } - } -} diff --git a/backend/fabric/src/main/java/io/github/_4drian3d/signedvelocity/fabric/mixins/ServerGamePacketListenerMixin.java b/backend/fabric/src/main/java/io/github/_4drian3d/signedvelocity/fabric/mixins/ServerGamePacketListenerMixin.java new file mode 100644 index 0000000..e2aff12 --- /dev/null +++ b/backend/fabric/src/main/java/io/github/_4drian3d/signedvelocity/fabric/mixins/ServerGamePacketListenerMixin.java @@ -0,0 +1,58 @@ +package io.github._4drian3d.signedvelocity.fabric.mixins; + +import io.github._4drian3d.signedvelocity.common.SignedResult; +import io.github._4drian3d.signedvelocity.fabric.SignedVelocity; +import io.github._4drian3d.signedvelocity.fabric.model.SignedChatCommandPacket; +import net.minecraft.network.chat.LastSeenMessages; +import net.minecraft.network.protocol.game.ServerboundChatCommandPacket; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.server.network.ServerGamePacketListenerImpl; +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(value = ServerGamePacketListenerImpl.class, priority = 1) +public abstract class ServerGamePacketListenerMixin { + @Shadow + public ServerPlayer player; + + @Shadow + protected abstract void performChatCommand(ServerboundChatCommandPacket serverboundChatCommandPacket, LastSeenMessages lastSeenMessages); + + @Inject( + method = "performChatCommand", + at = @At("HEAD"), + cancellable = true) + public void signedVelocity$handleChatCommand( + ServerboundChatCommandPacket packet, + LastSeenMessages lastSeenMessages, + CallbackInfo ci + ) { + if (!((SignedChatCommandPacket)(Object) packet).signedVelocity$handled()) { + return; + } + ((SignedChatCommandPacket)(Object) packet).signedVelocity$handled(true); + final SignedResult result = SignedVelocity.COMMAND_QUEUE.dataFrom(player.getUUID()) + .nextResult().join(); + if (result.cancelled()) { + ci.cancel(); + return; + } + final String modified = result.toModify(); + if (modified != null) { + // TODO: verify + this.performChatCommand( + new ServerboundChatCommandPacket( + modified, + packet.timeStamp(), + packet.salt(), + packet.argumentSignatures(), + packet.lastSeenMessages() + ), + lastSeenMessages + ); + } + } +} diff --git a/backend/fabric/src/main/java/io/github/_4drian3d/signedvelocity/fabric/mixins/ServerGamePacketListenerModifiableMixin.java b/backend/fabric/src/main/java/io/github/_4drian3d/signedvelocity/fabric/mixins/ServerGamePacketListenerModifiableMixin.java deleted file mode 100644 index 6d058c5..0000000 --- a/backend/fabric/src/main/java/io/github/_4drian3d/signedvelocity/fabric/mixins/ServerGamePacketListenerModifiableMixin.java +++ /dev/null @@ -1,44 +0,0 @@ -package io.github._4drian3d.signedvelocity.fabric.mixins; - -import com.llamalad7.mixinextras.sugar.Local; -import io.github._4drian3d.signedvelocity.common.SignedResult; -import io.github._4drian3d.signedvelocity.fabric.SignedVelocity; -import net.minecraft.network.chat.LastSeenMessages; -import net.minecraft.network.protocol.game.ServerboundChatCommandPacket; -import net.minecraft.server.level.ServerPlayer; -import net.minecraft.server.network.ServerGamePacketListenerImpl; -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.ModifyVariable; - -@Mixin(value = ServerGamePacketListenerImpl.class, priority = 100) -public abstract class ServerGamePacketListenerModifiableMixin { - @Shadow - public ServerPlayer player; - - @ModifyVariable( - at = @At("HEAD"), - method = "performChatCommand", - index = 1, - argsOnly = true) - public ServerboundChatCommandPacket signedVelocity$handlePlayerCommand( - ServerboundChatCommandPacket packet, - @Local LastSeenMessages lastSeenMessages - ) { - final SignedResult result = SignedVelocity.COMMAND_QUEUE.dataFrom(player.getUUID()) - .nextResultWithoutAdvance().join(); - final String modified = result.toModify(); - if (modified != null) { - // TODO: verify - return new ServerboundChatCommandPacket( - modified, - packet.timeStamp(), - packet.salt(), - packet.argumentSignatures(), - packet.lastSeenMessages() - ); - } - return packet; - } -} diff --git a/backend/fabric/src/main/java/io/github/_4drian3d/signedvelocity/fabric/model/SignedChatCommandPacket.java b/backend/fabric/src/main/java/io/github/_4drian3d/signedvelocity/fabric/model/SignedChatCommandPacket.java new file mode 100644 index 0000000..53ee766 --- /dev/null +++ b/backend/fabric/src/main/java/io/github/_4drian3d/signedvelocity/fabric/model/SignedChatCommandPacket.java @@ -0,0 +1,7 @@ +package io.github._4drian3d.signedvelocity.fabric.model; + +public interface SignedChatCommandPacket { + boolean signedVelocity$handled(); + + void signedVelocity$handled(boolean handled); +} diff --git a/backend/fabric/src/main/java/io/github/_4drian3d/signedvelocity/fabric/model/SignedPlayerChatMessage.java b/backend/fabric/src/main/java/io/github/_4drian3d/signedvelocity/fabric/model/SignedPlayerChatMessage.java new file mode 100644 index 0000000..f039cc2 --- /dev/null +++ b/backend/fabric/src/main/java/io/github/_4drian3d/signedvelocity/fabric/model/SignedPlayerChatMessage.java @@ -0,0 +1,7 @@ +package io.github._4drian3d.signedvelocity.fabric.model; + +public interface SignedPlayerChatMessage { + boolean signedVelocity$handled(); + + void signedVelocity$handled(boolean handled); +} diff --git a/backend/fabric/src/main/resources/signedvelocity.mixins.json b/backend/fabric/src/main/resources/signedvelocity.mixins.json index 1ebc933..5903484 100644 --- a/backend/fabric/src/main/resources/signedvelocity.mixins.json +++ b/backend/fabric/src/main/resources/signedvelocity.mixins.json @@ -4,11 +4,11 @@ "package": "io.github._4drian3d.signedvelocity.fabric.mixins", "compatibilityLevel": "JAVA_17", "mixins": [ - "PlayerListCancellableMixin", - "PlayerListModifiableMixin", + "ChatCommandPacketMixin", + "PlayerChatMixin", + "PlayerListMixin", "ServerCommonPacketListenerMixin", - "ServerGamePacketListenerCancellableMixin", - "ServerGamePacketListenerModifiableMixin" + "ServerGamePacketListenerMixin" ], "injectors": { "defaultRequire": 1 diff --git a/settings.gradle.kts b/settings.gradle.kts index 9ac41cb..0624f1e 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -18,7 +18,7 @@ dependencyResolutionManagement { maven("https://papermc.io/repo/repository/maven-public/") maven("https://repo.codemc.io/repository/maven-releases/") maven("https://maven.fabricmc.net/") - maven("https://mvn.exceptionflug.de/repository/exceptionflug-public/") + maven("https://maven.elytrium.net/repo/") maven("https://jitpack.io") } }