Search in sources :

Example 6 with WeightedTable

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;
}
Also used : KeyProvider(org.spongepowered.common.registry.provider.KeyProvider) HashMap(java.util.HashMap) Supplier(java.util.function.Supplier) Value(org.spongepowered.api.data.value.Value) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) BiPredicate(java.util.function.BiPredicate) WeightedCollectionValue(org.spongepowered.api.data.value.WeightedCollectionValue) AbstractResourceKeyedBuilder(org.spongepowered.common.util.AbstractResourceKeyedBuilder) Map(java.util.Map) Nullable(org.checkerframework.checker.nullness.qual.Nullable) TypeFactory(io.leangen.geantyref.TypeFactory) ListValue(org.spongepowered.api.data.value.ListValue) Set(java.util.Set) TypeToken(io.leangen.geantyref.TypeToken) SetValue(org.spongepowered.api.data.value.SetValue) Key(org.spongepowered.api.data.Key) Objects(java.util.Objects) List(java.util.List) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WeightedTable(org.spongepowered.api.util.weighted.WeightedTable) Preconditions(com.google.common.base.Preconditions) GenericTypeReflector(io.leangen.geantyref.GenericTypeReflector) MapValue(org.spongepowered.api.data.value.MapValue) Comparator(java.util.Comparator) WeightedTable(org.spongepowered.api.util.weighted.WeightedTable) ArrayList(java.util.ArrayList) MapValue(org.spongepowered.api.data.value.MapValue) Value(org.spongepowered.api.data.value.Value) WeightedCollectionValue(org.spongepowered.api.data.value.WeightedCollectionValue) ListValue(org.spongepowered.api.data.value.ListValue) SetValue(org.spongepowered.api.data.value.SetValue) MapValue(org.spongepowered.api.data.value.MapValue) SetValue(org.spongepowered.api.data.value.SetValue) Key(org.spongepowered.api.data.Key) HashSet(java.util.HashSet)

Example 7 with WeightedTable

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);
}
Also used : Weather(org.spongepowered.api.world.weather.Weather) LanternWeather(org.lanternpowered.server.world.weather.LanternWeather) LanternWeather(org.lanternpowered.server.world.weather.LanternWeather) WeightedTable(org.spongepowered.api.util.weighted.WeightedTable) ArrayList(java.util.ArrayList)

Aggregations

WeightedTable (org.spongepowered.api.util.weighted.WeightedTable)7 EntityArchetype (org.spongepowered.api.entity.EntityArchetype)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Set (java.util.Set)2 CompoundTag (net.minecraft.nbt.CompoundTag)2 EntityType (org.spongepowered.api.entity.EntityType)2 Preconditions (com.google.common.base.Preconditions)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Pair (com.mojang.datafixers.util.Pair)1 GenericTypeReflector (io.leangen.geantyref.GenericTypeReflector)1 TypeFactory (io.leangen.geantyref.TypeFactory)1 TypeToken (io.leangen.geantyref.TypeToken)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Objects (java.util.Objects)1