Skip to content

Commit

Permalink
a few important fixes (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
zardoy authored Sep 30, 2023
2 parents b8ff91f + d63ba0b commit 60824ec
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 43 deletions.
4 changes: 1 addition & 3 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"defaultHost": "pjs.deptofcraft.com",
"defaultHostPort": 25565,
"defaultProxy": "",
"defaultProxyPort": 0,
"defaultProxy": "zardoy.site:2344",
"defaultVersion": "1.18.2"
}
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 28 additions & 15 deletions prismarine-viewer/viewer/lib/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,41 @@ const TWEEN = require('@tweenjs/tween.js')
const Entity = require('./entity/Entity')
const { dispose3 } = require('./dispose')

function getEntityMesh (entity, scene) {
function getUsernameTexture(username, { fontFamily = 'sans-serif' }) {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');

const fontSize = 50;
const padding = 5
ctx.font = `${fontSize}px ${fontFamily}`;

const textWidth = ctx.measureText(username).width + padding * 2;

canvas.width = textWidth;
canvas.height = fontSize + padding * 2;

ctx.fillStyle = 'rgba(0, 0, 0, 0.3)';
ctx.fillRect(0, 0, canvas.width, canvas.height);

ctx.font = `${fontSize}px ${fontFamily}`;
ctx.fillStyle = 'white'
ctx.fillText(username, padding, fontSize);

return canvas;
}

function getEntityMesh (entity, scene, options) {
if (entity.name) {
try {
const e = new Entity('1.16.4', entity.name, scene)

if (entity.username !== undefined) {
const canvas = document.createElement('canvas')
canvas.width = 500
canvas.height = 100

const ctx = canvas.getContext('2d')
ctx.font = '50pt Arial'
ctx.fillStyle = '#000000'
ctx.textAlign = 'left'
ctx.textBaseline = 'top'

const txt = entity.username
ctx.fillText(txt, 100, 0)

const canvas = getUsernameTexture(entity.username, options);
const tex = new THREE.Texture(canvas)
tex.needsUpdate = true
const spriteMat = new THREE.SpriteMaterial({ map: tex })
const sprite = new THREE.Sprite(spriteMat)
sprite.scale.set(canvas.width * 0.005, canvas.height * 0.005, 1)
sprite.position.y += entity.height + 0.6

e.mesh.add(sprite)
Expand All @@ -48,6 +60,7 @@ class Entities {
constructor (scene) {
this.scene = scene
this.entities = {}
this.entitiesOptions = {}
}

clear () {
Expand All @@ -60,7 +73,7 @@ class Entities {

update (entity) {
if (!this.entities[entity.id]) {
const mesh = getEntityMesh(entity, this.scene)
const mesh = getEntityMesh(entity, this.scene, this.entitiesOptions)
if (!mesh) return
this.entities[entity.id] = mesh
this.scene.add(mesh)
Expand Down
2 changes: 1 addition & 1 deletion prismarine-viewer/viewer/lib/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ function getVersion (version) {
return lastOfMajor[toMajor(version)] ?? Object.values(lastOfMajor).at(-1)
}

module.exports = { getVersion }
module.exports = { getVersion, toMajor }
3 changes: 2 additions & 1 deletion prismarine-viewer/viewer/lib/worldrenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const { EventEmitter } = require('events')
const { dispose3 } = require('./dispose')
const { dynamicMcDataFiles } = require('../../buildWorkerConfig.mjs')
const mcDataRaw = require('minecraft-data/data.js')
const { toMajor } = require('./version.js')

function mod (x, n) {
return ((x % n) + n) % n
Expand Down Expand Up @@ -88,7 +89,7 @@ class WorldRenderer {
this.resetWorld()
this.active = true

const allMcData = mcDataRaw.pc[this.version]
const allMcData = mcDataRaw.pc[this.version] ?? mcDataRaw.pc[toMajor(this.version)]
for (const worker of this.workers) {
const mcData = Object.fromEntries(Object.entries(allMcData).filter(([key]) => dynamicMcDataFiles.includes(key)))
mcData.version = JSON.parse(JSON.stringify(mcData.version))
Expand Down
20 changes: 9 additions & 11 deletions src/updateTime.ts → src/dayCycle.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
export default (bot: import('mineflayer').Bot) => {
export default () => {
bot.on('time', () => {
// 0 morning
const dayTotal = 24_000
const evening = 12_542 / dayTotal
const night = 17_843 / dayTotal
const morningStart = 22_300 / dayTotal
const morningEnd = 23_961 / dayTotal
const timeProgress = bot.time.time / dayTotal
const evening = 12_542
const night = 17_843
const morningStart = 22_300
const morningEnd = 23_961
const timeProgress = bot.time.time

// todo check actual colors
const dayColorRainy = { r: 111 / 255, g: 156 / 255, b: 236 / 255 }
Expand All @@ -30,11 +30,9 @@ export default (bot: import('mineflayer').Bot) => {
}
// todo need to think wisely how to set these values & also move directional light around!
const colorInt = Math.max(int, 0.1)
window.viewer.scene.background = new THREE.Color(dayColor.r * colorInt, dayColor.g * colorInt, dayColor.b * colorInt)
// todo and these too!
// ambient light
window.viewer.scene.children[0].intensity = Math.max(int, 0.25)
viewer.scene.background = new THREE.Color(dayColor.r * colorInt, dayColor.g * colorInt, dayColor.b * colorInt)
viewer.ambientLight.intensity = Math.max(int, 0.25)
// directional light
window.viewer.scene.children[1].intensity = Math.min(int, 0.5)
viewer.directionalLight.intensity = Math.min(int, 0.5)
})
}
17 changes: 9 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ import {

import { startLocalServer, unsupportedLocalServerFeatures } from './createLocalServer'
import serverOptions from './defaultLocalServerOptions'
import updateTime from './updateTime'
import dayCycle from './dayCycle'

import { subscribeKey } from 'valtio/utils'
import _ from 'lodash-es'
Expand Down Expand Up @@ -283,14 +283,15 @@ async function connect(connectOptions: {
removeAllListeners()
}
const handleError = (err) => {
errorAbortController.abort()
console.log('Encountered error!', err)

// #region rejoin key
const controller = new AbortController()
window.addEventListener('keydown', (e) => {
window.addEventListener('keyup', (e) => {
if (e.code !== 'KeyR') return
controller.abort()
connect(connectOptions)
void connect(connectOptions)
loadingScreen.hasError = false
}, { signal: controller.signal })
// #endregion
Expand Down Expand Up @@ -546,7 +547,7 @@ async function connect(connectOptions: {
worldView.listenToBot(bot)
worldView.init(bot.entity.position)

updateTime(bot)
dayCycle()

// Bot position callback
function botPosition() {
Expand Down Expand Up @@ -692,8 +693,8 @@ async function connect(connectOptions: {
hud.style.display = 'block'
blockInteraction.init()

errorAbortController.abort()
setTimeout(() => {
errorAbortController.abort()
if (loadingScreen.hasError) return
// remove loading screen, wait a second to make sure a frame has properly rendered
setLoadingScreenStatus(undefined)
Expand All @@ -706,8 +707,8 @@ async function connect(connectOptions: {
})
}

window.addEventListener('mousedown', (e) => {
pointerLock.requestPointerLock()
window.addEventListener('mousedown', () => {
void pointerLock.requestPointerLock()
})

window.addEventListener('keydown', (e) => {
Expand All @@ -720,7 +721,7 @@ window.addEventListener('keydown', (e) => {
})
} else if (pointerLock.hasPointerLock) {
if (options.autoExitFullscreen) {
document.exitFullscreen()
void document.exitFullscreen()
}
} else {
document.dispatchEvent(new Event('pointerlockchange'))
Expand Down

0 comments on commit 60824ec

Please sign in to comment.