prismarine-item
Advanced tools
Comparing version 1.5.0 to 1.6.0
@@ -6,2 +6,3 @@ /// <reference types="node" /> | ||
type: number; | ||
slot: number; | ||
count: number; | ||
@@ -13,5 +14,10 @@ metadata: number; | ||
stackSize: number; | ||
durabilityUsed: number; | ||
enchants: NormalizedEnchant[]; | ||
repairCost: number; | ||
customName: string; | ||
static equal(item1: Item, item2: Item): boolean; | ||
static toNotch(item: Item): NotchItem; | ||
static fromNotch(item: NotchItem): Item; | ||
static anvil (itemOne: Item, itemTwo: Item | null, creative: boolean, rename: string | undefined): {xpCost: number, item: Item} | ||
} | ||
@@ -29,2 +35,8 @@ declare interface NotchItem { | ||
} | ||
export declare function loader(mcVersion: string): keyof Item; | ||
declare interface NormalizedEnchant { | ||
name: string; | ||
lvl: number | ||
} | ||
export declare function loader(mcVersion: string): keyof Item; |
227
index.js
@@ -1,87 +0,170 @@ | ||
module.exports = loader | ||
const nbt = require('prismarine-nbt') | ||
function loader (version) { | ||
const mcData = require('minecraft-data')(version) | ||
class Item { | ||
constructor (type, count, metadata, nbt) { | ||
if (type == null) return | ||
function loader (mcVersion) { | ||
const mcData = require('minecraft-data')(mcVersion) | ||
findItemOrBlockById = mcData.findItemOrBlockById | ||
version = mcData.version.majorVersion | ||
return Item | ||
} | ||
if (metadata instanceof Object && metadata !== null) { | ||
nbt = metadata | ||
metadata = 0 | ||
} | ||
let findItemOrBlockById | ||
let version | ||
this.type = type | ||
this.count = count | ||
this.metadata = metadata == null ? 0 : metadata | ||
this.nbt = nbt || null | ||
function Item (type, count, metadata, nbt) { | ||
if (type == null) return | ||
const itemEnum = mcData.findItemOrBlockById(type) | ||
if (itemEnum) { | ||
this.name = itemEnum.name | ||
this.displayName = itemEnum.displayName | ||
if ('variations' in itemEnum) { | ||
for (const i in itemEnum.variations) { | ||
if (itemEnum.variations[i].metadata === metadata) { this.displayName = itemEnum.variations[i].displayName } | ||
} | ||
} | ||
this.stackSize = itemEnum.stackSize | ||
} else { | ||
this.name = 'unknown' | ||
this.displayName = 'unknown' | ||
this.stackSize = 1 | ||
} | ||
} | ||
if (metadata instanceof Object && metadata !== null) { | ||
nbt = metadata | ||
metadata = 0 | ||
} | ||
static equal (item1, item2) { | ||
if (item1 == null && item2 == null) { | ||
return true | ||
} else if (item1 == null) { | ||
return false | ||
} else if (item2 == null) { | ||
return false | ||
} else { | ||
return item1.type === item2.type && | ||
item1.count === item2.count && | ||
item1.metadata === item2.metadata | ||
} | ||
} | ||
this.type = type | ||
this.count = count | ||
this.metadata = metadata == null ? 0 : metadata | ||
this.nbt = nbt || null | ||
static toNotch (item) { | ||
if (mcData.isNewerOrEqualTo('1.13')) { | ||
if (item == null) return { present: false } | ||
const notchItem = { | ||
present: true, | ||
itemId: item.type, | ||
itemCount: item.count | ||
} | ||
if (item.nbt && item.nbt.length !== 0) { notchItem.nbtData = item.nbt } | ||
return notchItem | ||
} else { | ||
if (item == null) return { blockId: -1 } | ||
const notchItem = { | ||
blockId: item.type, | ||
itemCount: item.count, | ||
itemDamage: item.metadata | ||
} | ||
if (item.nbt && item.nbt.length !== 0) { notchItem.nbtData = item.nbt } | ||
return notchItem | ||
} | ||
} | ||
const itemEnum = findItemOrBlockById(type) | ||
if (itemEnum) { | ||
this.name = itemEnum.name | ||
this.displayName = itemEnum.displayName | ||
if ('variations' in itemEnum) { | ||
for (var i in itemEnum.variations) { | ||
if (itemEnum.variations[i].metadata === metadata) { this.displayName = itemEnum.variations[i].displayName } | ||
static fromNotch (item) { | ||
if (mcData.isNewerOrEqualTo('1.13')) { | ||
if (item.present === false) return null | ||
return new Item(item.itemId, item.itemCount, item.nbtData) | ||
} else { | ||
if (item.blockId === -1) return null | ||
return new Item(item.blockId, item.itemCount, item.itemDamage, item.nbtData) | ||
} | ||
} | ||
this.stackSize = itemEnum.stackSize | ||
} else { | ||
this.name = 'unknown' | ||
this.displayName = 'unknown' | ||
this.stackSize = 1 | ||
} | ||
} | ||
Item.equal = function (item1, item2) { | ||
if (item1 == null && item2 == null) { | ||
return true | ||
} else if (item1 == null) { | ||
return false | ||
} else if (item2 == null) { | ||
return false | ||
} else { | ||
return item1.type === item2.type && | ||
item1.count === item2.count && | ||
item1.metadata === item2.metadata | ||
} | ||
} | ||
get customName () { | ||
if (Object.keys(this).length === 0) return null | ||
return this?.nbt?.value?.display?.value?.Name?.value ?? 0 | ||
} | ||
Item.toNotch = function (item) { | ||
if (version === '1.13' || version === '1.14' || version === '1.15' || version === '1.16') { | ||
if (item == null) return { present: false } | ||
const notchItem = { | ||
present: true, | ||
itemId: item.type, | ||
itemCount: item.count | ||
set customName (newName) { | ||
if (!this.nbt) this.nbt = { name: '', type: 'compound', value: {} } | ||
if (!this.nbt.value.display) this.nbt.value.display = { type: 'compound', value: {} } | ||
this.nbt.value.display.value.Name = { type: 'string', value: newName } | ||
} | ||
if (item.nbt && item.nbt.length !== 0) { notchItem.nbtData = item.nbt } | ||
return notchItem | ||
} else { | ||
if (item == null) return { blockId: -1 } | ||
const notchItem = { | ||
blockId: item.type, | ||
itemCount: item.count, | ||
itemDamage: item.metadata | ||
// gets the cost based on previous anvil uses | ||
get repairCost () { | ||
if (Object.keys(this).length === 0) return 0 | ||
return this?.nbt?.value?.RepairCost?.value ?? 0 | ||
} | ||
if (item.nbt && item.nbt.length !== 0) { notchItem.nbtData = item.nbt } | ||
return notchItem | ||
set repairCost (value) { | ||
if (!this?.nbt) this.nbt = { name: '', type: 'compound', value: {} } | ||
this.nbt.value.RepairCost = { type: 'int', value } | ||
} | ||
get enchants () { | ||
if (Object.keys(this).length === 0) return null | ||
if (mcData.isOlderThan('1.13')) { | ||
let itemEnch | ||
if (this.name === 'enchanted_book' && this?.nbt?.value?.StoredEnchantments) { | ||
itemEnch = nbt.simplify(this.nbt).StoredEnchantments | ||
} else if (this?.nbt?.value?.ench) { | ||
itemEnch = nbt.simplify(this.nbt).ench | ||
} else { | ||
itemEnch = [] | ||
} | ||
return itemEnch.map(ench => ({ lvl: ench.lvl, name: mcData.enchantments[ench.id].name })) | ||
} else { | ||
let itemEnch = [] | ||
if (this?.nbt?.value?.Enchantments) { | ||
itemEnch = nbt.simplify(this.nbt).Enchantments | ||
} else if (this?.nbt?.value?.StoredEnchantments) { | ||
itemEnch = nbt.simplify(this.nbt).StoredEnchantments | ||
} else { | ||
itemEnch = [] | ||
} | ||
return itemEnch.map(ench => ({ lvl: ench.lvl, name: ench.id.replace(/minecraft:/, '') })) | ||
} | ||
} | ||
set enchants (normalizedEnchArray) { | ||
const isBook = this.name === 'enchanted_book' | ||
const postEnchantChange = mcData.isOlderThan('1.13') | ||
const enchListName = postEnchantChange ? 'ench' : 'Enchantments' | ||
const type = postEnchantChange ? 'short' : 'string' | ||
if (!this.nbt) this.nbt = { name: '', type: 'compound', value: {} } | ||
const enchs = normalizedEnchArray.map(({ name, lvl }) => { | ||
const value = postEnchantChange ? mcData.enchantmentsByName[name].id : `minecraft:${mcData.enchantmentsByName[name].name}` | ||
return { id: { type, value }, lvl: { type: 'short', value: lvl } } | ||
}) | ||
if (enchs.length !== 0) { | ||
this.nbt.value[isBook ? 'StoredEnchantments' : enchListName] = { type: 'list', value: { type: 'compound', value: enchs } } | ||
} | ||
if (mcData.isNewerOrEqualTo('1.13') && mcData.itemsByName[this.name].maxDurability) this.nbt.value.Damage = { type: 'int', value: 0 } | ||
} | ||
get durabilityUsed () { | ||
if (Object.keys(this).length === 0) return null | ||
if (mcData.isNewerOrEqualTo('1.13')) { | ||
return this?.nbt?.value?.Damage?.value ?? 0 | ||
} else { | ||
return this.metadata ?? 0 | ||
} | ||
} | ||
set durabilityUsed (value) { | ||
if (mcData.isNewerOrEqualTo('1.13')) { | ||
if (!this?.nbt) this.nbt = { name: '', type: 'compound', value: {} } | ||
this.nbt.value.Damage = { type: 'int', value } | ||
} else { | ||
this.metadata = value | ||
} | ||
} | ||
} | ||
Item.anvil = require('./lib/anvil.js')(mcData, Item) | ||
return Item | ||
} | ||
Item.fromNotch = function (item) { | ||
if (version === '1.13' || version === '1.14' || version === '1.15' || version === '1.16') { | ||
if (item.present === false) return null | ||
return new Item(item.itemId, item.itemCount, item.nbtData) | ||
} else { | ||
if (item.blockId === -1) return null | ||
return new Item(item.blockId, item.itemCount, item.itemDamage, item.nbtData) | ||
} | ||
} | ||
module.exports = loader |
{ | ||
"name": "prismarine-item", | ||
"version": "1.5.0", | ||
"version": "1.6.0", | ||
"description": "Represent a minecraft item with its associated data", | ||
@@ -8,5 +8,6 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "npm run lint", | ||
"lint": "standard", | ||
"fix": "standard --fix" | ||
"test": "jest --verbose", | ||
"pretest": "npm run lint", | ||
"fix": "standard --fix", | ||
"lint": "standard" | ||
}, | ||
@@ -18,8 +19,9 @@ "repository": { | ||
"peerDependencies": { | ||
"minecraft-data": "^2.35.0" | ||
"minecraft-data": "^2.79.0" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^14.0.1", | ||
"minecraft-data": "^2.35.0", | ||
"standard": "^14.3.4" | ||
"jest": "^26.1.0", | ||
"minecraft-data": "2.79.0", | ||
"standard": "^16.0.1" | ||
}, | ||
@@ -36,3 +38,6 @@ "keywords": [ | ||
}, | ||
"homepage": "https://github.com/PrismarineJS/prismarine-item#readme" | ||
"homepage": "https://github.com/PrismarineJS/prismarine-item#readme", | ||
"dependencies": { | ||
"prismarine-nbt": "^1.4.0" | ||
} | ||
} |
@@ -34,2 +34,6 @@ # prismarine-item | ||
### item.anvil(itemOne, itemTwo, creative[, newName]) | ||
Take two seperate `item` instances, and makes one item using the same combining done by the vanilla anvil | ||
#### item.type | ||
@@ -60,5 +64,26 @@ | ||
#### item.durabilityUsed | ||
A getter/setter for abstracting the underlying nbt | ||
#### item.customName | ||
A getter/setter for abstracting the underlying nbt | ||
#### item.enchants | ||
A getter/setter for abstracting the underlying nbt (does calculations) | ||
#### item.repairCost | ||
A getter/setter for abstracting the underlying nbt. | ||
See https://minecraft.gamepedia.com/Anvil_mechanics#Anvil_Uses | ||
## History | ||
## 1.6.0 | ||
* Item.anvil added, along with a ton of getters & setters for Item (thanks @u9g) | ||
### 1.5.0 | ||
@@ -65,0 +90,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
61519
10
927
127
2
4
1
+ Addedprismarine-nbt@^1.4.0
+ Addedajv@6.12.6(transitive)
+ Addedfast-deep-equal@3.1.3(transitive)
+ Addedfast-json-stable-stringify@2.1.0(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedjson-schema-traverse@0.4.1(transitive)
+ Addedlodash.get@4.4.2(transitive)
+ Addedlodash.reduce@4.6.0(transitive)
+ Addedprismarine-nbt@1.6.0(transitive)
+ Addedprotodef@1.15.0(transitive)
+ Addedprotodef-validator@1.3.1(transitive)
+ Addedpunycode@2.3.1(transitive)
+ Addedreadable-stream@3.6.2(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedstring_decoder@1.3.0(transitive)
+ Addeduri-js@4.4.1(transitive)
+ Addedutil-deprecate@1.0.2(transitive)