prismarine-item
Advanced tools
Comparing version 1.11.5 to 1.12.0
46
index.js
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 { | ||
@@ -18,3 +18,3 @@ constructor (type, count, metadata, nbt) { | ||
const itemEnum = mcData.items[type] | ||
const itemEnum = registry.items[type] | ||
if (itemEnum) { | ||
@@ -52,3 +52,3 @@ this.name = itemEnum.name | ||
static toNotch (item) { | ||
if (mcData.supportFeature('itemSerializationAllowsPresent')) { | ||
if (registry.supportFeature('itemSerializationAllowsPresent')) { | ||
if (item == null) return { present: false } | ||
@@ -62,3 +62,3 @@ const notchItem = { | ||
return notchItem | ||
} else if (mcData.supportFeature('itemSerializationUsesBlockId')) { | ||
} else if (registry.supportFeature('itemSerializationUsesBlockId')) { | ||
if (item == null) return { blockId: -1 } | ||
@@ -77,9 +77,9 @@ const notchItem = { | ||
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 | ||
@@ -126,4 +126,4 @@ return new Item(item.blockId, item.itemCount, item.itemDamage, item.nbtData) | ||
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') { | ||
@@ -138,3 +138,3 @@ let 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') { | ||
@@ -156,4 +156,4 @@ let itemEnch = [] | ||
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") | ||
@@ -163,3 +163,3 @@ 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 } } | ||
@@ -172,4 +172,4 @@ }) | ||
// 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 } | ||
@@ -181,3 +181,3 @@ } | ||
if (Object.keys(this).length === 0) return null | ||
const where = mcData.supportFeature('whereDurabilityIsSerialized') | ||
const where = registry.supportFeature('whereDurabilityIsSerialized') | ||
if (where === 'Damage') { | ||
@@ -192,3 +192,3 @@ return this?.nbt?.value?.Damage?.value ?? 0 | ||
set durabilityUsed (value) { | ||
const where = mcData.supportFeature('whereDurabilityIsSerialized') | ||
const where = registry.supportFeature('whereDurabilityIsSerialized') | ||
if (where === 'Damage') { | ||
@@ -205,6 +205,6 @@ if (!this?.nbt) this.nbt = { name: '', type: 'compound', value: {} } | ||
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) | ||
@@ -214,3 +214,3 @@ const entityName = data.EntityTag.id | ||
} | ||
if (mcData.supportFeature('spawnEggsHaveSpawnedEntityInName')) { | ||
if (registry.supportFeature('spawnEggsHaveSpawnedEntityInName')) { | ||
return this.name.replace('_spawn_egg', '') | ||
@@ -222,3 +222,3 @@ } | ||
Item.anvil = require('./lib/anvil.js')(mcData, Item) | ||
Item.anvil = require('./lib/anvil.js')(registry, Item) | ||
return Item | ||
@@ -225,0 +225,0 @@ } |
@@ -1,2 +0,2 @@ | ||
function loader (mcData, Item) { | ||
function loader (registry, Item) { | ||
function combine (itemOne, itemTwo, creative, renamedName) { | ||
@@ -65,7 +65,7 @@ const rename = typeof renamedName === 'string' | ||
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 | ||
@@ -121,5 +121,5 @@ xpLevelCost++ | ||
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) { | ||
@@ -167,3 +167,3 @@ return 0 // Enchanted book can't fix | ||
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']) | ||
@@ -170,0 +170,0 @@ return fixMaterials.includes(itemTwo.name) |
{ | ||
"name": "prismarine-item", | ||
"version": "1.11.5", | ||
"version": "1.12.0", | ||
"description": "Represent a minecraft item with its associated data", | ||
@@ -18,7 +18,7 @@ "main": "index.js", | ||
"devDependencies": { | ||
"@types/node": "^17.0.4", | ||
"expect": "^27.3.1", | ||
"mocha": "^9.1.3", | ||
"@types/node": "^18.6.4", | ||
"expect": "^29.1.2", | ||
"mocha": "^10.0.0", | ||
"prismarine-item": "file:.", | ||
"standard": "^16.0.1" | ||
"standard": "^17.0.0" | ||
}, | ||
@@ -38,4 +38,4 @@ "keywords": [ | ||
"prismarine-nbt": "^2.0.0", | ||
"minecraft-data": "^3.0.0" | ||
"prismarine-registry": "^1.4.0" | ||
} | ||
} |
@@ -99,2 +99,6 @@ # prismarine-item | ||
## 1.12.0 | ||
* uses registry instead of mcData (thanks @Epirito) | ||
## 1.11.5 | ||
@@ -101,0 +105,0 @@ |
/* eslint-env mocha */ | ||
const expect = require('expect') | ||
const expect = require('expect').default | ||
@@ -141,6 +141,6 @@ describe('1.8.9 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 | ||
@@ -285,3 +285,3 @@ if (repairCost > 0) i.repairCost = repairCost | ||
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) | ||
@@ -296,3 +296,3 @@ const a3 = makeBook([{ name: 'thorns', lvl: 3 }], 0) | ||
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 }] | ||
@@ -325,3 +325,3 @@ eqItem.repairCost = 1 | ||
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 }] | ||
@@ -342,3 +342,3 @@ eqItem.repairCost = 3 | ||
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 }] | ||
@@ -345,0 +345,0 @@ eqItem.repairCost = 7 |
/* eslint-env mocha */ | ||
const expect = require('expect') | ||
const expect = require('expect').default | ||
@@ -270,18 +270,18 @@ describe('test based on examples', () => { | ||
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 }] | ||
@@ -291,5 +291,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 }] | ||
@@ -296,0 +296,0 @@ expect(Item.equal(itemOne, itemTwo)).toStrictEqual(false) |
Sorry, the diff of this file is not supported yet
69565
200
+ Addedprismarine-registry@^1.4.0
+ Addedprismarine-registry@1.7.0(transitive)
- Removedminecraft-data@^3.0.0