Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// TODO: could also use ping pre-connect to save description, type, max players, etc.
const motd = response.description
debug('Server description:', motd) // TODO: save
// Pass server-reported version to protocol handler
// The version string is interpreted by https://github.com/PrismarineJS/node-minecraft-data
const brandedMinecraftVersion = response.version.name // 1.8.9, 1.7.10
const protocolVersion = response.version.protocol// 47, 5
const versions = [brandedMinecraftVersion]
.concat(brandedMinecraftVersion.match(/((\d+\.)+\d+)/g) || [])
.map(function (version) {
return minecraftData.versionsByMinecraftVersion.pc[version]
})
.filter(function (info) { return info })
.sort(function (a, b) { return b.version - a.version })
.concat(minecraftData.postNettyVersionsByProtocolVersion.pc[protocolVersion] || [])
if (versions.length === 0) {
client.emit('error', new Error(`unsupported/unknown protocol version: ${protocolVersion}, update minecraft-data`))
}
const minecraftVersion = versions[0].minecraftVersion
debug(`Server version: ${minecraftVersion}, protocol: ${protocolVersion}`)
options.version = minecraftVersion
options.protocolVersion = protocolVersion
// Reinitialize client object with new version TODO: move out of its constructor?
client.version = minecraftVersion
client.state = states.HANDSHAKING
// Let other plugins such as Forge/FML (modinfo) respond to the ping response
if (client.autoVersionHooks) {
module.exports.entity = function (entity, serv, { version }) {
const blocks = require('minecraft-data')(version).blocks
entity.calculatePhysics = async (delta) => {
if (entity.gravity) {
addGravity(entity, 'x', delta)
addGravity(entity, 'y', delta)
addGravity(entity, 'z', delta)
}
const vSign = getSign(entity.velocity)
const sizeSigned = new Vec3(vSign.x * entity.size.x, vSign.y * entity.size.y, vSign.z * entity.size.z)
const xVec = entity.position.offset(entity.velocity.x * delta + sizeSigned.x / 2, 0, 0)
const yVec = entity.position.offset(0, entity.velocity.y * delta + sizeSigned.y / 2, 0)
const zVec = entity.position.offset(0, 0, entity.velocity.z * delta + sizeSigned.z / 2)
// Get block for each (x/y/z)Vec, check to avoid duplicate getBlockTypes
module.exports = {
//vec3: require('vec3'), // not really needed
createBot: createBot,
Block: require('mineflayer/lib/block'),
Location: require('mineflayer/lib/location'),
Biome: require('mineflayer/lib/biome'),
Entity: require('mineflayer/lib/entity'),
Painting: require('mineflayer/lib/painting'),
Item: require('mineflayer/lib/item'),
Recipe: require('mineflayer/lib/recipe'),
windows: require('mineflayer/lib/windows'),
Chest: require('mineflayer/lib/chest'),
Furnace: require('mineflayer/lib/furnace'),
Dispenser: require('mineflayer/lib/dispenser'),
EnchantmentTable: require('mineflayer/lib/enchantment_table'),
blocks: mcData.blocks,
biomes: mcData.biomes,
items: mcData.items,
recipes: mcData.recipes,
instruments: mcData.instruments,
materials: mcData.materials,
};
function createBot(options) {
options.username = options.username || 'Player';
if (!options.stream) throw new Error('createBot requires options.stream');
var bot = new Bot();
bot.connect(options);
return bot;
}
debug('ping response',response);
// TODO: could also use ping pre-connect to save description, type, max players, etc.
const motd = response.description;
debug('Server description:',motd); // TODO: save
// Pass server-reported version to protocol handler
// The version string is interpereted by https://github.com/PrismarineJS/node-minecraft-data
const minecraftVersion = response.version.name; // 1.8.9, 1.7.10
const protocolVersion = response.version.protocol;// 47, 5
debug(`Server version: ${minecraftVersion}, protocol: ${protocolVersion}`);
// Note that versionName is a descriptive version stirng like '1.8.9' on vailla, but other
// servers add their own name (Spigot 1.8.8, Glowstone++ 1.8.9) so we cannot use it directly,
// even though it is in a format accepted by minecraft-data. Instead, translate the protocol.
// TODO: pre-Netty version support (uses overlapping version numbers, so would have to check versionName)
const versionInfos = minecraft_data.postNettyVersionsByProtocolVersion[protocolVersion];
if (!versionInfos && versionInfos.length < 1) throw new Error(`unsupported/unknown protocol version: ${protocolVersion}, update minecraft-data`);
const versionInfo = versionInfos[0]; // use newest
options.version = versionInfo.minecraftVersion;
options.protocolVersion = protocolVersion;
// Reinitialize client object with new version TODO: move out of its constructor?
client.version = versionInfo.majorVersion;
client.state = states.HANDSHAKING;
if (response.modinfo && response.modinfo.type === 'FML') {
// Use the list of Forge mods from the server ping, so client will match server
const forgeMods = response.modinfo.modList;
debug('Using forgeMods:',forgeMods);
options.forgeMods = forgeMods;
forgeHandshake(client, options);
}
//vec3: require('vec3'), // not really needed
createBot: createBot,
Block: require('mineflayer/lib/block'),
Location: require('mineflayer/lib/location'),
Biome: require('mineflayer/lib/biome'),
Entity: require('mineflayer/lib/entity'),
Painting: require('mineflayer/lib/painting'),
Item: require('mineflayer/lib/item'),
Recipe: require('mineflayer/lib/recipe'),
windows: require('mineflayer/lib/windows'),
Chest: require('mineflayer/lib/chest'),
Furnace: require('mineflayer/lib/furnace'),
Dispenser: require('mineflayer/lib/dispenser'),
EnchantmentTable: require('mineflayer/lib/enchantment_table'),
blocks: mcData.blocks,
biomes: mcData.biomes,
items: mcData.items,
recipes: mcData.recipes,
instruments: mcData.instruments,
materials: mcData.materials,
};
function createBot(options) {
options.username = options.username || 'Player';
if (!options.stream) throw new Error('createBot requires options.stream');
var bot = new Bot();
bot.connect(options);
return bot;
}
function Bot() {
module.exports.server = function (serv, options) {
const version = options.version
const mobsById = require('minecraft-data')(version).mobs
const objectsById = require('minecraft-data')(version).objects
serv.initEntity = (type, entityType, world, position) => {
if (Object.keys(serv.entities).length > options['max-entities']) { throw new Error('Too many mobs !') }
serv.entityMaxId++
const entity = new Entity(serv.entityMaxId)
Object.keys(plugins)
.filter(pluginName => plugins[pluginName].entity !== undefined)
.forEach(pluginName => plugins[pluginName].entity(entity, serv, options))
entity.initEntity(type, entityType, world, position)
serv.emit('newEntity', entity)
return entity
module.exports.server = function (serv, options) {
const version = options.version
const mobsById = require('minecraft-data')(version).mobs
const objectsById = require('minecraft-data')(version).objects
serv.initEntity = (type, entityType, world, position) => {
if (Object.keys(serv.entities).length > options['max-entities']) { throw new Error('Too many mobs !') }
serv.entityMaxId++
const entity = new Entity(serv.entityMaxId)
Object.keys(plugins)
.filter(pluginName => plugins[pluginName].entity !== undefined)
.forEach(pluginName => plugins[pluginName].entity(entity, serv, options))
entity.initEntity(type, entityType, world, position)
serv.emit('newEntity', entity)
return entity
}
module.exports = function (version) {
var Block = require('./block')(version);
var Item = require('prismarine-item')(version);
var itemsByName = require('minecraft-data')(version).itemsByName;
function container(self, raw) {
if (raw.CustomName) self.customName = raw.CustomName.value;
self.lockKey = raw.Lock.value;
self.items = [];
raw.Items.value.value.forEach(function (item) {
var data = itemsByName[item.id.value.split(':')[1]];
self.items.push(new Item(data ? data.id : 1,
item.Count.value, item.Damage.value, item.tag ? item.tag.value : {}));
});
}
function BlockEntity(schem, raw) {
Block.apply(this, schem._blockArgs(vec3(raw.x.value, raw.y.value, raw.z.value)));
this.raw = raw;
function next () {
const version = require('minecraft-data')(self._client.version).version
if (supportedVersions.indexOf(version.majorVersion) === -1) {
throw new Error(`Version ${version.minecraftVersion} is not supported.`)
}
self.protocolVersion = version.version
self.majorVersion = version.majorVersion
self.version = version.minecraftVersion
options.version = version.minecraftVersion
self.emit('inject_allowed')
}
}
function generation ({ version }) {
const Chunk = require('prismarine-chunk')(version)
const blocks = require('minecraft-data')(version).blocks
function generateSimpleChunk () {
const chunk = new Chunk()
let i = 2
for (let x = 0; x < 16; x++) {
for (let z = 0; z < 16; z++) {
let y
for (y = 47; y <= 50; y++) {
chunk.setBlockType(new Vec3(x, y, z), i)
i = (i + 1) % Object.keys(blocks).length
}
for (y = 0; y < 256; y++) {
chunk.setSkyLight(new Vec3(x, y, z), 15)
}
}