use of org.spongepowered.api.util.weighted.WeightedTable in project SpongeCommon by SpongePowered.
the class SpongeKeyBuilder method build0.
@Override
public Key<V> build0() {
Objects.requireNonNull(this.valueType, "The value type must be set");
Objects.requireNonNull(this.elementType, "The element type must be set");
BiPredicate<? super E, ? super E> includesTester = this.includesTester;
if (includesTester == null) {
includesTester = (e, e2) -> false;
}
Comparator<? super E> comparator = this.comparator;
if (comparator == null) {
if (Comparable.class.isAssignableFrom(GenericTypeReflector.erase(this.elementType))) {
// noinspection unchecked
comparator = Comparator.comparing(o -> ((Comparable) o));
} else {
comparator = (o1, o2) -> {
if (o1.equals(o2))
return 0;
// There could be collisions, but yeah, what can you do about that..
if (o1.hashCode() > o2.hashCode())
return 1;
return -1;
};
}
}
Supplier<E> defaultValueSupplier = () -> null;
final Class<?> rawType = GenericTypeReflector.erase(this.valueType);
if (ListValue.class.isAssignableFrom(rawType)) {
defaultValueSupplier = () -> (E) new ArrayList();
} else if (SetValue.class.isAssignableFrom(rawType)) {
defaultValueSupplier = () -> (E) new HashSet();
} else if (WeightedCollectionValue.class.isAssignableFrom(rawType)) {
defaultValueSupplier = () -> (E) new WeightedTable();
} else if (MapValue.class.isAssignableFrom(rawType)) {
defaultValueSupplier = () -> (E) new HashMap<>();
}
final SpongeKey<Value<E>, E> key = new SpongeKey<>(this.key, this.valueType, this.elementType, comparator, includesTester, defaultValueSupplier);
KeyProvider.INSTANCE.register(this.key, (Key<Value<?>>) (Object) key);
return (Key<V>) key;
}
use of org.spongepowered.api.util.weighted.WeightedTable in project LanternServer by LanternPowered.
the class LanternWeatherUniverse method nextWeather.
/**
* Gets the next possible {@link LanternWeather}, ignoring
* the last weather type.
*
* @return The next weather type
*/
@SuppressWarnings("unchecked")
private LanternWeather nextWeather() {
final List<LanternWeather> weathers = new ArrayList(this.world.game.getRegistry().getAllOf(Weather.class));
final LanternWeather current = this.weatherData.getWeather();
weathers.remove(current);
if (weathers.isEmpty()) {
return current;
}
final WeightedTable<LanternWeather> table = new WeightedTable<>();
weathers.forEach(weather -> table.add(weather, weather.getWeight()));
return table.get(this.random).get(0);
}
Aggregations