# Spigot服与客户端python通信
## 使用方法
1. spigot服需要安装SpigotMaster插件,插件api文档见[SpigotMasterAPI文档](./81-SpigotMasterAPI文档.html)
2. 客户端到spigot
- 在spigot使用spigotMaster.listenForEvent监听事件。
- 在客户端使用NotifyToServer发送事件
3. spigot到客户端
- 在客户端使用ListenForEvent监听事件
- 在spigot使用spigotMaster.notifyToClient或其他多播接口发送事件
示例:
- spigot侧
```java
public void onEnable() {
SpigotMaster spigotMaster = (SpigotMaster) Bukkit.getPluginManager().getPlugin("SpigotMaster");
if (spigotMaster != null){
// 监听事件,然后原封不动发回去
spigotMaster.listenForEvent("MyMod", "MySystemClient", "clientEvent", new PyRpcHandler() {
@Override
public void onEvent(Player player, Map map) {
spigotMaster.notifyToClient(player, "MyMod", "MySystemServer", "serverEvent", map);
}
});
}
}
```
- python侧
```python
# modMain.py
@Mod.InitClient()
def InitClient(self):
clientApi.RegisterSystem("MyMod", "MySystemClient", client_system_class_path)
# clientSystem
class MySystemClient(ClientSystem):
def __init__(self, namespace, systemName):
ClientSystem.__init__(self, namespace, systemName)
# 注册事件,在回调函数中打印参数
self.ListenForEvent("MyMod", "MySystemServer", "serverEvent", self, self.onEvent)
# 给spigot发一个事件
self.NotifyToServer("clientEvent", {'a': 1})
def onEvent(self, data):
# 可以在客户端日志中看到onEvent {"a": 1}
print 'onEvent', data
```
## 事件支持的参数类型及映射关系
### Java发送给Python
| Java类型 | Python类型 |
| ------------------------ | ---------- |
| null | None |
| boolean | bool |
| int | int |
| long | long |
| BigInteger(2^63到2^64-1) | long |
| float | float |
| double | float |
| String | str |
| List\