mirror of
https://github.com/misode/misode.github.io.git
synced 2026-04-24 23:56:51 +00:00
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:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user