2021-11-12 16:08:28 -05:00
|
|
|
const { LitElement, html, css } = require('lit')
|
2021-03-14 15:34:35 -04:00
|
|
|
|
2021-02-27 18:18:58 -05:00
|
|
|
const styles = {
|
|
|
|
black: 'color:#000000',
|
|
|
|
dark_blue: 'color:#0000AA',
|
|
|
|
dark_green: 'color:#00AA00',
|
|
|
|
dark_aqua: 'color:#00AAAA',
|
|
|
|
dark_red: 'color:#AA0000',
|
|
|
|
dark_purple: 'color:#AA00AA',
|
|
|
|
gold: 'color:#FFAA00',
|
|
|
|
gray: 'color:#AAAAAA',
|
|
|
|
dark_gray: 'color:#555555',
|
|
|
|
blue: 'color:#5555FF',
|
|
|
|
green: 'color:#55FF55',
|
|
|
|
aqua: 'color:#55FFFF',
|
|
|
|
red: 'color:#FF5555',
|
|
|
|
light_purple: 'color:#FF55FF',
|
|
|
|
yellow: 'color:#FFFF55',
|
|
|
|
white: 'color:#FFFFFF',
|
|
|
|
bold: 'font-weight:900',
|
|
|
|
strikethrough: 'text-decoration:line-through',
|
|
|
|
underlined: 'text-decoration:underline',
|
|
|
|
italic: 'font-style:italic'
|
|
|
|
}
|
|
|
|
const dictionary = {
|
|
|
|
'chat.stream.emote': '(%s) * %s %s',
|
|
|
|
'chat.stream.text': '(%s) <%s> %s',
|
|
|
|
// 'chat.type.achievement': '%s has just earned the achievement %s', // 1.8? Not tested
|
|
|
|
// 'chat.type.advancement.task': '%s has just earned the advancement %s',
|
|
|
|
// 'chat.type.advancement.goal': '%s has just reached the goal %s',
|
|
|
|
// 'chat.type.advancement.challenge': '%s did a challenge lolol %s',
|
|
|
|
'chat.type.admin': '[%s: %s]',
|
|
|
|
'chat.type.announcement': '[%s] %s',
|
|
|
|
'chat.type.emote': '* %s %s',
|
|
|
|
'chat.type.text': '<%s> %s'
|
|
|
|
}
|
|
|
|
|
2021-03-14 15:34:35 -04:00
|
|
|
class ChatBox extends LitElement {
|
|
|
|
static get styles () {
|
|
|
|
return css`
|
|
|
|
.chat-wrapper {
|
|
|
|
position: fixed;
|
2021-12-24 07:00:07 -05:00
|
|
|
z-index: 10;
|
2021-03-14 15:34:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
.chat-display-wrapper {
|
2021-12-24 07:00:07 -05:00
|
|
|
bottom: 40px;
|
2021-03-14 15:34:35 -04:00
|
|
|
padding: 4px;
|
2021-12-24 07:00:07 -05:00
|
|
|
padding-left: 0;
|
|
|
|
max-height: var(--chatHeight);
|
|
|
|
width: var(--chatWidth);
|
2021-03-14 15:34:35 -04:00
|
|
|
}
|
2021-02-27 18:18:58 -05:00
|
|
|
|
2021-03-14 15:34:35 -04:00
|
|
|
.chat-input-wrapper {
|
2021-12-24 07:00:07 -05:00
|
|
|
bottom: 2px;
|
|
|
|
width: calc(100% - 2px);
|
|
|
|
position: relative;
|
|
|
|
left: 1px;
|
2021-03-14 15:34:35 -04:00
|
|
|
overflow: hidden;
|
|
|
|
background-color: rgba(0, 0, 0, 0);
|
|
|
|
}
|
2021-02-27 18:18:58 -05:00
|
|
|
|
2021-03-14 15:34:35 -04:00
|
|
|
.chat {
|
|
|
|
overflow: hidden;
|
|
|
|
color: white;
|
2021-12-24 07:00:07 -05:00
|
|
|
font-size: 10px;
|
2021-03-14 15:34:35 -04:00
|
|
|
margin: 0px;
|
|
|
|
line-height: 100%;
|
2021-12-24 07:00:07 -05:00
|
|
|
text-shadow: 1px 1px 0px #3f3f3f;
|
2021-03-14 15:34:35 -04:00
|
|
|
font-family: mojangles, minecraft, monospace;
|
|
|
|
width: 100%;
|
2021-12-24 07:00:07 -05:00
|
|
|
max-height: var(--chatHeight);
|
2021-03-14 15:34:35 -04:00
|
|
|
}
|
2021-02-27 18:18:58 -05:00
|
|
|
|
2021-03-14 15:34:35 -04:00
|
|
|
input[type=text], #chatinput {
|
|
|
|
background-color: rgba(0, 0, 0, 0.5);
|
2021-12-24 07:00:07 -05:00
|
|
|
border: 1px solid rgba(0, 0, 0, 0.5);
|
2021-03-14 15:34:35 -04:00
|
|
|
display: none;
|
2021-12-24 07:00:07 -05:00
|
|
|
outline: none;
|
|
|
|
}
|
|
|
|
|
|
|
|
#chatinput:focus {
|
|
|
|
border-color: white;
|
2021-03-14 15:34:35 -04:00
|
|
|
}
|
2021-11-05 13:00:28 -04:00
|
|
|
|
|
|
|
.chat-message {
|
2021-03-14 15:34:35 -04:00
|
|
|
display: block;
|
2021-12-24 07:00:07 -05:00
|
|
|
padding-left: 4px;
|
2021-11-05 13:00:28 -04:00
|
|
|
background-color: rgba(0, 0, 0, 0.5);
|
|
|
|
}
|
|
|
|
|
|
|
|
.chat-message-fadeout {
|
|
|
|
opacity: 1;
|
|
|
|
transition: all 3s;
|
|
|
|
}
|
|
|
|
|
|
|
|
.chat-message-fade {
|
|
|
|
opacity: 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
.chat-message-faded {
|
|
|
|
transition: none !important;
|
|
|
|
}
|
|
|
|
|
|
|
|
.chat-message-chat-opened {
|
|
|
|
opacity: 1 !important;
|
|
|
|
transition: none !important;
|
|
|
|
}
|
2021-03-14 15:34:35 -04:00
|
|
|
`
|
2021-03-01 23:06:51 -05:00
|
|
|
}
|
|
|
|
|
2021-03-14 15:34:35 -04:00
|
|
|
render () {
|
|
|
|
return html`
|
|
|
|
<div id="chat-wrapper" class="chat-wrapper chat-display-wrapper">
|
2021-12-24 07:00:07 -05:00
|
|
|
<div class="chat" id="chat">
|
|
|
|
<li class="chat-message chat-message-fade chat-message-faded">Welcome to prismarine-web-client! Chat appears here.</li>
|
|
|
|
</div>
|
2021-03-14 15:34:35 -04:00
|
|
|
</div>
|
|
|
|
<div id="chat-wrapper2" class="chat-wrapper chat-input-wrapper">
|
|
|
|
<div class="chat" id="chat-input">
|
2021-12-24 07:00:07 -05:00
|
|
|
<input type="text" class="chat" id="chatinput" spellcheck="false" autocomplete="off"></input>
|
2021-03-14 15:34:35 -04:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
`
|
2021-02-27 18:18:58 -05:00
|
|
|
}
|
|
|
|
|
2021-03-14 15:34:35 -04:00
|
|
|
init (client, renderer) {
|
|
|
|
this.inChat = false
|
|
|
|
const chat = this.shadowRoot.querySelector('#chat')
|
2021-12-24 07:00:07 -05:00
|
|
|
const gameMenu = document.getElementById('pause-screen')
|
2021-03-14 15:34:35 -04:00
|
|
|
const chatInput = this.shadowRoot.querySelector('#chatinput')
|
|
|
|
|
|
|
|
const chatHistory = []
|
|
|
|
let chatHistoryPos = 0
|
|
|
|
|
|
|
|
renderer.domElement.requestPointerLock = renderer.domElement.requestPointerLock ||
|
|
|
|
renderer.domElement.mozRequestPointerLock ||
|
|
|
|
renderer.domElement.webkitRequestPointerLock
|
|
|
|
|
|
|
|
// Show chat
|
|
|
|
chat.style.display = 'block'
|
2021-02-27 18:18:58 -05:00
|
|
|
|
2021-03-14 15:34:35 -04:00
|
|
|
const self = this
|
|
|
|
|
2021-12-24 07:00:07 -05:00
|
|
|
// Esc event - Doesnt work with onkeypress?! - keypressed is deprecated uk
|
2021-03-15 22:16:37 -04:00
|
|
|
document.addEventListener('keydown', e => {
|
2021-04-06 07:31:02 -04:00
|
|
|
if (gameMenu.inMenu) return
|
2021-03-14 15:34:35 -04:00
|
|
|
if (!self.inChat) return
|
|
|
|
e = e || window.event
|
2021-12-24 07:00:07 -05:00
|
|
|
if (e.code === 'Escape') {
|
2021-03-14 15:34:35 -04:00
|
|
|
disableChat()
|
|
|
|
} else if (e.keyCode === 38) {
|
|
|
|
if (chatHistoryPos === 0) return
|
|
|
|
chatInput.value = chatHistory[--chatHistoryPos] !== undefined ? chatHistory[chatHistoryPos] : ''
|
|
|
|
} else if (e.keyCode === 40) {
|
|
|
|
if (chatHistoryPos === chatHistory.length) return
|
|
|
|
chatInput.value = chatHistory[++chatHistoryPos] !== undefined ? chatHistory[chatHistoryPos] : ''
|
2021-02-27 18:18:58 -05:00
|
|
|
}
|
2021-03-15 22:16:37 -04:00
|
|
|
})
|
2021-02-27 18:18:58 -05:00
|
|
|
|
2021-12-24 07:00:07 -05:00
|
|
|
const keyBindScrn = document.getElementById('keybinds-screen')
|
|
|
|
|
2021-03-14 15:34:35 -04:00
|
|
|
// Chat events
|
2021-03-15 22:16:37 -04:00
|
|
|
document.addEventListener('keypress', e => {
|
2021-04-06 07:31:02 -04:00
|
|
|
if (gameMenu.inMenu) return
|
2021-03-14 15:34:35 -04:00
|
|
|
e = e || window.event
|
|
|
|
if (self.inChat === false) {
|
2021-12-24 07:00:07 -05:00
|
|
|
keyBindScrn.keymaps.forEach(km => {
|
|
|
|
if (e.code === km.key) {
|
|
|
|
switch (km.defaultKey) {
|
|
|
|
case 'KeyT':
|
|
|
|
setTimeout(() => enableChat(false), 0)
|
|
|
|
break
|
|
|
|
case 'Slash':
|
|
|
|
setTimeout(() => enableChat(true), 0)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2021-03-14 15:34:35 -04:00
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!self.inChat) return
|
|
|
|
e.stopPropagation()
|
|
|
|
if (e.code === 'Enter') {
|
|
|
|
chatHistory.push(chatInput.value)
|
|
|
|
client.write('chat', { message: chatInput.value })
|
|
|
|
disableChat()
|
|
|
|
}
|
2021-03-15 22:16:37 -04:00
|
|
|
})
|
2021-03-14 15:34:35 -04:00
|
|
|
// Enable inputs back when focused
|
|
|
|
/* document.addEventListener("pointerlockchange", function(event) {
|
|
|
|
const canvas = document.getElementById("noa-canvas");
|
|
|
|
if (
|
|
|
|
document.pointerLockElement === canvas ||
|
|
|
|
document.mozPointerLockElement === canvas
|
|
|
|
) {
|
|
|
|
// Someone focused the game back so we hide chat.
|
|
|
|
chatState.inChat = false;
|
|
|
|
hideChat();
|
|
|
|
}
|
|
|
|
}); */
|
|
|
|
function enableChat (isCommand) {
|
|
|
|
// Set inChat value
|
|
|
|
self.inChat = true
|
|
|
|
// Exit the pointer lock
|
|
|
|
document.exitPointerLock()
|
|
|
|
// Show chat input
|
|
|
|
chatInput.style.display = 'block'
|
|
|
|
// Show extended chat history
|
2021-12-24 07:00:07 -05:00
|
|
|
chat.style.maxHeight = 'calc(var(--chatHeight) * var(--chatScale))'
|
2021-03-14 15:34:35 -04:00
|
|
|
chat.scrollTop = chat.scrollHeight // Stay bottom of the list
|
|
|
|
if (isCommand) { // handle commands
|
|
|
|
chatInput.value = '/'
|
|
|
|
}
|
|
|
|
// Focus element
|
|
|
|
chatInput.focus()
|
|
|
|
chatHistoryPos = chatHistory.length
|
2021-12-24 07:00:07 -05:00
|
|
|
document.querySelector('#hud').shadowRoot.querySelector('#chat').shadowRoot.querySelectorAll('.chat-message').forEach(e => e.classList.add('chat-message-chat-opened'))
|
2021-02-27 18:18:58 -05:00
|
|
|
}
|
2021-03-01 23:06:51 -05:00
|
|
|
|
2021-03-14 15:34:35 -04:00
|
|
|
function disableChat () {
|
|
|
|
self.inChat = false
|
|
|
|
hideChat()
|
|
|
|
renderer.domElement.requestPointerLock()
|
|
|
|
}
|
2021-03-01 23:06:51 -05:00
|
|
|
|
2021-03-14 15:34:35 -04:00
|
|
|
function hideChat () {
|
|
|
|
// Clear chat input
|
|
|
|
chatInput.value = ''
|
|
|
|
// Unfocus it
|
|
|
|
chatInput.blur()
|
|
|
|
// Hide it
|
|
|
|
chatInput.style.display = 'none'
|
|
|
|
// Hide extended chat history
|
2021-12-24 07:00:07 -05:00
|
|
|
chat.style.maxHeight = 'var(--chatHeight)'
|
2021-03-14 15:34:35 -04:00
|
|
|
chat.scrollTop = chat.scrollHeight // Stay bottom of the list
|
2021-12-24 07:00:07 -05:00
|
|
|
document.querySelector('#hud').shadowRoot.querySelector('#chat').shadowRoot.querySelectorAll('.chat-message').forEach(e => e.classList.remove('chat-message-chat-opened'))
|
2021-03-14 15:34:35 -04:00
|
|
|
}
|
2021-02-27 18:18:58 -05:00
|
|
|
|
2021-03-14 15:34:35 -04:00
|
|
|
function readExtra (extra) {
|
|
|
|
const shouldReturn = []
|
|
|
|
for (const i in extra) {
|
|
|
|
if (extra[i].text) {
|
|
|
|
shouldReturn.push({
|
|
|
|
text: extra[i].text,
|
|
|
|
color: extra[i].color,
|
|
|
|
bold: !!extra[i].bold,
|
|
|
|
italic: !!extra[i].italic,
|
|
|
|
underlined: !!extra[i].underlined,
|
|
|
|
strikethrough: !!extra[i].strikethrough,
|
|
|
|
obfuscated: !!extra[i].obfuscated
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
readExtra(extra).forEach(function (el) {
|
|
|
|
shouldReturn.push(el)
|
|
|
|
})
|
|
|
|
}
|
2021-02-27 18:18:58 -05:00
|
|
|
}
|
2021-03-14 15:34:35 -04:00
|
|
|
return shouldReturn
|
2021-02-27 18:18:58 -05:00
|
|
|
}
|
2021-03-01 23:06:51 -05:00
|
|
|
|
2021-03-14 15:34:35 -04:00
|
|
|
client.on('chat', (packet) => {
|
|
|
|
// Reading of chat message
|
|
|
|
const fullmessage = JSON.parse(packet.message.toString())
|
|
|
|
let msglist = []
|
|
|
|
if (
|
|
|
|
fullmessage.extra &&
|
|
|
|
fullmessage.extra.length > 0 &&
|
|
|
|
!fullmessage.translate
|
|
|
|
) {
|
|
|
|
msglist = readExtra(fullmessage.extra)
|
|
|
|
} else if (fullmessage.text && fullmessage.text.length > 0) {
|
|
|
|
msglist.push({ text: fullmessage.text, color: undefined })
|
|
|
|
} else if (dictionary[fullmessage.translate]) {
|
|
|
|
let msg = dictionary[fullmessage.translate]
|
|
|
|
fullmessage.with.forEach(obj => {
|
|
|
|
if (obj.insertion && obj.text) {
|
|
|
|
msg = msg.replace('%s', obj.text)
|
|
|
|
}
|
|
|
|
if (obj.extra) {
|
|
|
|
if (obj.text && obj.text.length > 0) {
|
|
|
|
msglist.push({ text: obj.text, color: undefined })
|
|
|
|
} else {
|
|
|
|
const text = readExtra(obj.extra)
|
|
|
|
if (text.length > 1) {
|
|
|
|
console.log('Unsupported chat alert :(')
|
|
|
|
}
|
|
|
|
msg = msg.replace('%s', text[0].text)
|
|
|
|
msglist.push({ text: msg, color: undefined })
|
2021-02-27 18:18:58 -05:00
|
|
|
}
|
|
|
|
}
|
2021-03-14 15:34:35 -04:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
// msglist.push({
|
|
|
|
// text:
|
|
|
|
// "Unsupported message (Please report this):\n" +
|
|
|
|
// JSON.stringify(fullmessage),
|
|
|
|
// color: undefined
|
|
|
|
// });
|
|
|
|
}
|
|
|
|
const li = document.createElement('li')
|
|
|
|
msglist.forEach(msg => {
|
|
|
|
const span = document.createElement('span')
|
|
|
|
span.appendChild(document.createTextNode(msg.text))
|
|
|
|
span.setAttribute(
|
|
|
|
'style',
|
|
|
|
`${msg.color ? styles[msg.color.toLowerCase()] : styles.white}; ${
|
|
|
|
msg.bold ? styles.bold + ';' : ''
|
|
|
|
}${msg.italic ? styles.italic + ';' : ''}${
|
|
|
|
msg.strikethrough ? styles.strikethrough + ';' : ''
|
|
|
|
}${msg.underlined ? styles.underlined + ';' : ''}`
|
|
|
|
)
|
|
|
|
li.appendChild(span)
|
2021-02-27 18:18:58 -05:00
|
|
|
})
|
2021-03-14 15:34:35 -04:00
|
|
|
chat.appendChild(li)
|
|
|
|
chat.scrollTop = chat.scrollHeight // Stay bottom of the list
|
2021-11-05 13:00:28 -04:00
|
|
|
// fading
|
|
|
|
li.classList.add('chat-message')
|
|
|
|
if (this.inChat) {
|
|
|
|
li.classList.add('chat-message-chat-opened')
|
|
|
|
}
|
|
|
|
setTimeout(() => {
|
|
|
|
li.classList.add('chat-message-fadeout')
|
|
|
|
li.classList.add('chat-message-fade')
|
|
|
|
setTimeout(() => {
|
|
|
|
li.classList.add('chat-message-faded')
|
|
|
|
}, 3000)
|
|
|
|
}, 5000)
|
2021-02-27 18:18:58 -05:00
|
|
|
})
|
2021-03-01 23:06:51 -05:00
|
|
|
|
2021-03-14 15:34:35 -04:00
|
|
|
hideChat()
|
|
|
|
}
|
2021-02-27 18:18:58 -05:00
|
|
|
}
|
2021-03-14 15:34:35 -04:00
|
|
|
|
|
|
|
window.customElements.define('chat-box', ChatBox)
|