136 lines
		
	
	
	
		
			4 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
			
		
		
	
	
			136 lines
		
	
	
	
		
			4 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
| package net.minecraft.server.packs.repository;
 | |
| 
 | |
| import com.google.common.collect.ImmutableList;
 | |
| import com.google.common.collect.ImmutableMap;
 | |
| import com.google.common.collect.ImmutableSet;
 | |
| import com.google.common.collect.Lists;
 | |
| import com.google.common.collect.Maps;
 | |
| import java.util.Collection;
 | |
| import java.util.List;
 | |
| import java.util.Map;
 | |
| import java.util.Objects;
 | |
| import java.util.Set;
 | |
| import java.util.stream.Collectors;
 | |
| import java.util.stream.Stream;
 | |
| import net.minecraft.Util;
 | |
| import net.minecraft.server.packs.PackResources;
 | |
| import net.minecraft.world.flag.FeatureFlagSet;
 | |
| import org.jetbrains.annotations.Nullable;
 | |
| 
 | |
| public class PackRepository {
 | |
| 	private final Set<RepositorySource> sources;
 | |
| 	private Map<String, Pack> available = ImmutableMap.of();
 | |
| 	private List<Pack> selected = ImmutableList.of();
 | |
| 
 | |
| 	public PackRepository(RepositorySource... sources) {
 | |
| 		this.sources = ImmutableSet.copyOf(sources);
 | |
| 	}
 | |
| 
 | |
| 	public static String displayPackList(Collection<Pack> packs) {
 | |
| 		return (String)packs.stream().map(pack -> pack.getId() + (pack.getCompatibility().isCompatible() ? "" : " (incompatible)")).collect(Collectors.joining(", "));
 | |
| 	}
 | |
| 
 | |
| 	public void reload() {
 | |
| 		List<String> list = (List<String>)this.selected.stream().map(Pack::getId).collect(ImmutableList.toImmutableList());
 | |
| 		this.available = this.discoverAvailable();
 | |
| 		this.selected = this.rebuildSelected(list);
 | |
| 	}
 | |
| 
 | |
| 	private Map<String, Pack> discoverAvailable() {
 | |
| 		Map<String, Pack> map = Maps.newTreeMap();
 | |
| 
 | |
| 		for (RepositorySource repositorySource : this.sources) {
 | |
| 			repositorySource.loadPacks(pack -> map.put(pack.getId(), pack));
 | |
| 		}
 | |
| 
 | |
| 		return ImmutableMap.copyOf(map);
 | |
| 	}
 | |
| 
 | |
| 	public boolean isAbleToClearAnyPack() {
 | |
| 		List<Pack> list = this.rebuildSelected(List.of());
 | |
| 		return !this.selected.equals(list);
 | |
| 	}
 | |
| 
 | |
| 	public void setSelected(Collection<String> ids) {
 | |
| 		this.selected = this.rebuildSelected(ids);
 | |
| 	}
 | |
| 
 | |
| 	public boolean addPack(String id) {
 | |
| 		Pack pack = (Pack)this.available.get(id);
 | |
| 		if (pack != null && !this.selected.contains(pack)) {
 | |
| 			List<Pack> list = Lists.<Pack>newArrayList(this.selected);
 | |
| 			list.add(pack);
 | |
| 			this.selected = list;
 | |
| 			return true;
 | |
| 		} else {
 | |
| 			return false;
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	public boolean removePack(String id) {
 | |
| 		Pack pack = (Pack)this.available.get(id);
 | |
| 		if (pack != null && this.selected.contains(pack)) {
 | |
| 			List<Pack> list = Lists.<Pack>newArrayList(this.selected);
 | |
| 			list.remove(pack);
 | |
| 			this.selected = list;
 | |
| 			return true;
 | |
| 		} else {
 | |
| 			return false;
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	private List<Pack> rebuildSelected(Collection<String> ids) {
 | |
| 		List<Pack> list = (List<Pack>)this.getAvailablePacks(ids).collect(Util.toMutableList());
 | |
| 
 | |
| 		for (Pack pack : this.available.values()) {
 | |
| 			if (pack.isRequired() && !list.contains(pack)) {
 | |
| 				pack.getDefaultPosition().insert(list, pack, Pack::selectionConfig, false);
 | |
| 			}
 | |
| 		}
 | |
| 
 | |
| 		return ImmutableList.copyOf(list);
 | |
| 	}
 | |
| 
 | |
| 	private Stream<Pack> getAvailablePacks(Collection<String> ids) {
 | |
| 		return ids.stream().map(this.available::get).filter(Objects::nonNull);
 | |
| 	}
 | |
| 
 | |
| 	public Collection<String> getAvailableIds() {
 | |
| 		return this.available.keySet();
 | |
| 	}
 | |
| 
 | |
| 	/**
 | |
| 	 * Gets all known packs, including those that are not enabled.
 | |
| 	 */
 | |
| 	public Collection<Pack> getAvailablePacks() {
 | |
| 		return this.available.values();
 | |
| 	}
 | |
| 
 | |
| 	public Collection<String> getSelectedIds() {
 | |
| 		return (Collection<String>)this.selected.stream().map(Pack::getId).collect(ImmutableSet.toImmutableSet());
 | |
| 	}
 | |
| 
 | |
| 	public FeatureFlagSet getRequestedFeatureFlags() {
 | |
| 		return (FeatureFlagSet)this.getSelectedPacks().stream().map(Pack::getRequestedFeatures).reduce(FeatureFlagSet::join).orElse(FeatureFlagSet.of());
 | |
| 	}
 | |
| 
 | |
| 	/**
 | |
| 	 * Gets all packs that have been enabled.
 | |
| 	 */
 | |
| 	public Collection<Pack> getSelectedPacks() {
 | |
| 		return this.selected;
 | |
| 	}
 | |
| 
 | |
| 	@Nullable
 | |
| 	public Pack getPack(String id) {
 | |
| 		return (Pack)this.available.get(id);
 | |
| 	}
 | |
| 
 | |
| 	public boolean isAvailable(String id) {
 | |
| 		return this.available.containsKey(id);
 | |
| 	}
 | |
| 
 | |
| 	public List<PackResources> openAllSelected() {
 | |
| 		return (List<PackResources>)this.selected.stream().map(Pack::open).collect(ImmutableList.toImmutableList());
 | |
| 	}
 | |
| }
 |