Files
netease-modsdk-wiki/docs/mcdocs/1-ModAPI/接口/通用/工具.md
2025-03-17 13:24:39 +08:00

731 lines
16 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 | 返回触发的定时器实例 |
- 示例
```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 | 返回触发的定时器实例 |
- 示例
```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 | 返回单次触发的定时器实例 |
- 示例
```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 | 返回单次触发的定时器实例 |
- 示例
```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 | AddOnceTimer和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本身 |
- 示例
```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本身 |
- 示例
```python
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateGame(levelId)
# 获取"entity.wolf.name"对应的中文("狼"
Chinese = comp.GetChinese("entity.wolf.name")
```
## 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")
```
## 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 | 函数生成器 |
- 示例
```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 | 函数生成器 |
- 示例
```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)
```