1
0

refactor: 整理代码

This commit is contained in:
2022-11-12 14:41:25 +08:00
parent 7b94a9ac46
commit 4c560755af
4 changed files with 122 additions and 104 deletions

View File

@@ -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} 生成客户端资源 - 完成`);
});

View File

@@ -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<TValue>(v: TValue, d: TValue): TValue;
/**
* @desc 设置方块基础属性
* @param block 方块
* @param opts 配置选项
*/
setBlockProps(block: B_Builder, opts: SetBlockPropsOpts): boolean;
/**
* @desc 写入 JSON 文件
* @param path 文件路径,相对于 `.minecraft`

View File

@@ -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',

View File

@@ -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} 相关内容`);
// 注册方块 - 路