Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. package me.dinnerbeef.compressium.datagen;
  2.  
  3. import com.google.gson.Gson;
  4. import com.google.gson.GsonBuilder;
  5. import net.minecraft.block.Block;
  6. import net.minecraft.data.DataGenerator;
  7. import net.minecraft.data.DirectoryCache;
  8. import net.minecraft.data.IDataProvider;
  9. import net.minecraft.data.LootTableProvider;
  10. import net.minecraft.util.ResourceLocation;
  11. import net.minecraft.world.storage.loot.*;
  12. import net.minecraft.world.storage.loot.functions.CopyName;
  13. import net.minecraft.world.storage.loot.functions.CopyNbt;
  14. import net.minecraft.world.storage.loot.functions.SetContents;
  15. import org.apache.logging.log4j.LogManager;
  16. import org.apache.logging.log4j.Logger;
  17.  
  18. import java.io.IOException;
  19. import java.nio.file.Path;
  20. import java.util.HashMap;
  21. import java.util.Map;
  22.  
  23. public abstract class BaseLootTableProvider extends LootTableProvider {
  24.  
  25. private static final Logger LOGGER = LogManager.getLogger();
  26. private static final Gson GSON = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
  27.  
  28. protected final Map<Block, LootTable.Builder> lootTables = new HashMap<>();
  29. private final DataGenerator generator;
  30.  
  31. public BaseLootTableProvider(DataGenerator dataGeneratorIn) {
  32. super(dataGeneratorIn);
  33. this.generator = dataGeneratorIn;
  34. }
  35.  
  36. protected abstract void addTables();
  37.  
  38. protected LootTable.Builder createStandardTable(String name, Block block) {
  39. LootPool.Builder builder = LootPool.builder()
  40. .name(name)
  41. .rolls(ConstantRange.of(1))
  42. .addEntry(ItemLootEntry.builder(block)
  43. .acceptFunction(CopyName.builder(CopyName.Source.BLOCK_ENTITY))
  44. .acceptFunction(CopyNbt.func_215881_a(CopyNbt.Source.BLOCK_ENTITY)
  45. .func_216055_a("inv", "BlockEntityTag.inv", CopyNbt.Action.REPLACE)
  46. .func_216055_a("energy", "BlockEntityTag.energy", CopyNbt.Action.REPLACE))
  47. .acceptFunction(SetContents.func_215920_b()
  48. .func_216075_a(DynamicLootEntry.func_216162_a(new ResourceLocation("minecraft", "contents"))))
  49. );
  50. return LootTable.builder().addLootPool(builder);
  51. }
  52.  
  53. @Override
  54. public void act(DirectoryCache cache) {
  55. addTables();
  56.  
  57. Map<ResourceLocation, LootTable> tables = new HashMap<>();
  58. for (Map.Entry<Block, LootTable.Builder> entry : lootTables.entrySet()) {
  59. tables.put(entry.getKey().getLootTable(), entry.getValue().setParameterSet(LootParameterSets.BLOCK).build());
  60. }
  61. writeTables(cache, tables);
  62. }
  63.  
  64. private void writeTables(DirectoryCache cache, Map<ResourceLocation, LootTable> tables) {
  65. Path outputFolder = this.generator.getOutputFolder();
  66. tables.forEach((key, lootTable) -> {
  67. Path path = outputFolder.resolve("data/" + key.getNamespace() + "/loot_tables/" + key.getPath() + ".json");
  68. try {
  69. IDataProvider.save(GSON, cache, LootTableManager.toJson(lootTable), path);
  70. } catch (IOException e) {
  71. LOGGER.error("Couldn't write loot table {}", path, e);
  72. }
  73. });
  74. }
  75.  
  76. @Override
  77. public String getName() {
  78. return "Compressium LootTables";
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement