2021-02-27 17:12:11 -05:00
|
|
|
/* global THREE, prompt */
|
2021-03-03 19:39:21 -05:00
|
|
|
const net = require('net')
|
2021-03-03 21:11:55 -05:00
|
|
|
const config = require('./config.json')
|
2021-02-27 17:12:11 -05:00
|
|
|
|
|
|
|
// 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-03-12 19:14:17 -05:00
|
|
|
const pathfinder = require('mineflayer-pathfinder')
|
2021-03-12 20:19:07 -05:00
|
|
|
const { Vec3 } = require('vec3')
|
2021-02-27 17:12:11 -05:00
|
|
|
global.THREE = require('three')
|
2021-03-01 23:06:51 -05:00
|
|
|
const Chat = require('./lib/chat')
|
2021-03-03 21:11:55 -05:00
|
|
|
|
2021-03-02 15:44:42 -05:00
|
|
|
let status = 'Waiting for user'
|
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-03-02 15:44:42 -05:00
|
|
|
async function statusRunner () {
|
|
|
|
const array = ['.', '..', '...', '']
|
|
|
|
// eslint-disable-next-line promise/param-names
|
|
|
|
const timer = ms => new Promise(res => setTimeout(res, ms))
|
|
|
|
|
|
|
|
async function load () {
|
|
|
|
for (let i = 0; true; i = ((i + 1) % array.length)) {
|
|
|
|
document.getElementById('loading-text').innerText = status + array[i]
|
|
|
|
await timer(500)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
load()
|
|
|
|
}
|
|
|
|
|
2021-03-07 18:15:33 -05:00
|
|
|
async function reloadHotbar (bot, viewer) {
|
2021-03-07 16:49:49 -05:00
|
|
|
console.log('Loading hotbar.')
|
|
|
|
for (let i = 0; i < 9; i++) {
|
|
|
|
// eslint-disable-next-line no-undef
|
|
|
|
const http = new XMLHttpRequest()
|
2021-03-07 18:10:37 -05:00
|
|
|
let url = bot.inventory.slots[bot.inventory.hotbarStart + i] ? window.location.href + 'textures/' + viewer.version + '/items/' + bot.inventory.slots[bot.inventory.hotbarStart + i].name + '.png' : ''
|
2021-03-07 16:49:49 -05:00
|
|
|
http.open('HEAD', url)
|
|
|
|
|
|
|
|
http.onreadystatechange = function () {
|
|
|
|
if (this.readyState === this.DONE) {
|
|
|
|
if (this.status === 404) {
|
2021-03-07 18:10:37 -05:00
|
|
|
url = bot.inventory.slots[bot.inventory.hotbarStart + i] ? window.location.href + 'textures/' + viewer.version + '/blocks/' + bot.inventory.slots[bot.inventory.hotbarStart + i].name + '.png' : ''
|
2021-03-07 16:49:49 -05:00
|
|
|
}
|
|
|
|
document.getElementById('hotbar-' + i).src = url
|
|
|
|
}
|
|
|
|
}
|
|
|
|
http.send()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function reloadHotbarSelected (bot, slot) {
|
|
|
|
console.log('Changing the selected hotbar slot to ' + slot.toString() + '!')
|
|
|
|
const planned = (20 * 4 * slot) + 'px'
|
|
|
|
document.getElementById('hotbar-highlight').style.marginLeft = planned
|
|
|
|
bot.setQuickBarSlot(slot)
|
|
|
|
console.log('Successfully changed to ' + planned + '!')
|
|
|
|
}
|
|
|
|
|
2021-02-27 17:12:11 -05:00
|
|
|
async function main () {
|
2021-03-02 15:44:42 -05:00
|
|
|
statusRunner()
|
2021-02-27 17:12:11 -05:00
|
|
|
const viewDistance = 6
|
2021-03-03 21:11:55 -05:00
|
|
|
const hostprompt = prompt('Host:port', config.defaultHost)
|
|
|
|
const proxyprompt = prompt('Proxy:port (blank for default)', config.defaultProxy)
|
2021-03-07 18:37:27 -05:00
|
|
|
const defaultUserName = window.localStorage.getItem('username') ?? 'pviewer' + (Math.floor(Math.random() * 1000))
|
|
|
|
const username = prompt('Username', defaultUserName)
|
|
|
|
window.localStorage.setItem('username', username)
|
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
|
2021-03-03 19:39:21 -05:00
|
|
|
|
|
|
|
let host, port, proxy, proxyport
|
|
|
|
if (!hostprompt.includes(':')) {
|
|
|
|
host = hostprompt
|
|
|
|
port = 25565
|
|
|
|
} else {
|
|
|
|
[host, port] = hostprompt.split(':')
|
|
|
|
port = parseInt(port, 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!proxyprompt.includes(':')) {
|
|
|
|
proxy = proxyprompt
|
|
|
|
proxyport = undefined
|
|
|
|
} else {
|
|
|
|
[proxy, proxyport] = proxyprompt.split(':')
|
|
|
|
proxyport = parseInt(proxyport, 10)
|
|
|
|
}
|
2021-02-27 17:12:11 -05:00
|
|
|
console.log(`connecting to ${host} ${port} with ${username}`)
|
|
|
|
|
2021-03-03 19:39:21 -05:00
|
|
|
if (proxy) {
|
|
|
|
console.log(`using proxy ${proxy} ${proxyport}`)
|
|
|
|
net.setProxy({ hostname: proxy, port: proxyport })
|
|
|
|
}
|
|
|
|
|
2021-03-02 15:44:42 -05:00
|
|
|
status = 'Logging in'
|
|
|
|
|
2021-02-27 17:12:11 -05:00
|
|
|
const bot = mineflayer.createBot({
|
|
|
|
host,
|
|
|
|
port,
|
|
|
|
username,
|
2021-03-12 20:35:07 -05:00
|
|
|
password,
|
|
|
|
checkTimeoutInterval: 120*1000
|
2021-02-27 17:12:11 -05:00
|
|
|
})
|
|
|
|
|
2021-03-07 13:32:41 -05:00
|
|
|
bot.on('error', (err) => {
|
|
|
|
console.log('Encountered error!', err)
|
2021-03-02 15:44:42 -05:00
|
|
|
status = 'Error encountered. Please reload the page'
|
|
|
|
})
|
|
|
|
|
2021-03-12 20:27:03 -05:00
|
|
|
bot.on('kicked', (kickReason) => {
|
|
|
|
console.log('User was kicked!', kickReason)
|
2021-03-02 15:44:42 -05:00
|
|
|
status = 'The Minecraft server kicked you. Please reload the page to rejoin'
|
|
|
|
})
|
|
|
|
|
2021-03-12 20:27:03 -05:00
|
|
|
bot.on('end', (endReason) => {
|
|
|
|
console.log('disconnected for', endReason)
|
2021-03-02 15:44:42 -05:00
|
|
|
status = 'You have been disconnected from the server. Please reload the page to rejoin'
|
|
|
|
})
|
2021-03-07 16:49:49 -05:00
|
|
|
|
2021-03-02 15:44:42 -05:00
|
|
|
bot.once('login', () => {
|
|
|
|
status = 'Loading world...'
|
2021-02-27 17:12:11 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
bot.once('spawn', () => {
|
2021-03-12 19:14:17 -05:00
|
|
|
const mcData = require('minecraft-data')(bot.version)
|
|
|
|
|
2021-03-07 16:49:49 -05:00
|
|
|
reloadHotbarSelected(bot, 0)
|
2021-03-02 15:44:42 -05:00
|
|
|
status = 'Placing blocks (starting viewer)...'
|
|
|
|
|
2021-02-27 17:12:11 -05:00
|
|
|
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-03-01 23:06:51 -05:00
|
|
|
const chat = Chat.init(bot._client, renderer)
|
2021-02-27 18:18:58 -05:00
|
|
|
|
2021-02-27 17:12:11 -05:00
|
|
|
// Create viewer
|
|
|
|
const viewer = new Viewer(renderer)
|
|
|
|
viewer.setVersion(version)
|
|
|
|
|
2021-03-12 19:14:17 -05:00
|
|
|
window.worldView = worldView
|
|
|
|
window.bot = bot
|
|
|
|
window.mcData = mcData
|
|
|
|
window.viewer = viewer
|
|
|
|
window.Vec3 = Vec3
|
|
|
|
window.pathfinder = pathfinder
|
|
|
|
|
2021-03-07 18:15:33 -05:00
|
|
|
reloadHotbar(bot, viewer)
|
|
|
|
|
2021-02-27 17:12:11 -05:00
|
|
|
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)
|
|
|
|
|
2021-03-02 15:44:42 -05:00
|
|
|
status = 'Setting callbacks...'
|
|
|
|
|
2021-02-27 17:12:11 -05:00
|
|
|
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 19:00:57 -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) => {
|
2021-03-01 23:06:51 -05:00
|
|
|
if (chat.inChat) return
|
2021-02-27 17:12:11 -05:00
|
|
|
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
|
|
|
}
|
2021-03-07 16:49:49 -05:00
|
|
|
if (e.code.startsWith('Digit')) {
|
|
|
|
const numPressed = e.code.substr(5)
|
|
|
|
console.log(numPressed)
|
|
|
|
reloadHotbarSelected(bot, numPressed - 1)
|
|
|
|
}
|
|
|
|
if (e.code === 'KeyQ') {
|
|
|
|
bot.tossStack(bot.heldItem)
|
|
|
|
}
|
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)) {
|
2021-03-01 23:06:51 -05:00
|
|
|
bot.dig(ButtonBlock, 'ignore')
|
2021-03-01 13:50:56 -05:00
|
|
|
}
|
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-03-02 15:44:42 -05:00
|
|
|
status = 'Done!'
|
|
|
|
console.log(status) // only do that because it's read in index.html and npm run fix complains.
|
|
|
|
|
|
|
|
setTimeout(function () {
|
|
|
|
// remove loading screen, wait a second to make sure a frame has properly rendered
|
|
|
|
document.querySelectorAll('.loader').forEach((item) => {
|
|
|
|
item.style = 'display: none;'
|
|
|
|
})
|
|
|
|
}, 2500)
|
|
|
|
|
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-03-07 16:41:29 -05:00
|
|
|
|
2021-03-07 16:49:49 -05:00
|
|
|
// inventory listener
|
|
|
|
bot.inventory.on('updateSlot', (slot, oldItem, newItem) => {
|
|
|
|
if (slot >= bot.inventory.hotbarStart + 9) return
|
|
|
|
if (slot < bot.inventory.hotbarStart) return
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-undef
|
|
|
|
const http = new XMLHttpRequest()
|
2021-03-07 18:10:37 -05:00
|
|
|
let url = newItem ? window.location.href + 'textures/' + viewer.version + '/items/' + newItem.name + '.png' : ''
|
2021-03-07 16:49:49 -05:00
|
|
|
http.open('HEAD', url)
|
|
|
|
|
|
|
|
http.onreadystatechange = function () {
|
|
|
|
if (this.readyState === this.DONE) {
|
|
|
|
if (this.status === 404) {
|
2021-03-07 18:10:37 -05:00
|
|
|
url = newItem ? window.location.href + 'textures/' + viewer.version + '/blocks/' + newItem.name + '.png' : ''
|
2021-03-07 16:49:49 -05:00
|
|
|
}
|
|
|
|
document.getElementById('hotbar-' + (slot - bot.inventory.hotbarStart)).src = url
|
|
|
|
}
|
|
|
|
}
|
|
|
|
http.send()
|
|
|
|
})
|
|
|
|
|
2021-03-07 16:41:29 -05:00
|
|
|
window.addEventListener('resize', () => {
|
|
|
|
viewer.camera.aspect = window.innerWidth / window.innerHeight
|
|
|
|
viewer.camera.updateProjectionMatrix()
|
|
|
|
renderer.setSize(window.innerWidth, window.innerHeight)
|
|
|
|
})
|
2021-02-27 17:12:11 -05:00
|
|
|
})
|
2021-02-27 16:27:59 -05:00
|
|
|
}
|
2021-02-27 17:12:11 -05:00
|
|
|
main()
|