Search in sources :

Example 6 with LootTableManager

use of net.minecraft.loot.LootTableManager in project minecolonies by ldtteam.

the class LootTableAnalyzer method toDrops.

public static List<LootDrop> toDrops(@NotNull final LootTableManager lootTableManager, @NotNull final JsonObject lootTableJson) {
    final List<LootDrop> drops = new ArrayList<>();
    if (!lootTableJson.has("pools")) {
        return drops;
    }
    final JsonArray pools = JSONUtils.getAsJsonArray(lootTableJson, "pools");
    for (final JsonElement pool : pools) {
        final JsonArray entries = JSONUtils.getAsJsonArray(pool.getAsJsonObject(), "entries", new JsonArray());
        final float totalWeight = StreamSupport.stream(entries.spliterator(), false).filter(entry -> {
            final String type = JSONUtils.getAsString(entry.getAsJsonObject(), "type");
            return type.equals("minecraft:empty") || type.equals("minecraft:item") || type.equals("minecraft:tag") || type.equals("minecraft:loot_table");
        }).mapToInt(entry -> JSONUtils.getAsInt(entry.getAsJsonObject(), "weight", 1)).sum();
        for (final JsonElement ej : entries) {
            final JsonObject entryJson = ej.getAsJsonObject();
            final String type = JSONUtils.getAsString(entryJson, "type");
            if (type.equals("minecraft:item")) {
                final Item item = ForgeRegistries.ITEMS.getValue(new ResourceLocation(JSONUtils.getAsString(entryJson, "name")));
                final float weight = JSONUtils.getAsFloat(entryJson, "weight", 1);
                final float quality = JSONUtils.getAsFloat(entryJson, "quality", 0);
                float modifier = 1.0F;
                final boolean conditional = JSONUtils.getAsJsonArray(entryJson, "conditions", new JsonArray()).size() > 0;
                ItemStack stack = new ItemStack(item);
                if (entryJson.has("functions")) {
                    final Tuple<ItemStack, Float> result = processFunctions(stack, JSONUtils.getAsJsonArray(entryJson, "functions"));
                    stack = result.getA();
                    modifier = result.getB();
                }
                if (stack.getItem().equals(ModItems.adventureToken)) {
                    final List<LootDrop> mobDrops = expandAdventureToken(lootTableManager, stack);
                    for (final LootTableAnalyzer.LootDrop drop : mobDrops) {
                        drops.add(new LootDrop(drop.getItemStacks(), drop.getProbability() * (weight / totalWeight), drop.getQuality() + quality, drop.getConditional() || conditional));
                    }
                } else {
                    drops.add(new LootDrop(Collections.singletonList(stack), weight / totalWeight * modifier, quality, conditional));
                }
            } else if (type.equals("minecraft:loot_table")) {
                final ResourceLocation table = new ResourceLocation(JSONUtils.getAsString(entryJson, "name"));
                final List<LootTableAnalyzer.LootDrop> tableDrops = toDrops(lootTableManager, table);
                final float weight = JSONUtils.getAsFloat(entryJson, "weight", 1);
                final float quality = JSONUtils.getAsFloat(entryJson, "quality", 0);
                final boolean conditional = JSONUtils.getAsJsonArray(entryJson, "conditions", new JsonArray()).size() > 0;
                for (final LootTableAnalyzer.LootDrop drop : tableDrops) {
                    drops.add(new LootDrop(drop.getItemStacks(), drop.getProbability() * (weight / totalWeight), drop.getQuality() + quality, drop.getConditional() || conditional));
                }
            }
        }
    }
    drops.sort(Comparator.comparing(LootDrop::getProbability).reversed());
    return drops;
}
Also used : EntityType(net.minecraft.entity.EntityType) JSONUtils(net.minecraft.util.JSONUtils) TAG_ENTITY_TYPE(com.minecolonies.api.util.constant.NbtTagConstants.TAG_ENTITY_TYPE) java.util(java.util) LootTable(net.minecraft.loot.LootTable) Item(net.minecraft.item.Item) JsonToNBT(net.minecraft.nbt.JsonToNBT) LootTableManager(net.minecraft.loot.LootTableManager) ItemStack(net.minecraft.item.ItemStack) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) ModItems(com.minecolonies.api.items.ModItems) StreamSupport(java.util.stream.StreamSupport) Log(com.minecolonies.api.util.Log) Tuple(net.minecraft.util.Tuple) Collectors(java.util.stream.Collectors) EnchantmentHelper(net.minecraft.enchantment.EnchantmentHelper) Nullable(org.jetbrains.annotations.Nullable) LootSerializers(net.minecraft.loot.LootSerializers) RandomValueRange(net.minecraft.loot.RandomValueRange) MathHelper(net.minecraft.util.math.MathHelper) ResourceLocation(net.minecraft.util.ResourceLocation) NotNull(org.jetbrains.annotations.NotNull) com.google.gson(com.google.gson) CommandSyntaxException(com.mojang.brigadier.exceptions.CommandSyntaxException) PacketBuffer(net.minecraft.network.PacketBuffer) ForgeRegistries(net.minecraftforge.registries.ForgeRegistries) Item(net.minecraft.item.Item) ResourceLocation(net.minecraft.util.ResourceLocation) ItemStack(net.minecraft.item.ItemStack)

