From 4c560755af3f79dc053e0fb93c112e75fed2b810 Mon Sep 17 00:00:00 2001 From: Frost-ZX <30585462+Frost-ZX@users.noreply.github.com> Date: Sat, 12 Nov 2022 14:41:25 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=95=B4=E7=90=86=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kubejs/client_scripts/_main_.js | 8 +++ kubejs/probe/user/constants.d.ts | 55 ++++++++++++++ kubejs/startup_scripts/_main_.js | 59 ++++++++++++++- kubejs/startup_scripts/wheat_plus.js | 104 +-------------------------- 4 files changed, 122 insertions(+), 104 deletions(-) diff --git a/kubejs/client_scripts/_main_.js b/kubejs/client_scripts/_main_.js index 81549ce..686476e 100644 --- a/kubejs/client_scripts/_main_.js +++ b/kubejs/client_scripts/_main_.js @@ -10,12 +10,20 @@ console.info(`${LOG_PREFIX} 加载客户端内容`); // event.add(new ResourceLocation(`minecraft:models/block/stone`), {}); onEvent('client.generate_assets', (event) => { + if (!Array.isArray(JSON_ASSETS)) { + return; + } + console.info(`${LOG_PREFIX} 生成客户端资源 - 开始`); + // 生成 JSON_ASSETS.forEach((item) => { event.add(new ResourceLocation(item.PATH), item.DATA); }); + // 重置 + global.JSON_ASSETS = null; + console.info(`${LOG_PREFIX} 生成客户端资源 - 完成`); }); diff --git a/kubejs/probe/user/constants.d.ts b/kubejs/probe/user/constants.d.ts index 626a392..225a849 100644 --- a/kubejs/probe/user/constants.d.ts +++ b/kubejs/probe/user/constants.d.ts @@ -1,3 +1,51 @@ +/** 方块构造器 */ +type B_Builder = Internal.BlockBuilder; + +/** 方块碰撞箱类型 */ +type B_BoxType = 'custom' | 'full' | 'half'; + +/** 方块材质类型 */ +type B_Material = Internal.MaterialJS_; + +/** + * 方块渲染类型 + * - `cutout`: required for blocks with texture like glass + * - `translucent`: required for blocks like stained glass + */ +type B_RenderType = 'solid' | 'cutout' | 'cutout_mipped' | 'translucent'; + +/** 配置选项 */ +type SetBlockPropsOpts = { + /** 碰撞箱配置(自定义) */ + boxConfig: number[]; + /** 碰撞箱类型(内置) */ + boxType: B_BoxType; + /** 开启碰撞箱,默认:true */ + collision: boolean; + /** 方块显示名称 */ + displayName: string; + /** 默认:8 */ + hardness: number; + /** 是否为完整方块,默认:true */ + isSolid: boolean; + /** 范围:0 ~ 1 */ + lightLevel: number; + /** 默认:stone */ + material: B_Material; + /** 模型文件路径 */ + modelPath: string; + /** 默认:solid */ + renderType: B_RenderType; + /** 默认:16 */ + resistance: number; + /** fullBlock(未使用)*/ + isFull: boolean; + /** opaque(未使用)*/ + isOpaque: boolean; + /** transparent(未使用)*/ + transparent: boolean; +}; + declare const global: { /** @@ -7,6 +55,13 @@ declare const global: { */ defaults(v: TValue, d: TValue): TValue; + /** + * @desc 设置方块基础属性 + * @param block 方块 + * @param opts 配置选项 + */ + setBlockProps(block: B_Builder, opts: SetBlockPropsOpts): boolean; + /** * @desc 写入 JSON 文件 * @param path 文件路径,相对于 `.minecraft` diff --git a/kubejs/startup_scripts/_main_.js b/kubejs/startup_scripts/_main_.js index 4f974f8..672d20d 100644 --- a/kubejs/startup_scripts/_main_.js +++ b/kubejs/startup_scripts/_main_.js @@ -5,12 +5,67 @@ global.defaults = function (v, d) { return ((typeof v === 'undefined' || v === null) ? d : v); }; +global.setBlockProps = function (block, opts) { + + if (!block) { + console.error(`${LOG_PREFIX} 设置方块属性失败:缺少“block”参数`); + return false; + } + + if (!opts) { + opts = {}; + } + + const boxConfig = defaults(opts.boxConfig, null); + const boxType = defaults(opts.boxType, ''); + const collision = defaults(opts.collision, true); + const displayName = defaults(opts.displayName, ''); + const hardness = defaults(opts.hardness, 8); + const isSolid = defaults(opts.isSolid, true); + const lightLevel = defaults(opts.lightLevel, 0); + const material = defaults(opts.material, 'stone'); + const modelPath = defaults(opts.modelPath, ''); + const renderType = defaults(opts.renderType, 'solid'); + const resistance = defaults(opts.resistance, 16); + + if (boxConfig) { + block.box.apply(block, boxConfig); + } else if (boxType === 'full') { + block.box(0, 0, 0, 16, 16, 16, true); + } else if (boxType === 'half') { + block.box(0, 0, 0, 16, 8, 16, true); + } + + if (!collision) { + block.noCollision(); + } + + if (displayName) { + block.displayName(displayName); + } + + if (!isSolid) { + block.notSolid(); + } + + if (modelPath) { + block.model(modelPath); + } + + block.hardness(hardness); + block.lightLevel(lightLevel); + block.material(material); + block.renderType(renderType); + block.resistance(resistance); + + return true; + +}; + global.writeJSON = function (path, data) { JsonIO.write(path, data); }; -// 参考: -// https://github.com/Railcraft/Railcraft/blob/mc-1.12.2/src/main/java/mods/railcraft/common/plugins/color/EnumColor.java global.COLORS = { WHITE: { CODE: 'white', diff --git a/kubejs/startup_scripts/wheat_plus.js b/kubejs/startup_scripts/wheat_plus.js index f71f858..03d4e01 100644 --- a/kubejs/startup_scripts/wheat_plus.js +++ b/kubejs/startup_scripts/wheat_plus.js @@ -1,36 +1,12 @@ // priority: 0 const defaults = global.defaults; +const setBlockProps = global.setBlockProps; + const COLORS = global.COLORS; const JSON_ASSETS = global.JSON_ASSETS; const LOG_PREFIX = global.LOG_PREFIX; -/** - * @desc 方块构造器 - * @typedef B.Builder - * @type {Internal.BlockBuilder} - */ - -/** - * @desc 方块碰撞箱类型 - * @typedef B.BoxType - * @type {'custom'|'full'|'half'} - */ - -/** - * @desc 方块材质类型 - * @typedef B.Material - * @type {Internal.MaterialJS_} - */ - -/** - * @desc 方块渲染类型 - * - `cutout`: required for blocks with texture like glass - * - `translucent`: required for blocks like stained glass - * @typedef B.RenderType - * @type {'solid'|'cutout'|'cutout_mipped'|'translucent'} - */ - /** 模组 ID */ const MOD_ID = 'wheat_plus'; @@ -46,82 +22,6 @@ const P_ITEM = `${MOD_ID}:item`; /** 物品模型文件基础长路径 */ const P_ITEM_MODEL = `${MOD_ID}:models/item`; -/** - * @description 设置方块基础属性 - * @param {B.Builder} block - * @param {object} options - * @param {number[]} options.boxConfig 碰撞箱配置(自定义) - * @param {B.BoxType} options.boxType 碰撞箱类型(内置) - * @param {boolean} options.collision 开启碰撞箱,默认:true - * @param {string} options.displayName 方块显示名称 - * @param {number} options.hardness 默认:8 - * @param {boolean} options.isSolid 是否为完整方块,默认:true - * @param {number} options.lightLevel 范围:0 ~ 1 - * @param {B.Material} options.material 默认:stone - * @param {string} options.modelPath 模型文件路径 - * @param {B.RenderType} options.renderType 默认:solid - * @param {number} options.resistance 默认:16 - * @param {boolean} options.isFull fullBlock(未使用) - * @param {boolean} options.isOpaque opaque(未使用) - * @param {boolean} options.transparent transparent(未使用) - */ -const setBlockProps = function (block, options) { - - if (!block) { - console.error(`${LOG_PREFIX} 设置方块属性失败:缺少“block”参数`); - return false; - } - - if (!options) { - options = {}; - } - - const boxConfig = defaults(options.boxConfig, null); - const boxType = defaults(options.boxType, ''); - const collision = defaults(options.collision, true); - const displayName = defaults(options.displayName, ''); - const hardness = defaults(options.hardness, 8); - const isSolid = defaults(options.isSolid, true); - const lightLevel = defaults(options.lightLevel, 0); - const material = defaults(options.material, 'stone'); - const modelPath = defaults(options.modelPath, ''); - const renderType = defaults(options.renderType, 'solid'); - const resistance = defaults(options.resistance, 16); - - if (boxConfig) { - block.box.apply(block, boxConfig); - } else if (boxType === 'full') { - block.box(0, 0, 0, 16, 16, 16, true); - } else if (boxType === 'half') { - block.box(0, 0, 0, 16, 8, 16, true); - } - - if (!collision) { - block.noCollision(); - } - - if (displayName) { - block.displayName(displayName); - } - - if (!isSolid) { - block.notSolid(); - } - - if (modelPath) { - block.model(modelPath); - } - - block.hardness(hardness); - block.lightLevel(lightLevel); - block.material(material); - block.renderType(renderType); - block.resistance(resistance); - - return true; - -}; - console.info(`${LOG_PREFIX} 处理 ${MOD_ID} 相关内容`); // 注册方块 - 路