Clean up CraftingHelper constants loading API

This commit is contained in:
tterrag committed 2018-12-02 03:05:53 -05:00
1 parent 5ef199f6d7
commit f1d1e8853b
6 files changed
+50 -35

No files matched your search

@@ -21,6 +21,7 @@ package net.minecraftforge.common.crafting;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URI;
@@ -29,6 +30,7 @@ import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
@@ -832,23 +834,18 @@ public class CraftingHelper {
return success;
}
public static JsonContext loadContext(File file) throws IOException
public static JsonContext loadContext(ResourceLocation path) throws IOException
{
ModContainer mod = Loader.instance().activeModContainer();
if(mod == null)
{
throw new RuntimeException("Error loading constants, no mod to load for found");
throw new IllegalStateException("No active mod container");
}
return loadContext(new JsonContext(mod.getModId()), file);
}
public static JsonContext loadContext(String path) throws IOException
return loadContext(path, mod);
}
public static JsonContext loadContext(ResourceLocation path, ModContainer mod) throws IOException
{
ModContainer mod = Loader.instance().activeModContainer();
if(mod == null)
{
throw new RuntimeException("Error loading constants, no mod to load for found");
}
return loadContext(mod, new JsonContext(mod.getModId()), path);
}
@@ -862,31 +859,32 @@ public class CraftingHelper {
}
catch (IOException e)
{
FMLLog.log.error("Error loading constants from file: {}", file.getAbsolutePath(), e);
throw e;
throw new IOException("Error loading constants from file: " + file.getAbsolutePath(), e);
}
}
private static JsonContext loadContext(ModContainer mod, JsonContext ctx, String path) throws IOException
private static JsonContext loadContext(ModContainer mod, JsonContext ctx, ResourceLocation path) throws IOException
{
Path fPath = null;
if(mod.getSource().isFile())
{
try(FileSystem fs = FileSystems.newFileSystem(mod.getSource().toPath(), null))
{
fPath = fs.getPath("/assets/" + ctx.getModId() + path);
fPath = fs.getPath("assets", path.getResourceDomain(), path.getResourcePath());
}
}
else if (mod.getSource().isDirectory())
{
fPath = mod.getSource().toPath().resolve("assets/" + ctx.getModId() + path);
fPath = mod.getSource().toPath().resolve(Paths.get("assets", path.getResourceDomain(), path.getResourcePath()));
}
if (fPath != null && Files.exists(fPath))
{
return loadContext(ctx, fPath.toFile());
} else {
throw new IOException("path could not be resolved: " + path);
}
else
{
throw new FileNotFoundException(fPath != null ? fPath.toString() : path.toString());
}
}
}
@@ -230,7 +230,7 @@ public class GameRegistry
public static void registerTileEntity(Class<? extends TileEntity> tileEntityClass, String key)
{
// As return is ignored for compatibility, always check namespace
GameData.checkPrefix(new ResourceLocation(key).toString());
GameData.checkPrefix(new ResourceLocation(key).toString(), true);
TileEntity.register(key, tileEntityClass);
}
@@ -797,14 +797,33 @@ public class GameData
*/
}
/**
* @deprecated Use {@link #checkPrefix(String, boolean)}.
*/
@Deprecated
public static ResourceLocation checkPrefix(String name)
{
return checkPrefix(name, true);
}
/**
* Check a name for a domain prefix, and if not present infer it from the
* current active mod container.
*
* @param name The name or resource location
* @param warnOverrides If true, logs a warning if domain differs from that of
* the currently currently active mod container
*
* @return The {@link ResourceLocation} with given or inferred domain
*/
public static ResourceLocation checkPrefix(String name, boolean warnOverrides)
{
int index = name.lastIndexOf(':');
String oldPrefix = index == -1 ? "" : name.substring(0, index).toLowerCase(Locale.ROOT);
name = index == -1 ? name : name.substring(index + 1);
ModContainer mc = Loader.instance().activeModContainer();
String prefix = mc == null || (mc instanceof InjectedModContainer && ((InjectedModContainer)mc).wrappedContainer instanceof FMLContainer) ? "minecraft" : mc.getModId().toLowerCase(Locale.ROOT);
if (!oldPrefix.equals(prefix) && oldPrefix.length() > 0)
if (warnOverrides && !oldPrefix.equals(prefix) && oldPrefix.length() > 0)
{
FMLLog.log.warn("Potentially Dangerous alternative prefix `{}` for name `{}`, expected `{}`. This could be a intended override, but in most cases indicates a broken mod.", oldPrefix, name, prefix);
prefix = oldPrefix;
@@ -70,7 +70,7 @@ public interface IForgeRegistryEntry<V>
if (getRegistryName() != null)
throw new IllegalStateException("Attempted to set registry name with existing registry name! New: " + name + " Old: " + getRegistryName());
this.registryName = GameData.checkPrefix(name);
this.registryName = GameData.checkPrefix(name, true);
return (T)this;
}