Example 7 with LootTableManager

use of net.minecraft.loot.LootTableManager in project minecolonies by ldtteam.

the class GatherDataHandler method dataGeneratorSetup.

/**
 * This method is for adding datagenerators. this does not run during normal client operations, only during building.
 *
 * @param event event sent when you run the "runData" gradle task
 */
public static void dataGeneratorSetup(final GatherDataEvent event) {
    final DataGenerator generator = event.getGenerator();
    final LootTableManager lootTableManager = new DatagenLootTableManager(event.getExistingFileHelper());
    final BlockTagsProvider blockTagsProvider = new DefaultBlockTagsProvider(generator, event.getExistingFileHelper());
    generator.addProvider(new DefaultBlockLootTableProvider(generator));
    generator.addProvider(new DefaultAdvancementsProvider(generator, event.getExistingFileHelper()));
    generator.addProvider(new DefaultSoundProvider(generator));
    generator.addProvider(blockTagsProvider);
    generator.addProvider(new DefaultItemTagsProvider(generator, blockTagsProvider, event.getExistingFileHelper()));
    generator.addProvider(new DefaultEntityTypeTagsProvider(generator, event.getExistingFileHelper()));
    generator.addProvider(new DefaultResearchProvider(generator));
    generator.addProvider(new DefaultRecipeProvider(generator));
    generator.addProvider(new SawmillTimberFrameRecipeProvider(generator));
    generator.addProvider(new DefaultSifterCraftingProvider(generator, lootTableManager));
    generator.addProvider(new DefaultEnchanterCraftingProvider(generator));
    generator.addProvider(new DefaultFishermanLootProvider(generator));
    generator.addProvider(new DefaultConcreteMixerCraftingProvider(generator));
    generator.addProvider(new DefaultNetherWorkerLootProvider(generator, lootTableManager));
}
Also used : DatagenLootTableManager(com.minecolonies.coremod.generation.DatagenLootTableManager) LootTableManager(net.minecraft.loot.LootTableManager) BlockTagsProvider(net.minecraft.data.BlockTagsProvider) DataGenerator(net.minecraft.data.DataGenerator) DatagenLootTableManager(com.minecolonies.coremod.generation.DatagenLootTableManager)

Aggregations

LootTableManager (net.minecraft.loot.LootTableManager)7 ResourceLocation (net.minecraft.util.ResourceLocation)6 Item (net.minecraft.item.Item)5 java.util (java.util)4 Collectors (java.util.stream.Collectors)4 ItemStack (net.minecraft.item.ItemStack)4 LootTable (net.minecraft.loot.LootTable)4 PacketBuffer (net.minecraft.network.PacketBuffer)4 NotNull (org.jetbrains.annotations.NotNull)4 Nullable (org.jetbrains.annotations.Nullable)4 com.google.gson (com.google.gson)2 IMinecoloniesAPI (com.minecolonies.api.IMinecoloniesAPI)2 BuildingEntry (com.minecolonies.api.colony.buildings.registry.BuildingEntry)2 StandardFactoryController (com.minecolonies.api.colony.requestsystem.StandardFactoryController)2 ItemStorage (com.minecolonies.api.crafting.ItemStorage)2 ModLootTables (com.minecolonies.api.loot.ModLootTables)2 Log (com.minecolonies.api.util.Log)2 Network (com.minecolonies.coremod.Network)2 AnimalHerdingModule (com.minecolonies.coremod.colony.buildings.modules.AnimalHerdingModule)2 CommandSyntaxException (com.mojang.brigadier.exceptions.CommandSyntaxException)2