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

122 lines
5.7 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-02-25 15:14:56 +01:00
import de.ellpeck.naturesaura.blocks.BlockFlowerPot;
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;
2021-12-15 16:24:53 +01:00
import net.minecraft.advancements.critereon.StatePropertiesPredicate;
2020-01-23 16:05:52 +01:00
import net.minecraft.data.DataGenerator;
2021-12-15 16:24:53 +01:00
import net.minecraft.data.DataProvider;
import net.minecraft.data.HashCache;
import net.minecraft.data.loot.BlockLoot;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.FlowerPotBlock;
import net.minecraft.world.level.storage.loot.LootPool;
import net.minecraft.world.level.storage.loot.LootTable;
import net.minecraft.world.level.storage.loot.LootTables;
import net.minecraft.world.level.storage.loot.entries.LootItem;
import net.minecraft.world.level.storage.loot.entries.LootPoolEntryContainer;
import net.minecraft.world.level.storage.loot.functions.SetItemCountFunction;
import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets;
import net.minecraft.world.level.storage.loot.predicates.ConditionUserBuilder;
import net.minecraft.world.level.storage.loot.predicates.LootItemBlockStatePropertyCondition;
import net.minecraft.world.level.storage.loot.predicates.LootItemRandomChanceCondition;
import net.minecraft.world.level.storage.loot.providers.number.ConstantValue;
import net.minecraft.world.level.storage.loot.providers.number.UniformGenerator;
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;
2021-12-15 16:24:53 +01:00
public class BlockLootProvider implements DataProvider {
2020-01-23 16:05:52 +01:00
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) {
2021-12-15 16:24:53 +01:00
if (!(item instanceof Block block))
2020-01-23 16:05:52 +01:00
continue;
if (block instanceof Slab) {
2020-01-24 17:05:41 +01:00
this.lootFunctions.put(block, LootTableHooks::genSlab);
2020-02-25 15:14:56 +01:00
} else if (block instanceof BlockFlowerPot) {
this.lootFunctions.put(block, LootTableHooks::genFlowerPot);
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, LootTableHooks::genSilkOnly);
2020-01-24 17:05:41 +01:00
this.lootFunctions.put(ModBlocks.DECAYED_LEAVES, LootTableHooks::genSilkOnly);
2021-12-15 16:24:53 +01:00
this.lootFunctions.put(ModBlocks.GOLDEN_LEAVES, b -> LootTable.lootTable().withPool(LootPool.lootPool().setRolls(ConstantValue.exactly(1)).add(LootTableHooks.survivesExplosion(b, LootItem.lootTableItem(ModItems.GOLD_LEAF)).when(LootItemBlockStatePropertyCondition.hasBlockStateProperties(b).setProperties(StatePropertiesPredicate.Builder.properties().hasProperty(BlockGoldenLeaves.STAGE, BlockGoldenLeaves.HIGHEST_STAGE)))).when(LootItemRandomChanceCondition.randomChance(0.75F))));
this.lootFunctions.put(ModBlocks.NETHER_WART_MUSHROOM, b -> LootTableHooks.genSilkOr(b, LootItem.lootTableItem(Items.NETHER_WART).apply(SetItemCountFunction.setCount(UniformGenerator.between(1, 2)))));
this.lootFunctions.put(ModBlocks.NETHER_GRASS, b -> LootTableHooks.genSilkOr(b, LootItem.lootTableItem(Blocks.NETHERRACK)));
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
2021-12-15 16:24:53 +01:00
public void run(HashCache cache) throws IOException {
2020-01-23 16:05:52 +01:00
for (Map.Entry<Block, Function<Block, LootTable.Builder>> function : this.lootFunctions.entrySet()) {
Block block = function.getKey();
Function<Block, LootTable.Builder> func = function.getValue();
2021-12-15 16:24:53 +01:00
LootTable table = func.apply(block).setParamSet(LootContextParamSets.BLOCK).build();
2020-01-23 16:05:52 +01:00
Path path = getPath(this.generator.getOutputFolder(), block.getRegistryName());
2021-12-15 16:24:53 +01:00
DataProvider.save(GSON, cache, LootTables.serialize(table), path);
2020-01-23 16:05:52 +01:00
}
}
@Nonnull
@Override
public String getName() {
return "Nature's Aura Loot";
}
2020-01-24 17:05:41 +01:00
// What a mess
2021-12-15 16:24:53 +01:00
private static class LootTableHooks extends BlockLoot {
2020-01-24 17:05:41 +01:00
public static LootTable.Builder genLeaves(Block block, Block drop) {
2021-12-15 16:24:53 +01:00
return createLeavesDrops(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) {
2021-12-15 16:24:53 +01:00
return createSlabItemTable(block);
2020-01-24 17:05:41 +01:00
}
public static LootTable.Builder genRegular(Block block) {
2021-12-15 16:24:53 +01:00
return createSingleItemTable(block);
2020-01-24 17:05:41 +01:00
}
public static LootTable.Builder genSilkOnly(Block block) {
2021-12-15 16:24:53 +01:00
return createSilkTouchOnlyTable(block);
2020-01-24 17:05:41 +01:00
}
2021-12-15 16:24:53 +01:00
public static LootTable.Builder genSilkOr(Block block, LootPoolEntryContainer.Builder<?> builder) {
return createSilkTouchOrShearsDispatchTable(block, builder);
2020-01-30 16:04:40 +01:00
}
2020-02-25 15:14:56 +01:00
public static LootTable.Builder genFlowerPot(Block block) {
2021-12-15 16:24:53 +01:00
return createPotFlowerItemTable(((FlowerPotBlock) block).getContent());
2020-02-25 15:14:56 +01:00
}
2021-12-15 16:24:53 +01:00
public static <T> T survivesExplosion(Block block, ConditionUserBuilder<T> then) {
return applyExplosionCondition(block, then);
2020-01-24 17:05:41 +01:00
}
}
2020-01-23 16:05:52 +01:00
}