From 4a5f10359f2efb53fc888ea15fe6207adf70c877 Mon Sep 17 00:00:00 2001 From: Epirito <96730122+Epirito@users.noreply.github.com> Date: Tue, 11 Oct 2022 05:39:23 -0300 Subject: [PATCH 1/4] now p-item uses registry instead of mcData (a step towards #2450) --- index.js | 46 +++++++++++++++++++++++----------------------- lib/anvil.js | 14 +++++++------- test/anvil.test.js | 14 +++++++------- test/basic.test.js | 20 ++++++++++---------- 4 files changed, 47 insertions(+), 47 deletions(-) diff --git a/index.js b/index.js index ec6051a..a00e139 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,6 @@ const nbt = require('prismarine-nbt') -function loader (version) { - const mcData = require('minecraft-data')(version) +function loader (registryOrVersion) { + const registry = typeof registryOrVersion === 'string' ? require('prismarine-registry')(registryOrVersion) : registryOrVersion class Item { constructor (type, count, metadata, nbt) { if (type == null) return @@ -15,7 +15,7 @@ function loader (version) { this.metadata = metadata == null ? 0 : metadata this.nbt = nbt || null - const itemEnum = mcData.items[type] + const itemEnum = registry.items[type] if (itemEnum) { this.name = itemEnum.name this.displayName = itemEnum.displayName @@ -48,7 +48,7 @@ function loader (version) { } static toNotch (item) { - if (mcData.supportFeature('itemSerializationAllowsPresent')) { + if (registry.supportFeature('itemSerializationAllowsPresent')) { if (item == null) return { present: false } const notchItem = { present: true, @@ -57,7 +57,7 @@ function loader (version) { } if (item.nbt && item.nbt.length !== 0) { notchItem.nbtData = item.nbt } return notchItem - } else if (mcData.supportFeature('itemSerializationUsesBlockId')) { + } else if (registry.supportFeature('itemSerializationUsesBlockId')) { if (item == null) return { blockId: -1 } const notchItem = { blockId: item.type, @@ -71,13 +71,13 @@ function loader (version) { } static fromNotch (item) { - if (mcData.supportFeature('itemSerializationWillOnlyUsePresent')) { + if (registry.supportFeature('itemSerializationWillOnlyUsePresent')) { if (item.present === false) return null return new Item(item.itemId, item.itemCount, item.nbtData) - } else if (mcData.supportFeature('itemSerializationAllowsPresent')) { + } else if (registry.supportFeature('itemSerializationAllowsPresent')) { if (item.itemId === -1 || item.present === false) return null return new Item(item.itemId, item.itemCount, item.nbtData) - } else if (mcData.supportFeature('itemSerializationUsesBlockId')) { + } else if (registry.supportFeature('itemSerializationUsesBlockId')) { if (item.blockId === -1) return null return new Item(item.blockId, item.itemCount, item.itemDamage, item.nbtData) } @@ -119,8 +119,8 @@ function loader (version) { get enchants () { if (Object.keys(this).length === 0) return [] - const enchantNbtKey = mcData.supportFeature('nbtNameForEnchant') - const typeOfEnchantLevelValue = mcData.supportFeature('typeOfValueForEnchantLevel') + const enchantNbtKey = registry.supportFeature('nbtNameForEnchant') + const typeOfEnchantLevelValue = registry.supportFeature('typeOfValueForEnchantLevel') if (typeOfEnchantLevelValue === 'short' && enchantNbtKey === 'ench') { let itemEnch if (this.name === 'enchanted_book' && this?.nbt?.value?.StoredEnchantments) { @@ -130,7 +130,7 @@ function loader (version) { } else { itemEnch = [] } - return itemEnch.map(ench => ({ lvl: ench.lvl, name: mcData.enchantments[ench.id]?.name || null })) + return itemEnch.map(ench => ({ lvl: ench.lvl, name: registry.enchantments[ench.id]?.name || null })) } else if (typeOfEnchantLevelValue === 'string' && enchantNbtKey === 'Enchantments') { let itemEnch = [] if (this?.nbt?.value?.Enchantments) { @@ -147,13 +147,13 @@ function loader (version) { set enchants (normalizedEnchArray) { const isBook = this.name === 'enchanted_book' - const enchListName = mcData.supportFeature('nbtNameForEnchant') - const type = mcData.supportFeature('typeOfValueForEnchantLevel') + const enchListName = registry.supportFeature('nbtNameForEnchant') + const type = registry.supportFeature('typeOfValueForEnchantLevel') if (type === null) throw new Error("Don't know the serialized type for enchant level") if (!this.nbt) this.nbt = { name: '', type: 'compound', value: {} } const enchs = normalizedEnchArray.map(({ name, lvl }) => { - const value = type === 'short' ? mcData.enchantmentsByName[name].id : `minecraft:${mcData.enchantmentsByName[name].name}` + const value = type === 'short' ? registry.enchantmentsByName[name].id : `minecraft:${registry.enchantmentsByName[name].name}` return { id: { type, value }, lvl: { type: 'short', value: lvl } } }) @@ -161,15 +161,15 @@ function loader (version) { this.nbt.value[isBook ? 'StoredEnchantments' : enchListName] = { type: 'list', value: { type: 'compound', value: enchs } } } - // The 'mcData.itemsByName[this.name].maxDurability' checks to see if this item can lose durability - if (mcData.supportFeature('whereDurabilityIsSerialized') === 'Damage' && mcData.itemsByName[this.name].maxDurability) { + // The 'registry.itemsByName[this.name].maxDurability' checks to see if this item can lose durability + if (registry.supportFeature('whereDurabilityIsSerialized') === 'Damage' && registry.itemsByName[this.name].maxDurability) { this.nbt.value.Damage = { type: 'int', value: 0 } } } get durabilityUsed () { if (Object.keys(this).length === 0) return null - const where = mcData.supportFeature('whereDurabilityIsSerialized') + const where = registry.supportFeature('whereDurabilityIsSerialized') if (where === 'Damage') { return this?.nbt?.value?.Damage?.value ?? 0 } else if (where === 'metadata') { @@ -179,7 +179,7 @@ function loader (version) { } set durabilityUsed (value) { - const where = mcData.supportFeature('whereDurabilityIsSerialized') + const where = registry.supportFeature('whereDurabilityIsSerialized') if (where === 'Damage') { if (!this?.nbt) this.nbt = { name: '', type: 'compound', value: {} } this.nbt.value.Damage = { type: 'int', value } @@ -191,22 +191,22 @@ function loader (version) { } get spawnEggMobName () { - if (mcData.supportFeature('spawnEggsUseInternalIdInNbt')) { - return mcData.entitiesArray.find(o => o.internalId === this.metadata).name + if (registry.supportFeature('spawnEggsUseInternalIdInNbt')) { + return registry.entitiesArray.find(o => o.internalId === this.metadata).name } - if (mcData.supportFeature('spawnEggsUseEntityTagInNbt')) { + if (registry.supportFeature('spawnEggsUseEntityTagInNbt')) { const data = nbt.simplify(this.nbt) const entityName = data.EntityTag.id return entityName.replace('minecraft:', '') } - if (mcData.supportFeature('spawnEggsHaveSpawnedEntityInName')) { + if (registry.supportFeature('spawnEggsHaveSpawnedEntityInName')) { return this.name.replace('_spawn_egg', '') } throw new Error("Don't know how to get spawn egg mob name for this mc version") } } - Item.anvil = require('./lib/anvil.js')(mcData, Item) + Item.anvil = require('./lib/anvil.js')(registry, Item) return Item } diff --git a/lib/anvil.js b/lib/anvil.js index add9524..104ec11 100644 --- a/lib/anvil.js +++ b/lib/anvil.js @@ -1,4 +1,4 @@ -function loader (mcData, Item) { +function loader (registry, Item) { function combine (itemOne, itemTwo, creative, renamedName) { const rename = typeof renamedName === 'string' const data = { @@ -62,11 +62,11 @@ function loader (mcData, Item) { let xpLevelCost = 0 for (const ench of itemTwoEnch) { const enchOnItemOne = finalEnchs.find(x => x.name === ench.name) - let { exclude, maxLevel, category, weight } = mcData.enchantmentsByName[ench.name] + let { exclude, maxLevel, category, weight } = registry.enchantmentsByName[ench.name] const multiplier = getMultipliers(weight, rightIsBook) - if (!(itemOne.name === 'enchanted_book' && rightIsBook) && !mcData.itemsByName[itemOne.name].enchantCategories.includes(category) && !creative) continue + if (!(itemOne.name === 'enchanted_book' && rightIsBook) && !registry.itemsByName[itemOne.name].enchantCategories.includes(category) && !creative) continue else if (enchOnItemOne === undefined) { // first item doesn't have this ench - exclude = exclude.map(name => mcData.enchantmentsByName[name].name) + exclude = exclude.map(name => registry.enchantmentsByName[name].name) if (exclude.some(excludedEnch => finalEnchsByName.includes(excludedEnch))) { // has an excluded enchant xpLevelCost++ } else { @@ -117,9 +117,9 @@ function loader (mcData, Item) { if (itemTwo === null) return { xpLevelCost: 0, fixedDurability: 0, usedMats: 0 } // air else if (itemTwo.name === 'enchanted_book') return { xpLevelCost: 0, fixedDurability: 0, usedMats: 0 } - const maxDurability = mcData.itemsByName[itemOne.name].maxDurability + const maxDurability = registry.itemsByName[itemOne.name].maxDurability const durabilityLost = itemOne.durabilityUsed - const fixMaterials = mcData.itemsByName[itemOne.name].repairWith.concat([itemOne.name]) + const fixMaterials = registry.itemsByName[itemOne.name].repairWith.concat([itemOne.name]) if (!fixMaterials.includes(itemTwo.name) && itemOne.name !== itemTwo.name) { return 0 // Enchanted book can't fix } @@ -162,7 +162,7 @@ function loader (mcData, Item) { function combinePossible (itemOne, itemTwo) { if (!itemOne?.name || !itemTwo?.name || (!itemOne?.name && !itemTwo?.name)) return false - let fixMaterials = (mcData.itemsByName[itemOne.name].repairWith ?? []).concat([itemOne.name]) + let fixMaterials = (registry.itemsByName[itemOne.name].repairWith ?? []).concat([itemOne.name]) if (itemOne.name !== 'enchanted_book') fixMaterials = fixMaterials.concat(['enchanted_book']) return fixMaterials.includes(itemTwo.name) } diff --git a/test/anvil.test.js b/test/anvil.test.js index eb82f4e..95b1e67 100644 --- a/test/anvil.test.js +++ b/test/anvil.test.js @@ -1,6 +1,6 @@ /* eslint-env mocha */ -const expect = require('expect').default +const expect = require('expect') describe('1.8.9 anvil', () => { const Item = require('prismarine-item')('1.8.8') @@ -138,10 +138,10 @@ describe('1.8.9 anvil', () => { describe('1.16.5 anvil', () => { const Item = require('prismarine-item')('1.16.5') - const mcData = require('minecraft-data')('1.16.5') + const registry = require('prismarine-registry')('1.16.5') function makeBook (ench, repairCost) { - const i = new Item(mcData.itemsByName.enchanted_book.id, 1) + const i = new Item(registry.itemsByName.enchanted_book.id, 1) i.enchants = ench if (repairCost > 0) i.repairCost = repairCost return i @@ -281,7 +281,7 @@ describe('1.16.5 anvil', () => { describe('Enchantment Order Diagram', () => { // this test is from https://minecraft.gamepedia.com/File:Enchantment_Order_Diagram.png // make items let b1, b2, b3, b4, c1, c2 - const a1 = new Item(mcData.itemsByName.diamond_boots.id, 1) + const a1 = new Item(registry.itemsByName.diamond_boots.id, 1) const a2 = makeBook([{ name: 'soul_speed', lvl: 3 }], 0) const a3 = makeBook([{ name: 'thorns', lvl: 3 }], 0) const a4 = makeBook([{ name: 'feather_falling', lvl: 4 }], 0) @@ -291,7 +291,7 @@ describe('1.16.5 anvil', () => { const a8 = makeBook([{ name: 'mending', lvl: 1 }], 0) describe('first combine', () => { it('enchant boot+ss3', () => { - const eqItem = new Item(mcData.itemsByName.diamond_boots.id, 1) + const eqItem = new Item(registry.itemsByName.diamond_boots.id, 1) eqItem.enchants = [{ name: 'soul_speed', lvl: 3 }] eqItem.repairCost = 1 const res = Item.anvil(a1, a2, false, undefined) @@ -319,7 +319,7 @@ describe('1.16.5 anvil', () => { }) describe('second combine', () => { it('ss3 boots + t3 ff4', () => { - const eqItem = new Item(mcData.itemsByName.diamond_boots.id, 1) + const eqItem = new Item(registry.itemsByName.diamond_boots.id, 1) eqItem.enchants = [{ name: 'soul_speed', lvl: 3 }, { name: 'thorns', lvl: 3 }, { name: 'feather_falling', lvl: 4 }] eqItem.repairCost = 3 const res = Item.anvil(b1, b2, false, undefined) @@ -335,7 +335,7 @@ describe('1.16.5 anvil', () => { }) describe('third combine', () => { it('d3p4 + u3m1', () => { - const eqItem = new Item(mcData.itemsByName.diamond_boots.id, 1) + const eqItem = new Item(registry.itemsByName.diamond_boots.id, 1) eqItem.enchants = [{ name: 'soul_speed', lvl: 3 }, { name: 'thorns', lvl: 3 }, { name: 'feather_falling', lvl: 4 }, { name: 'depth_strider', lvl: 3 }, { name: 'protection', lvl: 4 }, { name: 'unbreaking', lvl: 3 }, { name: 'mending', lvl: 1 }] eqItem.repairCost = 7 const res = Item.anvil(c1, c2, false, undefined) diff --git a/test/basic.test.js b/test/basic.test.js index b4dc5e4..89f78da 100644 --- a/test/basic.test.js +++ b/test/basic.test.js @@ -1,6 +1,6 @@ /* eslint-env mocha */ -const expect = require('expect').default +const expect = require('expect') describe('test based on examples', () => { describe('1.8 iron shovel', () => { @@ -267,29 +267,29 @@ describe('test anvil functions', () => { describe('use Item.equal', () => { const Item = require('prismarine-item')('1.16.5') - const mcData = require('minecraft-data')('1.16.5') + const registry = require('prismarine-registry')('1.16.5') it('sh5 wep + not sh5 wep', () => { - const itemOne = new Item(mcData.itemsByName.diamond_sword.id, 1) + const itemOne = new Item(registry.itemsByName.diamond_sword.id, 1) itemOne.enchants = [{ name: 'sharpness', lvl: 5 }] - const itemTwo = new Item(mcData.itemsByName.diamond_sword.id, 1) + const itemTwo = new Item(registry.itemsByName.diamond_sword.id, 1) expect(Item.equal(itemOne, itemTwo)).toStrictEqual(false) }) it('two unenchanted', () => { - const itemOne = new Item(mcData.itemsByName.diamond_sword.id, 1) - const itemTwo = new Item(mcData.itemsByName.diamond_sword.id, 1) + const itemOne = new Item(registry.itemsByName.diamond_sword.id, 1) + const itemTwo = new Item(registry.itemsByName.diamond_sword.id, 1) expect(Item.equal(itemOne, itemTwo)).toStrictEqual(true) }) it('two enchanted', () => { - const itemOne = new Item(mcData.itemsByName.diamond_sword.id, 1) + const itemOne = new Item(registry.itemsByName.diamond_sword.id, 1) itemOne.enchants = [{ name: 'sharpness', lvl: 5 }] - const itemTwo = new Item(mcData.itemsByName.diamond_sword.id, 1) + const itemTwo = new Item(registry.itemsByName.diamond_sword.id, 1) itemTwo.enchants = [{ name: 'sharpness', lvl: 5 }] expect(Item.equal(itemOne, itemTwo)).toStrictEqual(true) }) it('two enchants in common on both items but diff orders', () => { - const itemOne = new Item(mcData.itemsByName.diamond_sword.id, 1) + const itemOne = new Item(registry.itemsByName.diamond_sword.id, 1) itemOne.enchants = [{ name: 'sharpness', lvl: 5 }, { name: 'unbreaking', lvl: 1 }] - const itemTwo = new Item(mcData.itemsByName.diamond_sword.id, 1) + const itemTwo = new Item(registry.itemsByName.diamond_sword.id, 1) itemTwo.enchants = [{ name: 'unbreaking', lvl: 1 }, { name: 'sharpness', lvl: 5 }] expect(Item.equal(itemOne, itemTwo)).toStrictEqual(false) }) From 60e8ad787b618e53752a223b91b2fe25d25aa19c Mon Sep 17 00:00:00 2001 From: Epirito <96730122+Epirito@users.noreply.github.com> Date: Tue, 11 Oct 2022 05:46:02 -0300 Subject: [PATCH 2/4] add registry dependency --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 205fa5e..fb9020f 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,6 @@ "homepage": "https://github.com/PrismarineJS/prismarine-item#readme", "dependencies": { "prismarine-nbt": "^2.0.0", - "minecraft-data": "^3.0.0" + "prismarine-registry": "^1.4.0" } } From d0f9807db612868352461ac1a4e5617ad9e791d8 Mon Sep 17 00:00:00 2001 From: Romain Beaumont Date: Tue, 11 Oct 2022 13:15:55 +0200 Subject: [PATCH 3/4] Fix expect usage --- test/anvil.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/anvil.test.js b/test/anvil.test.js index 95b1e67..74aac4c 100644 --- a/test/anvil.test.js +++ b/test/anvil.test.js @@ -1,6 +1,6 @@ /* eslint-env mocha */ -const expect = require('expect') +const expect = require('expect').default describe('1.8.9 anvil', () => { const Item = require('prismarine-item')('1.8.8') From 6a6550a0a4e555901f0abe9c5aff5e8b131925bd Mon Sep 17 00:00:00 2001 From: Romain Beaumont Date: Tue, 11 Oct 2022 13:18:45 +0200 Subject: [PATCH 4/4] Fix expect usage --- test/basic.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/basic.test.js b/test/basic.test.js index 89f78da..2a66cdb 100644 --- a/test/basic.test.js +++ b/test/basic.test.js @@ -1,6 +1,6 @@ /* eslint-env mocha */ -const expect = require('expect') +const expect = require('expect').default describe('test based on examples', () => { describe('1.8 iron shovel', () => {