-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Bedrock connection & chat support #3539
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
Open
FreezeEngine
wants to merge
6
commits into
PrismarineJS:master
Choose a base branch
from
FreezeEngine:bedrock-chat
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
6 commits
Select commit
Hold shift + click to select a range
43ae8f6
Bedrock initialization support
FreezeEngine df4faac
Chat module
FreezeEngine bbc63c4
Bedrock chat ping-pong example
FreezeEngine cf96832
Formatting
FreezeEngine 071918b
New 'version' format, support for it in loader, example and CI
FreezeEngine f113272
More CI fixes
FreezeEngine 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 hidden or 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 hidden or 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,20 @@ | ||
const mineflayer = require('mineflayer') | ||
|
||
const username = 'Bot' | ||
const server = 'localhost' | ||
|
||
const options = { | ||
version: 'bedrock', | ||
host: server, | ||
port: 19132, | ||
offline: true, | ||
username | ||
} | ||
|
||
const bot = mineflayer.createBot(options) | ||
|
||
bot.on('message', (message, type, sender, verified) => { | ||
if (sender === bot._client.username) return | ||
console.log([message, type, sender, verified]) | ||
bot.chat(message.getText(0)) | ||
}) |
This file contains hidden or 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 hidden or 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,205 @@ | ||
const assert = require('assert') | ||
|
||
const USERNAME_REGEX = '(?:\\(.{1,15}\\)|\\[.{1,15}\\]|.){0,5}?(\\w+)' | ||
const LEGACY_VANILLA_CHAT_REGEX = new RegExp(`^${USERNAME_REGEX}\\s?[>:\\-»\\]\\)~]+\\s(.*)$`) | ||
|
||
module.exports = inject | ||
|
||
function inject (bot, options) { | ||
const CHAT_LENGTH_LIMIT = options.chatLengthLimit ?? (bot.supportFeature('lessCharsInChat') ? 100 : 256) | ||
const defaultChatPatterns = options.defaultChatPatterns ?? true | ||
|
||
const ChatMessage = require('prismarine-chat')(bot.registry) | ||
// chat.pattern.type will emit an event for bot.on() of the same type, eg chatType = whisper will trigger bot.on('whisper') | ||
const _patterns = {} | ||
let _length = 0 | ||
// deprecated | ||
bot.chatAddPattern = (patternValue, typeValue) => { | ||
return bot.addChatPattern(typeValue, patternValue, { deprecated: true }) | ||
} | ||
|
||
bot.addChatPatternSet = (name, patterns, opts = {}) => { | ||
if (!patterns.every(p => p instanceof RegExp)) throw new Error('Pattern parameter should be of type RegExp') | ||
const { repeat = true, parse = false } = opts | ||
_patterns[_length++] = { | ||
name, | ||
patterns, | ||
position: 0, | ||
matches: [], | ||
messages: [], | ||
repeat, | ||
parse | ||
} | ||
return _length | ||
} | ||
|
||
bot.addChatPattern = (name, pattern, opts = {}) => { | ||
if (!(pattern instanceof RegExp)) throw new Error('Pattern parameter should be of type RegExp') | ||
const { repeat = true, deprecated = false, parse = false } = opts | ||
_patterns[_length] = { | ||
name, | ||
patterns: [pattern], | ||
position: 0, | ||
matches: [], | ||
messages: [], | ||
deprecated, | ||
repeat, | ||
parse | ||
} | ||
return _length++ // increment length after we give it back to the user | ||
} | ||
|
||
bot.removeChatPattern = name => { | ||
if (typeof name === 'number') { | ||
_patterns[name] = undefined | ||
} else { | ||
const matchingPatterns = Object.entries(_patterns).filter(pattern => pattern[1]?.name === name) | ||
matchingPatterns.forEach(([indexString]) => { | ||
_patterns[+indexString] = undefined | ||
}) | ||
} | ||
} | ||
|
||
function findMatchingPatterns (msg) { | ||
const found = [] | ||
for (const [indexString, pattern] of Object.entries(_patterns)) { | ||
if (!pattern) continue | ||
const { position, patterns } = pattern | ||
if (patterns[position].test(msg)) { | ||
found.push(+indexString) | ||
} | ||
} | ||
return found | ||
} | ||
|
||
bot.on('messagestr', (msg, _, originalMsg) => { | ||
const foundPatterns = findMatchingPatterns(msg) | ||
|
||
for (const ix of foundPatterns) { | ||
_patterns[ix].matches.push(msg) | ||
_patterns[ix].messages.push(originalMsg) | ||
_patterns[ix].position++ | ||
|
||
if (_patterns[ix].deprecated) { | ||
const [, ...matches] = _patterns[ix].matches[0].match(_patterns[ix].patterns[0]) | ||
bot.emit(_patterns[ix].name, ...matches, _patterns[ix].messages[0].translate, ..._patterns[ix].messages) | ||
_patterns[ix].messages = [] // clear out old messages | ||
} else { // regular parsing | ||
if (_patterns[ix].patterns.length > _patterns[ix].matches.length) return // we have all the matches, so we can emit the done event | ||
if (_patterns[ix].parse) { | ||
const matches = _patterns[ix].patterns.map((pattern, i) => { | ||
const [, ...matches] = _patterns[ix].matches[i].match(pattern) // delete full message match | ||
return matches | ||
}) | ||
bot.emit(`chat:${_patterns[ix].name}`, matches) | ||
} else { | ||
bot.emit(`chat:${_patterns[ix].name}`, _patterns[ix].matches) | ||
} | ||
// these are possibly null-ish if the user deletes them as soon as the event for the match is emitted | ||
} | ||
if (_patterns[ix]?.repeat) { | ||
_patterns[ix].position = 0 | ||
_patterns[ix].matches = [] | ||
} else { | ||
_patterns[ix] = undefined | ||
} | ||
} | ||
}) | ||
|
||
addDefaultPatterns() | ||
|
||
bot._client.on('text', (data) => { | ||
const msg = ChatMessage.fromNotch(data.message) | ||
|
||
if (['chat', 'whisper', 'announcement'].includes(data.type)) { | ||
bot.emit('message', msg, 'chat', data.source_name, null) | ||
bot.emit('messagestr', msg.toString(), data.type, msg, data.source_name, null) | ||
} else if (['popup', 'jukebox_popup'].includes(data.type)) { | ||
bot.emit('actionBar', msg, null) | ||
} else { | ||
bot.emit('message', msg, data.type, null) | ||
bot.emit('messagestr', msg.toString(), data.type, msg, null) | ||
} | ||
}) | ||
|
||
function chatWithHeader (message) { | ||
if (typeof message === 'number') message = message.toString() | ||
if (typeof message !== 'string') { | ||
throw new Error('Chat message type must be a string or number: ' + typeof message) | ||
} | ||
|
||
if (message.startsWith('/')) { | ||
// Do not try and split a command without a header | ||
bot._client.write('command_request', { | ||
command: message, | ||
origin: { | ||
type: 'player', | ||
uuid: bot.player.uuid, | ||
request_id: '' | ||
}, | ||
internal: false, | ||
version: 76 | ||
}) | ||
return | ||
} | ||
|
||
const lengthLimit = CHAT_LENGTH_LIMIT | ||
message.split('\n').forEach((subMessage) => { | ||
if (!subMessage) return | ||
let i | ||
let smallMsg | ||
for (i = 0; i < subMessage.length; i += lengthLimit) { | ||
smallMsg = subMessage.substring(i, i + lengthLimit) | ||
bot._client.write('text', { | ||
type: 'chat', | ||
needs_translation: false, | ||
source_name: bot._client.username, | ||
message: smallMsg, | ||
xuid: bot._client.profile.xuid.toString(), // bot._client.startGameData, | ||
platform_chat_id: '', | ||
filtered_message: '' | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
async function tabComplete (text, assumeCommand = false, sendBlockInSight = true, timeout = 5000) { | ||
assert(false, 'Unimplemented') | ||
return [] | ||
} | ||
|
||
bot.whisper = (username, message) => { | ||
chatWithHeader(`/tell ${username} ${message}`) | ||
} | ||
bot.chat = (message) => { | ||
chatWithHeader(message) | ||
} | ||
|
||
bot.tabComplete = tabComplete | ||
|
||
function addDefaultPatterns () { | ||
// 1.19 changes the chat format to move <sender> prefix from message contents to a seperate field. | ||
// TODO: new chat lister to handle this | ||
if (!defaultChatPatterns) return | ||
bot.addChatPattern('whisper', new RegExp(`^${USERNAME_REGEX} whispers(?: to you)?:? (.*)$`), { deprecated: true }) | ||
bot.addChatPattern('whisper', new RegExp(`^\\[${USERNAME_REGEX} -> \\w+\\s?\\] (.*)$`), { deprecated: true }) | ||
bot.addChatPattern('chat', LEGACY_VANILLA_CHAT_REGEX, { deprecated: true }) | ||
} | ||
|
||
function awaitMessage (...args) { | ||
return new Promise((resolve, reject) => { | ||
const resolveMessages = args.flatMap(x => x) | ||
|
||
function messageListener (msg) { | ||
if (resolveMessages.some(x => x instanceof RegExp ? x.test(msg) : msg === x)) { | ||
resolve(msg) | ||
bot.off('messagestr', messageListener) | ||
} | ||
} | ||
|
||
bot.on('messagestr', messageListener) | ||
}) | ||
} | ||
|
||
bot.awaitMessage = awaitMessage | ||
} |
This file contains hidden or 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 hidden or 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,8 +1,14 @@ | ||
const testedVersions = ['1.8.8', '1.9.4', '1.10.2', '1.11.2', '1.12.2', '1.13.2', '1.14.4', '1.15.2', '1.16.5', '1.17.1', '1.18.2', '1.19', '1.19.2', '1.19.3', '1.19.4', '1.20.1', '1.20.2', '1.20.4', '1.20.6', '1.21.1'] | ||
const bedrockTestedVersions = ['1.17.10', '1.18.30', '1.19.1', '1.19.30', '1.19.63', '1.19.70', '1.19.80', '1.20.40', '1.20.61', '1.20.71', '1.21.50'] | ||
module.exports = { | ||
|
||
testedVersions, | ||
latestSupportedVersion: testedVersions[testedVersions.length - 1], | ||
oldestSupportedVersion: testedVersions[0] | ||
|
||
bedrock: { | ||
testedVersions: bedrockTestedVersions, | ||
latestSupportedVersion: bedrockTestedVersions[bedrockTestedVersions.length - 1], | ||
oldestSupportedVersion: bedrockTestedVersions[0] | ||
}, | ||
pc: { | ||
testedVersions, | ||
latestSupportedVersion: testedVersions[testedVersions.length - 1], | ||
oldestSupportedVersion: testedVersions[0] | ||
} | ||
} |
This file contains hidden or 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 hidden or 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.
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.
@rom1504, @extremeheat have you considered using consistent bedrock version naming in all PrismarineJS libraries? It would be less messy if bedrock version names always started with the bedrock_ prefix. ex minecraft-data requires prefix, bedrock-protocol requires version without prefix.
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.
It already is consistent, because the versions in the loaders are in the end passed down to minecraft-data which has the prefix handling. The only place that allows without the prefix is bedrock-protocol, where the version has a different meaning.