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

move item in scope of loader, remove all utility mcData vars with 1 #12

Merged
merged 6 commits into from
Feb 24, 2021
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
127 changes: 61 additions & 66 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,86 +2,81 @@ module.exports = loader

function loader (mcVersion) {
const mcData = require('minecraft-data')(mcVersion)
findItemOrBlockById = mcData.findItemOrBlockById
version = mcData.version.majorVersion
return Item
}

let findItemOrBlockById
let version
function Item (type, count, metadata, nbt) {
if (type == null) return

function Item (type, count, metadata, nbt) {
if (type == null) return

if (metadata instanceof Object && metadata !== null) {
nbt = metadata
metadata = 0
}
if (metadata instanceof Object && metadata !== null) {
nbt = metadata
metadata = 0
}

this.type = type
this.count = count
this.metadata = metadata == null ? 0 : metadata
this.nbt = nbt || null
this.type = type
this.count = count
this.metadata = metadata == null ? 0 : metadata
this.nbt = nbt || null

const itemEnum = 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 }
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
}
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 &&
Item.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
}
}
}

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
}
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
Item.toNotch = (item) => {
if (mcData.version.majorVersion === '1.13' || mcData.version.majorVersion === '1.14' || mcData.version.majorVersion === '1.15' || mcData.version.majorVersion === '1.16') {
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
}
if (item.nbt && item.nbt.length !== 0) { notchItem.nbtData = item.nbt }
return notchItem
}
}

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)
Item.fromNotch = (item) => {
if (mcData.version.majorVersion === '1.13' || mcData.version.majorVersion === '1.14' || mcData.version.majorVersion === '1.15' || mcData.version.majorVersion === '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)
}
}
return Item
}