Hello, today I will be walking you through the basics of creating your own mod loot tables. I will also show you how to manipulate already existing tables such as adding your own mod items to a dungeon chest's loot.
This tutorial will cover forge version 1.10, however it should work for 1.9 and above.
Today we will cover the following:
Creating a Json loot table
Registering a loot table
Creating an event handler to modify other loot tables
Before starting you should already know how to create a basic mod and how to use forge events.
For this tutorial I will be using snippets of the mod, if you get stuck or want more details, please review the full source.
Feel free to copy, and play with the provided code.
Lets get started.
First off, we will create our own Json loot table
What is a json? It's an external resource for code which can latter be manipulated by the user much like textures to suite their needs.
This dramatically increases flexibility and control over the game for the user. As of version 1.9 and latter this is the required method to setup and establish loot tables. You should review the format and features the new json files provide here -> http://minecraft.gamepedia.com/Loot_table.
Pretty much we're creating item entries, and their conditions to generate them, then assigning said entries to pools which are hosted by the loot table.
As you can see they can be quite complex and elaborate. Here is our practice mod's implementation. -> Custom_Chest_Loot.json
Note that we use unlocalized names for references, both vanilla and modded objects. You can use an application like notepad to create a json file, simply save it as a ".json" extension when you're done.
Once satisfied with our new loot table we will now add it to our assets folder for our mod. To do so we will navigate to our resources folder in our workspace. We will go through the following;
We will then create a new folder named "loot_tables" and place our json loot tables inside it. Later on when we set a resource location, it will look here for our mod. The end result should look like this -> Resource path
public static final ResourceLocation Custom_Chest_Loot = register("Custom_Chest_Loot");
private static ResourceLocation register(String id) {
return LootTableList.register(new ResourceLocation(Main.MODID, id));
}
As you can see we are creating a new ResourceLocation reference to our mod's loot table.
We use the vanilla LootTableList.class and it's register method to add our resource location to it's master table. Inside we are feeding it our mod's ID and the json's file name, which should be in the folder we created earlier.
Now that we have a reference and it's registered we can now take it for a spin and use it.
Inside the event we are provided with the current LootTable object and it's ResourceLocation that is being loaded. Keep in the mind the LootTable objects aren't easily readable, so we will ignore that for now. Instead we will focus on the ResourceLocation.
To start off we can add a funtion like
System.out.println(event.getName().toString());
inside our event. Once minecraft loads, and fires all of the events, we'll be able to figure out every LootTable we can work with, we should even see the one we added earlier show up.
Now that we know what our table resources look like we can do something like this to single out the Tables we want to work with
Note that I use the string.matches function deliberately as other comparators are a bit finicky in this situation.
Once we've singled out we can start manipulating the Loot Table. For our example we are adding another Loot table's pool to our target using something like this Pool Injection Method. Because accessing the current Tables and their pools directly isn't fun, we'll just make a new one to inject.
Our Example mod's event Handler looks like this -> Example Handler
@SubscribeEvent
public void Table_Additives(LootTableLoadEvent event){
String name = event.getName().toString();
try{
if(name.matches("minecraft:chests/spawn_bonus_chest")
|| name.matches("minecraft:entities/chicken")){
Main.logger.info("Matched our targets");
event.getTable().addPool(getAdditive("demo:Loot_Additive"));
}
if(name.matches("demo:Custom_Chest_Loot")){
Main.logger.info("Found our own loot");
}
Main.logger.info(name);
}catch(Exception exc){}
}
private LootPool getAdditive(String entryName) {
return new LootPool(new LootEntry[] { getAdditiveEntry(entryName, 1) }, new LootCondition[0], new RandomValueRange(1), new RandomValueRange(0, 1), "Additive_pool");
}
private LootEntryTable getAdditiveEntry(String name, int weight) {
return new LootEntryTable(new ResourceLocation(name), weight, 0, new LootCondition[0], "Additive_entry");
}
Inside it we are scanning for spawn chests and chicken's loot tables, then adding our own custom tables into them.
We can also observe non vanilla tables and modify them as well.
If you are using the Example mod you can test your new handler by bonking a few chickens or using the placement tool again.
Hello! I am currently looking for answers as to why this is not working for me.I have registered the mod events and have made every class with the exact base code except I configured it to fit my mod.
The code I have provided may give you more clues as to why the item is not working and why the chest isn't spawning the Pork and the Bow.
Hello, today I will be walking you through the basics of creating your own mod loot tables. I will also show you how to manipulate already existing tables such as adding your own mod items to a dungeon chest's loot.
This tutorial will cover forge version 1.10, however it should work for 1.9 and above.
Today we will cover the following:
Before starting you should already know how to create a basic mod and how to use forge events.
I have provided a fully working mod for reference Here -> Demo Mod Github
For this tutorial I will be using snippets of the mod, if you get stuck or want more details, please review the full source.
Feel free to copy, and play with the provided code.
Lets get started.
First off, we will create our own Json loot table
What is a json? It's an external resource for code which can latter be manipulated by the user much like textures to suite their needs.
This dramatically increases flexibility and control over the game for the user. As of version 1.9 and latter this is the required method to setup and establish loot tables. You should review the format and features the new json files provide here -> http://minecraft.gamepedia.com/Loot_table.
Pretty much we're creating item entries, and their conditions to generate them, then assigning said entries to pools which are hosted by the loot table.
As you can see they can be quite complex and elaborate. Here is our practice mod's implementation. -> Custom_Chest_Loot.json
Note that we use unlocalized names for references, both vanilla and modded objects. You can use an application like notepad to create a json file, simply save it as a ".json" extension when you're done.
Once satisfied with our new loot table we will now add it to our assets folder for our mod. To do so we will navigate to our resources folder in our workspace. We will go through the following;
We will then create a new folder named "loot_tables" and place our json loot tables inside it. Later on when we set a resource location, it will look here for our mod. The end result should look like this -> Resource path
Next we will register our new Json loot table
This part is pretty simple as seen here -> Loot Table Registry class
As you can see we are creating a new ResourceLocation reference to our mod's loot table.
We use the vanilla LootTableList.class and it's register method to add our resource location to it's master table. Inside we are feeding it our mod's ID and the json's file name, which should be in the folder we created earlier.
Now that we have a reference and it's registered we can now take it for a spin and use it.
Here I have an item already setup to demonstrate it. -> Chest Loot Placer Item
Using the vanilla set table loot method, we can feed it our ResourceLocation to test things out.
We should see our new loot table and it's contents appear.
If we had a custom monster, we could also use the
method for entities to reference and drop our new loot table.
Creating our Loot Table Event Handler
Before we begin here lets learn a little about the Loot table Event.
Firstly, when minecraft initially loads the server world it will iterate through all of it's registered loot tables, including the one we added.
Every time it begins to load the next loot table, it will fire the event, once done loading they will be finalized and can't be edited later.
During this event we can add on to the loading loot table and so on.
This is where we come in.
To get started we will create a new event handler class, and register it with the event bus.
Inside the event we are provided with the current LootTable object and it's ResourceLocation that is being loaded. Keep in the mind the LootTable objects aren't easily readable, so we will ignore that for now. Instead we will focus on the ResourceLocation.
To start off we can add a funtion like
inside our event. Once minecraft loads, and fires all of the events, we'll be able to figure out every LootTable we can work with, we should even see the one we added earlier show up.
Now that we know what our table resources look like we can do something like this to single out the Tables we want to work with
Note that I use the string.matches function deliberately as other comparators are a bit finicky in this situation.
Once we've singled out we can start manipulating the Loot Table. For our example we are adding another Loot table's pool to our target using something like this Pool Injection Method. Because accessing the current Tables and their pools directly isn't fun, we'll just make a new one to inject.
Our Example mod's event Handler looks like this -> Example Handler
Inside it we are scanning for spawn chests and chicken's loot tables, then adding our own custom tables into them.
We can also observe non vanilla tables and modify them as well.
If you are using the Example mod you can test your new handler by bonking a few chickens or using the placement tool again.
Credits:
This tutorial is based off of Vazkii's Botania Loot Handler and Choonster's testmod for registry and Json basics.
Some additional Information can be found here, here and here by Williewillus
Hello! I am currently looking for answers as to why this is not working for me.I have registered the mod events and have made every class with the exact base code except I configured it to fit my mod.
The code I have provided may give you more clues as to why the item is not working and why the chest isn't spawning the Pork and the Bow.
https://gist.github.com/anonymous/cb28f1f23e036d2c6c815400bf115eab
Inactive account