2021-03-14 14:33:41 -04:00
const { LitElement , html , css } = require ( 'lit-element' )
2021-03-14 14:56:54 -04:00
const invsprite = require ( './invsprite.json' )
2021-03-14 14:33:41 -04:00
class HotBar extends LitElement {
updated ( changedProperties ) {
if ( changedProperties . has ( 'bot' ) ) {
// inventory listener
this . bot . on ( 'spawn' , ( ) => {
this . init ( )
} )
}
}
init ( ) {
this . reloadHotbar ( )
this . reloadHotbarSelected ( 0 )
document . addEventListener ( 'wheel' , ( e ) => {
const newSlot = ( ( this . bot . quickBarSlot + Math . sign ( e . deltaY ) ) % 9 + 9 ) % 9
this . reloadHotbarSelected ( newSlot )
} )
this . bot . inventory . on ( 'updateSlot' , ( slot , oldItem , newItem ) => {
if ( slot >= this . bot . inventory . hotbarStart + 9 ) return
if ( slot < this . bot . inventory . hotbarStart ) return
2021-03-14 14:56:54 -04:00
const sprite = newItem ? invsprite [ newItem . name ] : invsprite . air
const slotImage = this . shadowRoot . getElementById ( 'hotbar-' + ( slot - this . bot . inventory . hotbarStart ) )
slotImage . style [ 'background-position-x' ] = ` - ${ sprite . x * 2 } px `
slotImage . style [ 'background-position-y' ] = ` - ${ sprite . y * 2 } px `
2021-03-15 16:17:33 -04:00
slotImage . innerHTML = newItem ? . count > 1 ? newItem . count : ''
2021-03-14 14:33:41 -04:00
} )
}
async reloadHotbar ( ) {
for ( let i = 0 ; i < 9 ; i ++ ) {
2021-03-14 14:56:54 -04:00
const item = this . bot . inventory . slots [ this . bot . inventory . hotbarStart + i ]
const sprite = item ? invsprite [ item . name ] : invsprite . air
const slotImage = this . shadowRoot . getElementById ( 'hotbar-' + i )
slotImage . style [ 'background-position-x' ] = ` - ${ sprite . x * 2 } px `
slotImage . style [ 'background-position-y' ] = ` - ${ sprite . y * 2 } px `
2021-03-15 16:17:33 -04:00
slotImage . innerHTML = item ? . count > 1 ? item . count : ''
2021-03-14 14:33:41 -04:00
}
}
async reloadHotbarSelected ( slot ) {
const planned = ( 20 * 4 * slot ) + 'px'
this . shadowRoot . getElementById ( 'hotbar-highlight' ) . style . marginLeft = planned
this . bot . setQuickBarSlot ( slot )
}
static get properties ( ) {
return {
bot : { type : Object } ,
viewerVersion : { type : String }
}
}
static get styles ( ) {
return css `
2021-03-15 16:17:33 -04:00
# hotbar - wrapper {
image - rendering : optimizeSpeed ;
image - rendering : - moz - crisp - edges ;
image - rendering : - webkit - optimize - contrast ;
image - rendering : - o - crisp - edges ;
image - rendering : pixelated ;
- ms - interpolation - mode : nearest - neighbor ;
}
2021-03-14 14:33:41 -04:00
# hotbar - image {
2021-03-15 16:17:33 -04:00
position : absolute ;
top : 100 % ;
left : 50 % ;
height : calc ( 256 px * 4 ) ;
width : calc ( 256 px * 4 ) ;
transform : translate ( calc ( - 182 px * 2 ) , calc ( - 22 px * 4 ) ) ;
clip - path : inset ( 0 px calc ( 74 px * 4 ) calc ( 234 px * 4 ) 0 px ) ;
2021-03-14 14:33:41 -04:00
}
2021-03-15 16:17:33 -04:00
2021-03-14 14:33:41 -04:00
# hotbar - items - wrapper {
2021-03-15 16:17:33 -04:00
position : absolute ;
top : 100 % ;
left : 50 % ;
height : calc ( 256 px * 4 ) ;
width : calc ( 256 px * 4 ) ;
transform : translate ( calc ( - 182 px * 2 ) , calc ( - 22 px * 4 ) ) ;
clip - path : inset ( 0 px calc ( 74 px * 4 ) calc ( 234 px * 4 ) 0 px ) ;
2021-03-14 14:33:41 -04:00
}
# hotbar - highlight {
2021-03-15 16:17:33 -04:00
position : absolute ;
top : 100 % ;
left : 50 % ;
height : calc ( 256 px * 4 ) ;
width : calc ( 256 px * 4 ) ;
margin - left : calc ( 20 px * 4 * 4 ) ; /* EDIT THIS TO CHANGE WHICH SLOT IS SELECTED */
transform : translate ( calc ( ( - 24 px * 2 ) - ( 20 px * 4 * 4 ) ) , calc ( ( - 22 px * 4 ) + ( - 24 px * 4 ) + 4 px ) ) ; /* first need to translate up to account for clipping, then account for size of image, then 1px to center vertically over the image*/
clip - path : inset ( calc ( 22 px * 4 ) calc ( 232 px * 4 ) calc ( 210 px * 4 ) 0 px ) ;
2021-03-14 14:33:41 -04:00
}
. hotbar - item {
2021-03-15 16:17:33 -04:00
display : inline - block ;
height : 32 px ;
width : 64 px ;
margin - top : 12 px ;
margin - left : 12 px ;
margin - right : 4.25 px ;
background - image : url ( 'invsprite.png' ) ;
background - size : 2048 px auto ;
text - align : right ;
font - size : 32 px ;
vertical - align : top ;
padding - top : 32 px ;
color : # ffffff ;
text - shadow : 2 px 2 px 0 px # 3 f3f3f ;
font - family : mojangles , minecraft , monospace ;
2021-03-14 14:33:41 -04:00
}
`
}
render ( ) {
return html `
< div id = "hotbar-wrapper" >
< img id = "hotbar-image" src = "textures/1.16.4/gui/widgets.png" >
< img id = "hotbar-highlight" src = "textures/1.16.4/gui/widgets.png" >
< div id = "hotbar-items-wrapper" >
2021-03-15 16:17:33 -04:00
< div class = "hotbar-item" id = "hotbar-0" > < / d i v
> < div class = "hotbar-item" id = "hotbar-1" > < / d i v
> < div class = "hotbar-item" id = "hotbar-2" > < / d i v
> < div class = "hotbar-item" id = "hotbar-3" > < / d i v
> < div class = "hotbar-item" id = "hotbar-4" > < / d i v
> < div class = "hotbar-item" id = "hotbar-5" > < / d i v
> < div class = "hotbar-item" id = "hotbar-6" > < / d i v
> < div class = "hotbar-item" id = "hotbar-7" > < / d i v
> < div class = "hotbar-item" id = "hotbar-8" > < / d i v >
2021-03-14 14:33:41 -04:00
< / d i v >
< / d i v >
`
}
}
window . customElements . define ( 'hot-bar' , HotBar )