Add LootTableManager to the LootTableLoadEvent (#3717)

This commit is contained in:
Paulo "JCranky" Siqueira 2017-04-08 01:22:49 +02:00 committed by LexManos
parent b99aa9bca4
commit be2d814155
6 changed files with 220 additions and 7 deletions

View file

@ -5,7 +5,7 @@
try
{
- return (LootTable)LootTableManager.field_186526_b.fromJson(s, LootTable.class);
+ return net.minecraftforge.common.ForgeHooks.loadLootTable(LootTableManager.field_186526_b, p_186517_1_, s, true);
+ return net.minecraftforge.common.ForgeHooks.loadLootTable(LootTableManager.field_186526_b, p_186517_1_, s, true, LootTableManager.this);
}
catch (JsonParseException jsonparseexception)
{
@ -14,7 +14,7 @@
try
{
- return (LootTable)LootTableManager.field_186526_b.fromJson(s, LootTable.class);
+ return net.minecraftforge.common.ForgeHooks.loadLootTable(LootTableManager.field_186526_b, p_186518_1_, s, false);
+ return net.minecraftforge.common.ForgeHooks.loadLootTable(LootTableManager.field_186526_b, p_186518_1_, s, false, LootTableManager.this);
}
catch (JsonParseException jsonparseexception)
{

View file

@ -93,6 +93,7 @@ import net.minecraft.world.EnumDifficulty;
import net.minecraft.world.GameType;
import net.minecraft.world.storage.loot.LootEntry;
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.util.BlockSnapshot;
import net.minecraftforge.event.AnvilUpdateEvent;
@ -1092,13 +1093,21 @@ public class ForgeHooks
LootTableContext ctx = lootContext.get().peek();
if (ctx == null)
throw new JsonParseException("Invalid call stack, could to grab json context!"); // Show I throw this? Do we care about custom deserializers outside the manager?
throw new JsonParseException("Invalid call stack, could not grab json context!"); // Should I throw this? Do we care about custom deserializers outside the manager?
return ctx;
}
@Nullable
// TODO: remove
/** @deprecated use {@link ForgeHooks#loadLootTable(Gson, ResourceLocation, String, boolean, LootTableManager)} */
@Deprecated
public static LootTable loadLootTable(Gson gson, ResourceLocation name, String data, boolean custom)
{
return loadLootTable(gson, name, data, custom, null);
}
@Nullable
public static LootTable loadLootTable(Gson gson, ResourceLocation name, String data, boolean custom, LootTableManager lootTableManager)
{
Deque<LootTableContext> que = lootContext.get();
if (que == null)
@ -1121,7 +1130,7 @@ public class ForgeHooks
}
if (!custom)
ret = ForgeEventFactory.loadLootTable(name, ret);
ret = ForgeEventFactory.loadLootTable(name, ret, lootTableManager);
if (ret != null)
ret.freeze();

View file

@ -64,6 +64,7 @@ import net.minecraft.world.chunk.IChunkGenerator;
import net.minecraft.world.storage.IPlayerFileData;
import net.minecraft.world.storage.SaveHandler;
import net.minecraft.world.storage.loot.LootTable;
import net.minecraft.world.storage.loot.LootTableManager;
import net.minecraftforge.client.event.ClientChatEvent;
import net.minecraftforge.client.event.ClientChatReceivedEvent;
import net.minecraftforge.client.event.RenderBlockOverlayEvent;
@ -645,9 +646,18 @@ public class ForgeEventFactory
MinecraftForge.EVENT_BUS.post(pre ? new PopulateChunkEvent.Pre(gen, world, rand, x, z, hasVillageGenerated) : new PopulateChunkEvent.Post(gen, world, rand, x, z, hasVillageGenerated));
}
/**
* @deprecated Use {@link #loadLootTable(ResourceLocation, LootTable, LootTableManager)}<br>
*/
@Deprecated
public static LootTable loadLootTable(ResourceLocation name, LootTable table)
{
LootTableLoadEvent event = new LootTableLoadEvent(name, table);
return loadLootTable(name, table, null);
}
public static LootTable loadLootTable(ResourceLocation name, LootTable table, LootTableManager lootTableManager)
{
LootTableLoadEvent event = new LootTableLoadEvent(name, table, lootTableManager);
if (MinecraftForge.EVENT_BUS.post(event))
return LootTable.EMPTY_LOOT_TABLE;
return event.getTable();

View file

@ -21,6 +21,7 @@ package net.minecraftforge.event;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.storage.loot.LootTable;
import net.minecraft.world.storage.loot.LootTableManager;
import net.minecraftforge.fml.common.eventhandler.Cancelable;
import net.minecraftforge.fml.common.eventhandler.Event;
@ -38,11 +39,21 @@ public class LootTableLoadEvent extends Event
{
private final ResourceLocation name;
private LootTable table;
private LootTableManager lootTableManager;
public LootTableLoadEvent(ResourceLocation name, LootTable table)
/**
* @deprecated Use {@link #LootTableLoadEvent(ResourceLocation, LootTable, LootTableManager)}<br>
*/
@Deprecated
public LootTableLoadEvent(ResourceLocation name, LootTable table) {
this(name, table, null);
}
public LootTableLoadEvent(ResourceLocation name, LootTable table, LootTableManager lootTableManager)
{
this.name = name;
this.table = table;
this.lootTableManager = lootTableManager;
}
public ResourceLocation getName()
@ -55,6 +66,11 @@ public class LootTableLoadEvent extends Event
return this.table;
}
public LootTableManager getLootTableManager()
{
return this.lootTableManager;
}
public void setTable(LootTable table)
{
this.table = table;

View file

@ -0,0 +1,32 @@
package net.minecraftforge.test;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.storage.loot.LootTable;
import net.minecraft.world.storage.loot.LootTableList;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.LootTableLoadEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
@Mod(modid = LootTableLoadEventTest.MODID, name = "LootTableLoadEventTest", version = "1.0", acceptableRemoteVersions = "*")
public class LootTableLoadEventTest {
public static final String MODID = "loottable_load_event_test";
@EventHandler
public void init(FMLInitializationEvent event)
{
MinecraftForge.EVENT_BUS.register(this);
}
@SubscribeEvent
public void onLootTableLoadEvent(LootTableLoadEvent event)
{
if (LootTableList.CHESTS_SPAWN_BONUS_CHEST.equals(event.getName())) {
ResourceLocation loc = new ResourceLocation(MODID,"chests/custom_spawn_bonus_chest");
LootTable customLootTable = event.getLootTableManager().getLootTableFromLocation(loc);
event.setTable(customLootTable);
}
}
}

View file

@ -0,0 +1,146 @@
{
"pools": [
{
"name": "custom-spawn-chest-1",
"rolls": 1,
"entries": [
{
"type": "item",
"name": "minecraft:diamond_axe",
"weight": 1
},
{
"type": "item",
"name": "minecraft:iron_axe",
"weight": 3
}
]
},
{
"name": "custom-spawn-chest-2",
"rolls": 1,
"entries": [
{
"type": "item",
"name": "minecraft:diamond_pickaxe",
"weight": 1
},
{
"type": "item",
"name": "minecraft:iron_pickaxe",
"weight": 3
}
]
},
{
"name": "custom-spawn-chest-3",
"rolls": 3,
"entries": [
{
"type": "item",
"name": "minecraft:golden_apple",
"weight": 5,
"functions": [
{
"function": "minecraft:set_count",
"count": {
"min": 1,
"max": 2
}
}
]
},
{
"type": "item",
"name": "minecraft:beef",
"weight": 3,
"functions": [
{
"function": "minecraft:set_count",
"count": {
"min": 1,
"max": 2
}
}
]
}
]
},
{
"name": "custom-spawn-chest-4",
"rolls": 4,
"entries": [
{
"type": "item",
"name": "minecraft:stick",
"weight": 10,
"functions": [
{
"function": "minecraft:set_count",
"count": {
"min": 1,
"max": 24
}
}
]
},
{
"type": "item",
"name": "minecraft:planks",
"weight": 10,
"functions": [
{
"function": "minecraft:set_count",
"count": {
"min": 1,
"max": 24
}
}
]
},
{
"type": "item",
"name": "minecraft:log",
"weight": 10,
"functions": [
{
"function": "minecraft:set_count",
"count": {
"min": 1,
"max": 3
}
},
{
"function": "minecraft:set_data",
"data": {
"min": 0,
"max": 3
}
}
]
},
{
"type": "item",
"name": "minecraft:log2",
"weight": 10,
"functions": [
{
"function": "minecraft:set_count",
"count": {
"min": 1,
"max": 3
}
},
{
"function": "minecraft:set_data",
"data": {
"min": 0,
"max": 1
}
}
]
}
]
}
]
}