-
-
Notifications
You must be signed in to change notification settings - Fork 128
/
DelegatingPackResources.java
132 lines (116 loc) · 4.43 KB
/
DelegatingPackResources.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/
package net.minecraftforge.resource;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.AbstractPackResources;
import net.minecraft.server.packs.PackResources;
import net.minecraft.server.packs.PackType;
import net.minecraft.server.packs.metadata.MetadataSectionSerializer;
import net.minecraft.server.packs.metadata.pack.PackMetadataSection;
import net.minecraft.server.packs.resources.IoSupplier;
import org.jetbrains.annotations.Nullable;
public class DelegatingPackResources extends AbstractPackResources
{
private final PackMetadataSection packMeta;
private final List<PackResources> delegates;
private Map<String, List<PackResources>> namespacesAssets;
private Map<String, List<PackResources>> namespacesData;
public DelegatingPackResources(String packId, boolean isBuiltin, PackMetadataSection packMeta, List<? extends PackResources> packs)
{
super(packId, isBuiltin);
this.packMeta = packMeta;
this.delegates = ImmutableList.copyOf(packs);
this.namespacesAssets = this.buildNamespaceMap(PackType.CLIENT_RESOURCES, delegates);
this.namespacesData = this.buildNamespaceMap(PackType.SERVER_DATA, delegates);
}
@Override
public void initForNamespace(final String nameSpace)
{
this.delegates.forEach(delegate -> delegate.initForNamespace(nameSpace));
}
@Override
public void init(final PackType packType)
{
this.delegates.forEach(packResources -> packResources.init(packType));
this.namespacesAssets = buildNamespaceMap(PackType.CLIENT_RESOURCES, delegates);
this.namespacesData = buildNamespaceMap(PackType.SERVER_DATA, delegates);
}
private Map<String, List<PackResources>> buildNamespaceMap(PackType type, List<PackResources> packList)
{
Map<String, List<PackResources>> map = new HashMap<>();
for (PackResources pack : packList)
{
for (String namespace : pack.getNamespaces(type))
{
map.computeIfAbsent(namespace, k -> new ArrayList<>()).add(pack);
}
}
map.replaceAll((k, list) -> ImmutableList.copyOf(list));
return ImmutableMap.copyOf(map);
}
@SuppressWarnings("unchecked")
@Nullable
@Override
public <T> T getMetadataSection(MetadataSectionSerializer<T> deserializer) throws IOException
{
return deserializer.getMetadataSectionName().equals("pack") ? (T) this.packMeta : null;
}
@Override
public void listResources(PackType type, String resourceNamespace, String paths, ResourceOutput resourceOutput)
{
for (PackResources delegate : this.delegates)
{
delegate.listResources(type, resourceNamespace, paths, resourceOutput);
}
}
@Override
public Set<String> getNamespaces(PackType type)
{
return type == PackType.CLIENT_RESOURCES ? namespacesAssets.keySet() : namespacesData.keySet();
}
@Override
public void close()
{
for (PackResources pack : delegates)
{
pack.close();
}
}
@Nullable
@Override
public IoSupplier<InputStream> getRootResource(String... paths)
{
// Root resources do not make sense here
throw null;
}
@Nullable
@Override
public IoSupplier<InputStream> getResource(PackType type, ResourceLocation location)
{
for (PackResources pack : getCandidatePacks(type, location))
{
IoSupplier<InputStream> ioSupplier = pack.getResource(type, location);
if (ioSupplier != null)
return pack.getResource(type, location);
}
return null;
}
private List<PackResources> getCandidatePacks(PackType type, ResourceLocation location)
{
Map<String, List<PackResources>> map = type == PackType.CLIENT_RESOURCES ? namespacesAssets : namespacesData;
List<PackResources> packsWithNamespace = map.get(location.getNamespace());
return packsWithNamespace == null ? Collections.emptyList() : packsWithNamespace;
}
}