|
5 | 5 |
|
6 | 6 | package meteordevelopment.meteorclient.settings; |
7 | 7 |
|
8 | | -public abstract class AbstractRegistryListSetting { |
| 8 | +import net.minecraft.nbt.NbtCompound; |
| 9 | +import net.minecraft.nbt.NbtElement; |
| 10 | +import net.minecraft.nbt.NbtList; |
| 11 | +import net.minecraft.nbt.NbtString; |
| 12 | +import net.minecraft.registry.Registry; |
| 13 | +import net.minecraft.util.Identifier; |
| 14 | +import org.apache.commons.lang3.StringUtils; |
| 15 | + |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.Collection; |
| 18 | +import java.util.List; |
| 19 | +import java.util.function.Consumer; |
| 20 | +import java.util.function.Predicate; |
| 21 | + |
| 22 | +public abstract class AbstractRegistryListSetting<C extends Collection<V>, V> extends Setting<C> { |
| 23 | + public final Predicate<V> filter; |
| 24 | + public final Registry<V> registry; |
| 25 | + |
| 26 | + protected AbstractRegistryListSetting(String name, String description, C defaultValue, Consumer<C> onChanged, Consumer<Setting<C>> onModuleActivated, IVisible visible, Predicate<V> filter, Registry<V> registry) { |
| 27 | + super(name, description, defaultValue, onChanged, onModuleActivated, visible); |
| 28 | + this.filter = filter; |
| 29 | + this.registry = registry; |
| 30 | + } |
| 31 | + |
| 32 | + protected abstract C transferCollection(Collection<V> from); |
| 33 | + |
| 34 | + @Override |
| 35 | + public final void resetImpl() { |
| 36 | + value = transferCollection(defaultValue); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + protected final C parseImpl(String str) { |
| 41 | + String[] values = StringUtils.split(str, ','); |
| 42 | + List<V> items = new ArrayList<>(values.length); |
| 43 | + |
| 44 | + try { |
| 45 | + for (String string : values) { |
| 46 | + V value = parseId(this.registry, string); |
| 47 | + if (value != null && (filter == null || filter.test(value))) items.add(value); |
| 48 | + } |
| 49 | + } catch (Exception ignored) {} |
| 50 | + |
| 51 | + return transferCollection(items); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + protected boolean isValueValid(C value) { |
| 56 | + return true; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public final Iterable<Identifier> getIdentifierSuggestions() { |
| 61 | + return this.registry.getIds(); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + protected final NbtCompound save(NbtCompound tag) { |
| 66 | + NbtList valueTag = new NbtList(); |
| 67 | + for (V value : get()) { |
| 68 | + valueTag.add(NbtString.of(this.registry.getId(value).toString())); |
| 69 | + } |
| 70 | + tag.put("value", valueTag); |
| 71 | + |
| 72 | + return tag; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + protected final C load(NbtCompound tag) { |
| 77 | + get().clear(); |
| 78 | + |
| 79 | + NbtList valueTag = tag.getListOrEmpty("value"); |
| 80 | + for (NbtElement tagI : valueTag) { |
| 81 | + V value = this.registry.get(Identifier.of(tagI.asString().orElse(""))); |
| 82 | + |
| 83 | + if (filter == null || filter.test(value)) get().add(value); |
| 84 | + } |
| 85 | + |
| 86 | + return get(); |
| 87 | + } |
| 88 | + |
| 89 | +// public static class Builder extends SettingBuilder<BlockListSetting.Builder, List<Block>, BlockListSetting> { |
| 90 | +// private Predicate<Block> filter; |
| 91 | +// |
| 92 | +// public Builder() { |
| 93 | +// super(new ArrayList<>(0)); |
| 94 | +// } |
| 95 | +// |
| 96 | +// public BlockListSetting.Builder defaultValue(Block... defaults) { |
| 97 | +// return defaultValue(defaults != null ? Arrays.asList(defaults) : new ArrayList<>()); |
| 98 | +// } |
| 99 | +// |
| 100 | +// public BlockListSetting.Builder filter(Predicate<Block> filter) { |
| 101 | +// this.filter = filter; |
| 102 | +// return this; |
| 103 | +// } |
| 104 | +// |
| 105 | +// @Override |
| 106 | +// public BlockListSetting build() { |
| 107 | +// return new BlockListSetting(name, description, defaultValue, onChanged, onModuleActivated, filter, visible); |
| 108 | +// } |
| 109 | +// } |
| 110 | + |
9 | 111 | } |
0 commit comments