diff options
author | Alkalus <3060479+draknyte1@users.noreply.github.com> | 2019-12-09 21:03:31 +0000 |
---|---|---|
committer | Alkalus <3060479+draknyte1@users.noreply.github.com> | 2019-12-09 21:03:31 +0000 |
commit | 051c46ab36a749954cff3bae1fbd44cea6f1fc99 (patch) | |
tree | baad2cc9886c5f0b7bf3a381c7002af9fe3ac80e /src/Java/gtPlusPlus/api/objects/data/WeightedCollection.java | |
parent | e49fbd1330c0875ff531ff25119afe15b54c9448 (diff) | |
download | GT5-Unofficial-051c46ab36a749954cff3bae1fbd44cea6f1fc99.tar.gz GT5-Unofficial-051c46ab36a749954cff3bae1fbd44cea6f1fc99.tar.bz2 GT5-Unofficial-051c46ab36a749954cff3bae1fbd44cea6f1fc99.zip |
+ Added GT++ API class.
+ Added handler for Void Miner to API.
+ Added handler for special multiblock logic to API.
+ Added Initial work For Chemical Plant.
% More work on the Algae Farm.
Diffstat (limited to 'src/Java/gtPlusPlus/api/objects/data/WeightedCollection.java')
-rw-r--r-- | src/Java/gtPlusPlus/api/objects/data/WeightedCollection.java | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/src/Java/gtPlusPlus/api/objects/data/WeightedCollection.java b/src/Java/gtPlusPlus/api/objects/data/WeightedCollection.java new file mode 100644 index 0000000000..f9966474b0 --- /dev/null +++ b/src/Java/gtPlusPlus/api/objects/data/WeightedCollection.java @@ -0,0 +1,102 @@ +package gtPlusPlus.api.objects.data; + +import java.util.Collection; +import java.util.Map; +import java.util.NavigableMap; +import java.util.Random; +import java.util.Set; +import java.util.TreeMap; + +import gtPlusPlus.api.objects.random.XSTR; + +public class WeightedCollection<E> implements Map<Integer, E> { + + private NavigableMap<Integer, E> map = new TreeMap<Integer, E>(); + private Random random; + private int total = 0; + + public WeightedCollection() { + this(new XSTR()); + } + + public WeightedCollection(Random random) { + this.random = random; + } + + public E add(int weight, E object) { + if (weight <= 0) return null; + total += weight; + return map.put(total, object); + } + + private E next() { + int value = random.nextInt(total) + 1; // Can also use floating-point weights + return map.ceilingEntry(value).getValue(); + } + + @Override + public int size() { + return map.size(); + } + + @Override + public boolean isEmpty() { + return map.isEmpty(); + } + + @Override + public boolean containsKey(Object key) { + return map.containsKey(key); + } + + @Override + public boolean containsValue(Object value) { + return map.containsValue(value); + } + + public E get() { + return next(); + } + + @Override + public E get(Object key) { + return next(); + } + + @Override + public void putAll(Map m) { + map.putAll(m); + } + + @Override + public void clear() { + map.clear(); + this.total = 0; + } + + @Override + public Set keySet() { + return map.keySet(); + } + + @Override + public Collection values() { + return map.values(); + } + + @Override + public Set entrySet() { + return map.entrySet(); + } + + @Override + public E put(Integer key, E value) { + return add(key, value); + } + + @Override + public E remove(Object key) { + return map.remove(key); + } + +}
\ No newline at end of file |