mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-28 07:55:53 +00:00
Add methods to allow loading json constants outside of _constants (#4975)
* add interface methods for loading json constants from an arbitary file * use try-with-resources * don't make modders create jsoncontext, clean up resource use * very minor cleanup
This commit is contained in:
1 parent
7f337cf230
commit
5f56b7cd09
4 files changed
+224
-111
No files matched your search
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Minecraft Forge
|
||||
* Copyright (c) 2016-2018.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation version 2.1
|
||||
* of the License.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package net.minecraftforge.debug.gameplay;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import net.minecraft.init.Items;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.item.crafting.Ingredient;
|
||||
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);
|
||||
|
||||
@EventHandler
|
||||
public void init(FMLInitializationEvent event)
|
||||
{
|
||||
if (!ENABLED)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
Ingredient flint = ctx.getConstant("FLINT");
|
||||
if (flint == null)
|
||||
{
|
||||
LOGGER.error("Ingredients not loaded correctly from json");
|
||||
return;
|
||||
}
|
||||
LOGGER.info("Ingredient matches correctly: {}", flint.apply(new ItemStack(Items.FLINT)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
[
|
||||
{
|
||||
"ingredient": {
|
||||
"item": "minecraft:flint"
|
||||
},
|
||||
"name": "FLINT"
|
||||
}
|
||||
]
|
||||
Reference in new issue
Block a user