refactor: 优化方块注册代码,将设置方块属性操作提取为函数
This commit is contained in:
Vendored
+14
@@ -1,5 +1,19 @@
|
|||||||
declare const global: {
|
declare const global: {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @desc 传入值为空时返回指定的默认值
|
||||||
|
* @param v 传入值
|
||||||
|
* @param d 默认值
|
||||||
|
*/
|
||||||
|
defaults<TValue>(v: TValue, d: TValue): TValue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @desc 写入 JSON 文件
|
||||||
|
* @param path 文件路径,相对于 `.minecraft`
|
||||||
|
* @param data JSON 内容对象
|
||||||
|
*/
|
||||||
|
writeJSON(path: string, data: object): void;
|
||||||
|
|
||||||
/** 颜色列表 */
|
/** 颜色列表 */
|
||||||
COLORS: {
|
COLORS: {
|
||||||
[key: string]: {
|
[key: string]: {
|
||||||
|
|||||||
@@ -1,5 +1,22 @@
|
|||||||
// priority: 100
|
// priority: 100
|
||||||
|
|
||||||
|
// 注意事项:
|
||||||
|
// - 暂不支持解构赋值(2022-08-28)
|
||||||
|
// - 暂不支持设置函数参数默认值(2022-08-28)
|
||||||
|
// - 暂不支持 try catch(2022-08-28)
|
||||||
|
|
||||||
|
// 自定义方块:
|
||||||
|
// https://mods.latvian.dev/books/kubejs/page/custom-blocks
|
||||||
|
|
||||||
|
global.defaults = function (v, d) {
|
||||||
|
(typeof d === 'undefined') && (d = null);
|
||||||
|
return ((typeof v === 'undefined' || v === null) ? d : v);
|
||||||
|
};
|
||||||
|
|
||||||
|
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
|
// https://github.com/Railcraft/Railcraft/blob/mc-1.12.2/src/main/java/mods/railcraft/common/plugins/color/EnumColor.java
|
||||||
global.COLORS = {
|
global.COLORS = {
|
||||||
|
|||||||
@@ -1,47 +1,107 @@
|
|||||||
// priority: 0
|
// priority: 0
|
||||||
|
|
||||||
console.info(`${global.LOG_PREFIX} 注册 wheat_plus 相关内容`);
|
console.info(`${LOG_PREFIX} 处理 ${MOD_ID} 相关内容`);
|
||||||
|
|
||||||
/** 模组 ID */
|
const defaults = global.defaults;
|
||||||
const modID = 'wheat_plus';
|
const LOG_PREFIX = global.LOG_PREFIX;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description 设置方块碰撞箱属性
|
* @desc 方块构造器
|
||||||
* @param {Internal.BlockBuilder} block
|
* @typedef B.Builder
|
||||||
* @param {'full'|'half'} type
|
* @type {Internal.BlockBuilder}
|
||||||
*/
|
*/
|
||||||
const setBlockBoxProps = function (block, type) {
|
|
||||||
if (type === 'full') {
|
|
||||||
block.box(0, 0, 0, 16, 16, 16, true);
|
|
||||||
block.fullBlock(true);
|
|
||||||
block.opaque(true);
|
|
||||||
} else if (type === 'half') {
|
|
||||||
block.box(0, 0, 0, 16, 8, 16, true);
|
|
||||||
block.fullBlock(false);
|
|
||||||
block.opaque(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// 注册方块
|
/**
|
||||||
onEvent('block.registry', (event) => {
|
* @desc 方块碰撞箱类型
|
||||||
|
* @typedef B.BoxType
|
||||||
|
* @type {'full'|'half'}
|
||||||
|
*/
|
||||||
|
|
||||||
console.info(`${global.LOG_PREFIX} 注册方块 - 开始`);
|
/**
|
||||||
|
* @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'|'translucent'}
|
||||||
|
*/
|
||||||
|
|
||||||
|
/** 模组 ID */
|
||||||
|
const MOD_ID = 'wheat_plus';
|
||||||
|
|
||||||
/** 模型文件基础路径 */
|
/** 模型文件基础路径 */
|
||||||
const modelPathBase = `${modID}:block/`;
|
const PATH_MODEL = `${MOD_ID}:block/`;
|
||||||
|
|
||||||
/** 纹理文件基础路径 */
|
/** 纹理文件基础路径 */
|
||||||
const texturePathBase = `${modID}:block/`;
|
const PATH_TEXTURE = `${MOD_ID}:block/`;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description 设置方块基础属性
|
||||||
|
* @param {B.Builder} block
|
||||||
|
* @param {object} options
|
||||||
|
* @param {B.BoxType} options.boxType 碰撞箱类型
|
||||||
|
* @param {string} options.displayName
|
||||||
|
* @param {number} options.hardness
|
||||||
|
* @param {boolean} options.isFull 是否完整方块
|
||||||
|
* @param {boolean} options.isOpaque 是否不透明
|
||||||
|
* @param {number} options.lightLevel 范围 0 ~ 1
|
||||||
|
* @param {B.Material} options.material
|
||||||
|
* @param {B.RenderType} options.renderType
|
||||||
|
* @param {number} options.resistance
|
||||||
|
*/
|
||||||
|
const setBlockProps = function (block, options) {
|
||||||
|
|
||||||
|
if (!block) {
|
||||||
|
console.error(`${LOG_PREFIX} 设置方块属性失败:缺少“block”参数`);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!options) {
|
||||||
|
options = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
const boxType = defaults(options.boxType, '');
|
||||||
|
const displayName = defaults(options.displayName, '');
|
||||||
|
const hardness = defaults(options.hardness, 8);
|
||||||
|
const isFull = defaults(options.isFull, true);
|
||||||
|
const isOpaque = defaults(options.isOpaque, true);
|
||||||
|
const lightLevel = defaults(options.lightLevel, 0);
|
||||||
|
const material = defaults(options.material, 'stone');
|
||||||
|
const renderType = defaults(options.renderType, 'solid');
|
||||||
|
const resistance = defaults(options.resistance, 16);
|
||||||
|
|
||||||
|
if (displayName) {
|
||||||
|
block.displayName(displayName);
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
block.fullBlock(isFull);
|
||||||
|
block.hardness(hardness);
|
||||||
|
block.lightLevel(lightLevel);
|
||||||
|
block.opaque(isOpaque);
|
||||||
|
block.material(material);
|
||||||
|
block.renderType(renderType);
|
||||||
|
block.resistance(resistance);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
|
||||||
/** 路方块基础属性 */
|
|
||||||
const roadBlockAttrs = {
|
|
||||||
hardness: 8.0,
|
|
||||||
lightLevel: 0,
|
|
||||||
material: 'rock',
|
|
||||||
renderType: 'cutout',
|
|
||||||
resistance: 16.0,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// 注册方块 - 路
|
||||||
|
onEvent('block.registry', (event) => {
|
||||||
|
|
||||||
|
console.info(`${LOG_PREFIX} 注册方块 - 路 - 开始`);
|
||||||
|
|
||||||
/** 普通路方块 */
|
/** 普通路方块 */
|
||||||
const roadBlocksNormal = [
|
const roadBlocksNormal = [
|
||||||
{
|
{
|
||||||
@@ -50,6 +110,7 @@ onEvent('block.registry', (event) => {
|
|||||||
texturePath: 'transparent',
|
texturePath: 'transparent',
|
||||||
hasNormal: true,
|
hasNormal: true,
|
||||||
hasSlant: false,
|
hasSlant: false,
|
||||||
|
isBlank: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'road_line_single_white',
|
name: 'road_line_single_white',
|
||||||
@@ -57,6 +118,7 @@ onEvent('block.registry', (event) => {
|
|||||||
texturePath: 'road/line/single_white',
|
texturePath: 'road/line/single_white',
|
||||||
hasNormal: true,
|
hasNormal: true,
|
||||||
hasSlant: true,
|
hasSlant: true,
|
||||||
|
isBlank: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'road_line_single_yellow',
|
name: 'road_line_single_yellow',
|
||||||
@@ -64,6 +126,7 @@ onEvent('block.registry', (event) => {
|
|||||||
texturePath: 'road/line/single_yellow',
|
texturePath: 'road/line/single_yellow',
|
||||||
hasNormal: true,
|
hasNormal: true,
|
||||||
hasSlant: true,
|
hasSlant: true,
|
||||||
|
isBlank: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'road_line_double_white',
|
name: 'road_line_double_white',
|
||||||
@@ -71,6 +134,7 @@ onEvent('block.registry', (event) => {
|
|||||||
texturePath: 'road/line/double_white',
|
texturePath: 'road/line/double_white',
|
||||||
hasNormal: true,
|
hasNormal: true,
|
||||||
hasSlant: true,
|
hasSlant: true,
|
||||||
|
isBlank: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'road_line_double_yellow',
|
name: 'road_line_double_yellow',
|
||||||
@@ -78,13 +142,14 @@ onEvent('block.registry', (event) => {
|
|||||||
texturePath: 'road/line/double_yellow',
|
texturePath: 'road/line/double_yellow',
|
||||||
hasNormal: true,
|
hasNormal: true,
|
||||||
hasSlant: true,
|
hasSlant: true,
|
||||||
|
isBlank: false,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
roadBlocksNormal.forEach((config) => {
|
roadBlocksNormal.forEach((config) => {
|
||||||
|
|
||||||
/** 方块 ID,包含模组 ID */
|
/** 方块 ID,包含模组 ID */
|
||||||
const id = `${modID}:${config.name}`;
|
const id = `${MOD_ID}:${config.name}`;
|
||||||
|
|
||||||
/** 方块名称文本 */
|
/** 方块名称文本 */
|
||||||
const label = (config.label || 'Unknown');
|
const label = (config.label || 'Unknown');
|
||||||
@@ -96,115 +161,106 @@ onEvent('block.registry', (event) => {
|
|||||||
if (config.hasNormal) {
|
if (config.hasNormal) {
|
||||||
|
|
||||||
const block = event.create(`${id}_full`, 'basic');
|
const block = event.create(`${id}_full`, 'basic');
|
||||||
|
const suffix = (config.isBlank ? '' : ',直线');
|
||||||
|
|
||||||
block.displayName(`${label},完整,直线`);
|
setBlockProps(block, {
|
||||||
|
boxType: 'full',
|
||||||
setBlockBoxProps(block, 'full');
|
displayName: `${label},完整${suffix}`,
|
||||||
|
renderType: 'cutout',
|
||||||
|
});
|
||||||
|
|
||||||
if (texturePath) {
|
if (texturePath) {
|
||||||
block.modelJson = {
|
block.modelJson = {
|
||||||
parent: `${modelPathBase}road/base_full`,
|
parent: `${PATH_MODEL}road/base_full`,
|
||||||
textures: {
|
textures: {
|
||||||
content: `${texturePathBase}${texturePath}`,
|
content: `${PATH_TEXTURE}${texturePath}`,
|
||||||
side: `${texturePathBase}${texturePath}`
|
side: `${PATH_TEXTURE}${texturePath}`
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let attr in roadBlockAttrs) {
|
|
||||||
block[attr](roadBlockAttrs[attr]);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 完整,斜线
|
// 完整,斜线
|
||||||
if (config.hasSlant) {
|
if (config.hasSlant) {
|
||||||
|
|
||||||
const block = event.create(`${id}_full_slant`, 'basic');
|
const block = event.create(`${id}_full_slant`, 'basic');
|
||||||
|
const suffix = (config.isBlank ? '' : ',斜线');
|
||||||
|
|
||||||
block.displayName(`${label},完整,斜线`);
|
setBlockProps(block, {
|
||||||
|
boxType: 'full',
|
||||||
setBlockBoxProps(block, 'full');
|
displayName: `${label},完整${suffix}`,
|
||||||
|
renderType: 'cutout',
|
||||||
|
});
|
||||||
|
|
||||||
if (texturePath) {
|
if (texturePath) {
|
||||||
block.modelJson = {
|
block.modelJson = {
|
||||||
parent: `${modelPathBase}road/base_full`,
|
parent: `${PATH_MODEL}road/base_full`,
|
||||||
textures: {
|
textures: {
|
||||||
content: `${texturePathBase}${texturePath}_slant`,
|
content: `${PATH_TEXTURE}${texturePath}_slant`,
|
||||||
side: `${texturePathBase}${texturePath}`
|
side: `${PATH_TEXTURE}${texturePath}`
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let attr in roadBlockAttrs) {
|
|
||||||
block[attr](roadBlockAttrs[attr]);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 一半,直线
|
// 一半,直线
|
||||||
if (config.hasNormal) {
|
if (config.hasNormal) {
|
||||||
|
|
||||||
const block = event.create(`${id}_half`, 'basic');
|
const block = event.create(`${id}_half`, 'basic');
|
||||||
|
const suffix = (config.isBlank ? '' : ',直线');
|
||||||
|
|
||||||
block.displayName(`${label},一半,直线`);
|
setBlockProps(block, {
|
||||||
|
boxType: 'half',
|
||||||
setBlockBoxProps(block, 'half');
|
displayName: `${label},一半${suffix}`,
|
||||||
|
renderType: 'cutout',
|
||||||
|
});
|
||||||
|
|
||||||
if (texturePath) {
|
if (texturePath) {
|
||||||
block.modelJson = {
|
block.modelJson = {
|
||||||
parent: `${modelPathBase}road/base_half`,
|
parent: `${PATH_MODEL}road/base_half`,
|
||||||
textures: {
|
textures: {
|
||||||
content: `${texturePathBase}${texturePath}`,
|
content: `${PATH_TEXTURE}${texturePath}`,
|
||||||
side: `${texturePathBase}${texturePath}`
|
side: `${PATH_TEXTURE}${texturePath}`
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let attr in roadBlockAttrs) {
|
|
||||||
block[attr](roadBlockAttrs[attr]);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 一半,斜线
|
// 一半,斜线
|
||||||
if (config.hasSlant) {
|
if (config.hasSlant) {
|
||||||
|
|
||||||
const block = event.create(`${id}_half_slant`, 'basic');
|
const block = event.create(`${id}_half_slant`, 'basic');
|
||||||
|
const suffix = (config.isBlank ? '' : ',斜线');
|
||||||
|
|
||||||
block.displayName(`${label},一半,斜线`);
|
setBlockProps(block, {
|
||||||
|
boxType: 'half',
|
||||||
setBlockBoxProps(block, 'half');
|
displayName: `${label},一半${suffix}`,
|
||||||
|
renderType: 'cutout',
|
||||||
|
});
|
||||||
|
|
||||||
if (texturePath) {
|
if (texturePath) {
|
||||||
block.modelJson = {
|
block.modelJson = {
|
||||||
parent: `${modelPathBase}road/base_half`,
|
parent: `${PATH_MODEL}road/base_half`,
|
||||||
textures: {
|
textures: {
|
||||||
content: `${texturePathBase}${texturePath}_slant`,
|
content: `${PATH_TEXTURE}${texturePath}_slant`,
|
||||||
side: `${texturePathBase}${texturePath}`
|
side: `${PATH_TEXTURE}${texturePath}`
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let attr in roadBlockAttrs) {
|
|
||||||
block[attr](roadBlockAttrs[attr]);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
console.info(`${global.LOG_PREFIX} 注册方块 - 完成`);
|
console.info(`${LOG_PREFIX} 注册方块 - 路 - 完成`);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// 注册物品
|
// 注册物品
|
||||||
onEvent('item.registry', (event) => {
|
// onEvent('item.registry', (event) => {
|
||||||
|
// console.info(`${LOG_PREFIX} 注册物品 - 开始`);
|
||||||
console.info(`${global.LOG_PREFIX} 注册物品 - 开始`);
|
// console.info(`${LOG_PREFIX} 注册物品 - 完成`);
|
||||||
|
// });
|
||||||
// event.create('example_item').displayName('Example Item');
|
|
||||||
|
|
||||||
console.info(`${global.LOG_PREFIX} 注册物品 - 完成`);
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|||||||
Reference in New Issue
Block a user