9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-31 04:46:37 +00:00

feat(client-mod): 更新客户端模组

- 添加取消方块更新功能
- 修复保存配置文件问题
- 修复网络通讯打包解包
This commit is contained in:
jhqwqmc
2025-05-12 09:55:17 +08:00
parent 988d498121
commit e9015345e0
14 changed files with 305 additions and 41 deletions

View File

@@ -1962,28 +1962,48 @@ public class PacketConsumers {
data = (byte[]) Reflections.method$DiscardedPayload$dataByteArray.invoke(payload);
}
String decodeData = new String(data, StandardCharsets.UTF_8);
if (!decodeData.endsWith("init")) return;
int firstColon = decodeData.indexOf(':');
if (firstColon == -1) return;
int secondColon = decodeData.indexOf(':', firstColon + 1);
if (secondColon == -1) return;
int clientBlockRegistrySize = Integer.parseInt(decodeData.substring(firstColon + 1, secondColon));
int serverBlockRegistrySize = RegistryUtils.currentBlockRegistrySize();
if (clientBlockRegistrySize != serverBlockRegistrySize) {
Object kickPacket = Reflections.constructor$ClientboundDisconnectPacket.newInstance(
ComponentUtils.adventureToMinecraft(
Component.translatable(
"disconnect.craftengine.block_registry_mismatch",
TranslationArgument.numeric(clientBlockRegistrySize),
TranslationArgument.numeric(serverBlockRegistrySize)
)
)
);
user.nettyChannel().writeAndFlush(kickPacket);
user.nettyChannel().disconnect();
return;
if (decodeData.endsWith("init")) {
int firstColon = decodeData.indexOf(':');
if (firstColon == -1) return;
int secondColon = decodeData.indexOf(':', firstColon + 1);
if (secondColon == -1) return;
String payloadData = decodeData.substring(firstColon + 1, secondColon);
int clientBlockRegistrySize = Integer.parseInt(payloadData);
int serverBlockRegistrySize = RegistryUtils.currentBlockRegistrySize();
if (clientBlockRegistrySize != serverBlockRegistrySize) {
Object kickPacket = Reflections.constructor$ClientboundDisconnectPacket.newInstance(
ComponentUtils.adventureToMinecraft(
Component.translatable(
"disconnect.craftengine.block_registry_mismatch",
TranslationArgument.numeric(clientBlockRegistrySize),
TranslationArgument.numeric(serverBlockRegistrySize)
)
)
);
user.nettyChannel().writeAndFlush(kickPacket);
user.nettyChannel().disconnect();
return;
}
user.setClientModState(true);
} else if (decodeData.endsWith("cancel")) {
if (!VersionHelper.isOrAbove1_20_2()) return;
int firstColon = decodeData.indexOf(':');
if (firstColon == -1) return;
int secondColon = decodeData.indexOf(':', firstColon + 1);
if (secondColon == -1) return;
String payloadData = decodeData.substring(firstColon + 1, secondColon);
boolean cancel = Boolean.parseBoolean(payloadData);
if (cancel) {
user.nettyChannel().writeAndFlush(
Reflections.constructor$ClientboundCustomPayloadPacket.newInstance(
Reflections.constructor$DiscardedPayload.newInstance(
KeyUtils.toResourceLocation(Key.of(NetworkManager.MOD_CHANNEL)),
(":true:cancel").getBytes(StandardCharsets.UTF_8)
)
)
);
}
}
user.setClientModState(true);
}
} catch (Exception e) {
CraftEngine.instance().logger().warn("Failed to handle ServerboundCustomPayloadPacket", e);

View File

@@ -6659,4 +6659,20 @@ public class Reflections {
"world.entity.projectile.AbstractArrow"
)
);
public static final Class<?> clazz$ClientboundCustomPayloadPacket = requireNonNull(
BukkitReflectionUtils.findReobfOrMojmapClass(
List.of("network.protocol.game.PacketPlayOutCustomPayload", "network.protocol.common.ClientboundCustomPayloadPacket"),
List.of("network.protocol.game.ClientboundCustomPayloadPacket", "network.protocol.common.ClientboundCustomPayloadPacket")
)
);
public static final Constructor<?> constructor$ClientboundCustomPayloadPacket = requireNonNull(
ReflectionUtils.getTheOnlyConstructor(clazz$ClientboundCustomPayloadPacket)
);
// 1.20.2+
public static final Constructor<?> constructor$DiscardedPayload = Optional.ofNullable(clazz$DiscardedPayload)
.map(clazz -> ReflectionUtils.getTheOnlyConstructor(clazz))
.orElse(null);
}