| 1 | package net.minecraft.client.gui.screens.packs;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.google.common.collect.Lists;
|
| 5 | import java.util.Collections;
|
| 6 | import java.util.List;
|
| 7 | import java.util.function.Consumer;
|
| 8 | import java.util.function.Function;
|
| 9 | import java.util.stream.Stream;
|
| 10 | import net.fabricmc.api.EnvType;
|
| 11 | import net.fabricmc.api.Environment;
|
| 12 | import net.minecraft.client.Minecraft;
|
| 13 | import net.minecraft.client.OptionInstance;
|
| 14 | import net.minecraft.network.chat.Component;
|
| 15 | import net.minecraft.resources.Identifier;
|
| 16 | import net.minecraft.server.packs.repository.Pack;
|
| 17 | import net.minecraft.server.packs.repository.PackCompatibility;
|
| 18 | import net.minecraft.server.packs.repository.PackRepository;
|
| 19 | import net.minecraft.server.packs.repository.PackSource;
|
| 20 |
|
| 21 | @Environment(EnvType.CLIENT)
|
| 22 | public class PackSelectionModel {
|
| 23 | private final PackRepository repository;
|
| 24 | private final List<Pack> selected;
|
| 25 | private final List<Pack> unselected;
|
| 26 | private final Function<Pack, Identifier> iconGetter;
|
| 27 | private final Consumer<PackSelectionModel.EntryBase> onListChanged;
|
| 28 | private final Consumer<PackRepository> output;
|
| 29 |
|
| 30 | public PackSelectionModel(
|
| 31 | final Consumer<PackSelectionModel.EntryBase> onListChanged,
|
| 32 | final Function<Pack, Identifier> iconGetter,
|
| 33 | final PackRepository repository,
|
| 34 | final Consumer<PackRepository> output
|
| 35 | ) {
|
| 36 | this.onListChanged = onListChanged;
|
| 37 | this.iconGetter = iconGetter;
|
| 38 | this.repository = repository;
|
| 39 | this.selected = Lists.newArrayList(repository.getSelectedPacks());
|
| 40 | Collections.reverse(this.selected);
|
| 41 | this.unselected = Lists.newArrayList(repository.getAvailablePacks());
|
| 42 | this.unselected.removeAll(this.selected);
|
| 43 | this.output = output;
|
| 44 | }
|
| 45 |
|
| 46 | public Stream<PackSelectionModel.Entry> getUnselected() {
|
| 47 | return this.unselected.stream().map(x$0 -> new PackSelectionModel.UnselectedPackEntry(x$0));
|
| 48 | }
|
| 49 |
|
| 50 | public Stream<PackSelectionModel.Entry> getSelected() {
|
| 51 | return this.selected.stream().map(x$0 -> new PackSelectionModel.SelectedPackEntry(x$0));
|
| 52 | }
|
| 53 |
|
| 54 | private void updateRepoSelectedList() {
|
| 55 | this.repository.setSelected(Lists.reverse(this.selected).stream().map(Pack::getId).collect(ImmutableList.toImmutableList()));
|
| 56 | }
|
| 57 |
|
| 58 | public void commit() {
|
| 59 | this.updateRepoSelectedList();
|
| 60 | this.output.accept(this.repository);
|
| 61 | }
|
| 62 |
|
| 63 | public void findNewPacks() {
|
| 64 | this.repository.reload();
|
| 65 | this.selected.retainAll(this.repository.getAvailablePacks());
|
| 66 | this.unselected.clear();
|
| 67 | this.unselected.addAll(this.repository.getAvailablePacks());
|
| 68 | this.unselected.removeAll(this.selected);
|
| 69 | }
|
| 70 |
|
| 71 | @Environment(EnvType.CLIENT)
|
| 72 | public interface Entry {
|
| 73 | Identifier getIconTexture();
|
| 74 |
|
| 75 | PackCompatibility getCompatibility();
|
| 76 |
|
| 77 | String getId();
|
| 78 |
|
| 79 | Component getTitle();
|
| 80 |
|
| 81 | Component getDescription();
|
| 82 |
|
| 83 | PackSource getPackSource();
|
| 84 |
|
| 85 | default Component getExtendedDescription() {
|
| 86 | return this.getPackSource().decorate(this.getDescription());
|
| 87 | }
|
| 88 |
|
| 89 | boolean isFixedPosition();
|
| 90 |
|
| 91 | boolean isRequired();
|
| 92 |
|
| 93 | void select();
|
| 94 |
|
| 95 | void unselect();
|
| 96 |
|
| 97 | void moveUp();
|
| 98 |
|
| 99 | void moveDown();
|
| 100 |
|
| 101 | boolean isSelected();
|
| 102 |
|
| 103 | default boolean canSelect() {
|
| 104 | return !this.isSelected();
|
| 105 | }
|
| 106 |
|
| 107 | default boolean canUnselect() {
|
| 108 | return this.isSelected() && !this.isRequired();
|
| 109 | }
|
| 110 |
|
| 111 | boolean canMoveUp();
|
| 112 |
|
| 113 | boolean canMoveDown();
|
| 114 | }
|
| 115 |
|
| 116 | @Environment(EnvType.CLIENT)
|
| 117 | public abstract class EntryBase implements PackSelectionModel.Entry {
|
| 118 | private final Pack pack;
|
| 119 |
|
| 120 | public EntryBase(final Pack pack) {
|
| 121 | this.pack = pack;
|
| 122 | }
|
| 123 |
|
| 124 | protected abstract List<Pack> getSelfList();
|
| 125 |
|
| 126 | protected abstract List<Pack> getOtherList();
|
| 127 |
|
| 128 | @Override
|
| 129 | public Identifier getIconTexture() {
|
| 130 | return PackSelectionModel.this.iconGetter.apply(this.pack);
|
| 131 | }
|
| 132 |
|
| 133 | @Override
|
| 134 | public PackCompatibility getCompatibility() {
|
| 135 | return this.pack.getCompatibility();
|
| 136 | }
|
| 137 |
|
| 138 | @Override
|
| 139 | public String getId() {
|
| 140 | return this.pack.getId();
|
| 141 | }
|
| 142 |
|
| 143 | @Override
|
| 144 | public Component getTitle() {
|
| 145 | return this.pack.getTitle();
|
| 146 | }
|
| 147 |
|
| 148 | @Override
|
| 149 | public Component getDescription() {
|
| 150 | return this.pack.getDescription();
|
| 151 | }
|
| 152 |
|
| 153 | @Override
|
| 154 | public PackSource getPackSource() {
|
| 155 | return this.pack.getPackSource();
|
| 156 | }
|
| 157 |
|
| 158 | @Override
|
| 159 | public boolean isFixedPosition() {
|
| 160 | return this.pack.isFixedPosition();
|
| 161 | }
|
| 162 |
|
| 163 | @Override
|
| 164 | public boolean isRequired() {
|
| 165 | return this.pack.isRequired();
|
| 166 | }
|
| 167 |
|
| 168 | protected void toggleSelection() {
|
| 169 | this.getSelfList().remove(this.pack);
|
| 170 | this.pack.getDefaultPosition().insert(this.getOtherList(), this.pack, Pack::selectionConfig, true);
|
| 171 | PackSelectionModel.this.onListChanged.accept(this);
|
| 172 | PackSelectionModel.this.updateRepoSelectedList();
|
| 173 | this.updateHighContrastOptionInstance();
|
| 174 | }
|
| 175 |
|
| 176 | private void updateHighContrastOptionInstance() {
|
| 177 | if (this.pack.getId().equals("high_contrast")) {
|
| 178 | OptionInstance<Boolean> highContrastMode = Minecraft.getInstance().options.highContrast();
|
| 179 | highContrastMode.set(!highContrastMode.get());
|
| 180 | }
|
| 181 | }
|
| 182 |
|
| 183 | protected void move(final int direction) {
|
| 184 | List<Pack> list = this.getSelfList();
|
| 185 | int currentPos = list.indexOf(this.pack);
|
| 186 | list.remove(currentPos);
|
| 187 | list.add(currentPos + direction, this.pack);
|
| 188 | PackSelectionModel.this.onListChanged.accept(this);
|
| 189 | }
|
| 190 |
|
| 191 | @Override
|
| 192 | public boolean canMoveUp() {
|
| 193 | List<Pack> list = this.getSelfList();
|
| 194 | int index = list.indexOf(this.pack);
|
| 195 | return index > 0 && !list.get(index - 1).isFixedPosition();
|
| 196 | }
|
| 197 |
|
| 198 | @Override
|
| 199 | public void moveUp() {
|
| 200 | this.move(-1);
|
| 201 | }
|
| 202 |
|
| 203 | @Override
|
| 204 | public boolean canMoveDown() {
|
| 205 | List<Pack> list = this.getSelfList();
|
| 206 | int index = list.indexOf(this.pack);
|
| 207 | return index >= 0 && index < list.size() - 1 && !list.get(index + 1).isFixedPosition();
|
| 208 | }
|
| 209 |
|
| 210 | @Override
|
| 211 | public void moveDown() {
|
| 212 | this.move(1);
|
| 213 | }
|
| 214 | }
|
| 215 |
|
| 216 | @Environment(EnvType.CLIENT)
|
| 217 | private class SelectedPackEntry extends PackSelectionModel.EntryBase {
|
| 218 | public SelectedPackEntry(final Pack pack) {
|
| 219 | super(pack);
|
| 220 | }
|
| 221 |
|
| 222 | @Override
|
| 223 | protected List<Pack> getSelfList() {
|
| 224 | return PackSelectionModel.this.selected;
|
| 225 | }
|
| 226 |
|
| 227 | @Override
|
| 228 | protected List<Pack> getOtherList() {
|
| 229 | return PackSelectionModel.this.unselected;
|
| 230 | }
|
| 231 |
|
| 232 | @Override
|
| 233 | public boolean isSelected() {
|
| 234 | return true;
|
| 235 | }
|
| 236 |
|
| 237 | @Override
|
| 238 | public void select() {
|
| 239 | }
|
| 240 |
|
| 241 | @Override
|
| 242 | public void unselect() {
|
| 243 | this.toggleSelection();
|
| 244 | }
|
| 245 | }
|
| 246 |
|
| 247 | @Environment(EnvType.CLIENT)
|
| 248 | private class UnselectedPackEntry extends PackSelectionModel.EntryBase {
|
| 249 | public UnselectedPackEntry(final Pack pack) {
|
| 250 | super(pack);
|
| 251 | }
|
| 252 |
|
| 253 | @Override
|
| 254 | protected List<Pack> getSelfList() {
|
| 255 | return PackSelectionModel.this.unselected;
|
| 256 | }
|
| 257 |
|
| 258 | @Override
|
| 259 | protected List<Pack> getOtherList() {
|
| 260 | return PackSelectionModel.this.selected;
|
| 261 | }
|
| 262 |
|
| 263 | @Override
|
| 264 | public boolean isSelected() {
|
| 265 | return false;
|
| 266 | }
|
| 267 |
|
| 268 | @Override
|
| 269 | public void select() {
|
| 270 | this.toggleSelection();
|
| 271 | }
|
| 272 |
|
| 273 | @Override
|
| 274 | public void unselect() {
|
| 275 | }
|
| 276 | }
|
| 277 | }
|
| 278 |
|