Skip to content

Commit

Permalink
mcData to registry refactoring (final step, at least for mineflayer) (P…
Browse files Browse the repository at this point in the history
…rismarineJS#2795)

* refactoring

* version args to registry refactoring

* lint fix

* undo registry arg on pwindow (not supported yet)
  • Loading branch information
Epirito authored Oct 12, 2022
1 parent 917c094 commit 6fc8a9a
Show file tree
Hide file tree
Showing 41 changed files with 73 additions and 85 deletions.
2 changes: 1 addition & 1 deletion docs/ru/FAQ_RU.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function getLore (item) {
if (item.nbt == null) return message

const nbt = require('prismarine-nbt')
const ChatMessage = require('prismarine-chat')(bot.version)
const ChatMessage = require('prismarine-chat')(bot.registry)

const data = nbt.simplify(item.nbt)
const display = data.display
Expand Down
3 changes: 1 addition & 2 deletions examples/guard.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ function stopGuarding () {

// Pathfinder to the guard position
function moveToGuardPos () {
const mcData = require('minecraft-data')(bot.version)
bot.pathfinder.setMovements(new Movements(bot, mcData))
bot.pathfinder.setMovements(new Movements(bot))
bot.pathfinder.setGoal(new goals.GoalBlock(guardPos.x, guardPos.y, guardPos.z))
}

Expand Down
3 changes: 1 addition & 2 deletions examples/pathfinder/gps.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ const RANGE_GOAL = 1 // get within this radius of the player
bot.loadPlugin(pathfinder)

bot.once('spawn', () => {
const mcData = require('minecraft-data')(bot.version)
const defaultMove = new Movements(bot, mcData)
const defaultMove = new Movements(bot)

bot.on('chat', (username, message) => {
if (username === bot.username) return
Expand Down
3 changes: 1 addition & 2 deletions examples/python/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
@On(bot, 'spawn')
def handle(*args):
print("I spawned 👋")
mcData = require('minecraft-data')(bot.version)
movements = pathfinder.Movements(bot, mcData)
movements = pathfinder.Movements(bot)

@On(bot, 'chat')
def handleMsg(this, sender, message, *args):
Expand Down
2 changes: 1 addition & 1 deletion examples/python/chatterbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"port": port
})

Item = require("prismarine-item")(bot.version)
Item = require("prismarine-item")(bot.registry)


@On(bot, "chat")
Expand Down
4 changes: 2 additions & 2 deletions lib/bossbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const divisions = [0, 6, 10, 12, 20]

module.exports = loader

function loader (mcVersion) {
ChatMessage = require('prismarine-chat')(mcVersion) // TODO: update for prismarine-registry
function loader (registry) {
ChatMessage = require('prismarine-chat')(registry)
return BossBar
}

Expand Down
4 changes: 2 additions & 2 deletions lib/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ module.exports = {
BossBar: require('./bossbar'),
supportedVersions,
testedVersions,
supportFeature: (feature, version) => require('minecraft-data')(version).supportFeature(feature)
supportFeature: (feature, version) => require('prismarine-registry')(version).supportFeature(feature)
}

function createBot (options = {}) {
Expand Down Expand Up @@ -111,7 +111,7 @@ function createBot (options = {}) {
}

const latestTestedVersion = testedVersions[testedVersions.length - 1]
const latestProtocolVersion = require('minecraft-data')(latestTestedVersion).protocolVersion
const latestProtocolVersion = require('prismarine-registry')(latestTestedVersion).protocolVersion
if (version.protocolVersion > latestProtocolVersion) {
throw new Error(`Version ${version.minecraftVersion} is not supported. Latest supported version is ${latestTestedVersion}.`)
}
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/anvil.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { once } = require('events')
module.exports = inject

function inject (bot) {
const Item = require('prismarine-item')(bot.version)
const Item = require('prismarine-item')(bot.registry)

const matchWindowType = window => /minecraft:(?:chipped_|damaged_)?anvil/.test(window.type)

Expand Down
6 changes: 3 additions & 3 deletions lib/plugins/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ const dimensionNames = {
}

function inject (bot, { version, storageBuilder }) {
const Block = require('prismarine-block')(version)
const Chunk = require('prismarine-chunk')(version)
const World = require('prismarine-world')(version)
const Block = require('prismarine-block')(bot.registry)
const Chunk = require('prismarine-chunk')(bot.registry)
const World = require('prismarine-world')(bot.registry)
const paintingsByPos = {}
const paintingsById = {}

Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/book.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { once } = require('events')
module.exports = inject

function inject (bot) {
const Item = require('prismarine-item')(bot.version)
const Item = require('prismarine-item')(bot.registry)

let editBook
if (bot.supportFeature('editBookIsPluginChannel')) {
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/boss_bar.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = inject

function inject (bot, { version }) {
const BossBar = require('../bossbar')(version)
const BossBar = require('../bossbar')(bot.registry)
const bars = {}

bot._client.on('boss_bar', (packet) => {
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function inject (bot, options) {
const CHAT_LENGTH_LIMIT = options.chatLengthLimit ?? (bot.supportFeature('lessCharsInChat') ? 100 : 256)
const defaultChatPatterns = options.defaultChatPatterns ?? true

const ChatMessage = require('prismarine-chat')(bot.version)
const ChatMessage = require('prismarine-chat')(bot.registry)
// chat.pattern.type will emit an event for bot.on() of the same type, eg chatType = whisper will trigger bot.on('whisper')
const _patterns = {}
let _length = 0
Expand Down
6 changes: 3 additions & 3 deletions lib/plugins/craft.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ const { once } = require('events')

module.exports = inject

function inject (bot, { version }) {
const Item = require('prismarine-item')(bot.version)
const Recipe = require('prismarine-recipe')(version).Recipe // TODO: update for prismarine-registry
function inject (bot) {
const Item = require('prismarine-item')(bot.registry)
const Recipe = require('prismarine-recipe')(bot.registry).Recipe

async function craft (recipe, count, craftingTable) {
assert.ok(recipe)
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/creative.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { once } = require('events')
module.exports = inject

function inject (bot) {
const Item = require('prismarine-item')(bot.version)
const Item = require('prismarine-item')(bot.registry)

// these features only work when you are in creative mode.
bot.creative = {
Expand Down
6 changes: 3 additions & 3 deletions lib/plugins/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ const entityStatusEvents = {

function inject (bot) {
const { objects, mobs, entitiesArray } = bot.registry
const Entity = require('prismarine-entity')(bot.version) // TODO: update for prismarine-registry
const Item = require('prismarine-item')(bot.version)
const ChatMessage = require('prismarine-chat')(bot.version) // TODO: update for prismarine-registry
const Entity = require('prismarine-entity')(bot.registry)
const Item = require('prismarine-item')(bot.registry)
const ChatMessage = require('prismarine-chat')(bot.registry)

// ONLY 1.17 has this destroy_entity packet which is the same thing as entity_destroy packet except the entity is singular
// 1.17.1 reverted this change so this is just a simpler fix
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/generic_place.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const assert = require('assert')
module.exports = inject

function inject (bot) {
const Item = require('prismarine-item')(bot.version)
const Item = require('prismarine-item')(bot.registry)
/**
*
* @param {import('prismarine-block').Block} referenceBlock
Expand Down
4 changes: 2 additions & 2 deletions lib/plugins/inventory.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ const ALWAYS_CONSUMABLES = [
]

function inject (bot, { hideErrors }) {
const Item = require('prismarine-item')(bot.version)
const windows = require('prismarine-windows')(bot.version) // TODO: update for prismarine-registry
const Item = require('prismarine-item')(bot.registry)
const windows = require('prismarine-windows')(bot.version)

let eatingTask = createDoneTask()

Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/place_entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const assert = require('assert')
module.exports = inject

function inject (bot) {
const Item = require('prismarine-item')(bot.version)
const Item = require('prismarine-item')(bot.registry)

/**
*
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/tablist.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const escapeValueNewlines = str => {
}

function inject (bot) {
const ChatMessage = require('prismarine-chat')(bot.version) // TODO: update for prismarine-registry
const ChatMessage = require('prismarine-chat')(bot.registry)

bot.tablist = {
header: new ChatMessage(''),
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/team.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = inject

function inject (bot) {
const Team = require('../team')(bot.version)
const Team = require('../team')(bot.registry)
const teams = {}

function teamHandler (packet) {
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/villager.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = inject

function inject (bot, { version }) {
const { entitiesByName } = bot.registry
const Item = require('prismarine-item')(bot.version)
const Item = require('prismarine-item')(bot.registry)

let selectTrade
if (bot.supportFeature('useMCTrSel')) {
Expand Down
2 changes: 1 addition & 1 deletion lib/scoreboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const sortItems = (a, b) => {
}

module.exports = (bot) => {
const ChatMessage = require('prismarine-chat')(bot.version) // TODO: update for prismarine-registry
const ChatMessage = require('prismarine-chat')(bot.registry)

class ScoreBoard {
constructor (packet) {
Expand Down
4 changes: 2 additions & 2 deletions lib/team.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ let MessageBuilder

module.exports = loader

function loader (version) {
ChatMessage = require('prismarine-chat')(version) // TODO: update for prismarine-registry
function loader (registry) {
ChatMessage = require('prismarine-chat')(registry) // TODO: update for prismarine-registry
MessageBuilder = ChatMessage.MessageBuilder
return Team
}
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
},
"license": "MIT",
"dependencies": {
"minecraft-data": "^3.0.0",
"minecraft-protocol": "^1.31.0",
"prismarine-biome": "^1.1.1",
"prismarine-block": "^1.13.1",
Expand Down
4 changes: 2 additions & 2 deletions test/externalTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ const MC_SERVER_PATH = path.join(__dirname, 'server')

for (const supportedVersion of mineflayer.testedVersions) {
let PORT = 25565
const mcData = require('minecraft-data')(supportedVersion)
const version = mcData.version
const registry = require('prismarine-registry')(supportedVersion)
const version = registry.version
const MC_SERVER_JAR_DIR = process.env.MC_SERVER_JAR_DIR || `${process.cwd()}/server_jars`
const MC_SERVER_JAR = `${MC_SERVER_JAR_DIR}/minecraft_server.${version.minecraftVersion}.jar`
const wrap = new Wrap(MC_SERVER_JAR, `${MC_SERVER_PATH}_${supportedVersion}`)
Expand Down
2 changes: 1 addition & 1 deletion test/externalTests/anvil.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { once } = require('events')

module.exports = () => {
async function runTest (bot, testFunction) {
const Item = require('prismarine-item')(bot.version)
const Item = require('prismarine-item')(bot.registry)
const renameCost = () => bot.registry.isNewerOrEqualTo('1.8.9') ? 0 : 1 // weird quirk of anvils
const renameName = (name) => bot.registry.isOlderThan('1.13.2') ? name : JSON.stringify({ text: name }) // weird quirk of anvils
await bot.test.becomeCreative()
Expand Down
4 changes: 1 addition & 3 deletions test/externalTests/bed.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ const assert = require('assert')
const { once } = require('events')

module.exports = () => async (bot) => {
const mcData = require('minecraft-data')(bot.version)

const midnight = 18000
const bedItem = mcData.itemsArray.find(item => item.name.endsWith('bed'))
const bedItem = bot.registry.itemsArray.find(item => item.name.endsWith('bed'))
const bedPos1 = bot.entity.position.offset(2, 0, 0).floored()
const bedPos2 = bedPos1.offset(0, 0, 1)

Expand Down
2 changes: 1 addition & 1 deletion test/externalTests/book.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const assert = require('assert')

module.exports = () => async (bot) => {
const Item = require('prismarine-item')(bot.version)
const Item = require('prismarine-item')(bot.registry)

const pages = [
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
Expand Down
5 changes: 2 additions & 3 deletions test/externalTests/consume.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const assert = require('assert')

module.exports = () => async (bot) => {
const Item = require('prismarine-item')(bot.version)
const mcData = require('minecraft-data')(bot.version)
const Item = require('prismarine-item')(bot.registry)

await bot.test.setInventorySlot(36, new Item(mcData.itemsByName.bread.id, 5, 0))
await bot.test.setInventorySlot(36, new Item(bot.registry.itemsByName.bread.id, 5, 0))
await bot.test.becomeSurvival()
// Cannot consume if bot.food === 20
await assert.rejects(bot.consume, (err) => {
Expand Down
2 changes: 1 addition & 1 deletion test/externalTests/crafting.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { Vec3 } = require('vec3')

module.exports = () => async (bot) => {
const { blocksByName, itemsByName } = bot.registry
const Item = require('prismarine-item')(bot.version)
const Item = require('prismarine-item')(bot.registry)

let populateBlockInventory
let craftItem
Expand Down
2 changes: 1 addition & 1 deletion test/externalTests/creative.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const wait = require('util').promisify(setTimeout)
const SLOT = 36

module.exports = () => async (bot) => {
const Item = require('prismarine-item')(bot.version)
const Item = require('prismarine-item')(bot.registry)

const item1 = new Item(1, 1, 0)
const item2 = new Item(2, 1, 0)
Expand Down
7 changes: 3 additions & 4 deletions test/externalTests/digAndBuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ const { Vec3 } = require('vec3')
const assert = require('assert')

module.exports = () => async (bot) => {
const mcData = require('minecraft-data')(bot.version)
const Item = require('prismarine-item')(bot.version)
const Item = require('prismarine-item')(bot.registry)

await bot.test.setInventorySlot(36, new Item(mcData.itemsByName.dirt.id, 1, 0))
await bot.test.setInventorySlot(36, new Item(bot.registry.itemsByName.dirt.id, 1, 0))
await bot.test.fly(new Vec3(0, 2, 0))
await bot.test.placeBlock(36, bot.entity.position.plus(new Vec3(0, -2, 0)))
await bot.test.clearInventory()
Expand All @@ -16,7 +15,7 @@ module.exports = () => async (bot) => {
await bot.dig(bot.blockAt(bot.entity.position.plus(new Vec3(0, -1, 0))))
// make sure we collected das dirt
await bot.test.wait(1000)
assert(Item.equal(bot.inventory.slots[36], new Item(mcData.itemsByName.dirt.id, 1, 0)))
assert(Item.equal(bot.inventory.slots[36], new Item(bot.registry.itemsByName.dirt.id, 1, 0)))
bot.test.sayEverywhere('dirt collect test: pass')

async function waitForFall () {
Expand Down
10 changes: 5 additions & 5 deletions test/externalTests/digEverything.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ const excludedBlocks = [
]

module.exports = (version) => {
const mcData = require('minecraft-data')(version)
const registry = require('prismarine-registry')(version)

const funcs = {}
for (const id in mcData.blocks) {
if (mcData.blocks[id] !== undefined) {
const block = mcData.blocks[id]
for (const id in registry.blocks) {
if (registry.blocks[id] !== undefined) {
const block = registry.blocks[id]
if (block.diggable && excludedBlocks.indexOf(block.name) === -1) {
funcs[block.name] = (blockId => async (bot) => {
await digSomething(blockId, bot)
Expand All @@ -66,7 +66,7 @@ module.exports = (version) => {
}

async function digSomething (blockId, bot) {
const Item = require('prismarine-item')(bot.version)
const Item = require('prismarine-item')(bot.registry)

await bot.test.setInventorySlot(36, new Item(blockId, 1, 0))
await bot.test.placeBlock(36, bot.entity.position.plus(new Vec3(1, 0, 0)))
Expand Down
2 changes: 1 addition & 1 deletion test/externalTests/enchanting.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const assert = require('assert')

module.exports = () => async (bot) => {
const Item = require('prismarine-item')(bot.version)
const Item = require('prismarine-item')(bot.registry)

const lapisId = bot.registry.itemsByName.lapis_lazuli ? bot.registry.itemsByName.lapis_lazuli.id : bot.registry.itemsByName.dye.id
const lapisData = bot.registry.itemsByName.lapis_lazuli ? 0 : 4
Expand Down
3 changes: 1 addition & 2 deletions test/externalTests/exampleInventory.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ const tests = [
}
]
module.exports = () => async (bot) => {
const mcData = require('minecraft-data')(bot.version)
await bot.test.runExample('examples/inventory.js', async (name) => {
assert.strictEqual(name, 'inventory')
bot.chat('/op inventory') // to counteract spawn protection
Expand All @@ -58,7 +57,7 @@ module.exports = () => async (bot) => {
bot.chat('/give inventory iron_ore 64')
bot.chat('/give inventory diamond_boots 1')
await bot.test.wait(2000)
if (mcData.isOlderThan('1.9')) {
if (bot.registry.isOlderThan('1.9')) {
tests.splice(tests.indexOf(tests.find(t => t.command.includes('off-hand'))), 2) // Delete off-hand command and the command after it as they don't work in 1.9
}
const testFuncs = tests.map(test => makeTest(test.command, test.wantedMessage))
Expand Down
2 changes: 1 addition & 1 deletion test/externalTests/fishing.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = () => async (bot) => {
const Item = require('prismarine-item')(bot.version)
const Item = require('prismarine-item')(bot.registry)

bot.test.sayEverywhere('/fill ~-5 ~-1 ~-5 ~5 ~-1 ~5 water')
await bot.test.setInventorySlot(36, new Item(bot.registry.itemsByName.fishing_rod.id, 1, 0))
Expand Down
Loading

0 comments on commit 6fc8a9a

Please sign in to comment.