Skip to content

Commit 52c6d2a

Browse files
authored
Add BossBars (#319)
* Add component * Add preload function to hud (required for already existing BossBars) * Remove useless debug log * Display bars * Values and colors * Add dividers * Add titles * Remove ; * Make titles smaller * Delete BossBars * Store the container * Update code standard * Move BossBars to components * Proper use of LitElement * Code style improvements * init hotbar before player spawn, fixes hotbar not working when joining
1 parent d82a70a commit 52c6d2a

File tree

3 files changed

+161
-1
lines changed

3 files changed

+161
-1
lines changed

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require('./lib/menus/components/food_bar')
1010
require('./lib/menus/components/breath_bar')
1111
require('./lib/menus/components/debug_overlay')
1212
require('./lib/menus/components/playerlist_overlay')
13+
require('./lib/menus/components/bossbars_overlay')
1314
require('./lib/menus/hud')
1415
require('./lib/menus/play_screen')
1516
require('./lib/menus/pause_screen')
@@ -186,6 +187,7 @@ async function connect (options) {
186187
noPongTimeout: 240 * 1000,
187188
closeTimeout: 240 * 1000
188189
})
190+
hud.preload(bot)
189191

190192
bot.on('error', (err) => {
191193
console.log('Encountered error!', err)
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
const { LitElement, html, css } = require('lit')
2+
const { styleMap } = require('lit/directives/style-map.js')
3+
4+
const colors = ['pink', 'blue', 'red', 'green', 'yellow', 'purple', 'white']
5+
const divs = [0, 6, 10, 12, 20]
6+
const translations = {
7+
'entity.minecraft.ender_dragon': 'Ender Dragon',
8+
'entity.minecraft.wither': 'Wither'
9+
}
10+
class BossBar extends LitElement {
11+
constructor (bar) {
12+
super()
13+
this.bar = bar
14+
this.title = ''
15+
this.progress = 0
16+
this.bossBarStyles = {}
17+
this.fillStyles = {}
18+
this.div1Styles = {}
19+
this.div2Styles = {}
20+
}
21+
22+
static get styles () {
23+
return css`
24+
.container {
25+
display: flex;
26+
flex-direction: column;
27+
align-items: center;
28+
}
29+
.title {
30+
font-size: 7px;
31+
color: #fff;
32+
}
33+
.bossbar {
34+
background-image: url("textures/1.18.1/gui/bars.png");
35+
width: 182px;
36+
height: 5px;
37+
position: relative;
38+
}
39+
.bossbar .fill {
40+
content: "";
41+
position: absolute;
42+
top: 0;
43+
left: 0;
44+
height: 5px;
45+
width: 0;
46+
background-image: url("textures/1.18.1/gui/bars.png");
47+
}`
48+
}
49+
50+
static get properties () {
51+
return {
52+
bar: { type: Object }
53+
}
54+
}
55+
56+
render () {
57+
this.updateBar(this.bar)
58+
59+
return html`
60+
<div class="container">
61+
<div class="title">${this.title}</div>
62+
<div class="bossbar" style=${styleMap(this.bossBarStyles)}>
63+
<div class="fill" style=${styleMap(this.fillStyles)}></div>
64+
<div class="fill" style=${styleMap(this.div1Styles)}></div>
65+
<div class="fill" style=${styleMap(this.div2Styles)}></div>
66+
</div>
67+
</div>`
68+
}
69+
70+
setTitle (bar) {
71+
if (bar._title.text) this.title = bar._title.text
72+
else this.title = translations[this._title.translate] || 'Unkown Entity'
73+
}
74+
75+
setColor (bar) {
76+
this.bossBarStyles.backgroundPositionY = `-${colors.indexOf(bar._color) * 10}px`
77+
this.fillStyles.backgroundPositionY = `-${colors.indexOf(bar._color) * 10 + 5}px`
78+
}
79+
80+
setProgress (bar) {
81+
this.fillStyles.width = `${bar._health * 100}%`
82+
this.div2Styles.width = `${bar._health * 100}%`
83+
}
84+
85+
setDiv (bar) {
86+
this.div1Styles.backgroundPositionY = `-${divs.indexOf(bar._dividers) * 10 + 70}px`
87+
this.div2Styles.backgroundPositionY = `-${divs.indexOf(bar._dividers) * 10 + 75}px`
88+
}
89+
90+
updateBar (bar) {
91+
this.setTitle(bar)
92+
this.setColor(bar)
93+
this.setDiv(bar)
94+
this.setProgress(bar)
95+
}
96+
}
97+
98+
class BossBars extends LitElement {
99+
constructor () {
100+
super()
101+
this.bossBars = new Map()
102+
}
103+
104+
static get styles () {
105+
return css`
106+
.bossBars {
107+
display: flex;
108+
flex-direction: column;
109+
gap: 5px;
110+
position: absolute;
111+
top: 9px;
112+
left: 50%;
113+
transform: translate(-50%);
114+
}`
115+
}
116+
117+
static get properties () {
118+
return {
119+
bossBars: { type: Map }
120+
}
121+
}
122+
123+
render () {
124+
return html`<div class="bossBars" id="bossBars">
125+
${[...this.bossBars.values()]}
126+
</div>`
127+
}
128+
129+
init () {
130+
this.bot.on('bossBarCreated', async (bossBar) => {
131+
this.bossBars.set(bossBar.entityUUID, new BossBar(bossBar))
132+
this.requestUpdate()
133+
})
134+
this.bot.on('bossBarUpdated', (bossBar) => {
135+
const bar = this.bossBars.get(bossBar.entityUUID)
136+
bar.bar = bossBar
137+
bar.requestUpdate()
138+
})
139+
this.bot.on('bossBarDeleted', (bossBar) => {
140+
this.bossBars.delete(bossBar.entityUUID)
141+
this.requestUpdate()
142+
})
143+
}
144+
}
145+
146+
window.customElements.define('pmui-bossbars-overlay', BossBars)
147+
window.customElements.define('pmui-bossbar', BossBar)

lib/menus/hud.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,16 @@ class Hud extends LitElement {
192192
}
193193
}
194194

195+
/**
196+
* @param {import('mineflayer').Bot} bot
197+
*/
198+
preload (bot) {
199+
const bossBars = this.shadowRoot.querySelector('#bossbars-overlay')
200+
bossBars.bot = bot
201+
202+
bossBars.init()
203+
}
204+
195205
/**
196206
* @param {globalThis.THREE.Renderer} renderer
197207
* @param {import('mineflayer').Bot} bot
@@ -211,6 +221,7 @@ class Hud extends LitElement {
211221
hotbar.bot = bot
212222
debugMenu.bot = bot
213223

224+
hotbar.init()
214225
chat.init(bot._client, renderer)
215226
bot.on('spawn', () => playerList.init(bot, host))
216227

@@ -260,7 +271,6 @@ class Hud extends LitElement {
260271
healthbar.updateHealth(bot.health)
261272
foodbar.updateHunger(bot.food)
262273
// breathbar.updateOxygen(bot.oxygenLevel ?? 20)
263-
hotbar.init()
264274
})
265275

266276
if (document.getElementById('options-screen').forceMobileControls || isMobile()) {
@@ -342,6 +352,7 @@ class Hud extends LitElement {
342352
343353
<pmui-debug-overlay id="debug-overlay"></pmui-debug-overlay>
344354
<pmui-playerlist-overlay id="playerlist-overlay"></pmui-playerlist-overlay>
355+
<pmui-bossbars-overlay id="bossbars-overlay"></pmui-bossbars-overlay>
345356
<div class="crosshair"></div>
346357
<chat-box id="chat"></chat-box>
347358
<!--<pmui-breathbar id="breath-bar"></pmui-breathbar>-->

0 commit comments

Comments
 (0)