Skip to content

Instantly share code, notes, and snippets.

@josantonius
Created November 18, 2022 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josantonius/d48498aca68503d46968e47f5c0ce428 to your computer and use it in GitHub Desktop.
Save josantonius/d48498aca68503d46968e47f5c0ce428 to your computer and use it in GitHub Desktop.
Automatically spectate locations (simulating cameras) and players on Sparked Host Minecraft servers
class MinecraftSpectateMode {
constructor(options) {
Object.assign(this, options)
this.lastUser = ''
this.mode = this.randomize(['cam', 'user'])
this.cam = this.randomize(options.cams)
this.input = document.querySelector(options.consoleInputQuerySelector)
this.excludedPlayers.push(this.spectator)
this.excludedPlayers.push('Anonymous Player')
}
getRandomUser() {
let users = Array.from(
document.querySelectorAll(this.playerNameQuerySelector)
).map((item) => item.innerText)
users = users.filter((e) => !this.excludedPlayers.includes(e))
let count = users.length
let user = this.randomize(users)
let selected = user()
return selected === this.lastUser && count >= 2 ? user() : selected
}
log(item) {
let now = new Date()
let current = now.getHours() + ':' + now.getMinutes()
console.log(`[${current}] Spectating: ${item}.`)
}
randomize(array) {
let copy = array.slice(0)
return function () {
if (copy.length < 1) {
copy = array.slice(0)
}
let index = Math.floor(Math.random() * copy.length)
let item = copy[index]
copy.splice(index, 1)
return item
}
}
run() {
this.setGameMode()
this.spectate()
setInterval(() => this.spectate(), this.intervalInSeconds * 1000)
return 'MinecraftSpectateMode'
}
sendCommandAsSpectator(command) {
this.input.value = `execute as ${this.spectator} run execute as @a[gamemode=spectator] ${command}`
this.simulateInputEnter()
}
setGameMode() {
this.input.value = `gamemode spectator ${this.spectator}`
this.simulateInputEnter()
}
simulateInputEnter() {
let evt = new Event('keydown', { bubbles: true, cancelable: true })
evt.keyCode = 13
evt.key = 'Enter'
this.input.dispatchEvent(evt)
}
sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms))
}
spectate() {
let user = this.getRandomUser()
if (!this.spectateUsers && this.spectateCams) {
this.spectateCam()
} else if (user && this.spectateUsers && !this.spectateCams) {
this.spectateUser(user)
} else if (this.spectateUsers && this.spectateCams) {
if (this.mode() === 'cam') {
return this.spectateCam()
}
user ? this.spectateUser(user) : this.spectateCam()
}
}
async spectateCam() {
let cam = this.cam()
this.log(cam.description)
await this.sleep(50)
this.simulateInputEnter()
this.sendCommandAsSpectator(
`run execute in minecraft:${cam.world} run tp @s ${cam.coordinates} ${cam.rotation}`
)
}
async spectateUser(user) {
this.log(user)
await this.sleep(50)
this.sendCommandAsSpectator(`run spectate ${user}`)
}
}
new MinecraftSpectateMode({
intervalInSeconds: 15,
spectator: 'YOU_USERNAME',
spectateCams: true,
spectateUsers: true,
excludedPlayers: ['USER_1', 'USER_2'],
cams: [
{
coordinates: '-66.500 81.73646 533.500',
rotation: '0.0 39.0',
world: 'overworld',
description: 'Village',
},
{
coordinates: '-66.500 83.09924 486.500',
rotation: '0.0 40.2',
world: 'the_nether',
description: 'Nether Fortress',
},
{
coordinates: '-48.074 86.58757 562.492',
rotation: '-90.0 41.7',
world: 'the_end',
description: 'Dragon Egg',
},
],
playerNameQuerySelector: '.sc-135uotw-2.cHwxrB code',
consoleInputQuerySelector: '.itaf37-1.jSyjnD',
}).run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment