NaturesAura/src/main/java/de/ellpeck/naturesaura/data/BlockLootProvider.java

103 lines
4.5 KiB
Java
Raw Normal View History

2020-01-29 00:40:28 +01:00
package de.ellpeck.naturesaura.data;
2020-01-23 16:05:52 +01:00
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
2020-01-24 17:05:41 +01:00
import de.ellpeck.naturesaura.blocks.BlockGoldenLeaves;
import de.ellpeck.naturesaura.blocks.ModBlocks;
2020-01-23 16:05:52 +01:00
import de.ellpeck.naturesaura.blocks.Slab;
2020-01-24 17:05:41 +01:00
import de.ellpeck.naturesaura.items.ModItems;
2020-01-23 16:05:52 +01:00
import de.ellpeck.naturesaura.reg.IModItem;
import de.ellpeck.naturesaura.reg.ModRegistry;
2020-01-28 18:08:56 +01:00
import net.minecraft.advancements.criterion.StatePropertiesPredicate;
2020-01-23 16:05:52 +01:00
import net.minecraft.block.Block;
import net.minecraft.data.DataGenerator;
import net.minecraft.data.DirectoryCache;
import net.minecraft.data.IDataProvider;
2020-01-24 17:05:41 +01:00
import net.minecraft.data.loot.BlockLootTables;
2020-01-30 16:04:40 +01:00
import net.minecraft.item.Items;
2020-01-23 16:05:52 +01:00
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.storage.loot.*;
import net.minecraft.world.storage.loot.conditions.BlockStateProperty;
2020-01-24 17:05:41 +01:00
import net.minecraft.world.storage.loot.conditions.RandomChance;
2020-01-30 16:04:40 +01:00
import net.minecraft.world.storage.loot.functions.SetCount;
2020-01-23 16:05:52 +01:00
import javax.annotation.Nonnull;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
public class BlockLootProvider implements IDataProvider {
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
private final DataGenerator generator;
private final Map<Block, Function<Block, LootTable.Builder>> lootFunctions = new HashMap<>();
public BlockLootProvider(DataGenerator generator) {
this.generator = generator;
for (IModItem item : ModRegistry.ALL_ITEMS) {
if (!(item instanceof Block))
continue;
Block block = (Block) item;
if (block instanceof Slab) {
2020-01-24 17:05:41 +01:00
this.lootFunctions.put(block, LootTableHooks::genSlab);
2020-01-23 16:05:52 +01:00
} else {
2020-01-24 17:05:41 +01:00
this.lootFunctions.put(block, LootTableHooks::genRegular);
2020-01-23 16:05:52 +01:00
}
}
2020-01-24 17:05:41 +01:00
this.lootFunctions.put(ModBlocks.ANCIENT_LEAVES, b -> LootTableHooks.genLeaves(b, ModBlocks.ANCIENT_SAPLING));
this.lootFunctions.put(ModBlocks.DECAYED_LEAVES, LootTableHooks::genSilkOnly);
2020-01-28 18:08:56 +01:00
this.lootFunctions.put(ModBlocks.GOLDEN_LEAVES, b -> LootTable.builder().addLootPool(LootPool.builder().rolls(ConstantRange.of(1)).addEntry(LootTableHooks.survivesExplosion(b, ItemLootEntry.builder(ModItems.GOLD_LEAF)).acceptCondition(BlockStateProperty.builder(b).fromProperties(StatePropertiesPredicate.Builder.newBuilder().withIntProp(BlockGoldenLeaves.STAGE, BlockGoldenLeaves.HIGHEST_STAGE)))).acceptCondition(RandomChance.builder(0.75F))));
2020-01-30 16:04:40 +01:00
this.lootFunctions.put(ModBlocks.NETHER_WART_MUSHROOM, b -> LootTableHooks.genSilkOr(b, ItemLootEntry.builder(Items.NETHER_WART).acceptFunction(SetCount.builder(RandomValueRange.of(1, 2)))));
2020-01-23 16:05:52 +01:00
}
2020-02-07 15:22:30 +01:00
private static Path getPath(Path root, ResourceLocation res) {
return root.resolve("data/" + res.getNamespace() + "/loot_tables/blocks/" + res.getPath() + ".json");
}
2020-01-23 16:05:52 +01:00
@Override
public void act(DirectoryCache cache) throws IOException {
for (Map.Entry<Block, Function<Block, LootTable.Builder>> function : this.lootFunctions.entrySet()) {
Block block = function.getKey();
Function<Block, LootTable.Builder> func = function.getValue();
LootTable table = func.apply(block).setParameterSet(LootParameterSets.BLOCK).build();
Path path = getPath(this.generator.getOutputFolder(), block.getRegistryName());
IDataProvider.save(GSON, cache, LootTableManager.toJson(table), path);
}
}
@Nonnull
@Override
public String getName() {
return "Nature's Aura Loot";
}
2020-01-24 17:05:41 +01:00
// What a mess
private static class LootTableHooks extends BlockLootTables {
public static LootTable.Builder genLeaves(Block block, Block drop) {
2020-01-28 18:08:56 +01:00
return droppingWithChancesAndSticks(block, drop, 0.05F, 0.0625F, 0.083333336F, 0.1F);
2020-01-24 17:05:41 +01:00
}
public static LootTable.Builder genSlab(Block block) {
2020-01-28 18:08:56 +01:00
return droppingSlab(block);
2020-01-24 17:05:41 +01:00
}
public static LootTable.Builder genRegular(Block block) {
2020-01-28 18:08:56 +01:00
return dropping(block);
2020-01-24 17:05:41 +01:00
}
public static LootTable.Builder genSilkOnly(Block block) {
2020-01-28 18:08:56 +01:00
return onlyWithSilkTouch(block);
2020-01-24 17:05:41 +01:00
}
2020-01-30 16:04:40 +01:00
public static LootTable.Builder genSilkOr(Block block, LootEntry.Builder<?> builder) {
return droppingWithSilkTouch(block, builder);
}
2020-01-24 17:05:41 +01:00
public static <T> T survivesExplosion(Block block, ILootConditionConsumer<T> then) {
2020-01-28 18:08:56 +01:00
return withSurvivesExplosion(block, then);
2020-01-24 17:05:41 +01:00
}
}
2020-01-23 16:05:52 +01:00
}