Allow conditional loading of advancements (#5255)

This commit is contained in:
Ben Staddon authored and LexManos committed 2019-08-28 19:02:29 -07:00
1 parent 7b4a01703b
commit a5d0a86760
2 files changed
+56 -31

No files matched your search

@@ -117,6 +117,7 @@ import net.minecraft.world.storage.loot.LootTable;
import net.minecraft.world.storage.loot.LootTableManager;
import net.minecraft.world.storage.loot.conditions.LootCondition;
import net.minecraftforge.common.crafting.CraftingHelper;
import net.minecraftforge.common.crafting.JsonContext;
import net.minecraftforge.common.util.BlockSnapshot;
import net.minecraftforge.event.AnvilUpdateEvent;
import net.minecraftforge.event.DifficultyChangeEvent;
@@ -1316,9 +1317,10 @@ public class ForgeHooks
public static boolean loadAdvancements(Map<ResourceLocation, Advancement.Builder> map)
{
CraftingHelper.init();
boolean errored = false;
setActiveModContainer(null);
//Loader.instance().getActiveModList().forEach((mod) -> loadFactories(mod));
Loader.instance().getActiveModList().forEach(ForgeHooks::loadFactories);
for (ModContainer mod : Loader.instance().getActiveModList())
{
errored |= !loadAdvancements(map, mod);
@@ -1327,6 +1329,11 @@ public class ForgeHooks
return errored;
}
private static void loadFactories(ModContainer mod)
{
CraftingHelper.loadFactories(mod, "assets/" + mod.getModId() + "/advancements", CraftingHelper.CONDITIONS);
}
@Nullable
public static CriticalHitEvent getCriticalHit(EntityPlayer player, Entity target, boolean vanillaCritical, float damageModifier)
{
@@ -1347,6 +1354,8 @@ public class ForgeHooks
private static boolean loadAdvancements(Map<ResourceLocation, Advancement.Builder> map, ModContainer mod)
{
JsonContext ctx = new JsonContext(mod.getModId());
return CraftingHelper.findFiles(mod, "assets/" + mod.getModId() + "/advancements", null,
(root, file) ->
{
@@ -1365,7 +1374,11 @@ public class ForgeHooks
try
{
reader = Files.newBufferedReader(file);
Advancement.Builder builder = JsonUtils.fromJson(AdvancementManager.GSON, reader, Advancement.Builder.class);
String contents = IOUtils.toString(reader);
JsonObject json = JsonUtils.gsonDeserialize(CraftingHelper.GSON, contents, JsonObject.class);
if (!CraftingHelper.processConditions(json, "conditions", ctx))
return true;
Advancement.Builder builder = JsonUtils.gsonDeserialize(AdvancementManager.GSON, contents, Advancement.Builder.class);
map.put(key, builder);
}
catch (JsonParseException jsonparseexception)
@@ -37,6 +37,7 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.BooleanSupplier;
import java.util.function.Function;
@@ -87,10 +88,10 @@ import net.minecraftforge.registries.RegistryManager;
public class CraftingHelper {
private static final boolean DEBUG_LOAD_MINECRAFT = false;
private static Gson GSON = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
private static Map<ResourceLocation, IConditionFactory> conditions = Maps.newHashMap();
private static Map<ResourceLocation, IIngredientFactory> ingredients = Maps.newHashMap();
private static Map<ResourceLocation, IRecipeFactory> recipes = Maps.newHashMap();
public static final Gson GSON = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
private static final Map<ResourceLocation, IConditionFactory> conditions = Maps.newHashMap();
private static final Map<ResourceLocation, IIngredientFactory> ingredients = Maps.newHashMap();
private static final Map<ResourceLocation, IRecipeFactory> recipes = Maps.newHashMap();
static {
init();
@@ -420,7 +421,7 @@ public class CraftingHelper {
// INTERNAL
//=======================================================
private static void init()
public static void init()
{
conditions.clear();
ingredients.clear();
@@ -556,35 +557,41 @@ public class CraftingHelper {
register(new ResourceLocation(name), fac);
}
static void loadFactories(JsonObject json, JsonContext context)
public static final class FactoryLoader<T>
{
if (json.has("ingredients"))
{
for (Entry<String, JsonElement> entry : JsonUtils.getJsonObject(json, "ingredients").entrySet())
{
ResourceLocation key = new ResourceLocation(context.getModId(), entry.getKey());
String clsName = JsonUtils.getString(entry.getValue(), "ingredients[" + entry.getValue() + "]");
register(key, getClassInstance(clsName, IIngredientFactory.class));
}
}
final String name;
final Class<T> type;
final BiConsumer<ResourceLocation, T> consumer;
if (json.has("recipes"))
FactoryLoader(String name, Class<T> type, BiConsumer<ResourceLocation, T> consumer)
{
for (Entry<String, JsonElement> entry : JsonUtils.getJsonObject(json, "recipes").entrySet())
{
ResourceLocation key = new ResourceLocation(context.getModId(), entry.getKey());
String clsName = JsonUtils.getString(entry.getValue(), "recipes[" + entry.getValue() + "]");
register(key, getClassInstance(clsName, IRecipeFactory.class));
}
this.name = name;
this.type = type;
this.consumer = consumer;
}
}
if (json.has("conditions"))
public static final FactoryLoader<IIngredientFactory> INGREDIENTS = new FactoryLoader<>("ingredients", IIngredientFactory.class, CraftingHelper::register);
public static final FactoryLoader<IRecipeFactory> RECIPES = new FactoryLoader<>("recipes", IRecipeFactory.class, CraftingHelper::register);
public static final FactoryLoader<IConditionFactory> CONDITIONS = new FactoryLoader<>("conditions", IConditionFactory.class, CraftingHelper::register);
private static void loadFactories(JsonObject json, JsonContext context, FactoryLoader... loaders)
{
for (FactoryLoader<?> loader : loaders)
{
for (Entry<String, JsonElement> entry : JsonUtils.getJsonObject(json, "conditions").entrySet())
loadFactory(json, context, loader);
}
}
private static <T> void loadFactory(JsonObject json, JsonContext context, FactoryLoader<T> loader)
{
if (json.has(loader.name))
{
for (Entry<String, JsonElement> entry : JsonUtils.getJsonObject(json, loader.name).entrySet())
{
ResourceLocation key = new ResourceLocation(context.getModId(), entry.getKey());
String clsName = JsonUtils.getString(entry.getValue(), "conditions[" + entry.getValue() + "]");
register(key, getClassInstance(clsName, IConditionFactory.class));
String clsName = JsonUtils.getString(entry.getValue(), loader.name + "[" + entry.getValue() + "]");
loader.consumer.accept(key, getClassInstance(clsName, loader.type));
}
}
}
@@ -633,6 +640,11 @@ public class CraftingHelper {
}
private static void loadFactories(ModContainer mod)
{
loadFactories(mod, "assets/" + mod.getModId() + "/recipes", INGREDIENTS, RECIPES, CONDITIONS);
}
public static void loadFactories(ModContainer mod, String base, FactoryLoader... loaders)
{
FileSystem fs = null;
try
@@ -643,11 +655,11 @@ public class CraftingHelper {
if (mod.getSource().isFile())
{
fs = FileSystems.newFileSystem(mod.getSource().toPath(), null);
fPath = fs.getPath("/assets/" + ctx.getModId() + "/recipes/_factories.json");
fPath = fs.getPath("/" + base, "_factories.json");
}
else if (mod.getSource().isDirectory())
{
fPath = mod.getSource().toPath().resolve("assets/" + ctx.getModId() + "/recipes/_factories.json");
fPath = mod.getSource().toPath().resolve(base).resolve("_factories.json");
}
if (fPath != null && Files.exists(fPath))
@@ -655,7 +667,7 @@ public class CraftingHelper {
try (BufferedReader reader = Files.newBufferedReader(fPath))
{
JsonObject json = JsonUtils.fromJson(GSON, reader, JsonObject.class);
loadFactories(json, ctx);
loadFactories(json, ctx, loaders);
}
}
}