Add loot table preview (#293)

* Initial loot table preview + item display counts

* Add loot functions without NBT

* Add loot conditions

* Render item tooltips with name and lore components

* Remove debug text component

* Disable advanced tooltips in the tree

* Minor style fixes

* Add item slot overlay and tweak tooltip offset

* Fix some items not rendering

* Translate item names and text components

* Translate params + more functions and tooltips

* Add durability bar

* Configurable stack mixing

* Correct tooltip background and border

* Add enchanting

* Enchantment glint

* Configurable luck, daytime and weather

* Improve tooltip spacing

* More tooltip spacing improvements

* Remove debug logging
This commit is contained in:
Misode
2022-10-13 02:05:33 +02:00
committed by GitHub
parent 86687ea6b9
commit ac259bb83e
24 changed files with 1630 additions and 56 deletions

View File

@@ -1,6 +1,7 @@
import type { DataModel } from '@mcschema/core'
import { Path } from '@mcschema/core'
import * as zip from '@zip.js/zip.js'
import type { Random } from 'deepslate/core'
import yaml from 'js-yaml'
import { route } from 'preact-router'
import rfdc from 'rfdc'
@@ -337,3 +338,21 @@ export async function computeIfAbsentAsync<K, V>(map: Map<K, V>, key: K, getter:
map.set(key, value)
return value
}
export function getWeightedRandom<T>(random: Random, entries: T[], getWeight: (entry: T) => number) {
let totalWeight = 0
for (const entry of entries) {
totalWeight += getWeight(entry)
}
if (totalWeight <= 0) {
return undefined
}
let n = random.nextInt(totalWeight)
for (const entry of entries) {
n -= getWeight(entry)
if (n < 0) {
return entry
}
}
return undefined
}