From a585c62d7b9f751792a3eb0cd7e78cfb3bb6f6c8 Mon Sep 17 00:00:00 2001 From: Bunny_i <1160706050@qq.com> Date: Tue, 9 Apr 2024 19:41:30 +0800 Subject: [PATCH 1/4] Fix mixin file suffix --- src/main/resources/fabric.mod.json | 2 +- .../{syncmatica.litematica_mixin.json => litematica.mixin.json} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename src/main/resources/{syncmatica.litematica_mixin.json => litematica.mixin.json} (100%) diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 4dd498b11..d96da7829 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -16,7 +16,7 @@ "entrypoints": {}, "mixins": [ "syncmatica.mixin.json", - "syncmatica.litematica_mixin.json" + "litematica.mixin.json" ], "depends": { }, diff --git a/src/main/resources/syncmatica.litematica_mixin.json b/src/main/resources/litematica.mixin.json similarity index 100% rename from src/main/resources/syncmatica.litematica_mixin.json rename to src/main/resources/litematica.mixin.json From 583dd054b6fa2789ace684171e140d755054c089 Mon Sep 17 00:00:00 2001 From: Bunny_i <1160706050@qq.com> Date: Tue, 9 Apr 2024 19:42:05 +0800 Subject: [PATCH 2/4] Remove "fabric-resource-loader-v0" dependencie --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 87a9194de..31a165894 100644 --- a/build.gradle +++ b/build.gradle @@ -25,7 +25,7 @@ dependencies { modImplementation "curse.maven:litematica-${project.litematica_minecraft_version}-308892:${project.litematica_fileid}" modImplementation "curse.maven:malilib-${project.litematica_minecraft_version}-303119:${project.malilib_fileid}" - modImplementation "net.fabricmc.fabric-api:fabric-resource-loader-v0:${project.fabric_resource_loader_version}" + // modImplementation "net.fabricmc.fabric-api:fabric-resource-loader-v0:${project.fabric_resource_loader_version}" } processResources { From 524e916a6fff4e0f9642c6cd8cdd8b23132b692c Mon Sep 17 00:00:00 2001 From: Bunny_i <1160706050@qq.com> Date: Tue, 9 Apr 2024 19:42:31 +0800 Subject: [PATCH 3/4] Text formatting alignment --- gradle.properties | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/gradle.properties b/gradle.properties index ae6c04f70..a8ad9505c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -2,19 +2,19 @@ org.gradle.jvmargs=-Xmx1G # Fabric Properties - # check these on https://fabricmc.net/versions.html - minecraft_version=1.20.2 - yarn_mappings=1.20.2+build.4 - loader_version=0.14.23 - fabric_version=0.90.4+1.20.2 +# check these on https://fabricmc.net/versions.html +minecraft_version=1.20.2 +yarn_mappings=1.20.2+build.4 +loader_version=0.14.23 +fabric_version=0.90.4+1.20.2 # Mod Properties - mod_version = 0.3.11 - maven_group = ch.endte - archives_base_name = syncmatica +mod_version=0.3.11 +maven_group=ch.endte +archives_base_name=syncmatica # Dependencies - litematica_fileid=4789765 - malilib_fileid=4788432 - litematica_minecraft_version=1.20.2 - fabric_resource_loader_version = 0.11.11+86b12645a0 +litematica_fileid=4789765 +malilib_fileid=4788432 +litematica_minecraft_version=1.20.2 +# fabric_resource_loader_version=0.11.11+86b12645a0 From 08eadedd1dc599411c29e755152d1b7ffd89954b Mon Sep 17 00:00:00 2001 From: Bunny_i <1160706050@qq.com> Date: Tue, 9 Apr 2024 19:43:19 +0800 Subject: [PATCH 4/4] Fix Channel --- .../mixin/MixinClientPlayNetworkHandler.java | 9 +- .../mixin/MixinCustomPayloadC2SPacket.java | 3 +- .../mixin/MixinCustomPayloadS2CPacket.java | 3 +- .../mixin/MixinMinecraftClient.java | 2 +- .../MixinServerCommonNetworkHandler.java | 29 ------ .../mixin/MixinServerPlayNetworkHandler.java | 46 +++++++-- .../syncmatica/network/ChannelManager.java | 93 ++++++++++++------- src/main/resources/syncmatica.mixin.json | 1 - 8 files changed, 107 insertions(+), 79 deletions(-) delete mode 100644 src/main/java/ch/endte/syncmatica/mixin/MixinServerCommonNetworkHandler.java diff --git a/src/main/java/ch/endte/syncmatica/mixin/MixinClientPlayNetworkHandler.java b/src/main/java/ch/endte/syncmatica/mixin/MixinClientPlayNetworkHandler.java index fe5354198..edfecf6f2 100644 --- a/src/main/java/ch/endte/syncmatica/mixin/MixinClientPlayNetworkHandler.java +++ b/src/main/java/ch/endte/syncmatica/mixin/MixinClientPlayNetworkHandler.java @@ -2,6 +2,7 @@ import ch.endte.syncmatica.communication.ExchangeTarget; import ch.endte.syncmatica.mixin_actor.ActorClientPlayNetworkHandler; +import ch.endte.syncmatica.network.ChannelManager; import ch.endte.syncmatica.network.SyncmaticaPayload; import net.minecraft.client.MinecraftClient; import net.minecraft.client.network.ClientPlayNetworkHandler; @@ -21,11 +22,11 @@ public abstract class MixinClientPlayNetworkHandler { @Inject(method = "method_52801", at = @At("HEAD"), cancellable = true) private void handlePacket(CustomPayload customPayload, CallbackInfo ci) { - // ChannelManager.onChannelRegisterHandle(getExchangeTarget(), packet.getChannel(), packet.getData()); - if (!MinecraftClient.getInstance().isOnThread()) { - return; //only execute packet on main thread - } if (customPayload instanceof SyncmaticaPayload payload) { + ChannelManager.onChannelRegisterHandle(getExchangeTarget(), payload.id(), payload.byteBuf()); + if (!MinecraftClient.getInstance().isOnThread()) { + return; //only execute packet on main thread + } ActorClientPlayNetworkHandler.getInstance().packetEvent((ClientPlayNetworkHandler) (Object) this, payload, ci); } } diff --git a/src/main/java/ch/endte/syncmatica/mixin/MixinCustomPayloadC2SPacket.java b/src/main/java/ch/endte/syncmatica/mixin/MixinCustomPayloadC2SPacket.java index af2232a88..10a638c71 100644 --- a/src/main/java/ch/endte/syncmatica/mixin/MixinCustomPayloadC2SPacket.java +++ b/src/main/java/ch/endte/syncmatica/mixin/MixinCustomPayloadC2SPacket.java @@ -1,6 +1,7 @@ package ch.endte.syncmatica.mixin; import ch.endte.syncmatica.Syncmatica; +import ch.endte.syncmatica.network.ChannelManager; import ch.endte.syncmatica.network.SyncmaticaPayload; import net.minecraft.network.PacketByteBuf; import net.minecraft.network.packet.CustomPayload; @@ -15,7 +16,7 @@ public class MixinCustomPayloadC2SPacket { @Inject(method = "readPayload", at = @At(value = "HEAD"), cancellable = true) private static void readPayload(Identifier id, PacketByteBuf buf, CallbackInfoReturnable cir) { - if (id.getNamespace().equals(Syncmatica.MOD_ID)) { + if (id.getNamespace().equals(Syncmatica.MOD_ID) || id.equals(ChannelManager.MINECRAFT_REGISTER)) { cir.setReturnValue(new SyncmaticaPayload(id, buf)); } } diff --git a/src/main/java/ch/endte/syncmatica/mixin/MixinCustomPayloadS2CPacket.java b/src/main/java/ch/endte/syncmatica/mixin/MixinCustomPayloadS2CPacket.java index e3093541b..7edbd1338 100644 --- a/src/main/java/ch/endte/syncmatica/mixin/MixinCustomPayloadS2CPacket.java +++ b/src/main/java/ch/endte/syncmatica/mixin/MixinCustomPayloadS2CPacket.java @@ -1,6 +1,7 @@ package ch.endte.syncmatica.mixin; import ch.endte.syncmatica.Syncmatica; +import ch.endte.syncmatica.network.ChannelManager; import ch.endte.syncmatica.network.SyncmaticaPayload; import net.minecraft.network.PacketByteBuf; import net.minecraft.network.packet.CustomPayload; @@ -15,7 +16,7 @@ public class MixinCustomPayloadS2CPacket { @Inject(method = "readPayload", at = @At(value = "HEAD"), cancellable = true) private static void readPayload(Identifier id, PacketByteBuf buf, CallbackInfoReturnable cir) { - if (id.getNamespace().equals(Syncmatica.MOD_ID)) { + if (id.getNamespace().equals(Syncmatica.MOD_ID) || id.equals(ChannelManager.MINECRAFT_REGISTER)) { cir.setReturnValue(new SyncmaticaPayload(id, buf)); } } diff --git a/src/main/java/ch/endte/syncmatica/mixin/MixinMinecraftClient.java b/src/main/java/ch/endte/syncmatica/mixin/MixinMinecraftClient.java index 7d263303e..84239b50d 100644 --- a/src/main/java/ch/endte/syncmatica/mixin/MixinMinecraftClient.java +++ b/src/main/java/ch/endte/syncmatica/mixin/MixinMinecraftClient.java @@ -16,7 +16,7 @@ public class MixinMinecraftClient { @Inject(method = "disconnect()V", at = @At("HEAD")) private void shutdownSyncmatica(final CallbackInfo ci) { - ChannelManager.onDisconnected(); + ChannelManager.onClientDisconnected(); ScreenHelper.close(); Syncmatica.shutdown(); LitematicManager.clear(); diff --git a/src/main/java/ch/endte/syncmatica/mixin/MixinServerCommonNetworkHandler.java b/src/main/java/ch/endte/syncmatica/mixin/MixinServerCommonNetworkHandler.java deleted file mode 100644 index a31b59aff..000000000 --- a/src/main/java/ch/endte/syncmatica/mixin/MixinServerCommonNetworkHandler.java +++ /dev/null @@ -1,29 +0,0 @@ -package ch.endte.syncmatica.mixin; - -import ch.endte.syncmatica.communication.PacketType; -import ch.endte.syncmatica.network.IServerPlayerNetworkHandler; -import ch.endte.syncmatica.network.SyncmaticaPayload; -import net.minecraft.network.NetworkThreadUtils; -import net.minecraft.network.packet.c2s.common.CustomPayloadC2SPacket; -import net.minecraft.server.network.ServerCommonNetworkHandler; -import net.minecraft.server.network.ServerPlayNetworkHandler; -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(ServerCommonNetworkHandler.class) -public class MixinServerCommonNetworkHandler { - @Inject(method = "onCustomPayload", at = @At("HEAD")) - private void onCustomCarpetPayload(CustomPayloadC2SPacket packet, CallbackInfo ci) { - Object thiss = this; - if (thiss instanceof ServerPlayNetworkHandler impl && packet.payload() instanceof SyncmaticaPayload payload) { - // ChannelManager.onChannelRegisterHandle(playerNetworkHandler.ggetExchangeTarget(), packet.getChannel(), packet.getData()); - if (PacketType.containsIdentifier(payload.id())) { - NetworkThreadUtils.forceMainThread(packet, impl, impl.player.getServerWorld()); - IServerPlayerNetworkHandler impll = ((IServerPlayerNetworkHandler) impl); - impll.syncmatica$operateComms(sm -> sm.onPacket(impll.syncmatica$getExchangeTarget(), payload.id(), payload.byteBuf())); - } - } - } -} diff --git a/src/main/java/ch/endte/syncmatica/mixin/MixinServerPlayNetworkHandler.java b/src/main/java/ch/endte/syncmatica/mixin/MixinServerPlayNetworkHandler.java index e1df3e3c4..2510b7cb5 100644 --- a/src/main/java/ch/endte/syncmatica/mixin/MixinServerPlayNetworkHandler.java +++ b/src/main/java/ch/endte/syncmatica/mixin/MixinServerPlayNetworkHandler.java @@ -1,37 +1,63 @@ package ch.endte.syncmatica.mixin; -import java.util.function.Consumer; - +import ch.endte.syncmatica.Context; +import ch.endte.syncmatica.Syncmatica; +import ch.endte.syncmatica.communication.ExchangeTarget; +import ch.endte.syncmatica.communication.PacketType; +import ch.endte.syncmatica.communication.ServerCommunicationManager; import ch.endte.syncmatica.network.ChannelManager; import ch.endte.syncmatica.network.IServerPlayerNetworkHandler; +import ch.endte.syncmatica.network.SyncmaticaPayload; +import net.minecraft.network.ClientConnection; +import net.minecraft.network.NetworkThreadUtils; +import net.minecraft.network.listener.ServerPlayPacketListener; +import net.minecraft.network.listener.TickablePacketListener; +import net.minecraft.network.packet.c2s.common.CustomPayloadC2SPacket; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.network.*; +import net.minecraft.text.Text; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import ch.endte.syncmatica.Context; -import ch.endte.syncmatica.Syncmatica; -import ch.endte.syncmatica.communication.ExchangeTarget; -import ch.endte.syncmatica.communication.ServerCommunicationManager; -import net.minecraft.server.network.ServerPlayNetworkHandler; -import net.minecraft.text.Text; +import java.util.function.Consumer; @Mixin(value = ServerPlayNetworkHandler.class, priority = 998) -public abstract class MixinServerPlayNetworkHandler implements IServerPlayerNetworkHandler { +public abstract class MixinServerPlayNetworkHandler extends ServerCommonNetworkHandler implements IServerPlayerNetworkHandler, ServerPlayPacketListener, PlayerAssociatedNetworkHandler, TickablePacketListener { + @Shadow + public ServerPlayerEntity player; + @Unique private ExchangeTarget exTarget = null; @Unique private ServerCommunicationManager comManager = null; + public MixinServerPlayNetworkHandler(MinecraftServer server, ClientConnection connection, ConnectedClientData clientData) { + super(server, connection, clientData); + } + @Inject(method = "onDisconnected", at = @At("HEAD")) public void onDisconnected(final Text reason, final CallbackInfo ci) { - ChannelManager.onDisconnected(); + ChannelManager.onServerPlayerDisconnected(syncmatica$getExchangeTarget()); syncmatica$operateComms(sm -> sm.onPlayerLeave(syncmatica$getExchangeTarget())); } + @Override + public void onCustomPayload(CustomPayloadC2SPacket packet) { + if (packet.payload() instanceof SyncmaticaPayload payload) { + ChannelManager.onChannelRegisterHandle(syncmatica$getExchangeTarget(), payload.id(), payload.byteBuf()); + if (PacketType.containsIdentifier(payload.id())) { + NetworkThreadUtils.forceMainThread(packet, this, player.getServerWorld()); + syncmatica$operateComms(sm -> sm.onPacket(syncmatica$getExchangeTarget(), payload.id(), payload.byteBuf())); + } + } + } + public void syncmatica$operateComms(final Consumer operation) { if (comManager == null) { final Context con = Syncmatica.getContext(Syncmatica.SERVER_CONTEXT); diff --git a/src/main/java/ch/endte/syncmatica/network/ChannelManager.java b/src/main/java/ch/endte/syncmatica/network/ChannelManager.java index db32a765a..354bfd9ce 100644 --- a/src/main/java/ch/endte/syncmatica/network/ChannelManager.java +++ b/src/main/java/ch/endte/syncmatica/network/ChannelManager.java @@ -1,23 +1,34 @@ package ch.endte.syncmatica.network; -import ch.endte.syncmatica.Syncmatica; import ch.endte.syncmatica.communication.ExchangeTarget; import ch.endte.syncmatica.communication.PacketType; -import ch.endte.syncmatica.util.StringTools; import io.netty.buffer.Unpooled; import net.minecraft.network.PacketByteBuf; import net.minecraft.util.Identifier; -import org.slf4j.LoggerFactory; import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.List; +import java.util.*; public class ChannelManager { - private static final Identifier MINECRAFT_REGISTER = new Identifier("minecraft:register"); - private static final Identifier MINECRAFT_UNREGISTER = new Identifier("minecraft:unregister"); - private static final List serverRegisterChannels = new ArrayList<>(); - private static final List clientRegisterChannels = new ArrayList<>(); + public static final Identifier MINECRAFT_REGISTER = new Identifier("minecraft:register"); + public static final Identifier MINECRAFT_UNREGISTER = new Identifier("minecraft:unregister"); + private static final Map> serverRegisteredChannels = new HashMap<>(); + private static final List clientRegisteredChannels = new ArrayList<>(); + + public static void initSendRegister(ExchangeTarget target) { + // 构建数据 + PacketByteBuf byteBuf = new PacketByteBuf(Unpooled.buffer()); + for (PacketType identifierType : PacketType.values()) { + Identifier identifier = identifierType.identifier; + byte[] bytes = identifier.toString().getBytes(StandardCharsets.UTF_8); + byteBuf.writeBytes(bytes); + byteBuf.writeByte(0x00); + } + // 有数据时才有意义发送 + if (byteBuf.writerIndex() > 0) { + target.sendPacket(MINECRAFT_REGISTER, byteBuf, null); + } + } private static List onReadRegisterIdentifier(PacketByteBuf data) { List identifiers = new ArrayList<>(); @@ -35,33 +46,51 @@ private static List onReadRegisterIdentifier(PacketByteBuf data) { } public static void onChannelRegisterHandle(ExchangeTarget target, Identifier channel, PacketByteBuf data) { - if (channel.equals(MINECRAFT_REGISTER)) { - // 拷贝一份数据, 因为可能其他插件也存在通道 - List identifiers = onReadRegisterIdentifier(new PacketByteBuf(data.copy())); - // 检查客户端是否已经注册该标识符, 如果已经注册那么向服务端发送注册请求 - PacketByteBuf byteBuf2 = new PacketByteBuf(Unpooled.buffer()); - List registerChannels = target.isClient() ? clientRegisterChannels : serverRegisterChannels; - for (Identifier identifier : identifiers) { - if (!registerChannels.contains(identifier) && PacketType.containsIdentifier(identifier)) { - LoggerFactory.getLogger("").info(identifier.toString()); - byte[] bytes = identifier.toString().getBytes(StandardCharsets.UTF_8); - byteBuf2.writeBytes(bytes); - byteBuf2.writeByte(0x00); - } - } - // 有数据时才有意义发送 - if (byteBuf2.writerIndex() > 0) { - target.sendPacket(MINECRAFT_REGISTER, byteBuf2, null); + if (!channel.equals(MINECRAFT_REGISTER)) { + return; + } + // 拷贝一份数据进行处理, 因为可能其他插件(fabric-api)也存在通道 + List identifiers = onReadRegisterIdentifier(new PacketByteBuf(data.copy())); + // 获取已当前目标已注册的标识符 + List registeredChannels = target.isClient() ? clientRegisteredChannels : serverRegisteredChannels.computeIfAbsent(target, value -> new ArrayList<>()); + // 构建数据 + PacketByteBuf byteBuf2 = new PacketByteBuf(Unpooled.buffer()); + for (Identifier identifier : identifiers) { + // 当前模组支持该通道且未被注册 + if (PacketType.containsIdentifier(identifier) && !registeredChannels.contains(identifier)) { + // LoggerFactory.getLogger("").info(identifier.toString()); + byte[] bytes = identifier.toString().getBytes(StandardCharsets.UTF_8); + byteBuf2.writeBytes(bytes); + byteBuf2.writeByte(0x00); } } -// else if (channel.equals(MINECRAFT_UNREGISTER)) { -// //TODO: 待实现,主要不知道数据格式,不过好像不影响使用 -// } + // 有数据时才有意义发送 + if (byteBuf2.writerIndex() > 0) { + target.sendPacket(MINECRAFT_REGISTER, byteBuf2, null); + } } + public static void onChannelUnRegisterHandle(ExchangeTarget target, Identifier channel, PacketByteBuf data) { + if (!channel.equals(MINECRAFT_UNREGISTER)) { + return; + } + // 拷贝一份数据进行处理, 因为可能其他插件(fabric-api)也存在通道 + List identifiers = onReadRegisterIdentifier(new PacketByteBuf(data.copy())); + // 获取已当前目标已注册的标识符 + List registeredChannels = target.isClient() ? clientRegisteredChannels : serverRegisteredChannels.computeIfAbsent(target, value -> new ArrayList<>()); + // 删除当前模组已注册的通道 + identifiers.removeIf(registeredChannels::contains); + } + + public static void onClientDisconnected() { + clientRegisteredChannels.clear(); + } + + public static void onServerDisconnected() { + serverRegisteredChannels.clear(); + } - public static void onDisconnected() { - clientRegisterChannels.clear(); - serverRegisterChannels.clear(); + public static void onServerPlayerDisconnected(ExchangeTarget target) { + serverRegisteredChannels.remove(target); } } diff --git a/src/main/resources/syncmatica.mixin.json b/src/main/resources/syncmatica.mixin.json index 606ddc280..78de38dfc 100644 --- a/src/main/resources/syncmatica.mixin.json +++ b/src/main/resources/syncmatica.mixin.json @@ -6,7 +6,6 @@ "mixins": [ "MixinCustomPayloadC2SPacket", "MixinCustomPayloadS2CPacket", - "MixinServerCommonNetworkHandler", "MixinMinecraftServer", "MixinServerPlayNetworkHandler", "MixinPlayerManager"