Line 46:
Line 46:
== The actual Class for Lootables ==
== The actual Class for Lootables ==
−
Here's an example Lootprovider class. The class is abstract and will be extended by the actual lootprovider class. This is however, optional. The class could also be used directly.
−
<syntaxhighlight lang="java">
−
public abstract class BaseLootTableProvider extends LootTableProvider {
−
−
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
−
private static final Logger LOGGER = LogManager.getLogger();
−
−
protected final Map<Block, LootTable.Builder> lootTables = new HashMap<>();
−
private final DataGenerator generator;
−
−
public BaseLootTableProvider(DataGenerator dataGeneratorIn) {
−
super(dataGeneratorIn);
−
this.generator = dataGeneratorIn;
−
}
−
−
protected abstract void addTables();
−
−
@Override
−
public void run(DirectoryCache cache) {
−
addTables();
−
−
Map<ResourceLocation, LootTable> tables = new HashMap<>();
−
for (Map.Entry<Block, LootTable.Builder> entry : lootTables.entrySet()) {
−
tables.put(entry.getKey().getLootTable(), entry.getValue().build());
−
}
−
writeTables(cache, tables);
−
}
−
−
private void writeTables(DirectoryCache cache, Map<ResourceLocation, LootTable> tables) {
−
Path outputFolder = this.generator.getOutputFolder();
−
tables.forEach((key, lootTable) -> {
−
Path path = outputFolder.resolve("data/" + key.getNamespace() + "/loot_tables/" + key.getPath() + ".json");
−
try {
−
IDataProvider.save(GSON, cache, LootTableManager.serialize(lootTable), path);
−
} catch (IOException e) {
−
LOGGER.error("Couldn't write loot table {}", path, (Object) e);
−
−
}
−
});
−
}
−
−
@Override
−
public String getName() {
−
return "Example LootTables";
−
}
−
}
−
</syntaxhighlight>
=== Another class (Optional) ===
=== Another class (Optional) ===
Create a new class that extends from the Class you created in the Section above and override the abstract function in there you can begin to create your Lootables.
Create a new class that extends from the Class you created in the Section above and override the abstract function in there you can begin to create your Lootables.
−
<syntaxhighlight lang="java">
−
public class LootTables extends BaseLootTableProvider{
−
@Override
−
protected void addTables() {
−
lootTables.put(BLOCK, LootTable.Builder);
−
}
−
}
−
</syntaxhighlight>
=== The LootPool Builder ===
=== The LootPool Builder ===
This is where you actually make a loottable (this is an explanation for a block loottable). If you have multiple blocks with similar loottables, making a general method could be a good idea. The method should return a <code>LootTable.Builder</code>. This builder can be made by using the <code>LootPool.lootPool()</code> method, but you still need to add attributes. You need a name for the pool, the amount you get and also "what" you get. The "what" can modified using functions and/or conditions (see [https://minecraft.fandom.com/wiki/Loot_table LootTables] for possible vanilla funtions and conditions). After having made the builder, you return <code>LootTable.lootTable().withPool(builder)</code>. An example of a "shulkerbox-like" block, copying its name, inventory and "energy" data to the block and restoring its contents when placed.
This is where you actually make a loottable (this is an explanation for a block loottable). If you have multiple blocks with similar loottables, making a general method could be a good idea. The method should return a <code>LootTable.Builder</code>. This builder can be made by using the <code>LootPool.lootPool()</code> method, but you still need to add attributes. You need a name for the pool, the amount you get and also "what" you get. The "what" can modified using functions and/or conditions (see [https://minecraft.fandom.com/wiki/Loot_table LootTables] for possible vanilla funtions and conditions). After having made the builder, you return <code>LootTable.lootTable().withPool(builder)</code>. An example of a "shulkerbox-like" block, copying its name, inventory and "energy" data to the block and restoring its contents when placed.