Files
netease-modsdk-wiki/docs/mcdocs/1-ModAPI/接口/通用/工具.md
2025-03-18 14:46:12 +08:00

896 lines
20 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
sidebarDepth: 1
---
# 工具
## AddRepeatedTimer
<span style="display:inline;color:#ff5555">服务端</span><span style="display:inline;color:#7575f9">客户端</span>
### 服务端接口
<span id="s0"></span>
method in mod.server.component.gameCompServer.GameComponentServer
- 描述
添加服务端触发的定时器,重复执行
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| delay | float | 延迟时间,单位秒 |
| func | function | 定时器触发函数 |
| *args | any | 变长参数,可以不设置 |
| **kwargs | any | 字典变长参数,可以不设置 |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| CallLater | 返回触发的定时器实例传入func不是函数类型时返回None |
- 示例
```python
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateGame(levelId)
comp.AddRepeatedTimer(3.0,func,args,kwargs)
```
### 客户端接口
<span id="c0"></span>
method in mod.client.component.gameCompClient.GameComponentClient
- 描述
添加客户端触发的定时器,重复执行
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| delay | float | 延迟时间,单位秒 |
| func | function | 定时器触发函数 |
| *args | any | 变长参数,可以不设置 |
| **kwargs | any | 字典变长参数,可以不设置 |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| CallLater | 返回触发的定时器实例传入func不是函数类型时返回None |
- 示例
```python
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateGame(levelId)
comp.AddRepeatedTimer(3.0,func,args,kwargs)
```
## AddTimer
<span style="display:inline;color:#ff5555">服务端</span><span style="display:inline;color:#7575f9">客户端</span>
### 服务端接口
<span id="s0"></span>
method in mod.server.component.gameCompServer.GameComponentServer
- 描述
添加服务端触发的定时器,非重复
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| delay | float | 延迟时间,单位秒 |
| func | function | 定时器触发函数 |
| *args | any | 变长参数,可以不设置 |
| **kwargs | any | 字典变长参数,可以不设置 |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| CallLater | 返回单次触发的定时器实例传入func不是函数类型时返回None |
- 示例
```python
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateGame(levelId)
comp.AddTimer(3.0,func,args,kwargs)
```
### 客户端接口
<span id="c0"></span>
method in mod.client.component.gameCompClient.GameComponentClient
- 描述
添加客户端触发的定时器,非重复
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| delay | float | 延迟时间,单位秒 |
| func | function | 定时器触发函数 |
| *args | any | 变长参数,可以不设置 |
| **kwargs | any | 字典变长参数,可以不设置 |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| CallLater | 返回单次触发的定时器实例传入func不是函数类型时返回None |
- 示例
```python
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateGame(levelId)
comp.AddTimer(3.0,func,args,kwargs)
```
## CancelTimer
<span style="display:inline;color:#ff5555">服务端</span><span style="display:inline;color:#7575f9">客户端</span>
### 服务端接口
<span id="s0"></span>
method in mod.server.component.gameCompServer.GameComponentServer
- 描述
取消定时器
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| timer | CallLater | AddTimer和AddRepeatedTimer时返回的定时器实例 |
- 返回值
- 示例
```python
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateGame(levelId)
comp.CancelTimer(timer)
```
### 客户端接口
<span id="c0"></span>
method in mod.client.component.gameCompClient.GameComponentClient
- 描述
取消定时器
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| timer | CallLater | AddTimer和AddRepeatedTimer时返回的定时器实例 |
- 返回值
- 示例
```python
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateGame(levelId)
comp.CancelTimer(timer)
```
## CheckNameValid
<span style="display:inline;color:#ff5555">服务端</span><span style="display:inline;color:#7575f9">客户端</span>
### 服务端接口
<span id="s0"></span>
method in mod.server.component.gameCompServer.GameComponentServer
- 描述
检查昵称是否合法,即不包含敏感词
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| name | str | 昵称 |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| bool | True:昵称合法 False:昵称非法 |
- 示例
```python
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateGame(levelId)
isValid = comp.CheckNameValid("史蒂夫")
```
### 客户端接口
<span id="c0"></span>
method in mod.client.component.gameCompClient.GameComponentClient
- 描述
检查昵称是否合法,即不包含敏感词
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| name | str | 昵称 |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| bool | True:昵称合法 False:昵称非法 |
- 示例
```python
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateGame(levelId)
isValid = comp.CheckNameValid("史蒂夫")
```
## CheckWordsValid
<span style="display:inline;color:#ff5555">服务端</span><span style="display:inline;color:#7575f9">客户端</span>
### 服务端接口
<span id="s0"></span>
method in mod.server.component.gameCompServer.GameComponentServer
- 描述
检查语句是否合法,即不包含敏感词
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| words | str | 语句 |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| bool | True:语句合法<br>False:语句非法 |
- 示例
```python
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateGame(levelId)
isValid = comp.CheckWordsValid("creeper? Aww man")
```
### 客户端接口
<span id="c0"></span>
method in mod.client.component.gameCompClient.GameComponentClient
- 描述
检查语句是否合法,即不包含敏感词
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| words | str | 语句 |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| bool | True:语句合法<br>False:语句非法 |
- 示例
```python
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateGame(levelId)
isValid = comp.CheckWordsValid("creeper? Aww man")
```
## GetChinese
<span style="display:inline;color:#ff5555">服务端</span><span style="display:inline;color:#7575f9">客户端</span>
### 服务端接口
<span id="s0"></span>
method in mod.server.component.gameCompServer.GameComponentServer
- 描述
获取langStr对应的中文可参考PC开发包中\handheld\localization\handheld\data\resource_packs\vanilla\texts\zh_CN.lang
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| langStr | str | 传入的langStr |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| str | langStr对应的中文若找不到对应的中文则会返回langStr本身,字符编码为Unicode |
- 示例
```python
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateGame(levelId)
# 获取"entity.wolf.name"对应的中文("狼"
Chinese = comp.GetChinese("entity.wolf.name")
```
### 客户端接口
<span id="c0"></span>
method in mod.client.component.gameCompClient.GameComponentClient
- 描述
获取langStr对应的中文可参考PC开发包中\handheld\localization\handheld\data\resource_packs\vanilla\texts\zh_CN.lang
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| langStr | str | 传入的langStr |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| str | langStr对应的中文若找不到对应的中文则会返回langStr本身,字符编码为Unicode |
- 示例
```python
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateGame(levelId)
# 获取"entity.wolf.name"对应的中文("狼"
Chinese = comp.GetChinese("entity.wolf.name")
```
## GetClipboardContent
<span style="display:inline;color:#7575f9">客户端</span>
method in mod.client.component.gameCompClient.GameComponentClient
- 描述
获取系统剪贴板内容
- 参数
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| str | 剪贴板内容获取失败时返回None |
- 备注
- 如果剪贴板里的内容并非文本内容,或者剪贴板里没有内容,则返回空字符串。
- 示例
```python
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateGame(levelId)
# 从其他地方复制一段文本后
# 获取剪贴板内容
content = comp.GetClipboardContent()
```
## GetFps
<span style="display:inline;color:#7575f9">客户端</span>
method in mod.client.component.gameCompClient.GameComponentClient
- 描述
获取fps
- 参数
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| float | 当前fps值 |
- 示例
```python
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateGame(levelId)
fps = comp.GetFps()
```
## GetMinecraftEnum
<span style="display:inline;color:#ff5555">服务端</span><span style="display:inline;color:#7575f9">客户端</span>
### 服务端接口
<span id="s0"></span>
method in mod.server.extraServerApi
- 描述
用于获取[枚举值文档](../../枚举值/索引.md)中的枚举值
- 参数
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| minecraftEnum | 枚举集合类 |
- 示例
```python
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreatePlayer(playerId)
# 使用枚举值作为其他接口的参数
comp.SetPlayerGameType(serverApi.GetMinecraftEnum().GameType.Survival)
```
### 客户端接口
<span id="c0"></span>
method in mod.client.extraClientApi
- 描述
用于获取[枚举值文档](../../枚举值/索引.md)中的枚举值
- 参数
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| minecraftEnum | 枚举集合类 |
- 示例
```python
import mod.client.extraClientApi as clientApi
# 使用枚举值与事件参数比较
class BattleUI(ScreenNode):
def OnButtonTouch(self, args):
touchEventEnum = clientApi.GetMinecraftEnum().TouchEvent
if touchEvent == touchEventEnum.TouchUp:
pass
def Create(self):
self.AddTouchEventHandler("/panel/test_btn", self.OnButtonTouch, {"isSwallow":True})
```
## GetModConfigJson
<span style="display:inline;color:#7575f9">客户端</span>
method in mod.client.extraClientApi
- 描述
以字典形式返回指定路径的json格式配置文件的内容文件必须放置在资源包的/modconfigs目录下
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| path | str | 指定路径的json文件 |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| dict | 配置内容的字典,当读取文件失败时返回空字典 |
- 示例
```python
import mod.client.extraClientApi as clientApi
data = clientApi.GetModConfigJson("modconfigs/monster.json")
```
## GetServerTickTime
<span style="display:inline;color:#ff5555">服务端</span>
method in mod.server.extraServerApi
- 描述
获取服务端引擎上一帧的帧消耗时间
- 参数
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| float | 时间,单位为毫秒 |
- 备注
- 服务端引擎的帧率为20帧而服务端脚本的帧率为30帧因此当脚本每帧获取引擎帧消耗时间时有可能连续获取到的是同一个引擎帧的时间
- 示例
```python
import mod.server.extraServerApi as serverApi
print serverApi.GetServerTickTime()
```
## ImportModule
<span style="display:inline;color:#ff5555">服务端</span><span style="display:inline;color:#7575f9">客户端</span>
### 服务端接口
<span id="s0"></span>
method in mod.server.extraServerApi
- 描述
使用字符串路径导入模块作用与importlib.import_module类似但只能导入当前加载的mod中的模块
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| path | str | 模块的绝对路径 |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| any | 模块对象当前加载的所有mod中不存在该模块则返回None |
- 示例
```python
import mod.server.extraServerApi as serverApi
serverApi.ImportModule("tutorialScripts.tutorialServerSystem")
```
### 客户端接口
<span id="c0"></span>
method in mod.client.extraClientApi
- 描述
使用字符串路径导入模块作用与importlib.import_module类似但只能导入当前加载的mod中的模块
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| path | str | 模块的绝对路径 |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| any | 模块对象当前加载的所有mod中不存在该模块则返回None |
- 示例
```python
import mod.client.extraClientApi as clientApi
clientApi.ImportModule("tutorialScripts.tutorialClientSystem")
```
## SetClipboardContent
<span style="display:inline;color:#7575f9">客户端</span>
method in mod.client.component.gameCompClient.GameComponentClient
- 描述
设置系统剪贴板内容
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| content | str | 需要设置的剪贴板内容 |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| bool | 设置是否成功 |
- 示例
```python
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateGame(levelId)
# 设置剪贴板内容为"Hello, minecraft"
comp.SetClipboardContent("Hello, minecraft")
# 可以粘贴到其他地方
```
## StartCoroutine
<span style="display:inline;color:#ff5555">服务端</span><span style="display:inline;color:#7575f9">客户端</span>
### 服务端接口
<span id="s0"></span>
method in mod.server.extraServerApi
- 描述
开启服务端协程,实现函数分段式执行,可用于缓解复杂逻辑计算导致游戏卡顿问题
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| iterOrFunc | generator或callable([],generator) | 传入带有yield的函数或传入生成器。如传入生成器则将从生成器中断位置开始执行,如传入函数将从头开始执行 |
| callback | function | 协程执行完后的回调函数默认为None |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| generator | 函数生成器。如果传入的iterOrFunc不是带有yield的函数或者生成器时返回None |
- 示例
```python
#通过生成器执行协程
import server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateGame(serverApi.GetLevelId())
def callback():
print "callback"
def coroutineTest():
for i in xrange(1000):
print i
yield
generator = serverApi.StartCoroutine(coroutineTest, callback)
#执行1秒后停止协程
comp.AddTimer(1.0, serverApi.StopCoroutine, generator)
#5秒后传入StartCoroutine返回的生成器则函数将从停止位置继续执行
comp.AddTimer(5.0, serverApi.StartCoroutine, generator, callback)
#----------------------------------------------------------------
#通过函数执行协程
import server.extraServerApi as serverApi
def callback():
print "callback"
def coroutineTest():
for i in xrange(1000):
print i
yield
#传入函数,函数将从头开始执行
serverApi.StartCoroutine(coroutineTest, callback)
```
### 客户端接口
<span id="c0"></span>
method in mod.client.extraClientApi
- 描述
开启客户端协程,实现函数分段式执行,可用于缓解复杂逻辑计算导致游戏卡顿问题
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| iterOrFunc | generator或callable([],generator) | 传入带有yield的函数或传入生成器。如传入生成器则将从生成器中断位置开始执行,如传入函数将从头开始执行 |
| callback | function | 协程执行完后的回调函数默认为None |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| generator | 函数生成器。如果传入的iterOrFunc不是带有yield的函数或者生成器时返回None |
- 示例
```python
#通过生成器执行协程
import client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateGame(clientApi.GetLevelId())
def callback():
print "callback"
def coroutineTest():
for i in xrange(1000):
print i
yield
generator = clientApi.StartCoroutine(coroutineTest, callback)
#执行1秒后停止协程
comp.AddTimer(1.0, clientApi.StopCoroutine, generator)
#执行5秒后传入StartCoroutine返回的生成器则函数将从停止位置继续执行
comp.AddTimer(5.0, clientApi.StartCoroutine, generator, callback)
#----------------------------------------------------------------
#通过函数执行协程
import client.extraClientApi as clientApi
def callback():
print "callback"
def coroutineTest():
for i in xrange(1000):
print i
yield
#传入函数,函数将从头开始执行
clientApi.StartCoroutine(coroutineTest, callback)
```
## StopCoroutine
<span style="display:inline;color:#ff5555">服务端</span><span style="display:inline;color:#7575f9">客户端</span>
### 服务端接口
<span id="s0"></span>
method in mod.server.extraServerApi
- 描述
停止协程
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| iter | generator | 需要停止的生成器对象,StartCoroutine的返回值 |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| bool | 是否成功 |
- 示例
```python
import mod.server.extraServerApi as serverApi
serverApi.StopCoroutine(generator)
```
### 客户端接口
<span id="c0"></span>
method in mod.client.extraClientApi
- 描述
停止客户端协程
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| iter | generator | 需要停止的生成器对象,StartCoroutine的返回值 |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| bool | 是否成功 |
- 示例
```python
import mod.client.extraClientApi as clientApi
clientApi.StopCoroutine(generator)
```