diff --git a/src/main/java/net/minecraftforge/common/crafting/CraftingHelper.java b/src/main/java/net/minecraftforge/common/crafting/CraftingHelper.java index 3b4396fe13..48b887b0ea 100644 --- a/src/main/java/net/minecraftforge/common/crafting/CraftingHelper.java +++ b/src/main/java/net/minecraftforge/common/crafting/CraftingHelper.java @@ -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()); } } } diff --git a/src/main/java/net/minecraftforge/fml/common/registry/GameRegistry.java b/src/main/java/net/minecraftforge/fml/common/registry/GameRegistry.java index 5c2f096745..9cdbc9eb73 100644 --- a/src/main/java/net/minecraftforge/fml/common/registry/GameRegistry.java +++ b/src/main/java/net/minecraftforge/fml/common/registry/GameRegistry.java @@ -230,7 +230,7 @@ public class GameRegistry public static void registerTileEntity(Class 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); } diff --git a/src/main/java/net/minecraftforge/registries/GameData.java b/src/main/java/net/minecraftforge/registries/GameData.java index e4be1c171b..f1bb1063d8 100644 --- a/src/main/java/net/minecraftforge/registries/GameData.java +++ b/src/main/java/net/minecraftforge/registries/GameData.java @@ -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; diff --git a/src/main/java/net/minecraftforge/registries/IForgeRegistryEntry.java b/src/main/java/net/minecraftforge/registries/IForgeRegistryEntry.java index 8a3cf07060..e46050fe64 100644 --- a/src/main/java/net/minecraftforge/registries/IForgeRegistryEntry.java +++ b/src/main/java/net/minecraftforge/registries/IForgeRegistryEntry.java @@ -70,7 +70,7 @@ public interface IForgeRegistryEntry 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; } diff --git a/src/test/java/net/minecraftforge/debug/gameplay/ConstantLoadingTest.java b/src/test/java/net/minecraftforge/debug/gameplay/ConstantLoadingTest.java index 1ee5cba265..e66b4a280e 100644 --- a/src/test/java/net/minecraftforge/debug/gameplay/ConstantLoadingTest.java +++ b/src/test/java/net/minecraftforge/debug/gameplay/ConstantLoadingTest.java @@ -24,23 +24,19 @@ import java.io.IOException; import net.minecraft.init.Items; import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.Ingredient; +import net.minecraft.util.ResourceLocation; import net.minecraftforge.common.crafting.CraftingHelper; import net.minecraftforge.common.crafting.JsonContext; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.Mod.EventHandler; import net.minecraftforge.fml.common.event.FMLInitializationEvent; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - @Mod(modid = ConstantLoadingTest.MODID, name = "ConstantLoadingTestMod", version = "1.0", acceptableRemoteVersions = "*") @Mod.EventBusSubscriber public class ConstantLoadingTest { public static final String MODID = "constantloadingtest"; - private static final boolean ENABLED = false; - - private static final Logger LOGGER = LogManager.getLogger(MODID); + private static final boolean ENABLED = true; @EventHandler public void init(FMLInitializationEvent event) @@ -49,22 +45,24 @@ public class ConstantLoadingTest { return; } - LOGGER.info("Loading constant for recipe..."); JsonContext ctx = null; try { - ctx = CraftingHelper.loadContext("/stuff/someConstants.json"); - } catch (IOException e) { - LOGGER.error("Something went wrong in loading the constant"); - return; + ctx = CraftingHelper.loadContext(new ResourceLocation(MODID, "test/_constants.json")); + } + catch (IOException e) + { + throw new RuntimeException("Exception loading test constants file", e); } Ingredient flint = ctx.getConstant("FLINT"); if (flint == null) { - LOGGER.error("Ingredients not loaded correctly from json"); - return; + throw new IllegalStateException("Constant ingredient not loaded properly"); + } + if (!flint.apply(new ItemStack(Items.FLINT))) + { + throw new IllegalStateException("Constant ingredient failed to match test input"); } - LOGGER.info("Ingredient matches correctly: {}", flint.apply(new ItemStack(Items.FLINT))); } } diff --git a/src/test/resources/assets/constantloadingtest/stuff/someConstants.json b/src/test/resources/assets/constantloadingtest/test/_constants.json similarity index 100% rename from src/test/resources/assets/constantloadingtest/stuff/someConstants.json rename to src/test/resources/assets/constantloadingtest/test/_constants.json