From 31ae3629b20448797cefcd2342b1bec69fdb55ad Mon Sep 17 00:00:00 2001 From: Gjum Date: Sun, 18 Aug 2024 20:19:52 +0200 Subject: [PATCH 1/2] optional minecraft: prefix --- lib/pc/index.js | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/lib/pc/index.js b/lib/pc/index.js index 09c394d..bb10471 100644 --- a/lib/pc/index.js +++ b/lib/pc/index.js @@ -4,6 +4,10 @@ nbt.float = value => ({ type: 'float', value }) const { networkBiomesToMcDataSchema, mcDataSchemaToNetworkBiomes } = require('./transforms') +const getEntry = (obj, key) => obj?.[key] ?? obj?.['minecraft:' + key] +// TODO depending on the MC version, entries need to be prefixed with "minecraft:" or not +const setEntry = (obj, key, val) => obj[key] = val + module.exports = (data, staticData) => { let hasDynamicDimensionData = false @@ -11,7 +15,7 @@ module.exports = (data, staticData) => { loadDimensionCodec (codec) { const dimensionCodec = nbt.simplify(codec) - const chat = dimensionCodec['minecraft:chat_type']?.value + const chat = getEntry(dimensionCodec, 'chat_type')?.value if (chat) { data.chatFormattingById = {} data.chatFormattingByName = {} @@ -22,15 +26,14 @@ module.exports = (data, staticData) => { id: chatType.id, name: chatType.name, formatString: staticData.language[d.translation_key] || d.translation_key /* chat type minecraft:raw has the formatString given directly by the translation key */, - parameters: d.parameters, - style: d.style + parameters: d.parameters } data.chatFormattingById[chatType.id] = n data.chatFormattingByName[chatType.name] = n } } - const dimensions = dimensionCodec['minecraft:dimension_type']?.value + const dimensions = getEntry(dimensionCodec, 'dimension_type')?.value if (dimensions) { data.dimensionsById = {} data.dimensionsByName = {} @@ -46,7 +49,7 @@ module.exports = (data, staticData) => { } } - const biomes = dimensionCodec['minecraft:worldgen/biome']?.value + const biomes = getEntry(dimensionCodec, 'worldgen/biome')?.value if (!biomes) { return // no biome data } @@ -78,7 +81,7 @@ module.exports = (data, staticData) => { // Keep the old dimension codec data if it exists (re-encoding) // We don't have this data statically, should probably be added to mcData if (data.dimensionsArray) { - codec['minecraft:dimension_type'] = nbt.comp({ + setEntry(codec, 'dimension_type', nbt.comp({ type: nbt.string('minecraft:dimension_type'), value: nbt.list(nbt.comp( data.dimensionsArray.map(dimension => ({ @@ -87,19 +90,19 @@ module.exports = (data, staticData) => { element: dimension.element })) )) - }) + })) } else { - codec['minecraft:dimension_type'] = staticData.loginPacket.dimensionCodec.value['minecraft:dimension_type'] + setEntry(codec, 'dimension_type', getEntry(staticData.loginPacket.dimensionCodec.value, 'dimension_type')) } // if we have dynamic biome data (re-encoding), we can count on biome.effects // being in place. Otherwise, we need to use static data exclusively, e.g. flying squid. - codec['minecraft:worldgen/biome'] = nbt.comp({ + setEntry(codec, 'worldgen/biome', nbt.comp({ type: nbt.string('minecraft:worldgen/biome'), value: nbt.list(nbt.comp(mcDataSchemaToNetworkBiomes(hasDynamicDimensionData ? data.biomesArray : null, staticData))) - }) + })) // 1.19 - codec['minecraft:chat_type'] = staticData.loginPacket.dimensionCodec?.value?.['minecraft:chat_type'] + setEntry(codec, 'chat_type', getEntry(staticData.loginPacket.dimensionCodec?.value, 'chat_type')) } return nbt.comp(codec) From cb5b8abff114dbb9b0928454d9e9dc4b1f116cfb Mon Sep 17 00:00:00 2001 From: Gjum Date: Sun, 18 Aug 2024 20:27:05 +0200 Subject: [PATCH 2/2] fix assignment in arrow function --- lib/pc/index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/pc/index.js b/lib/pc/index.js index bb10471..a3b8510 100644 --- a/lib/pc/index.js +++ b/lib/pc/index.js @@ -6,7 +6,9 @@ const { networkBiomesToMcDataSchema, mcDataSchemaToNetworkBiomes } = require('./ const getEntry = (obj, key) => obj?.[key] ?? obj?.['minecraft:' + key] // TODO depending on the MC version, entries need to be prefixed with "minecraft:" or not -const setEntry = (obj, key, val) => obj[key] = val +const setEntry = (obj, key, val) => { + obj[key] = val +} module.exports = (data, staticData) => { let hasDynamicDimensionData = false