Skip to content

Commit

Permalink
add code
Browse files Browse the repository at this point in the history
  • Loading branch information
rom1504 committed Feb 27, 2021
1 parent d186c09 commit d978f5c
Show file tree
Hide file tree
Showing 11 changed files with 434 additions and 56 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
package-lock.json
.vscode
.vscode
public
38 changes: 24 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
# prismarine-template
[![NPM version](https://img.shields.io/npm/v/prismarine-template.svg)](http://npmjs.com/package/prismarine-template)
[![Build Status](https://github.com/PrismarineJS/prismarine-template/workflows/CI/badge.svg)](https://github.com/PrismarineJS/prismarine-template/actions?query=workflow%3A%22CI%22)
# prismarine-web-client
[![NPM version](https://img.shields.io/npm/v/prismarine-web-client.svg)](http://npmjs.com/package/prismarine-web-client)
[![Build Status](https://github.com/PrismarineJS/prismarine-web-client/workflows/CI/badge.svg)](https://github.com/PrismarineJS/prismarine-web-client/actions?query=workflow%3A%22CI%22)
[![Discord](https://img.shields.io/badge/chat-on%20discord-brightgreen.svg)](https://discord.gg/GsEFRM8)
[![Gitter](https://img.shields.io/badge/chat-on%20gitter-brightgreen.svg)](https://gitter.im/PrismarineJS/general)
[![Irc](https://img.shields.io/badge/chat-on%20irc-brightgreen.svg)](https://irc.gitter.im/)
[![Try it on gitpod](https://img.shields.io/badge/try-on%20gitpod-brightgreen.svg)](https://gitpod.io/#https://github.com/PrismarineJS/prismarine-template)
[![Try it on gitpod](https://img.shields.io/badge/try-on%20gitpod-brightgreen.svg)](https://gitpod.io/#https://github.com/PrismarineJS/prismarine-web-client)

A template repository to make it easy to create new prismarine repo
A minecraft client running in a web page.

## Usage
It runs mineflayer in the browser which connects to a websocket minecraft server.
It provides a simple websocket to tcp proxy as a backend to make it possible to connect to any minecraft server.

```js
const template = require('prismarine-template')
## Features

* display blocks
* display entities as colored rectangles
* movement sync

## Roadmap

* chat
* block placing and breaking

template.helloWorld()
## Run

```js
npm install
npm run build-start
```

## API
Then connect to http://localhost:8080


### helloWorld()

Prints hello world
39 changes: 39 additions & 0 deletions dns.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* global XMLHttpRequest */

// Custom DNS resolver made by SiebeDW. Powered by google dns.
// Supported: SRV (not all errors support)
module.exports.resolveSrv = function (hostname, callback) {
const Http = new XMLHttpRequest()
const url = `https://dns.google.com/resolve?name=${hostname}&type=SRV`
Http.open('GET', url)
Http.responseType = 'json'
Http.send()

Http.onload = function () {
const response = Http.response
if (response.Status === 3) {
const err = new Error('querySrv ENOTFOUND')
err.code = 'ENOTFOUND'
callback(err)
return
}
if (!response.Answer || response.Answer.length < 1) {
const err = new Error('querySrv ENODATA')
err.code = 'ENODATA'
callback(err)
return
}
const willreturn = []
response.Answer.forEach(function (object) {
const data = object.data.split(' ')
willreturn.push({
priority: data[0],
weight: data[1],
port: data[2],
name: data[3]
})
})
console.log(willreturn)
callback(null, willreturn)
}
}
3 changes: 0 additions & 3 deletions example.js

This file was deleted.

30 changes: 30 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!DOCTYPE html>
<html>
<head>
<title>Prismarine Viewer</title>
<style type="text/css">
html {
overflow: hidden;
}

html, body {
height: 100%;

margin: 0;
padding: 0;
}

canvas {
height: 100%;
width: 100%;
font-size: 0;

margin: 0;
padding: 0;
}
</style>
</head>
<body>
<script type="text/javascript" src="index.js"></script>
</body>
</html>
137 changes: 130 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,132 @@
if (typeof process !== 'undefined' && parseInt(process.versions.node.split('.')[0]) < 14) {
console.error('Your node version is currently', process.versions.node)
console.error('Please update it to a version >= 14.x.x from https://nodejs.org/')
process.exit(1)
}
/* 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')
global.THREE = require('three')

async function main () {
const viewDistance = 6
const host = prompt('Host', '95.111.249.143')
const port = parseInt(prompt('Port', '10000'))
const username = prompt('Username', 'pviewer_person')
let password = prompt('Password (blank for offline)')
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)

// 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
bot.entity.yaw -= e.movementX * 0.01
viewer.setFirstPersonCamera(bot.entity.position, bot.entity.yaw, bot.entity.pitch)
}
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)
}
}
document.addEventListener('pointerlockchange', changeCallback, false)
document.addEventListener('mozpointerlockchange', changeCallback, false)
document.addEventListener('webkitpointerlockchange', changeCallback, false)
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)
document.addEventListener('keydown', (e) => {
console.log(e.code)
if (e.code === 'KeyW') {
bot.setControlState('forward', true)
} else if (e.code === 'KeyS') {
bot.setControlState('back', true)
} else if (e.code === 'KeyA') {
bot.setControlState('right', true)
} else if (e.code === 'KeyD') {
bot.setControlState('left', true)
} else if (e.code === 'Space') {
bot.setControlState('jump', true)
} else if (e.code === 'ShiftLeft') {
bot.setControlState('sneak', true)
} else if (e.code === 'ControlLeft') {
bot.setControlState('sprint', true)
}
}, false)
document.addEventListener('keyup', (e) => {
if (e.code === 'KeyW') {
bot.setControlState('forward', false)
} else if (e.code === 'KeyS') {
bot.setControlState('back', false)
} else if (e.code === 'KeyA') {
bot.setControlState('right', false)
} else if (e.code === 'KeyD') {
bot.setControlState('left', false)
} else if (e.code === 'Space') {
bot.setControlState('jump', false)
} else if (e.code === 'ShiftLeft') {
bot.setControlState('sneak', false)
} else if (e.code === 'ControlLeft') {
bot.setControlState('sprint', false)
}
}, false)

module.exports.helloWorld = function () {
console.log('Hello world !')
// Browser animation loop
const animate = () => {
window.requestAnimationFrame(animate)
renderer.render(viewer.scene, viewer.camera)
}
animate()
})
}
main()
77 changes: 48 additions & 29 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,31 +1,50 @@
{
"name": "prismarine-template",
"version": "1.0.0",
"description": "A template repository to make it easy to create new prismarine repo",
"main": "index.js",
"scripts": {
"test": "jest --verbose",
"pretest": "npm run lint",
"lint": "standard",
"fix": "standard --fix"
},
"repository": {
"type": "git",
"url": "git+https://github.com/PrismarineJS/prismarine-template.git"
},
"keywords": [
"prismarine",
"template"
],
"author": "Romain Beaumont",
"license": "MIT",
"bugs": {
"url": "https://github.com/PrismarineJS/prismarine-template/issues"
},
"homepage": "https://github.com/PrismarineJS/prismarine-template#readme",
"devDependencies": {
"jest": "^26.1.0",
"prismarine-template": "file:.",
"standard": "^16.0.1"
}
"name": "web_client",
"private": true,
"version": "1.0.0",
"description": "web_client",
"main": "index.js",
"scripts": {
"prepare": "webpack",
"start": "webpack serve",
"prod-start": "node server.js",
"build-start": "npm run prepare && npm run prod-start",
"lint": "standard",
"fix": "standard --fix",
"test": "npm run lint && mocha"
},
"dependencies": {
"assert": "^2.0.0",
"browserify-zlib": "^0.2.0",
"buffer": "^6.0.3",
"clean-webpack-plugin": "^3.0.0",
"compression": "^1.7.4",
"constants-browserify": "^1.0.0",
"copy-webpack-plugin": "^7.0.0",
"crypto-browserify": "^3.12.0",
"events": "^3.2.0",
"express": "^4.17.1",
"http-browserify": "^1.7.0",
"https-browserify": "^1.0.0",
"memfs": "^3.2.0",
"mineflayer": "^2.39.2",
"net-browserify": "^0.2.4",
"os-browserify": "^0.3.0",
"path-browserify": "^1.0.1",
"prismarine-viewer": "^1.14.0",
"process": "^0.11.10",
"request": "^2.88.2",
"stream-browserify": "^3.0.0",
"three": "^0.124.0",
"timers-browserify": "^2.0.12",
"webpack": "^5.11.0",
"webpack-cli": "^4.2.0",
"webpack-dev-server": "^3.11.0"
},
"devDependencies": {
"http-server": "^0.12.3",
"lodash-webpack-plugin": "^0.11.6",
"mocha": "^8.3.0",
"standard": "^16.0.3"
}
}
1 change: 1 addition & 0 deletions perf_hooks_replacement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports.performance = window.performance
Loading

0 comments on commit d978f5c

Please sign in to comment.