Skip to content

Commit

Permalink
move item in scope of loader, remove all utility mcData vars with 1 (#12
Browse files Browse the repository at this point in the history
)

* move item in scope of loader, remove all utility mcData funcs with one mcData var

* lint

* es6-ify

* used isNewerOrEqualTo

* fixed usage of isneweror=to

* added tests

Co-authored-by: U9G <winworkswow@gmail.com>
  • Loading branch information
u9g and U9G committed Feb 24, 2021
1 parent c4e0f69 commit 4ff6026
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 70 deletions.
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.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
}
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.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)
}
}
return Item
}
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
"main": "index.js",
"types": "index.d.ts",
"scripts": {
"test": "npm run lint",
"lint": "standard",
"fix": "standard --fix"
"test": "jest --verbose",
"pretest": "npm run lint",
"fix": "standard --fix",
"lint": "standard"
},
"repository": {
"type": "git",
Expand All @@ -19,7 +20,8 @@
"devDependencies": {
"@types/node": "^14.0.1",
"minecraft-data": "^2.35.0",
"standard": "^16.0.1"
"standard": "^16.0.1",
"jest": "^26.1.0"
},
"keywords": [
"minecraft",
Expand Down
45 changes: 45 additions & 0 deletions test/basic.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* eslint-env jest */

describe('test based on examples', () => {
describe('1.8 iron shovel', () => {
const Item = require('../')('1.8')
const ironShovelItem = new Item(256, 1)

test('constructor makes item correctly', () => {
const val = { type: 256, count: 1, metadata: 0, nbt: null, name: 'iron_shovel', displayName: 'Iron Shovel', stackSize: 1 }
expect(JSON.parse(JSON.stringify(ironShovelItem))).toStrictEqual(val)
})

test('use .toNotch', () => {
expect(Item.toNotch(ironShovelItem)).toStrictEqual({ blockId: 256, itemCount: 1, itemDamage: 0 })
})

test('use .fromNotch', () => {
const toNotch = Item.toNotch(ironShovelItem)
const fromNotch = Item.fromNotch(toNotch)
const expectedObj = { count: 1, displayName: 'Iron Shovel', metadata: 0, name: 'iron_shovel', nbt: null, stackSize: 1, type: 256 }
expect(JSON.parse(JSON.stringify(fromNotch))).toStrictEqual(expectedObj)
})
})
describe('1.13.2 iron shovel', () => {
const Item = require('../')('1.13.2')
const ironShovelItem = new Item(472, 1)

test('constructor makes item correctly', () => {
const expectedObj = { count: 1, displayName: 'Iron Shovel', metadata: 0, name: 'iron_shovel', nbt: null, stackSize: 64, type: 472 }
expect(JSON.parse(JSON.stringify(ironShovelItem))).toStrictEqual(expectedObj)
})

test('use .toNotch', () => {
const expectedObj = { itemCount: 1, itemId: 472, present: true }
expect(Item.toNotch(ironShovelItem)).toStrictEqual(expectedObj)
})

test('use .fromNotch', () => {
const toNotch = Item.toNotch(ironShovelItem)
const fromNotch = Item.fromNotch(toNotch)
const expectedObj = { count: 1, displayName: 'Iron Shovel', metadata: 0, name: 'iron_shovel', nbt: null, stackSize: 64, type: 472 }
expect(JSON.parse(JSON.stringify(fromNotch))).toStrictEqual(expectedObj)
})
})
})

0 comments on commit 4ff6026

Please sign in to comment.