| 1 | package net.minecraft.world.flag;
|
| 2 |
|
| 3 | import com.google.common.collect.Sets;
|
| 4 | import com.mojang.logging.LogUtils;
|
| 5 | import com.mojang.serialization.Codec;
|
| 6 | import com.mojang.serialization.DataResult;
|
| 7 | import java.util.Arrays;
|
| 8 | import java.util.HashSet;
|
| 9 | import java.util.LinkedHashMap;
|
| 10 | import java.util.List;
|
| 11 | import java.util.Map;
|
| 12 | import java.util.Set;
|
| 13 | import java.util.function.Consumer;
|
| 14 | import net.minecraft.resources.Identifier;
|
| 15 | import org.slf4j.Logger;
|
| 16 |
|
| 17 | public class FeatureFlagRegistry {
|
| 18 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 19 | private final FeatureFlagUniverse universe;
|
| 20 | private final Map<Identifier, FeatureFlag> names;
|
| 21 | private final FeatureFlagSet allFlags;
|
| 22 |
|
| 23 | private FeatureFlagRegistry(final FeatureFlagUniverse universe, final FeatureFlagSet allFlags, final Map<Identifier, FeatureFlag> names) {
|
| 24 | this.universe = universe;
|
| 25 | this.names = names;
|
| 26 | this.allFlags = allFlags;
|
| 27 | }
|
| 28 |
|
| 29 | public boolean isSubset(final FeatureFlagSet set) {
|
| 30 | return set.isSubsetOf(this.allFlags);
|
| 31 | }
|
| 32 |
|
| 33 | public FeatureFlagSet allFlags() {
|
| 34 | return this.allFlags;
|
| 35 | }
|
| 36 |
|
| 37 | public FeatureFlagSet fromNames(final Iterable<Identifier> flagIds) {
|
| 38 | return this.fromNames(flagIds, flagId -> LOGGER.warn("Unknown feature flag: {}", flagId));
|
| 39 | }
|
| 40 |
|
| 41 | public FeatureFlagSet subset(final FeatureFlag... flags) {
|
| 42 | return FeatureFlagSet.create(this.universe, Arrays.asList(flags));
|
| 43 | }
|
| 44 |
|
| 45 | public FeatureFlagSet fromNames(final Iterable<Identifier> flagIds, final Consumer<Identifier> unknownFlags) {
|
| 46 | Set<FeatureFlag> flags = Sets.newIdentityHashSet();
|
| 47 |
|
| 48 | for (Identifier flagId : flagIds) {
|
| 49 | FeatureFlag flag = this.names.get(flagId);
|
| 50 | if (flag == null) {
|
| 51 | unknownFlags.accept(flagId);
|
| 52 | } else {
|
| 53 | flags.add(flag);
|
| 54 | }
|
| 55 | }
|
| 56 |
|
| 57 | return FeatureFlagSet.create(this.universe, flags);
|
| 58 | }
|
| 59 |
|
| 60 | public Set<Identifier> toNames(final FeatureFlagSet set) {
|
| 61 | Set<Identifier> result = new HashSet<>();
|
| 62 | this.names.forEach((id, flag) -> {
|
| 63 | if (set.contains(flag)) {
|
| 64 | result.add(id);
|
| 65 | }
|
| 66 | });
|
| 67 | return result;
|
| 68 | }
|
| 69 |
|
| 70 | public Codec<FeatureFlagSet> codec() {
|
| 71 | return Identifier.CODEC.listOf().comapFlatMap(ids -> {
|
| 72 | Set<Identifier> unknownIds = new HashSet<>();
|
| 73 | FeatureFlagSet result = this.fromNames(ids, unknownIds::add);
|
| 74 | return !unknownIds.isEmpty() ? DataResult.error(() -> "Unknown feature ids: " + unknownIds, result) : DataResult.success(result);
|
| 75 | }, set -> List.copyOf(this.toNames(set)));
|
| 76 | }
|
| 77 |
|
| 78 | public static class Builder {
|
| 79 | private final FeatureFlagUniverse universe;
|
| 80 | private int id;
|
| 81 | private final Map<Identifier, FeatureFlag> flags = new LinkedHashMap<>();
|
| 82 |
|
| 83 | public Builder(final String universeId) {
|
| 84 | this.universe = new FeatureFlagUniverse(universeId);
|
| 85 | }
|
| 86 |
|
| 87 | public FeatureFlag createVanilla(final String name) {
|
| 88 | return this.create(Identifier.withDefaultNamespace(name));
|
| 89 | }
|
| 90 |
|
| 91 | public FeatureFlag create(final Identifier name) {
|
| 92 | if (this.id >= 64) {
|
| 93 | throw new IllegalStateException("Too many feature flags");
|
| 94 | } else {
|
| 95 | FeatureFlag result = new FeatureFlag(this.universe, this.id++);
|
| 96 | FeatureFlag previous = this.flags.put(name, result);
|
| 97 | if (previous != null) {
|
| 98 | throw new IllegalStateException("Duplicate feature flag " + name);
|
| 99 | } else {
|
| 100 | return result;
|
| 101 | }
|
| 102 | }
|
| 103 | }
|
| 104 |
|
| 105 | public FeatureFlagRegistry build() {
|
| 106 | FeatureFlagSet allValues = FeatureFlagSet.create(this.universe, this.flags.values());
|
| 107 | return new FeatureFlagRegistry(this.universe, allValues, Map.copyOf(this.flags));
|
| 108 | }
|
| 109 | }
|
| 110 | }
|
| 111 |
|