ProxLib
ProxLib 是一个库,允许 Minecraft 中的玩家进行基于距离的通信。它允许附近的玩家通过服务器直接互相交换数据,而无需服务器安装任何模组或插件来中继这些数据。
概述
ProxLib 为 ProxChat 提供动力,并可被其他模组使用以实现基于距离的通信功能。它提供了一个简单的 API,用于在彼此相距约 26 格以内的玩家之间发送和接收数据包。
该库通过将数据巧妙地编码到 Minecraft 现有的数据包系统中来实现工作,具体是通过方块破坏动作数据包(仅限中止操作),这些数据包会被中继给所有附近的玩家,且服务器不进行验证(是的,即使在空气中也能正常工作!)。
工作原理
- 通信方式:使用带有
ABORT_DESTROY_BLOCK 动作的 PlayerActionC2SPacket
- 数据编码:数据被编码到玩家周围的相对方块位置中
- 范围:约 26 格(技术上受限于 Minecraft 的 32 格动作数据包中继范围)
- 数据容量:每个 Minecraft 数据包 9 位
- 发送的数据前会附加 2 个 Minecraft 数据包作为魔法值(用于识别其为 Prox 数据包),其中 2 字节用于供应商和包 ID,另外 3 字节用于包长度。
- 因此,如果你发送一个包含 4 字节数据的数据包,这将变为
2 + (2 + 2 + 3 + 4) * (8 / 9) » 11.77 » 12 个 Minecraft 数据包
- 可靠性:几乎适用于所有服务器,包括那些拥有反作弊系统的服务器
- 请注意,反作弊系统可能会将其视为数据包垃圾邮件或某种奇怪的炸服尝试。这可能导致玩家因作弊而被标记。
- 我的测试主要在 2b2t 上进行。反作弊系统没有阻止任何数据包,也没有踢出我,但最近的更新导致在发送数据和移动时出现延迟回退(数据仍然正常发送)。
- 当你距离上下建筑限制不到约 6 格时停止工作(可能在世界边界附近也会失效)
- 即使发送方处于创造模式也能正常工作!
免责声明: 由于此模组会发出大量上述 ABORT_DESTROY_BLOCK 数据包,反作弊系统可能会将玩家标记为数据包垃圾邮件发送者甚至炸服者(尽管真正的炸服者绝不会发送中止操作)。
因此,请确保玩家在启用相应的发送功能之前知晓这种情况可能发生。
安装
对于模组用户
ProxLib 是一个库模组,应与依赖它的模组一起安装。
模组可能会捆绑此库,除非你在游戏内模组菜单中查看库部分,否则你可能永远不会意识到它已被包含在内。
对于模组开发者
在你的 build.gradle 中将 ProxLib 添加为依赖项:
exclusiveContent {
forRepository {
maven {
name = "Modrinth"
url = "https://api.modrinth.com/maven"
}
}
filter {
includeGroup "maven.modrinth"
}
}
dependencies {
// 例如 MC 版本 1.21.4(请务必检查哪个是最新版本)。
// 如果你不想将此模组直接捆绑到你的模组中,请移除 "include"(目前 <30 KiB)。
include modApi("maven.modrinth:proxlib:0.2.4+1.21")
}
在你的 fabric.mod.json 中添加:
"depends": {
"proxlib": ">=0.2.0"
}
构建
如果你希望构建此模组,你应该注意:
- 该项目使用 Stonecutter 一次性生成多个版本
- 你不应运行
build 任务,而是运行 chiseledBuild(在 project 类别中)
- 有关更多信息,请参阅其文档
版本控制
一般来说,我尝试按照 SemVer(主版本.次版本.修订号)来处理版本号。
该库仍处于 1.0.0 之前的阶段。因此,稳定性可能并不完美。然而,该库可能永远无法达到 1.0.0,但当前版本仍然相当稳定。
特别是对于 ProxLib 类,我会尽力保持现有公共方法的兼容性。如果我破坏了兼容性,我会提升次版本号。但是,如果添加了大量新功能,我也可能会提升次版本号。所以这不一定意味着破坏性更改。
有关更改日志,请参阅 GitHub 上的发行版或 Modrinth 上的 changelog。
协议兼容性
自从 ProxChat 的第一个版本 于 2024 年 1 月发布以来,我就确定了协议,并且此后未对其做任何更改。
我的目标是永远不需要破坏兼容性。我也尽量对我的数据包做同样的事情。
迄今为止的唯一更改是:
- ID (u16) 已拆分为 10 位供应商 ID 和 6 位数据包 ID。这不是破坏性更改,但改变了如何解释 ID。
开发者用法
数据包 ID
数据包由一个 2 字节的 ID 标识,该 ID 分为:
- 10 位:供应商 ID(0 到 1023)
- 6 位:数据包 ID(0 到 63)
因此,如果你想制作自己的数据包,你应该:
- 选择你自己的供应商 ID。最好使用随机数生成器生成一个 ID 供你的模组使用。
- 然后开始对任何数据包进行编号
示例:
final int VENDOR_ID = 932; // 使用随机数生成器生成
final ProxPacketIdentifier AWESOME_PACKET_ID = ProxPacketIdentifier.of(VENDOR_ID, 0);
final ProxPacketIdentifier ANOTHER_PACKET_ID = ProxPacketIdentifier.of(VENDOR_ID, 1);
发送与接收数据包
您可能需要的所有方法都可以轻松从 ProxLib 类中调用。
发送示例
public void sendAwesomePacket(String message) throws IOException {
// 创建数据包的数据(byte[])
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
DataOutputStream dataOut = new DataOutputStream(bytesOut);
dataOut.writeByte(1); // 类型
dataOut.writeUTF(message);
// 发送数据包
int packets = ProxLib.sendPacket(MinecraftClient.getInstance(), AWESOME_PACKET_ID, bytesOut.toByteArray());
LOGGER.info("Sent my awesome packet using {} packets!", packets);
}
接收示例
public class MyAwesomeMod implements ClientModInitializer {
@Override
public void onInitializeClient() {
ProxLib.addHandlerFor(AWESOME_PACKET_ID, (sender, identifier, data) -> {
try {
DataInputStream dataIn = new DataInputStream(new ByteArrayInputStream(data));
int type = dataIn.readUnsignedByte(1);
if(type == 1) {
String message = dataIn.readUTF();
// 对 message 进行处理...
}else {
LOGGER.warn("Received unsupported type in my awesome packet: {}", type);
}
} catch (IOException ex) {
LOGGER.error("Failed to proccess my awesome packet!", ex);
}
});
ProxLib.addHandlerFor(ANOTHER_PACKET_ID, (sender, identifier, data) -> {
// ...
});
}
}
已知的数据包 ID:
| Mod |
Vendor ID |
Packet ID |
Name |
Description |
Link |
| ProxChat |
0 |
1 |
Chat |
Simple chat message in chat (type% <msg> with mod). |
PC-Chat |
| ProxChat |
0 |
2 |
PatPat-PatEntity |
Petting someone (legcay) |
PC-Pat |
| ProxChat |
0 |
3 |
EmoteCraft |
Play / Repeat / Stop own Emotes |
PC-Emote |
| ProxChat |
0 |
4 |
TextDisplay |
Show one or more customizable text displays to others (type %% <msg> with mod). |
PC-TextDisplay |
| PatPat |
2 |
0 |
Pat |
Patting someone |
PP-Pat |
注意:ProxChat 的 Vendor ID 为 0,因为在当时 ID 尚未拆分为 Vendor ID 和 Packet ID(且该库是 ProxChat 的一部分)。因此使用了从 1 开始的数字,这实际上对应 Vendor ID 0,并且为了与旧版本的 ProxChat 保持向后兼容而保留下来。
与其他语言的集成
ItzN00bPvP 开发了一个与本项目兼容的 Rust 基础库(crates.io: proxchat,仓库: proxchat-rs)。如果您希望例如 azalea-bot 发送 ProxChat 消息,这将非常有用。
致谢
由 EnderKill98 开发,其基础是在检查他自己的 N00bBots 日志时偶然与 ItzN00bPvP 共同发现的。没有他,这个项目就不会存在!
ProxLib
ProxLib is a library that enables proximity-based communication between players in Minecraft. It allows nearby players to directly exchange data with each other through the server without the server needing to have andy mods or plugins installed to relay this data.
Overview
ProxLib powers ProxChat and can be used by other mods to implement proximity-based communication features. It provides a simple API for sending and receiving data packets between players who are within approximately 26 blocks of each other.
The library works by cleverly encoding data into Minecraft's existing packet system through block breaking action packets (only aborts), which are relayed to all nearby players without server validation (yes, also works on air!).
How It Works
- Communication Method: Uses
PlayerActionC2SPacket with the ABORT_DESTROY_BLOCK action
- Data Encoding: Data is encoded into the relative block positions around the player
- Range: Approximately 26 blocks (technically limited by Minecraft's 32-block action packet relay)
- Data Capacity: 9 bits per Minecraft packet
- Data sent is prepended by 2 minecraft packets as a magic (to recognize it as a Prox Packet), 2 bytes for Vendor and Packet Id as well a 3 bytes for the packet length.
- So if you send a packet that has 4 bytes of data, this would become
2 + (2 + 2 + 3 + 4) * (8 / 9) » 11.77 » 12 minecraft packets
- Reliability: Works on virtually all servers, including those with anti-cheat systems
- Be aware that Anti Cheats might treat this as packet spam or some weird attempt at nuker. This could cause the player to get flagged for cheating.
- My testing here was mainly on 2b2t. The Anti Cheat did not block any of the packets, did not kick me, but a recent update causes a lagback when sending data and moving (data is still sent fine).
- Stops working when you're less than ~6 blocks away from the lower and upper vertical build limits (and possibly when next to world border)
- Works even when the sending player is in creative mode!
Disclaimer: Because this mod sends out a lot of the aforementioned ABORT_DESTROY_BLOCK packets, an anticheat might flag the player for packet spamming or even nuking (even though no nuker would ever send aborts).
So make sure that the player is aware that this might happen before they enable the corresponding sending functionality.
Installation
For Mod Users
ProxLib is a library mod and should be installed alongside mods that depend on it.
Mods may bundle this library, so you might never be aware of this being included unless you scout the Libraries in your ingame mod menu.
For Mod Developers
Add ProxLib as a dependency in your build.gradle:
exclusiveContent {
forRepository {
maven {
name = "Modrinth"
url = "https://api.modrinth.com/maven"
}
}
filter {
includeGroup "maven.modrinth"
}
}
dependencies {
// E.g. for MC Version 1.21.4 (make sure to check which is the most recent version).
// Remove the "include" if you don't wish bundling this mod directly into yours (currently <30 KiB).
include modApi("maven.modrinth:proxlib:0.2.4+1.21")
}
In your fabric.mod.json, add:
"depends": {
"proxlib": ">=0.2.0"
}
Building
If you wish to build this mod, you should be aware:
- This project uses Stonecutter to produce multiple versions at once
- You should not run the
build task, but rather chiseledBuild (inside category project)
- See their documentation for more information
Versioning
In general, I try to treat the version as SemVer (MAJOR.MINOR.PATCH).
This library is still pre 1.0.0. So expect stability to not be perfect. However, this library might end up never reaching 1.0.0 and the current versions to be pretty stable nonetheless.
Especially for the ProxLib class, I'll try my best to never break the compatibility of existing, public methods. If I do, I'll raise the MINOR version. However, I might also raise the MINOR version if a significant amount of new features was added. So it doesn't have to mean breaking changes.
For change logs, see the releases on GitHub or changelog on Modrinth.
Protocol Compatibility
I decided on the Protocol ever since the first release of ProxChat in January 2024 and have not altered it since.
My goal is to never need to break compatibility. I also try to do the same with my packets.
The only change so far was:
- The ID (u16) has been split into 10-bit Vendor ID and 6-bit Packet ID. This is not breaking, but changes how the ID should be interpreted.
Developer usage
Packet IDs
Packets are identified by a 2 byte id, which is split into:
- 10 bits: Vendor ID (0 to 1023)
- 6 bits: Packet ID (0 to 63)
So if you want to make your own packets, you should:
- Choose your own Vendor ID. Best use a random number generator to generate an ID your mod will use.
- Then just start numbering any packets
Example:
final int VENDOR_ID = 932; // Generated using a random number generator
final ProxPacketIdentifier AWESOME_PACKET_ID = ProxPacketIdentifier.of(VENDOR_ID, 0);
final ProxPacketIdentifier ANOTHER_PACKET_ID = ProxPacketIdentifier.of(VENDOR_ID, 1);
Sending & Receiving packets
All the methods you may need are easily callable from the class ProxLib.
Sending Example
public void sendAwesomePacket(String message) throws IOException {
// Create the data for your packet (byte[])
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
DataOutputStream dataOut = new DataOutputStream(bytesOut);
dataOut.writeByte(1); // Type
dataOut.writeUTF(message);
// Send the packet
int packets = ProxLib.sendPacket(MinecraftClient.getInstance(), AWESOME_PACKET_ID, bytesOut.toByteArray());
LOGGER.info("Sent my awesome packet using {} packets!", packets);
}
Receiving Example
public class MyAwesomeMod implements ClientModInitializer {
@Override
public void onInitializeClient() {
ProxLib.addHandlerFor(AWESOME_PACKET_ID, (sender, identifier, data) -> {
try {
DataInputStream dataIn = new DataInputStream(new ByteArrayInputStream(data));
int type = dataIn.readUnsignedByte(1);
if(type == 1) {
String message = dataIn.readUTF();
// Do something with message...
}else {
LOGGER.warn("Received unsupported type in my awesome packet: {}", type);
}
} catch (IOException ex) {
LOGGER.error("Failed to proccess my awesome packet!", ex);
}
});
ProxLib.addHandlerFor(ANOTHER_PACKET_ID, (sender, identifier, data) -> {
// ...
});
}
}
Known packet ids:
| Mod |
Vendor ID |
Packet ID |
Name |
Description |
Link |
| ProxChat |
0 |
1 |
Chat |
Simple chat message in chat (type% <msg> with mod). |
PC-Chat |
| ProxChat |
0 |
2 |
PatPat-PatEntity |
Petting someone (legcay) |
PC-Pat |
| ProxChat |
0 |
3 |
EmoteCraft |
Play / Repeat / Stop own Emotes |
PC-Emote |
| ProxChat |
0 |
4 |
TextDisplay |
Show one or more customizable text displays to others (type %% <msg> with mod). |
PC-TextDisplay |
| PatPat |
2 |
0 |
Pat |
Patting someone |
PP-Pat |
Note: ProxChat has Vendor ID 0 because at the time the ID was not yet split into Vendor/Packet ID (and this library was part of ProxChat). So the numbers starting from 1 where used which is effectively Vendor ID 0 and is kept for backwards compatibility to old versions of ProxChat.
Integration with other Languages
ItzN00bPvP developed a basic library for Rust that is compatible with this (crates.io: proxchat, repo: proxchat-rs). So this is handy if you want your e.g. azalea-bot to send ProxChat messages.
Credits
Developed by EnderKill98, the foundation was found randomly together with ItzN00bPvP when examining logs of his own N00bBots. Without him, this would not exist!
评论 0
登录后参与评分
还没有评论,来抢沙发吧