| Line 46: |
Line 46: |
| | | | |
| | == The actual Class for Lootables == | | == The actual Class for Lootables == |
| | + | <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().setParamSet(LootParameterSets.BLOCK).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 "MagicScrolls 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 |