Search in sources :

Example 1 with LootTableManager

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

the class CustomRecipeManager method buildLootData.

/**
 * Analyses and builds an approximate list of possible loot drops from registered recipes.
 * @param lootTableManager the loot table manager
 */
public void buildLootData(@NotNull final LootTableManager lootTableManager) {
    final List<ResourceLocation> lootIds = new ArrayList<>();
    for (final Map<ResourceLocation, CustomRecipe> recipes : recipeMap.values()) {
        for (final CustomRecipe recipe : recipes.values()) {
            final ResourceLocation lootTable = recipe.getLootTable();
            if (lootTable != null) {
                lootIds.add(lootTable);
            }
        }
    }
    for (final BuildingEntry building : IMinecoloniesAPI.getInstance().getBuildingRegistry()) {
        building.getModuleProducers().stream().map(Supplier::get).filter(m -> m instanceof AnimalHerdingModule).map(m -> (AnimalHerdingModule) m).forEach(herding -> lootIds.add(herding.getDefaultLootTable()));
    }
    lootIds.add(ModLootTables.FISHING);
    lootIds.addAll(ModLootTables.FISHERMAN_BONUS.values());
    lootTables.clear();
    lootTables.putAll(lootIds.stream().filter(// just in case
    Objects::nonNull).distinct().collect(Collectors.toConcurrentMap(Function.identity(), id -> LootTableAnalyzer.toDrops(lootTableManager, id))));
}
Also used : AnimalHerdingModule(com.minecolonies.coremod.colony.buildings.modules.AnimalHerdingModule) BuildingEntry(com.minecolonies.api.colony.buildings.registry.BuildingEntry) java.util(java.util) Item(net.minecraft.item.Item) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) LootTableManager(net.minecraft.loot.LootTableManager) Unpooled(io.netty.buffer.Unpooled) StandardFactoryController(com.minecolonies.api.colony.requestsystem.StandardFactoryController) ItemStack(net.minecraft.item.ItemStack) Nullable(org.jetbrains.annotations.Nullable) MinecraftForge(net.minecraftforge.common.MinecraftForge) IMinecoloniesAPI(com.minecolonies.api.IMinecoloniesAPI) ResourceLocation(net.minecraft.util.ResourceLocation) Network(com.minecolonies.coremod.Network) ItemStorage(com.minecolonies.api.crafting.ItemStorage) NotNull(org.jetbrains.annotations.NotNull) PacketBuffer(net.minecraft.network.PacketBuffer) ModLootTables(com.minecolonies.api.loot.ModLootTables) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) BuildingEntry(com.minecolonies.api.colony.buildings.registry.BuildingEntry) ResourceLocation(net.minecraft.util.ResourceLocation) AnimalHerdingModule(com.minecolonies.coremod.colony.buildings.modules.AnimalHerdingModule)

Example 2 with LootTableManager

use of net.minecraft.loot.LootTableManager in project Mekanism by mekanism.

the class LootTableModifierReloadListener method onResourceManagerReload.

@Override
public void onResourceManagerReload(@Nonnull IResourceManager resourceManager) {
    // Grab the current shears tag instance as we will be doing identity compares and even if we weren't
    // the named wrapper for the tag won't be populated yet
    ITag<Item> shearsTag = dataPackRegistries.getTags().getItems().getTag(new ResourceLocation("forge", "shears"));
    LootTableManager lootTableManager = dataPackRegistries.getLootTables();
    for (ResourceLocation id : lootTableManager.getIds()) {
        LootTable lootTable = lootTableManager.get(id);
        boolean modified = false;
        // Scan all the loot tables in the upcoming/"current" loot table manager and evaluate if their pools need modifying
        for (LootPool pool : LOOT_TABLE_POOLS.getValue(lootTable)) {
            // 2. As the conditions to the loot pool itself
            for (LootEntry entry : LOOT_POOL_ENTRIES.getValue(pool)) {
                modified |= modifyEntry(entry, shearsTag);
            }
            for (ILootCondition condition : LOOT_POOL_CONDITIONS.getValue(pool)) {
                modified |= modifyCondition(condition, shearsTag);
            }
        }
        if (modified) {
            // If we changed any of the conditions in this loot table, add a debug message stating that we did so
            Mekanism.logger.debug("Modified and wrapped condition(s) in loot table '{}' to support items with the tool type of shears.", id);
        }
    }
    // We also need to scan all loot predicate in the upcoming/"current" manager, given even though vanilla doesn't add
    // any predicates by default that have to do with shears, we want to make sure we catch any that may be defined by
    // a mod or data pack
    LootPredicateManager predicateManager = dataPackRegistries.getPredicateManager();
    for (Map.Entry<ResourceLocation, ILootCondition> entry : LOOT_PREDICATE_CONDITIONS.getValue(predicateManager).entrySet()) {
        if (modifyCondition(entry.getValue(), shearsTag)) {
            // If we changed any of the conditions in this loot predicate, add a debug message stating that we did so
            Mekanism.logger.debug("Modified and wrapped condition for loot predicate '{}' to support items with the tool type of shears.", entry.getKey());
        }
    }
}
Also used : LootTable(net.minecraft.loot.LootTable) Item(net.minecraft.item.Item) ParentedLootEntry(net.minecraft.loot.ParentedLootEntry) LootEntry(net.minecraft.loot.LootEntry) LootPredicateManager(net.minecraft.loot.LootPredicateManager) ResourceLocation(net.minecraft.util.ResourceLocation) LootTableManager(net.minecraft.loot.LootTableManager) LootPool(net.minecraft.loot.LootPool) ILootCondition(net.minecraft.loot.conditions.ILootCondition) Map(java.util.Map)

Example 3 with LootTableManager

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

the class CustomRecipeManager method buildLootData.

/**
 * Analyses and builds an approximate list of possible loot drops from registered recipes.
 * @param lootTableManager the loot table manager
 */
public void buildLootData(@NotNull final LootTableManager lootTableManager) {
    final List<ResourceLocation> lootIds = new ArrayList<>();
    for (final Map<ResourceLocation, CustomRecipe> recipes : recipeMap.values()) {
        for (final CustomRecipe recipe : recipes.values()) {
            final ResourceLocation lootTable = recipe.getLootTable();
            if (lootTable != null) {
                lootIds.add(lootTable);
            }
        }
    }
    for (final BuildingEntry building : IMinecoloniesAPI.getInstance().getBuildingRegistry()) {
        building.getModuleProducers().stream().map(Supplier::get).filter(m -> m instanceof AnimalHerdingModule).map(m -> (AnimalHerdingModule) m).forEach(herding -> lootIds.add(herding.getDefaultLootTable()));
    }
    lootIds.add(ModLootTables.FISHING);
    lootIds.addAll(ModLootTables.FISHERMAN_BONUS.values());
    lootTables.clear();
    lootTables.putAll(lootIds.stream().filter(// just in case
    Objects::nonNull).distinct().collect(Collectors.toConcurrentMap(Function.identity(), id -> LootTableAnalyzer.toDrops(lootTableManager, id))));
}
Also used : AnimalHerdingModule(com.minecolonies.coremod.colony.buildings.modules.AnimalHerdingModule) BuildingEntry(com.minecolonies.api.colony.buildings.registry.BuildingEntry) java.util(java.util) Item(net.minecraft.item.Item) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) LootTableManager(net.minecraft.loot.LootTableManager) Unpooled(io.netty.buffer.Unpooled) StandardFactoryController(com.minecolonies.api.colony.requestsystem.StandardFactoryController) ItemStack(net.minecraft.item.ItemStack) Nullable(org.jetbrains.annotations.Nullable) MinecraftForge(net.minecraftforge.common.MinecraftForge) IMinecoloniesAPI(com.minecolonies.api.IMinecoloniesAPI) ResourceLocation(net.minecraft.util.ResourceLocation) Network(com.minecolonies.coremod.Network) ItemStorage(com.minecolonies.api.crafting.ItemStorage) NotNull(org.jetbrains.annotations.NotNull) PacketBuffer(net.minecraft.network.PacketBuffer) ModLootTables(com.minecolonies.api.loot.ModLootTables) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) BuildingEntry(com.minecolonies.api.colony.buildings.registry.BuildingEntry) ResourceLocation(net.minecraft.util.ResourceLocation) AnimalHerdingModule(com.minecolonies.coremod.colony.buildings.modules.AnimalHerdingModule)

Example 4 with LootTableManager

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

the class LootTableAnalyzer method toDrops.

public static List<LootDrop> toDrops(@Nullable 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();
                }
                drops.add(new LootDrop(Collections.singletonList(stack), weight / totalWeight * modifier, quality, conditional));
            } else if (type.equals("minecraft:loot_table") && lootTableManager != null) {
                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 : JSONUtils(net.minecraft.util.JSONUtils) java.util(java.util) LootTable(net.minecraft.loot.LootTable) Item(net.minecraft.item.Item) Tuple(net.minecraft.util.Tuple) JsonToNBT(net.minecraft.nbt.JsonToNBT) Collectors(java.util.stream.Collectors) EnchantmentHelper(net.minecraft.enchantment.EnchantmentHelper) LootTableManager(net.minecraft.loot.LootTableManager) ItemStack(net.minecraft.item.ItemStack) Nullable(org.jetbrains.annotations.Nullable) LootSerializers(net.minecraft.loot.LootSerializers) RandomValueRange(net.minecraft.loot.RandomValueRange) MathHelper(net.minecraft.util.math.MathHelper) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) ResourceLocation(net.minecraft.util.ResourceLocation) StreamSupport(java.util.stream.StreamSupport) Log(com.minecolonies.api.util.Log) 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 5 with LootTableManager

use of net.minecraft.loot.LootTableManager in project ChocolateQuestRepoured by TeamChocoQuest.

the class LootTableLoader method registerCustomLootTables.

public static void registerCustomLootTables(ServerWorld worldServer) {
    Collection<File> files = FileUtils.listFiles(new File(CQRMain.CQ_CHEST_FOLDER, "chests"), new String[] { "json", "properties" }, false);
    Set<ResourceLocation> cqrChestLootTables = CQRLoottables.getChestLootTables();
    LootTableManager lootTableManager = worldServer.getServer().getLootTables();
    LoadingCache<ResourceLocation, LootTable> registeredLootTables = FIELD_REGISTERED_LOOT_TABLES.get(lootTableManager);
    for (File file : files) {
        String s = file.getName();
        ResourceLocation name = new ResourceLocation(CQRMain.MODID, "chests/" + s.substring(0, s.lastIndexOf('.')));
        if (cqrChestLootTables.contains(name)) {
            continue;
        }
        LootTable table = LootTable.EMPTY;
        table = ForgeEventFactory.loadLootTable(name, table, lootTableManager);
        registeredLootTables.put(name, table);
    }
}
Also used : LootTable(net.minecraft.loot.LootTable) ResourceLocation(net.minecraft.util.ResourceLocation) LootTableManager(net.minecraft.loot.LootTableManager) File(java.io.File)

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