多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
>[danger] 接入道具需要让你的模块依赖`ItemModule`模块 物品组件顶层接口为 `com.pxpmc.pxrpg.api.item.piece.common.BuildPiece` pr同时也封装了一些基本类型的抽象类 `com.pxpmc.pxrpg.api.item.piece.common.BasicItemBuildPiece` ![](https://img.kancloud.cn/23/f2/23f23f9b53f16ea5bcb03255d8bc5003_1109x371.png) >[danger]写一个自定义`MyItemStrPrice`组件,继承`StringPiece<ItemInter>` 泛型填入 `ItemInter` -> 道具中间件 这个组件只支持道具(Item) ![](https://img.kancloud.cn/b3/5a/b35a1ddeb2a9c1c25defd6d3af047c38_873x736.png) >[danger]然后在自身的模块中维护并注册这个组件 ![](https://img.kancloud.cn/7d/2b/7d2b04e7279f5252a6f512855bbb386e_774x1054.png) >[danger] 在某些操作的时候获取到自定义组件值 ![](https://img.kancloud.cn/36/f2/36f24d168584aa7430def5b87d9253dd_1006x413.png) >[danger] 接入装备(Equip)或宝石(Gem)也是同理 ![](https://img.kancloud.cn/5f/5d/5f5dc5b886c4a539721e57c2c734bd03_564x485.png) >[danger]完整代码 `MyItemModule` ``` /** * 道具组建示例 */ public class MyItemModule extends AbstractModule { private MyItemStrPrice myItemStrPrice; public MyItemModule(Plugin plugin) { //这里需要传入pxrpg的代理插件类 super(MAPI.getBukkitPxRpgAPI().toPxRpgPlugin(plugin)); } @Override public String getModuleName() { return "MyItemPrice"; } @Override public String[] getDepends() { return new String[]{ //ItemModule强依赖道具模块 "ItemModule" }; } /** * 通过返回方法让其他模块可以调用这个组件 */ public MyItemStrPrice getMyItemStrPrice() { return myItemStrPrice; } @Override public void onInitialization() { //在初始化方法中先new一个组件对象 myItemStrPrice = new MyItemStrPrice(); } @Override public void onLoad() { //在初始化完成后注册道具组件 //获取到道具模块 ItemModule module = Module.getModule(ItemModule.class); //获取道具管理器注册道具组件 module.getItemManager().registerItemBuildPiece(myItemStrPrice, super.getPlugin()); } } ``` >[danger]完整代码 `MyItemPrice` ``` public class MyItemStrPrice extends StringPiece<ItemInter> { public MyItemStrPrice() { //传入道具配置的根节点路径 //如: name // type // equip-exp super("my-item-str"); //让组件能够读取到配置内容 autoLoad(false); //注册道具变量 //在道具模板内, 可以使用 {item.my-item-str} 来解析成道具配置的文本 autoVariable(); //注册元数据序列化 //会自动把值写入道具NBT,方便后面做操作,如: 强化后更改这个数值,绑定后修改为玩家名字做为绑定对象 //参数为NBT的key,不可为null,不可重复 //autoMetaSerialize("myitemstr"); //Tip: 一般这种是不需要从配置读取,根据玩家操作进行更改(如强化等级,宝石镶嵌等),或者从创建参数中传递的参数进行设置值 // 如果要使用这种, 就不需要 autoLoad(false); } @Override protected String getDefaultValue() { return "my-item-str-默认值"; } } ```