This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
component:database:zh [2023/10/26 02:51] hfsr created |
component:database:zh [2023/12/10 15:46] (current) hfsr [组件:数据库] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | Component: Database | + | 组件:数据库 |
================= | ================= | ||
- | This component is provided by the [[item:database_upgrade|database upgrade]]. | + | 此组件由[[item:database_upgrade:zh|数据库升级]]提供。 |
- | + | ||
- | Component name: `database`. | + | 数据库组件主要用于处理“完整的”物品堆,包括NBT标签,这些标签(默认情况下)对Lua脚本不可用(以避免利用漏洞/破坏其他mod的游戏玩法)。将物品堆放入数据库组件,然后指定数据库组件地址及其槽位号,就可以给一些组件提供物品堆数据。例如,AE2的输出总线驱动程序就利用了这个功能。 |
- | + | ||
- | The database component is primarily useful to work with "full" item stacks, including NBT tags, which are (by default) not available to Lua scripts (to avoid exploits / breaking of other mods' gameplay). Some components allow specifying item stacks by instead specifying the address of a database component, and slot the item stack is in that database - for example, the Export Bus driver for Applied Energistics 2 makes use of this functionality. | + | 组件名:`database`。 |
- | + | ||
- | Callbacks: | + | 回调函数: |
- `get(slot:number):table` | - `get(slot:number):table` | ||
- | Get the representation of the item stack stored in the specified slot. | + | 获取储存于指定槽位的物品堆的数据,以表的形式展示。 |
+ | \\ | ||
- `computeHash(slot:number):string` | - `computeHash(slot:number):string` | ||
- | Computes a hash value for the item stack in the specified slot. This value is guaranteed to be the same for identical item stacks, allowing comparison of item stacks across a network (by comparing the hash values). | + | 计算指定槽位中物品堆的哈希值。相同物品堆的结果保证相同,可用以在网络中比较物品堆(通过比较哈希值)。 |
+ | \\ | ||
- `indexOf(hash:string):number` | - `indexOf(hash:string):number` | ||
- | Get the index of an item stack with the specified hash. Returns a negative value if no such stack was found. | + | 获取具有指定哈希值的物品堆的槽位号。若未找到则返回一个负数。 |
+ | \\ | ||
- `clear(slot:number):boolean` | - `clear(slot:number):boolean` | ||
- | Clears the specified slot. Returns true if there was something in the slot before. | + | 清空指定槽位。若槽位中有内容以供清除则返回`true`。 |
+ | \\ | ||
- `copy(fromSlot:number, toSlot:number[, address:string]):boolean` | - `copy(fromSlot:number, toSlot:number[, address:string]):boolean` | ||
- | Copies an entry to another slot, optionally to another database. Returns true if something was overwritten. | + | 将一个条目复制到另一个槽位,可选择是否为复制到另一个数据库。若有内容成功覆写则返回`true`。 |
+ | \\ | ||
- `clone(address:string):number` | - `clone(address:string):number` | ||
- | Copies the data stored in this database to another database with the specified address. | + | 将此数据库中存储的数据复制到指定地址对应的数据库。 |
+ | \\ | ||
- | Example Use: | + | 使用例: |
```lua | ```lua | ||
Line 29: | Line 34: | ||
local sides = require("sides") | local sides = require("sides") | ||
- | local db = component.database -- primary database component | + | local db = component.database --首选数据库组件 |
- | local invcontrol = component.inventory_controller -- primary inventory controller | + | local invcontrol = component.inventory_controller --首选物品栏控制器 |
- | -- define slot numbers | + | --定义槽位序号 |
dbSlot = 1 | dbSlot = 1 | ||
invSlot = 1 | invSlot = 1 | ||
- | -- compare item inside remote inventory to item in first slot of database | + | --将外部物品容器中的物品与数据库第一个槽位中的物品进行对比 |
if db.get(dbSlot).label == invcontrol.getStackInSlot(sides.north, invSlot).label then | if db.get(dbSlot).label == invcontrol.getStackInSlot(sides.north, invSlot).label then | ||
- | -- items match, do stuff with it. | + | --物品匹配,进行对应操作。 |
else | else | ||
- | -- items don't match, do nothing, or do something else. | + | --物品不匹配,什么都不做,或者进行其他操作 |
end | end | ||
``` | ``` |