2021-02-27 17:12:11 -05:00
|
|
|
/* global THREE, prompt */
|
|
|
|
|
|
|
|
// Workaround for process.versions.node not existing in the browser
|
|
|
|
process.versions.node = '14.0.0'
|
|
|
|
|
|
|
|
const mineflayer = require('mineflayer')
|
|
|
|
const { WorldView, Viewer } = require('prismarine-viewer/viewer')
|
2021-02-28 21:32:13 -05:00
|
|
|
const Vec3 = require('vec3').Vec3
|
2021-02-27 17:12:11 -05:00
|
|
|
global.THREE = require('three')
|
2021-02-27 18:18:58 -05:00
|
|
|
const chat = require('./lib/chat')
|
2021-02-27 17:12:11 -05:00
|
|
|
|
2021-02-28 14:02:22 -05:00
|
|
|
const maxPitch = 0.5 * Math.PI
|
|
|
|
const minPitch = -0.5 * Math.PI
|
|
|
|
|
2021-02-27 17:12:11 -05:00
|
|
|
async function main () {
|
|
|
|
const viewDistance = 6
|
|
|
|
const host = prompt('Host', '95.111.249.143')
|
|
|
|
const port = parseInt(prompt('Port', '10000'))
|
2021-03-01 18:43:08 -05:00
|
|
|
const username = prompt('Username', 'pviewer_person' + (Math.floor(Math.random() * 1000)))
|
2021-03-01 17:49:31 -05:00
|
|
|
let password = prompt('Password (blank for offline)', '')
|
2021-02-27 17:12:11 -05:00
|
|
|
password = password === '' ? undefined : password
|
|
|
|
console.log(`connecting to ${host} ${port} with ${username}`)
|
|
|
|
|
|
|
|
const bot = mineflayer.createBot({
|
|
|
|
host,
|
|
|
|
port,
|
|
|
|
username,
|
|
|
|
password
|
|
|
|
})
|
|
|
|
|
|
|
|
bot.on('end', () => {
|
|
|
|
console.log('disconnected')
|
|
|
|
})
|
|
|
|
|
|
|
|
bot.once('spawn', () => {
|
|
|
|
console.log('bot spawned - starting viewer')
|
|
|
|
|
|
|
|
const version = bot.version
|
|
|
|
|
|
|
|
const center = bot.entity.position
|
|
|
|
|
|
|
|
const worldView = new WorldView(bot.world, viewDistance, center)
|
|
|
|
|
|
|
|
// Create three.js context, add to page
|
|
|
|
const renderer = new THREE.WebGLRenderer()
|
|
|
|
renderer.setPixelRatio(window.devicePixelRatio || 1)
|
|
|
|
renderer.setSize(window.innerWidth, window.innerHeight)
|
|
|
|
document.body.appendChild(renderer.domElement)
|
|
|
|
|
2021-02-27 18:18:58 -05:00
|
|
|
chat.init(undefined, bot._client, renderer)
|
|
|
|
|
2021-02-27 17:12:11 -05:00
|
|
|
// Create viewer
|
|
|
|
const viewer = new Viewer(renderer)
|
|
|
|
viewer.setVersion(version)
|
|
|
|
|
|
|
|
worldView.listenToBot(bot)
|
|
|
|
worldView.init(bot.entity.position)
|
|
|
|
|
|
|
|
function botPosition () {
|
|
|
|
viewer.setFirstPersonCamera(bot.entity.position, bot.entity.yaw, bot.entity.pitch)
|
|
|
|
worldView.updatePosition(bot.entity.position)
|
|
|
|
}
|
|
|
|
|
|
|
|
bot.on('move', botPosition)
|
|
|
|
|
|
|
|
// Link WorldView and Viewer
|
|
|
|
viewer.listen(worldView)
|
|
|
|
viewer.camera.position.set(center.x, center.y, center.z)
|
|
|
|
|
|
|
|
function moveCallback (e) {
|
|
|
|
bot.entity.pitch -= e.movementY * 0.01
|
2021-02-28 14:02:22 -05:00
|
|
|
bot.entity.pitch = Math.max(minPitch, Math.min(maxPitch, bot.entity.pitch))
|
2021-02-27 17:12:11 -05:00
|
|
|
bot.entity.yaw -= e.movementX * 0.01
|
2021-02-28 14:02:22 -05:00
|
|
|
|
2021-03-01 18:44:41 -05:00
|
|
|
viewer.setFirstPersonCamera(null, bot.entity.yaw, bot.entity.pitch)
|
2021-02-27 17:12:11 -05:00
|
|
|
}
|
2021-03-01 18:44:41 -05:00
|
|
|
|
2021-02-27 17:12:11 -05:00
|
|
|
function changeCallback () {
|
|
|
|
if (document.pointerLockElement === renderer.domElement ||
|
|
|
|
document.mozPointerLockElement === renderer.domElement ||
|
|
|
|
document.webkitPointerLockElement === renderer.domElement) {
|
|
|
|
document.addEventListener('mousemove', moveCallback, false)
|
|
|
|
} else {
|
|
|
|
document.removeEventListener('mousemove', moveCallback, false)
|
|
|
|
}
|
|
|
|
}
|
2021-03-01 18:44:41 -05:00
|
|
|
|
2021-02-27 17:12:11 -05:00
|
|
|
document.addEventListener('pointerlockchange', changeCallback, false)
|
|
|
|
document.addEventListener('mozpointerlockchange', changeCallback, false)
|
|
|
|
document.addEventListener('webkitpointerlockchange', changeCallback, false)
|
2021-03-01 18:44:41 -05:00
|
|
|
|
2021-02-27 17:12:11 -05:00
|
|
|
renderer.domElement.requestPointerLock = renderer.domElement.requestPointerLock ||
|
|
|
|
renderer.domElement.mozRequestPointerLock ||
|
|
|
|
renderer.domElement.webkitRequestPointerLock
|
|
|
|
document.addEventListener('mousedown', (e) => {
|
|
|
|
renderer.domElement.requestPointerLock()
|
|
|
|
})
|
|
|
|
|
|
|
|
document.addEventListener('contextmenu', (e) => e.preventDefault(), false)
|
2021-03-01 03:41:12 -05:00
|
|
|
|
|
|
|
const codes = {
|
|
|
|
KeyW: 'forward',
|
|
|
|
KeyS: 'back',
|
|
|
|
KeyA: 'right',
|
|
|
|
KeyD: 'left',
|
|
|
|
Space: 'jump',
|
|
|
|
ShiftLeft: 'sneak',
|
|
|
|
ControlLeft: 'sprint'
|
|
|
|
}
|
|
|
|
|
2021-02-27 17:12:11 -05:00
|
|
|
document.addEventListener('keydown', (e) => {
|
|
|
|
console.log(e.code)
|
2021-03-01 03:41:12 -05:00
|
|
|
|
|
|
|
if (e.code in codes) {
|
|
|
|
bot.setControlState(codes[e.code], true)
|
2021-02-27 17:12:11 -05:00
|
|
|
}
|
|
|
|
}, false)
|
2021-03-01 03:41:12 -05:00
|
|
|
|
2021-02-27 17:12:11 -05:00
|
|
|
document.addEventListener('keyup', (e) => {
|
2021-03-01 03:41:12 -05:00
|
|
|
if (e.code in codes) {
|
|
|
|
bot.setControlState(codes[e.code], false)
|
2021-02-27 17:12:11 -05:00
|
|
|
}
|
|
|
|
}, false)
|
2021-03-01 18:44:41 -05:00
|
|
|
|
2021-02-28 21:32:13 -05:00
|
|
|
document.addEventListener('mousedown', (e) => {
|
2021-03-01 13:50:56 -05:00
|
|
|
const ButtonBlock = bot.blockAtCursor()
|
2021-02-28 21:32:13 -05:00
|
|
|
if (!ButtonBlock) return
|
|
|
|
if (e.button === 0) {
|
2021-03-01 13:50:56 -05:00
|
|
|
if (bot.canDigBlock(ButtonBlock)) {
|
|
|
|
bot.dig(ButtonBlock)
|
|
|
|
}
|
2021-02-28 21:32:13 -05:00
|
|
|
} else if (e.button === 2) {
|
|
|
|
const vecArray = [new Vec3(0, -1, 0), new Vec3(0, 1, 0), new Vec3(0, 0, -1), new Vec3(0, 0, 1), new Vec3(-1, 0, 0), new Vec3(1, 0, 0)]
|
|
|
|
const vec = vecArray[ButtonBlock.face]
|
|
|
|
|
|
|
|
bot.placeBlock(ButtonBlock, vec)
|
|
|
|
}
|
|
|
|
}, false)
|
|
|
|
|
|
|
|
document.addEventListener('mouseup', (e) => {
|
|
|
|
bot.stopDigging()
|
|
|
|
}, false)
|
2021-02-27 16:27:59 -05:00
|
|
|
|
2021-02-27 17:12:11 -05:00
|
|
|
// Browser animation loop
|
|
|
|
const animate = () => {
|
|
|
|
window.requestAnimationFrame(animate)
|
2021-03-01 18:44:41 -05:00
|
|
|
viewer.update()
|
2021-02-27 17:12:11 -05:00
|
|
|
renderer.render(viewer.scene, viewer.camera)
|
|
|
|
}
|
|
|
|
animate()
|
|
|
|
})
|
2021-02-27 16:27:59 -05:00
|
|
|
}
|
2021-02-27 17:12:11 -05:00
|
|
|
main()
|