|
| 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) |
0 commit comments