Use improved Forge LootTableLoadEvent, remove hacky LootTableLoader

This commit is contained in:
mezz committed 2017-04-07 22:09:44 -07:00
1 parent c4ecdde110
commit 3e175aa8e6
4 files changed
+12 -66

No files matched your search

+1 -1
View File
@@ -54,7 +54,7 @@ import net.minecraftforge.fml.common.registry.GameRegistry;
version = Constants.VERSION,
guiFactory = "forestry.core.config.ForestryGuiConfigFactory",
acceptedMinecraftVersions = "[1.11]",
dependencies = "required-after:forge@[13.20.0.2230,);"
dependencies = "required-after:forge@[13.20.0.2270,);"
+ "after:JEI@[4.3.0,);")
// + "after:" + PluginIC2.modId + ";")
public class Forestry {
@@ -10,6 +10,7 @@
******************************************************************************/
package forestry.core;
import java.net.URL;
import java.util.Collection;
import forestry.api.genetics.AlleleManager;
@@ -18,7 +19,6 @@ import forestry.api.genetics.IBreedingTracker;
import forestry.api.genetics.ISpeciesRoot;
import forestry.core.config.Constants;
import forestry.core.errors.ErrorStateRegistry;
import forestry.core.loot.LootTableLoader;
import forestry.core.models.ModelBlockCached;
import forestry.core.render.TextureManagerForestry;
import forestry.plugins.PluginManager;
@@ -107,12 +107,15 @@ public class EventHandlerCore {
}
ResourceLocation resourceLocation = new ResourceLocation(Constants.MOD_ID, event.getName().getResourcePath());
LootTable forestryChestAdditions = LootTableLoader.loadBuiltinLootTable(resourceLocation);
if (forestryChestAdditions != null) {
for (String poolName : PluginManager.getLootPoolNames()) {
LootPool pool = forestryChestAdditions.getPool(poolName);
if (pool != null) {
event.getTable().addPool(pool);
URL url = EventHandlerCore.class.getResource("/assets/" + resourceLocation.getResourceDomain() + "/loot_tables/" + resourceLocation.getResourcePath() + ".json");
if (url != null) {
LootTable forestryChestAdditions = event.getLootTableManager().getLootTableFromLocation(resourceLocation);
if (forestryChestAdditions != LootTable.EMPTY_LOOT_TABLE) {
for (String poolName : PluginManager.getLootPoolNames()) {
LootPool pool = forestryChestAdditions.getPool(poolName);
if (pool != null) {
event.getTable().addPool(pool);
}
}
}
}
@@ -1,57 +0,0 @@
package forestry.core.loot;
import javax.annotation.Nullable;
import java.io.IOException;
import java.net.URL;
import com.google.common.base.Charsets;
import com.google.common.io.Resources;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
import forestry.core.utils.Log;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.storage.loot.LootContext;
import net.minecraft.world.storage.loot.LootEntry;
import net.minecraft.world.storage.loot.LootPool;
import net.minecraft.world.storage.loot.LootTable;
import net.minecraft.world.storage.loot.LootTableManager;
import net.minecraft.world.storage.loot.RandomValueRange;
import net.minecraft.world.storage.loot.conditions.LootCondition;
import net.minecraft.world.storage.loot.conditions.LootConditionManager;
import net.minecraft.world.storage.loot.functions.LootFunction;
import net.minecraft.world.storage.loot.functions.LootFunctionManager;
/**
* Copy of {@link LootTableManager} that can load Forestry's loot table additions.
* This is a workaround so I can load loot table jsons that have pools to be added to vanilla's chests.
* During {@link net.minecraftforge.event.LootTableLoadEvent} the world's lootTable is not set yet.
*/
public class LootTableLoader {
private static final Gson GSON_INSTANCE = (new GsonBuilder()).registerTypeAdapter(RandomValueRange.class, new RandomValueRange.Serializer()).registerTypeAdapter(LootPool.class, new LootPool.Serializer()).registerTypeAdapter(LootTable.class, new LootTable.Serializer()).registerTypeHierarchyAdapter(LootEntry.class, new LootEntry.Serializer()).registerTypeHierarchyAdapter(LootFunction.class, new LootFunctionManager.Serializer()).registerTypeHierarchyAdapter(LootCondition.class, new LootConditionManager.Serializer()).registerTypeHierarchyAdapter(LootContext.EntityTarget.class, new LootContext.EntityTarget.Serializer()).create();
@Nullable
public static LootTable loadBuiltinLootTable(ResourceLocation resource) {
URL url = LootTableLoader.class.getResource("/assets/" + resource.getResourceDomain() + "/loot_tables/" + resource.getResourcePath() + ".json");
if (url != null) {
String s;
try {
s = Resources.toString(url, Charsets.UTF_8);
} catch (IOException ioexception) {
Log.warning("Couldn\'t load loot table " + resource + " from " + url, ioexception);
return LootTable.EMPTY_LOOT_TABLE;
}
try {
return net.minecraftforge.common.ForgeHooks.loadLootTable(GSON_INSTANCE, resource, s, false);
} catch (JsonParseException jsonparseexception) {
Log.error("Couldn\'t load loot table " + resource + " from " + url, jsonparseexception);
return LootTable.EMPTY_LOOT_TABLE;
}
} else {
return null;
}
}
}