Skip to content

Commit

Permalink
Clean up CraftingHelper constants loading API
Browse files Browse the repository at this point in the history
  • Loading branch information
tterrag1098 committed Dec 2, 2018
1 parent 5ef199f commit f1d1e88
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URI;
Expand All @@ -29,6 +30,7 @@
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;
Expand Down Expand Up @@ -832,23 +834,18 @@ else if (cont == null || !cont)
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);
}

Expand All @@ -862,31 +859,32 @@ private static JsonContext loadContext(JsonContext ctx, File file) throws IOExce
}
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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public static void addSmelting(@Nonnull ItemStack input, @Nonnull ItemStack outp
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);
}

Expand Down
21 changes: 20 additions & 1 deletion src/main/java/net/minecraftforge/registries/GameData.java
Original file line number Diff line number Diff line change
Expand Up @@ -797,14 +797,33 @@ public static void fireRegistryEvents(Predicate<ResourceLocation> filter)
*/
}

/**
* @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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public final T setRegistryName(String name)
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,19 @@
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)
Expand All @@ -49,22 +45,24 @@ public void init(FMLInitializationEvent event)
{
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)));
}
}

0 comments on commit f1d1e88

Please sign in to comment.