| 1 | package net.minecraft.world.level.gamerules;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import it.unimi.dsi.fastutil.objects.Reference2ObjectMap;
|
| 5 | import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap;
|
| 6 | import java.util.Map;
|
| 7 | import java.util.Objects;
|
| 8 | import java.util.Set;
|
| 9 | import java.util.function.Predicate;
|
| 10 | import java.util.stream.Stream;
|
| 11 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 12 | import net.minecraft.resources.Identifier;
|
| 13 | import net.minecraft.util.datafix.DataFixTypes;
|
| 14 | import net.minecraft.world.level.saveddata.SavedData;
|
| 15 | import net.minecraft.world.level.saveddata.SavedDataType;
|
| 16 | import org.jspecify.annotations.Nullable;
|
| 17 |
|
| 18 | public final class GameRuleMap extends SavedData {
|
| 19 | public static final Codec<GameRuleMap> CODEC = Codec.dispatchedMap(BuiltInRegistries.GAME_RULE.byNameCodec(), GameRule::valueCodec)
|
| 20 | .xmap(GameRuleMap::ofTrusted, GameRuleMap::map);
|
| 21 | public static final SavedDataType<GameRuleMap> TYPE = new SavedDataType<>(
|
| 22 | Identifier.withDefaultNamespace("game_rules"), GameRuleMap::of, CODEC, DataFixTypes.SAVED_DATA_GAME_RULES
|
| 23 | );
|
| 24 | private final Reference2ObjectMap<GameRule<?>, Object> map;
|
| 25 |
|
| 26 | private GameRuleMap(final Reference2ObjectMap<GameRule<?>, Object> map) {
|
| 27 | this.map = map;
|
| 28 | }
|
| 29 |
|
| 30 | private static GameRuleMap ofTrusted(final Map<GameRule<?>, Object> map) {
|
| 31 | return new GameRuleMap(new Reference2ObjectOpenHashMap<>(map));
|
| 32 | }
|
| 33 |
|
| 34 | public static GameRuleMap of() {
|
| 35 | return new GameRuleMap(new Reference2ObjectOpenHashMap<>());
|
| 36 | }
|
| 37 |
|
| 38 | public static GameRuleMap of(final Stream<GameRule<?>> gameRuleTypeStream) {
|
| 39 | Reference2ObjectOpenHashMap<GameRule<?>, Object> map = new Reference2ObjectOpenHashMap<>();
|
| 40 | gameRuleTypeStream.forEach(gameRule -> map.put((GameRule<?>)gameRule, gameRule.defaultValue()));
|
| 41 | return new GameRuleMap(map);
|
| 42 | }
|
| 43 |
|
| 44 | public static GameRuleMap copyOf(final GameRuleMap gameRuleMap) {
|
| 45 | return new GameRuleMap(new Reference2ObjectOpenHashMap<>(gameRuleMap.map));
|
| 46 | }
|
| 47 |
|
| 48 | public boolean has(final GameRule<?> gameRule) {
|
| 49 | return this.map.containsKey(gameRule);
|
| 50 | }
|
| 51 |
|
| 52 | public <T> @Nullable T get(final GameRule<T> gameRule) {
|
| 53 | return (T)this.map.get(gameRule);
|
| 54 | }
|
| 55 |
|
| 56 | public <T> void set(final GameRule<T> gameRule, final T value) {
|
| 57 | this.setDirty();
|
| 58 | this.map.put(gameRule, value);
|
| 59 | }
|
| 60 |
|
| 61 | public <T> void reset(final GameRule<T> gameRule) {
|
| 62 | this.set(gameRule, gameRule.defaultValue());
|
| 63 | }
|
| 64 |
|
| 65 | public <T> @Nullable T remove(final GameRule<T> gameRule) {
|
| 66 | this.setDirty();
|
| 67 | return (T)this.map.remove(gameRule);
|
| 68 | }
|
| 69 |
|
| 70 | public Set<GameRule<?>> keySet() {
|
| 71 | return this.map.keySet();
|
| 72 | }
|
| 73 |
|
| 74 | public int size() {
|
| 75 | return this.map.size();
|
| 76 | }
|
| 77 |
|
| 78 | @Override
|
| 79 | public String toString() {
|
| 80 | return this.map.toString();
|
| 81 | }
|
| 82 |
|
| 83 | public GameRuleMap withOther(final GameRuleMap other) {
|
| 84 | GameRuleMap result = copyOf(this);
|
| 85 | result.setFromIf(other, r -> true);
|
| 86 | return result;
|
| 87 | }
|
| 88 |
|
| 89 | public void setFromIf(final GameRuleMap other, final Predicate<GameRule<?>> predicate) {
|
| 90 | for (GameRule<?> gameRule : other.keySet()) {
|
| 91 | if (predicate.test(gameRule)) {
|
| 92 | setGameRule(other, gameRule, this);
|
| 93 | }
|
| 94 | }
|
| 95 | }
|
| 96 |
|
| 97 | private static <T> void setGameRule(final GameRuleMap other, final GameRule<T> gameRule, final GameRuleMap result) {
|
| 98 | result.set(gameRule, Objects.requireNonNull(other.get(gameRule)));
|
| 99 | }
|
| 100 |
|
| 101 | private Reference2ObjectMap<GameRule<?>, Object> map() {
|
| 102 | return this.map;
|
| 103 | }
|
| 104 |
|
| 105 | @Override
|
| 106 | public boolean equals(final Object obj) {
|
| 107 | if (obj == this) {
|
| 108 | return true;
|
| 109 | } else if (obj != null && obj.getClass() == this.getClass()) {
|
| 110 | GameRuleMap that = (GameRuleMap)obj;
|
| 111 | return Objects.equals(this.map, that.map);
|
| 112 | } else {
|
| 113 | return false;
|
| 114 | }
|
| 115 | }
|
| 116 |
|
| 117 | @Override
|
| 118 | public int hashCode() {
|
| 119 | return Objects.hash(this.map);
|
| 120 | }
|
| 121 |
|
| 122 | public static class Builder {
|
| 123 | private final Reference2ObjectMap<GameRule<?>, Object> map = new Reference2ObjectOpenHashMap<>();
|
| 124 |
|
| 125 | public <T> GameRuleMap.Builder set(final GameRule<T> gameRule, final T value) {
|
| 126 | this.map.put(gameRule, value);
|
| 127 | return this;
|
| 128 | }
|
| 129 |
|
| 130 | public GameRuleMap build() {
|
| 131 | return new GameRuleMap(this.map);
|
| 132 | }
|
| 133 | }
|
| 134 | }
|
| 135 |
|