From 541809322e3d9e7e936da1be4a6156f23bccd31e Mon Sep 17 00:00:00 2001 From: TexTrue <3140846162@qq.com> Date: Sun, 14 Jul 2024 11:10:37 +0800 Subject: [PATCH] use BadPackets replace `fabric-networking-api` --- .gitignore | 1 + CHANGELOG.md | 2 +- build.gradle | 30 +++++++--------- gradle.properties | 4 ++- .../servux/network/IPluginChannelHandler.java | 5 +-- .../masa/servux/network/PacketSplitter.java | 6 ++-- .../network/ServerPacketChannelHandler.java | 34 +++++++++++++++---- 7 files changed, 53 insertions(+), 29 deletions(-) diff --git a/.gitignore b/.gitignore index e2d09f5..6e1766b 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ logs/ .project build.number /.idea/ +/run/ diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c5147a..13784fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,2 +1,2 @@ ## Change -- port to NeoForge \ No newline at end of file +- use BadPackets replace `fabric-networking-api` \ No newline at end of file diff --git a/build.gradle b/build.gradle index 1ef2f87..191f9f9 100644 --- a/build.gradle +++ b/build.gradle @@ -4,15 +4,20 @@ plugins { id "com.hypherionmc.modutils.modpublisher" version "2.+" } -sourceCompatibility = targetCompatibility = JavaVersion.VERSION_17 - group = project.maven_group -archivesBaseName = project.archives_base_name +base.archivesName = project.archives_base_name version = "${project.mod_version}-mc${project.minecraft_version}" repositories { maven { url "https://www.jitpack.io" } maven { url "https://maven.neoforged.net/releases/" } + + maven { + url "https://maven2.bai.lol" + content { + includeGroup "lol.bai" + } + } } dependencies { @@ -20,7 +25,7 @@ dependencies { mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2" neoForge "net.neoforged:neoforge:${project.neoforge_version}" - modApi(include("com.github.ThinkingStudios:ForgedNetworkingAPI:0.1.0-mc1.20.4")) + modImplementation "lol.bai:badpackets:neo-${project.badpackets_version}" implementation "com.google.code.findbugs:jsr305:3.0.2" } @@ -45,20 +50,9 @@ tasks.withType(JavaCompile).configureEach { java { withSourcesJar() -} -jar { - manifest { - attributes([ - "Specification-Title" : project.mod_id, - "Specification-Vendor" : project.mod_author, - "Specification-Version" : "1", - "Implementation-Title" : project.name, - "Implementation-Version" : version, - "Implementation-Vendor" : project.mod_author, - "Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ") - ]) - } + sourceCompatibility = JavaVersion.VERSION_17 + targetCompatibility = JavaVersion.VERSION_17 } // configure the maven publication @@ -95,4 +89,6 @@ publisher { setCurseEnvironment("client") setArtifact(remapJar) addAdditionalFile(sourcesJar) + modrinthDepends.required("badpackets") + curseDepends.required("badpackets") } diff --git a/gradle.properties b/gradle.properties index f69c616..d4ac5f9 100644 --- a/gradle.properties +++ b/gradle.properties @@ -17,4 +17,6 @@ mod_id=servux mod_author=ThinkingStudio modrinth_id=H9LzoDgA -curseforge_id=1013154 \ No newline at end of file +curseforge_id=1013154 + +badpackets_version=0.6.1 \ No newline at end of file diff --git a/src/main/java/fi/dy/masa/servux/network/IPluginChannelHandler.java b/src/main/java/fi/dy/masa/servux/network/IPluginChannelHandler.java index 35f1a07..763b7ee 100644 --- a/src/main/java/fi/dy/masa/servux/network/IPluginChannelHandler.java +++ b/src/main/java/fi/dy/masa/servux/network/IPluginChannelHandler.java @@ -1,6 +1,7 @@ package fi.dy.masa.servux.network; -import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking.PlayChannelHandler; +import lol.bai.badpackets.api.play.ServerPlayPacketReceiver; +//import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking.PlayChannelHandler; import net.minecraft.network.PacketByteBuf; import net.minecraft.server.MinecraftServer; @@ -11,7 +12,7 @@ public interface IPluginChannelHandler { Identifier getChannel(); - default PlayChannelHandler getServerPacketHandler() + default ServerPlayPacketReceiver getServerPacketHandler() { if (this.usePacketSplitter()) { diff --git a/src/main/java/fi/dy/masa/servux/network/PacketSplitter.java b/src/main/java/fi/dy/masa/servux/network/PacketSplitter.java index f42846a..ed39159 100644 --- a/src/main/java/fi/dy/masa/servux/network/PacketSplitter.java +++ b/src/main/java/fi/dy/masa/servux/network/PacketSplitter.java @@ -4,7 +4,8 @@ import java.util.Map; import javax.annotation.Nullable; import io.netty.buffer.Unpooled; -import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; +//import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; +import lol.bai.badpackets.api.PacketSender; import org.apache.commons.lang3.tuple.Pair; import net.minecraft.nbt.NbtCompound; @@ -49,7 +50,8 @@ private static void send(Identifier channel, PacketByteBuf packet, int payloadLi buf.writeBytes(packet, thisLen); - ServerPlayNetworking.send(player, channel, buf); + PacketSender.s2c(player).send(channel, buf); + //ServerPlayNetworking.send(player, channel, buf); } packet.release(); diff --git a/src/main/java/fi/dy/masa/servux/network/ServerPacketChannelHandler.java b/src/main/java/fi/dy/masa/servux/network/ServerPacketChannelHandler.java index bc7ab66..04466b7 100644 --- a/src/main/java/fi/dy/masa/servux/network/ServerPacketChannelHandler.java +++ b/src/main/java/fi/dy/masa/servux/network/ServerPacketChannelHandler.java @@ -1,9 +1,12 @@ package fi.dy.masa.servux.network; import java.util.HashMap; -import net.fabricmc.fabric.api.networking.v1.S2CPlayChannelEvents; -import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; +import lol.bai.badpackets.api.play.PlayPackets; +//import net.fabricmc.fabric.api.networking.v1.S2CPlayChannelEvents; +//import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; + +import lol.bai.badpackets.impl.registry.ChannelRegistry; import net.minecraft.util.Identifier; public class ServerPacketChannelHandler @@ -28,19 +31,37 @@ public void registerServerChannelHandler(IPluginChannelHandler handler) if (handler.isSubscribable()) { - S2CPlayChannelEvents.REGISTER.register((net, server, sender, channels) -> { + PlayPackets.registerServerReadyCallback((net, sender, server) -> { + var channels = ChannelRegistry.PLAY_S2C.getChannels(); + if (channels.contains(channel)) { handler.subscribe(net); } }); - S2CPlayChannelEvents.UNREGISTER.register((net, server, sender, channels) -> { + + PlayPackets.registerServerReadyCallback((net, sender, server) -> { + var channels = ChannelRegistry.PLAY_S2C.getChannels(); + if (channels.contains(channel)) { handler.unsubscribe(net); } }); - ServerPlayNetworking.registerGlobalReceiver(channel, handler.getServerPacketHandler()); +// S2CPlayChannelEvents.REGISTER.register((net, server, sender, channels) -> { +// if (channels.contains(channel)) +// { +// handler.subscribe(net); +// } +// }); +// S2CPlayChannelEvents.UNREGISTER.register((net, server, sender, channels) -> { +// if (channels.contains(channel)) +// { +// handler.unsubscribe(net); +// } +// }); + PlayPackets.registerServerReceiver(channel, handler.getServerPacketHandler()); + //ServerPlayNetworking.registerGlobalReceiver(channel, handler.getServerPacketHandler()); } } } @@ -54,7 +75,8 @@ public void unregisterServerChannelHandler(IPluginChannelHandler handler) if (this.handlers.remove(channel, handler)) { - ServerPlayNetworking.unregisterGlobalReceiver(channel); + ChannelRegistry.PLAY_S2C.getChannels().remove(channel); + //ServerPlayNetworking.unregisterGlobalReceiver(channel); } } }