Skip to content

Commit d4fea4e

Browse files
resolve conflicts 1.14
1 parent 7ae67f4 commit d4fea4e

2 files changed

Lines changed: 133 additions & 90 deletions

File tree

src/main/java/cam72cam/mod/ModCore.java

Lines changed: 50 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cam72cam.mod;
22

33
import java.io.*;
4+
import java.nio.charset.StandardCharsets;
45
import java.nio.file.Path;
56
import java.nio.file.Paths;
67
import java.util.*;
@@ -210,17 +211,59 @@ public void setup() {
210211
List<IResourcePack> packs = new ArrayList<>();
211212

212213
for (Mod mod : instance.mods) {
213-
BuiltinPack.loadModResource(mod);
214+
BuiltinPack.loadModResource(mod, packs);
214215
}
215216

216-
IResourcePack modPack = BuiltinPack.attach();
217-
// Ensure people will get our result first via getResourceStream() and getLastResourceStream()
218-
// (Also injects last modified time access)
219-
// BUG: sounds can still be overridden by resource packs
220-
packs.add(1, modPack);
221-
packs.add(modPack);
222217
BuiltinPack.onConstruct(packs);
223218

219+
//Wrapper for lang format language files
220+
BuiltinPack.conditional(ident -> {
221+
String path = ident.getPath();
222+
if (!path.startsWith("lang/") || !path.endsWith(".json")) {
223+
return null;
224+
}
225+
226+
Identifier lang = new Identifier(ident.toString().replace(".json", ".lang"));
227+
if (!lang.canLoad()) {
228+
return null;
229+
}
230+
231+
Map<String, String> translationMap = new HashMap<>();
232+
try {
233+
for (InputStream stream : lang.getResourceStreamAll()) {
234+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) {
235+
String line;
236+
while ((line = reader.readLine()) != null) {
237+
//Remove comment
238+
line = line.trim();
239+
if (line.isEmpty() || line.startsWith("#")) {
240+
continue;
241+
}
242+
243+
String[] splits = line.split("=", 2);
244+
if (splits.length == 2) {
245+
translationMap.put(splits[0].trim(), splits[1].trim());
246+
}
247+
}
248+
}
249+
}
250+
} catch (IOException e) {
251+
throw new RuntimeException(e);
252+
}
253+
254+
Set<String> translations = new HashSet<>();
255+
translationMap.forEach((key, value) -> {
256+
if (!key.isEmpty()) {
257+
translations.add(String.format("\"%s\": \"%s\"", key, value));
258+
translations.add(String.format("\"%s\": \"%s\"", key.replace(":", "."), value));
259+
translations.add(String.format("\"%s\": \"%s\"", key.replace(".name", ""), value));
260+
translations.add(String.format("\"%s\": \"%s\"", key.replace(".name", "").replace(":", "."), value));
261+
}
262+
});
263+
String output = "{" + String.join(",", translations) + "}";
264+
return output.getBytes(StandardCharsets.UTF_8);
265+
});
266+
224267
Minecraft.getInstance().getResourcePackList().addPackFinder(new IPackFinder() {
225268
@Override
226269
public <T extends ResourcePackInfo> void addPackInfosToMap(Map<String, T> nameToPackMap, ResourcePackInfo.IFactory<T> packInfoFactory) {
@@ -246,47 +289,6 @@ public void event(ModEvent event, Mod m) {
246289
super.event(event, m);
247290
m.clientEvent(event);
248291
}
249-
250-
private static class UMCFolderPack extends FolderPack {
251-
public UMCFolderPack(File folder) {
252-
super(folder);
253-
}
254-
255-
@Override
256-
public InputStream getInputStream(String name) throws IOException {
257-
InputStream stream = super.getInputStream(name);
258-
File file = this.getFile(name);
259-
return new Identifier.InputStreamMod(stream, file.lastModified());
260-
}
261-
262-
@Override
263-
public boolean resourceExists(String resourcePath) {
264-
return super.resourceExists(resourcePath);
265-
}
266-
}
267-
268-
private static class UMCFilePack extends FilePack {
269-
private final File path;
270-
271-
public UMCFilePack(File fileIn) {
272-
super(fileIn);
273-
this.path = fileIn;
274-
}
275-
276-
@Override
277-
public InputStream getInputStream(String name) throws IOException {
278-
return new Identifier.InputStreamMod(super.getInputStream(name), path.lastModified());
279-
}
280-
}
281-
282-
283-
private static ResourcePack createPack(File path) {
284-
if (path.isDirectory()) {
285-
return new UMCFolderPack(path);
286-
} else {
287-
return new UMCFilePack(path);
288-
}
289-
}
290292
}
291293

292294
public static class ServerProxy extends Proxy {

src/main/java/cam72cam/mod/resource/BuiltinPack.java

Lines changed: 83 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
package cam72cam.mod.resource;
22

33
import cam72cam.mod.ModCore;
4-
import net.minecraft.client.Minecraft;
5-
import net.minecraft.client.resources.*;
6-
import net.minecraft.client.resources.data.IMetadataSection;
7-
import net.minecraft.client.resources.data.MetadataSerializer;
8-
import net.minecraftforge.fml.common.Loader;
9-
import net.minecraftforge.fml.relauncher.Side;
10-
import net.minecraftforge.fml.relauncher.SideOnly;
11-
4+
import net.minecraft.resources.*;
5+
import net.minecraft.resources.data.IMetadataSectionSerializer;
6+
import net.minecraft.util.ResourceLocation;
7+
import net.minecraftforge.api.distmarker.Dist;
8+
import net.minecraftforge.api.distmarker.OnlyIn;
9+
import net.minecraftforge.fml.ModList;
10+
import net.minecraftforge.fml.loading.FMLPaths;
11+
12+
import javax.annotation.Nullable;
1213
import java.io.*;
1314
import java.util.*;
1415
import java.util.function.Function;
16+
import java.util.function.Predicate;
1517
import java.util.stream.Collectors;
1618

1719
/**
@@ -77,36 +79,27 @@ public static void redirect(Identifier requestedPrefix, Identifier actualPrefix)
7779
/**
7880
* Registers a file or folder as a resource pack to the game.
7981
*/
80-
@SideOnly(Side.CLIENT)
82+
@OnlyIn(Dist.CLIENT)
8183
public static IResourcePack attach(File path) {
8284
if (path.isDirectory()) {
83-
return new FolderResourcePack(path) {
84-
@Override
85-
protected InputStream getInputStreamByName(String name) throws IOException {
86-
InputStream stream = super.getInputStreamByName(name);
87-
File file = this.getFile(name);
88-
return new Identifier.InputStreamMod(stream, file.lastModified());
89-
}
90-
};
85+
return new UMCFolderPack(path);
9186
} else {
92-
return new FileResourcePack(path) {
93-
@Override
94-
protected InputStream getInputStreamByName(String name) throws IOException {
95-
return new Identifier.InputStreamMod(super.getInputStreamByName(name), resourcePackFile.lastModified());
96-
}
97-
};
87+
return new UMCFilePack(path);
9888
}
9989
}
10090

10191
/**
10292
* Internal
10393
*/
104-
public static void loadModResource(ModCore.Mod mod) {
105-
List<IResourcePack> packs = Minecraft.getMinecraft().defaultResourcePacks;
106-
107-
String configDir = Loader.instance().getConfigDir().toString();
94+
public static void loadModResource(ModCore.Mod mod, List<IResourcePack> packs) {
95+
String configDir = FMLPaths.CONFIGDIR.toString();
10896
new File(configDir).mkdirs();
10997

98+
IResourcePack modPack = BuiltinPack.attach(ModList.get().getModFileById(mod.modID()).getFile().getFilePath().toFile());
99+
// Ensure people will get our result first via getResourceStream() and getLastResourceStream()
100+
// (Also injects last modified time access)
101+
// BUG: sounds can still be overridden by resource packs
102+
packs.add(modPack);
110103
File folder = new File(configDir + File.separator + mod.modID());
111104
if (folder.exists()) {
112105
if (folder.isDirectory()) {
@@ -123,6 +116,7 @@ public static void loadModResource(ModCore.Mod mod) {
123116
} else {
124117
folder.mkdirs();
125118
}
119+
packs.add(modPack);
126120
}
127121

128122
/**
@@ -143,17 +137,15 @@ public static void reload() {
143137
}
144138

145139
/**
146-
* Internal, Client side
140+
* Internal, Client side assets loading
147141
*/
148-
@SideOnly(Side.CLIENT)
149-
private static class InternalPack extends AbstractResourcePack {
142+
private static class InternalPack extends ResourcePack {
150143
public InternalPack() {
151-
//We're initializing UMC
152-
super(Loader.instance().activeModContainer().getSource());
144+
super(ModList.get().getModFileById("universalmodcore").getFile().getFilePath().toFile());
153145
}
154146

155147
@Override
156-
protected InputStream getInputStreamByName(String resourcePath) throws IOException {
148+
public InputStream getInputStream(String resourcePath) throws IOException {
157149
if("pack.mcmeta".equals(resourcePath)) {
158150
return new ByteArrayInputStream("{}".getBytes());
159151
}
@@ -181,7 +173,7 @@ protected InputStream getInputStreamByName(String resourcePath) throws IOExcepti
181173
}
182174

183175
@Override
184-
protected boolean hasResourceName(String resourcePath) {
176+
public boolean resourceExists(String resourcePath) {
185177
if (resourcePath.endsWith("mcmeta") && !"pack.mcmeta".equals(resourcePath)) {
186178
//We don't handle resource metadata
187179
return false;
@@ -219,27 +211,38 @@ && handleRedirect(ident, entry.getKey(), entry.getValue()).canLoad()) {
219211
}
220212

221213
@Override
222-
public Set<String> getResourceDomains() {
214+
public Collection<ResourceLocation> getAllResourceLocations(ResourcePackType type, String pathIn, int maxDepth, Predicate<String> filter) {
215+
return Collections.emptyList();
216+
}
217+
218+
@Override
219+
public Set<String> getResourceNamespaces(ResourcePackType type) {
223220
Set<String> collect = ModCore.instance.getLoadedMods().stream().map(ModCore.Mod::modID).collect(Collectors.toSet());
224221
collect.add("universalmodcore");
225222
return collect;
226223
}
227224

228225
@Override
229-
public String getPackName() {
226+
public String getName() {
230227
return "UMC Generated Resources";
231228
}
232229

230+
@Nullable
233231
@Override
234-
public <T extends IMetadataSection> T getPackMetadata(MetadataSerializer metadataSerializer, String metadataSectionName) throws IOException {
235-
return super.getPackMetadata(metadataSerializer, metadataSectionName);
232+
public <T> T getMetadata(IMetadataSectionSerializer<T> p_195760_1_) throws IOException {
233+
return getResourceMetadata(p_195760_1_, new ByteArrayInputStream("{}".getBytes()));
234+
}
235+
236+
@Override
237+
public void close() throws IOException {
238+
//Have nothing to do here
236239
}
237240
}
238241

239242
/**
240-
* Internal, Server side
243+
* Internal, Server side assets loading
241244
*/
242-
@SideOnly(Side.SERVER)
245+
@OnlyIn(Dist.DEDICATED_SERVER)
243246
public static InputStream loadServerResource(Identifier ident) throws IOException {
244247
if (ident.getPath().endsWith("mcmeta")) {
245248
//We don't handle resource metadata
@@ -275,6 +278,38 @@ public static InputStream loadServerResource(Identifier ident) throws IOExceptio
275278
return null;
276279
}
277280

281+
private static class UMCFolderPack extends FolderPack {
282+
public UMCFolderPack(File folder) {
283+
super(folder);
284+
}
285+
286+
@Override
287+
public InputStream getInputStream(String name) throws IOException {
288+
InputStream stream = super.getInputStream(name);
289+
File file = this.getFile(name);
290+
return new Identifier.InputStreamMod(stream, file.lastModified());
291+
}
292+
293+
@Override
294+
public boolean resourceExists(String resourcePath) {
295+
return super.resourceExists(resourcePath);
296+
}
297+
}
298+
299+
private static class UMCFilePack extends FilePack {
300+
private final File path;
301+
302+
public UMCFilePack(File fileIn) {
303+
super(fileIn);
304+
this.path = fileIn;
305+
}
306+
307+
@Override
308+
public InputStream getInputStream(String name) throws IOException {
309+
return new Identifier.InputStreamMod(super.getInputStream(name), path.lastModified());
310+
}
311+
}
312+
278313
private static Identifier handleRedirect(Identifier src, String requestedPrefix, String actualPrefix) {
279314
//Replace [requestedPrefix] with [actualPrefix] to redirect the request back
280315
String suffix = src.toString().substring(requestedPrefix.length());
@@ -283,12 +318,18 @@ private static Identifier handleRedirect(Identifier src, String requestedPrefix,
283318

284319
private static Identifier nameToLocation(String path) {
285320
if(path.startsWith("assets/")) {
286-
//assets/[domain]/[path] -> domain:path, for 1.12- path
321+
//assets/[domain]/[path] -> domain:path
287322
path = path.substring(7);
288323
int x = path.indexOf('/');
289324
return new Identifier(path.substring(0, x), path.substring(x + 1));
325+
} else if (path.startsWith("data/")) {
326+
//data/[domain]/[path] -> domain:path
327+
//However we added this as client-only resources...
328+
//TODO (internal) datapack API
329+
path = path.substring(5);
330+
int x = path.indexOf('/');
331+
return new Identifier(path.substring(0, x), path.substring(x + 1));
290332
}
291-
//Not possible to hit below 1.12, except for pack.mcmeta
292333
return new Identifier("universalmodcore", "invalid");
293334
}
294335
}

0 commit comments

Comments
 (0)