RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
WeightedCollection.java
1package com.osroyale.game.world.entity.mob.npc.drop;
2
3import java.util.NavigableMap;
4import java.util.Random;
5import java.util.TreeMap;
6
7public class WeightedCollection<E> {
8 private final NavigableMap<Double, E> map = new TreeMap<>();
9 private final Random random;
10 private double total = 0;
11
12 public double getTotal() {
13 return total;
14 }
15
16 public WeightedCollection() {
17 this(new Random());
18 }
19
20 public WeightedCollection(Random random) {
21 this.random = random;
22 }
23
24 public WeightedCollection<E> add(double weight, E result) {
25 if (weight <= 0) return this;
26 total += weight;
27 map.put(total, result);
28 return this;
29 }
30
31 public E next() {
32 double value = random.nextDouble() * total;
33 return map.higherEntry(value).getValue();
34 }
35}