Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add getRenamedData #335

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ module.exports.legacy = {
pc: require('./minecraft-data/data/pc/common/legacy.json'),
bedrock: require('./minecraft-data/data/bedrock/common/legacy.json')
}
module.exports.getRenamedData = require('./lib/dataRenames.js').getRenamedData

const schemas = {
biomes: require('./minecraft-data/schemas/biomes_schema.json'),
Expand Down
34 changes: 34 additions & 0 deletions lib/dataRenames.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const itemBlockRenames = require('../minecraft-data/data/pc/common/dataRenames.json')

exports.versionToNumber = (ver) => {
const [x, y = '0', z = '0'] = ver.split('.')
return +`${x.padStart(2, '0')}${(parseInt(y).toString().padStart(2, '0'))}${parseInt(z).toString().padStart(2, '0')}`
}

exports.getRenamedData = (type, blockOrItem, versionFrom, versionTo) => {
const verFrom = exports.versionToNumber(versionFrom)
const verTo = exports.versionToNumber(versionTo)
const dir = verFrom < verTo ? 1 : -1
const targetIdx = dir > 0 ? 1 : 0
let renamed = blockOrItem
const mapVersions = Object.keys(itemBlockRenames).sort((a, b) => dir * (exports.versionToNumber(a) - exports.versionToNumber(b)))
const upperBoundVer = dir > 0 ? verTo : verFrom
const lowerBoundVer = dir > 0 ? verFrom : verTo
for (const mapVersion of mapVersions) {
if (dir > 0 && versionToNumber(mapVersion) >= upperBoundVer) break
if (dir < 0 && versionToNumber(mapVersion) <= lowerBoundVer) break
const nextMapData = itemBlockRenames[mapVersion][type]
if (!nextMapData) continue
for (const namesArr of nextMapData) {
const targetName = namesArr[targetIdx]
const compareName = namesArr[1 - targetIdx]
if (Array.isArray(renamed)) {
if (renamed.includes(compareName)) renamed = renamed.map(x => x === compareName ? targetName : x)
} else if (renamed === compareName) {
renamed = targetName
break
}
}
}
return renamed
}
1 change: 1 addition & 0 deletions typings/index-template.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,4 @@ const postNettyVersionsByProtocolVersion: VersionSet
const supportedVersions: SupportedVersions
const legacy: { pc: { blocks: { [id: string]: string } } }
const schemas: Schemas
const getRenamedData: <T extends string | string[]>(type: 'blocks' | 'items', blockOrItem: T, versionFrom: string, versionTo: string) => T extends string ? string : string[]
2 changes: 2 additions & 0 deletions typings/test-typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@ console.log(getMcData.supportedVersions.pc)
console.log(getMcData.schemas.blocks)

console.log(getMcData('1.12').language['options.sensitivity.max'])
console.log(getMcData.getRenamedData('blocks', 'short_grass', '1.20.3', '1.8.8') === 'tallgrass') // test string comparison
console.log(getMcData.getRenamedData('blocks', ['standing_sign', 'grass'], '1.8.8', '1.16'))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will be [ 'oak_sign', 'grass_block' ]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an example of calling the API but not an example of usage. I don't think we should ever try to do that.

Clearly tall and short are not the same thing

Oak is a specific kind of wood

Grass and grass block may be different

Having tags and groups make sense to note similar blocks that should be treated similarly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having tags and groups make sense to note similar blocks that should be treated similarly

It depends on the use case. As I mentioned earlier there are a lot of use cases when you want to track block renames across the versions (such as world upgrades and so on).

Here short_grass and tallgrass are the same thing actually (dont' let 1.8.8 naming fool you)

Loading