Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 1.20.2 Channel #70

Open
wants to merge 4 commits into
base: migrate_to_1.20.2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
24 changes: 12 additions & 12 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<CustomPayload> 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));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<CustomPayload> 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));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<ServerCommunicationManager> operation) {
if (comManager == null) {
final Context con = Syncmatica.getContext(Syncmatica.SERVER_CONTEXT);
Expand Down
93 changes: 61 additions & 32 deletions src/main/java/ch/endte/syncmatica/network/ChannelManager.java
Original file line number Diff line number Diff line change
@@ -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<Identifier> serverRegisterChannels = new ArrayList<>();
private static final List<Identifier> 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<ExchangeTarget, List<Identifier>> serverRegisteredChannels = new HashMap<>();
private static final List<Identifier> 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<Identifier> onReadRegisterIdentifier(PacketByteBuf data) {
List<Identifier> identifiers = new ArrayList<>();
Expand All @@ -35,33 +46,51 @@ private static List<Identifier> onReadRegisterIdentifier(PacketByteBuf data) {
}

public static void onChannelRegisterHandle(ExchangeTarget target, Identifier channel, PacketByteBuf data) {
if (channel.equals(MINECRAFT_REGISTER)) {
// 拷贝一份数据, 因为可能其他插件也存在通道
List<Identifier> identifiers = onReadRegisterIdentifier(new PacketByteBuf(data.copy()));
// 检查客户端是否已经注册该标识符, 如果已经注册那么向服务端发送注册请求
PacketByteBuf byteBuf2 = new PacketByteBuf(Unpooled.buffer());
List<Identifier> 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<Identifier> identifiers = onReadRegisterIdentifier(new PacketByteBuf(data.copy()));
// 获取已当前目标已注册的标识符
List<Identifier> 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<Identifier> identifiers = onReadRegisterIdentifier(new PacketByteBuf(data.copy()));
// 获取已当前目标已注册的标识符
List<Identifier> 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);
}
}
2 changes: 1 addition & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"entrypoints": {},
"mixins": [
"syncmatica.mixin.json",
"syncmatica.litematica_mixin.json"
"litematica.mixin.json"
],
"depends": {
},
Expand Down
1 change: 0 additions & 1 deletion src/main/resources/syncmatica.mixin.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"mixins": [
"MixinCustomPayloadC2SPacket",
"MixinCustomPayloadS2CPacket",
"MixinServerCommonNetworkHandler",
"MixinMinecraftServer",
"MixinServerPlayNetworkHandler",
"MixinPlayerManager"
Expand Down