-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Various Typing Fixes & Client-Side Render Event (Multiple Client Compatible) #428
Open
GenerelSchwerz
wants to merge
11
commits into
PrismarineJS:master
Choose a base branch
from
GenerelSchwerz:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
26c6373
Added support for a render event that is multiple-user compliant.
GenerelSchwerz 34073a6
Added support for a render event that is multiple-user compliant.
GenerelSchwerz 0ec3b58
fix typing
GenerelSchwerz 3d38a3b
cleanup
GenerelSchwerz 40c7f9e
cleanup
GenerelSchwerz 9c6f9ae
cleanup
GenerelSchwerz d66a470
change
GenerelSchwerz 6ef8107
add a handle to monitor fps decreases but not a disconnect
GenerelSchwerz f9ebe7f
add a handle to monitor fps decreases but not a disconnect
GenerelSchwerz 8518325
add a handle to monitor fps decreases but not a disconnect
GenerelSchwerz 85105d4
respond to review
GenerelSchwerz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
node_modules | ||
package-lock.json | ||
yarn.lock | ||
/test/*.png | ||
versions/ | ||
public/index.js* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const mineflayer = require('mineflayer') | ||
const mineflayerViewer = require('prismarine-viewer').mineflayer | ||
|
||
const bot = mineflayer.createBot({ | ||
username: 'Bot', | ||
host: process.argv[2], | ||
port: isNaN(parseInt(process.argv[3])) ? 25565 : parseInt(process.argv[3]), | ||
version: process.argv[4] ?? '1.16.5' | ||
}) | ||
|
||
bot.once('spawn', () => { | ||
mineflayerViewer(bot, { port: 3000 }) | ||
|
||
// approximate maximum FPS of all clients connected to our viewer | ||
bot.viewer.on('onRender', (fps) => { | ||
// do something every frame | ||
|
||
// random offset of bot position, +/5 in x and z, +5 in y | ||
const pos = bot.entity.position.offset(Math.random() * 10 - 5, 5, Math.random() * 10 - 5) | ||
|
||
bot.viewer.erase('posExample') | ||
bot.viewer.drawBoxGrid('posExample', pos, pos.offset(1, 1, 1), 'aqua') | ||
}) | ||
|
||
const path = [bot.entity.position.clone()] | ||
bot.on('move', () => { | ||
if (path[path.length - 1].distanceTo(bot.entity.position) > 1) { | ||
path.push(bot.entity.position.clone()) | ||
bot.viewer.drawLine('path', path) | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import * as mineflayer from "mineflayer"; | ||
import { mineflayer as mineflayerLoader } from "prismarine-viewer"; | ||
|
||
import type { Vec3 } from "vec3"; | ||
|
||
const bot = mineflayer.createBot({ | ||
username: "Bot", | ||
}); | ||
|
||
bot.once("spawn", async () => { | ||
mineflayerLoader(bot, { port: 3000 }); | ||
|
||
const path: Vec3[] = [bot.entity.position.clone()]; | ||
if (path[path.length - 1].distanceTo(bot.entity.position) > 1) { | ||
path.push(bot.entity.position.clone()); | ||
bot.viewer.drawLine("path", path); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,7 +45,83 @@ module.exports = (bot, { viewDistance = 6, firstPerson = false, port = 3000, pre | |
} | ||
} | ||
|
||
let renderInterval = null | ||
|
||
let maxFPS = 0 | ||
let maxFPSId = null | ||
|
||
const fpsMap = new Map() | ||
|
||
io.on('connection', (socket) => { | ||
const getRenderInterval = (fps) => setInterval(() => bot.viewer.emit('onRender', fps), 1000 / fps) | ||
|
||
const updateListener = ({ id, fps }) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add a comment explaining the intent of this; it's far from obvious |
||
if (id == null || fps == null) return | ||
|
||
fpsMap.set(id, fps) | ||
|
||
if (fps > maxFPS) { | ||
maxFPS = fps | ||
if (renderInterval) clearInterval(renderInterval) | ||
|
||
renderInterval = getRenderInterval(maxFPS) | ||
} else if (id === maxFPSId && fps < maxFPS) { | ||
// handle where maxFPSId's fps decreases | ||
|
||
let secondHighest = 0 | ||
let secondHighestId = null | ||
|
||
// check for alternative highest fps | ||
for (const [id1, fps1] of fpsMap) { | ||
if (fps1 > secondHighest && id1 !== id) { | ||
secondHighest = fps1 | ||
secondHighestId = id1 | ||
} | ||
} | ||
|
||
// if there is no alternative highest fps, set maxFPS to current fps | ||
// note: if secondHighest is 0, then there is no alternative highest fps | ||
if (fps > secondHighest) { | ||
maxFPS = fps | ||
maxFPSId = id | ||
if (renderInterval) clearInterval(renderInterval) | ||
|
||
renderInterval = getRenderInterval(maxFPS) | ||
} else { | ||
// if there is an alternative highest fps that is higher than current FPS, | ||
// set maxFPS to the alternative highest fps | ||
maxFPS = secondHighest | ||
maxFPSId = secondHighestId | ||
if (renderInterval) clearInterval(renderInterval) | ||
|
||
renderInterval = getRenderInterval(maxFPS) | ||
} | ||
} | ||
} | ||
|
||
const onSocketRemoval = () => { | ||
if (fpsMap.has(socket.id)) { | ||
fpsMap.delete(socket.id) | ||
} | ||
|
||
if (fpsMap.size === 0) { | ||
maxFPS = 0 | ||
if (renderInterval) clearInterval(renderInterval) | ||
return | ||
} | ||
|
||
for (const [, fps] of fpsMap) { | ||
if (fps > maxFPS) { | ||
maxFPS = fps | ||
} | ||
} | ||
|
||
if (renderInterval) clearInterval(renderInterval) | ||
|
||
renderInterval = getRenderInterval(maxFPS) | ||
} | ||
|
||
socket.on('renderFPS', updateListener) | ||
socket.emit('version', bot.version) | ||
sockets.push(socket) | ||
|
||
|
@@ -75,6 +151,7 @@ module.exports = (bot, { viewDistance = 6, firstPerson = false, port = 3000, pre | |
bot.removeListener('move', botPosition) | ||
worldView.removeListenersFromBot(bot) | ||
sockets.splice(sockets.indexOf(socket), 1) | ||
onSocketRemoval() | ||
}) | ||
}) | ||
|
||
|
@@ -87,5 +164,9 @@ module.exports = (bot, { viewDistance = 6, firstPerson = false, port = 3000, pre | |
for (const socket of sockets) { | ||
socket.disconnect() | ||
} | ||
|
||
// should already always be handled, but hey you never know. | ||
if (renderInterval) clearInterval(renderInterval) | ||
fpsMap.clear() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add to doc + use it in an example to explain what it's good for
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not know where I would put this server-sided event (renderFPS) firing into docs as there is no mention of server-sided code in the READMEs.
I included documentation on the bot.viewer event firing, however.