Jump to content

How to register new LootFunction - 1.16.5


Tut

Recommended Posts

Hi, I'm trying to register a new LootFunction to use in my loot table data generator. I was loosely basing it off of Astral Sorcery's loot table data generators, since I'm trying to achieve a similar effect.

Here's my loot table provider: 

package org.tutmods.shungite.data;

import com.google.common.collect.ImmutableList;
import com.mojang.datafixers.util.Pair;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.data.DataGenerator;
import net.minecraft.data.LootTableProvider;
import net.minecraft.data.loot.BlockLootTables;
import net.minecraft.enchantment.Enchantments;
import net.minecraft.item.Item;
import net.minecraft.item.Items;
import net.minecraft.loot.ItemLootEntry;
import net.minecraft.loot.LootParameterSet;
import net.minecraft.loot.LootParameterSets;
import net.minecraft.loot.LootPool;
import net.minecraft.loot.LootTable;
import net.minecraft.loot.LootTableManager;
import net.minecraft.loot.RandomValueRange;
import net.minecraft.loot.ValidationTracker;
import net.minecraft.loot.functions.ApplyBonus;
import net.minecraft.loot.functions.ExplosionDecay;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.RegistryObject;
import org.tutmods.shungite.loot.ShungiteCrystalPropertiesLoot;
import org.tutmods.shungite.setup.ModBlocks;
import org.tutmods.shungite.setup.ModItems;
import org.tutmods.shungite.setup.Registration;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;

public class ModLootTableProvider extends LootTableProvider {
    public ModLootTableProvider(DataGenerator generator) {
        super(generator);
    }

    @Override
    protected List<Pair<Supplier<Consumer<BiConsumer<ResourceLocation, LootTable.Builder>>>, LootParameterSet>> getTables() {
        return ImmutableList.of(Pair.of(ModBlockLootTables::new, LootParameterSets.BLOCK));
    }

    @Override
    protected void validate(Map<ResourceLocation, LootTable> map, ValidationTracker validationtracker) {
        map.forEach( (one, two) -> LootTableManager.validate(validationtracker, one, two));
    }

   public static class ModBlockLootTables extends BlockLootTables {
       @Override
       protected void addTables() {
           this.add(ModBlocks.SHUNGITE_CRYSTAL_ORE.get(), (block) ->
                   LootTable.lootTable()
                            .withPool(LootPool.lootPool()
                                              .setRolls(RandomValueRange.between(2F, 5F))
                                              .add(ItemLootEntry.lootTableItem(ModItems.SHUNGITE.get())
                                                                .apply(ExplosionDecay.explosionDecay())
                                                                .apply(ShungiteCrystalPropertiesLoot.builder()))
                           ));
       }

       @Override
       protected Iterable<Block> getKnownBlocks() {
           return Registration.BLOCK_DEFERRED_REGISTER.getEntries()
                   .stream().map(RegistryObject::get).collect(Collectors.toList());
       }
   }
}

And the loot function I want to add: 

package org.tutmods.shungite.loot;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonObject;
import net.minecraft.item.ItemStack;
import net.minecraft.loot.LootContext;
import net.minecraft.loot.LootFunction;
import net.minecraft.loot.LootFunctionType;
import net.minecraft.loot.conditions.ILootCondition;
import org.tutmods.shungite.items.crystal.ShungiteCrystal;
import org.tutmods.shungite.items.crystal.properties.ShungiteCrystalProperties;
import org.tutmods.shungite.util.crystal.CrystalUtils;

public class ShungiteCrystalPropertiesLoot extends LootFunction {
    protected ShungiteCrystalPropertiesLoot(ILootCondition[] conditions) {
        super(conditions);
    }

    @Override
    protected ItemStack run(ItemStack stack, LootContext lootContext) {
        if (stack.getItem() instanceof ShungiteCrystal) {
            final ShungiteCrystalProperties properties = CrystalPropertyGenerator.getRandomNewCrystalProperties();
            CrystalUtils.putProperties(stack, properties);
        }

        return stack;
    }

    public static LootFunction.Builder<?> builder() {
        return simpleBuilder(ShungiteCrystalPropertiesLoot::new);
    }

    @Override
    public LootFunctionType getType() {
        return Functions.ShungiteCrystalsLoot;
    }

    public static class Functions {
        public static LootFunctionType ShungiteCrystalsLoot;
    }

    public static class Serializer extends LootFunction.Serializer<ShungiteCrystalPropertiesLoot> {
        @Override
        public ShungiteCrystalPropertiesLoot deserialize(JsonObject jsonObject, JsonDeserializationContext jsonDeserializationContext, ILootCondition[] iLootConditions) {
            return new ShungiteCrystalPropertiesLoot(iLootConditions);
        }
    }
}

 

I see Astral doing similar things here:

https://github.com/HellFirePvP/AstralSorcery/blob/1.16-indev/src/main/java/hellfirepvp/astralsorcery/common/loot/RandomCrystalProperty.java

https://github.com/HellFirePvP/AstralSorcery/blob/e8434f1a09356632babefee1925626dc3deefb2a/src/main/java/hellfirepvp/astralsorcery/datagen/data/loot/BlockLootTableProvider.java#L77

However, my trouble comes when running the data generator. I can't register the serializer as Astral does here https://github.com/HellFirePvP/AstralSorcery/blob/e8434f1a09356632babefee1925626dc3deefb2a/src/main/java/hellfirepvp/astralsorcery/common/registry/RegistryLoot.java#L49, since LootFunctionManager.register is private. Is there a new way of doing this?

 

I tried just calling Registry.register as LootFunctionManager.register does itself, and I was able to generate JSON with that, however my game hung as soon as I broke the block; so I assume I'm doing something wrong. I saw there's a new DeferredRegister for LootModifiers, but I'm not sure if that fulfills the same role as a LootFunction?

 

So I guess I have two questions:

How do I register my serializer for my LootFunction correctly?

What's the difference between a LootFunction and a LootModifier?

 

Link to comment
Share on other sites

Slight update - I had a bug in my code that caused minecraft to hang. Pretty sure directly registering it with Registry.register would have worked; but my question of how to do it the "right" way still stands. Right now the access transformer works as intended.

Edited by Tut
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • On February 14th, Valentine’s Day, I found myself in a desperate situation: $150,000 worth of Bitcoin trapped in a wallet because I had forgotten the password. It might sound like a rookie mistake, but when Bitcoin surged, I decided it was time to sell some, only to realize I couldn’t access my wallet. Panic set in as I frantically tried everything—password managers, old notebooks, and even my fading muscle memory. Each failed attempt heightened my anxiety, and I felt foolish for not securing my information better. In a moment of frustration, I mentioned my dilemma to my cousin, who works in tech. He casually suggested ADWARE RECOVERY SPECIALIST, sharing how a colleague had a positive experience with them. Skeptical yet desperate, I decided to reach out. From the very first interaction, I was met with professionalism and empathy. The team reassured me that cases like mine were more common than I thought, which eased my embarrassment and offered a glimmer of hope. After providing the necessary details about my wallet, they initiated the recovery process. Their methodical approach kept me informed and hopeful. I appreciated how they broke down each step, allowing me to feel involved and reassured that my case was in good hands. A few days later, I received an email with the subject line: "Wallet Recovery Successful." Hesitant but hopeful, I opened it, and when I saw that $145,000 balance, relief washed over me. It felt like a heavy weight had been lifted; I could hardly believe it. This experience didn’t just allow me to regain access to my funds; it taught me invaluable lessons about digital security. I realized the importance of being proactive in protecting my digital assets. Since then, I’ve adopted a secure password manager and enabled two-factor authentication wherever possible. This ordeal highlighted the necessity of safeguarding my information and the potential consequences of negligence. Ultimately, my experience with ADWARE RECOVERY SPECIALIST transformed a nightmare into a success story. They made what seemed like an impossible situation manageable, and I couldn’t be more grateful. I can’t recommend them highly enough for anyone facing similar issues. Their expertise and support were truly magical, reminding me that even on a day meant for love, unexpected challenges can turn into victories. I now feel empowered to handle my digital assets with much greater care and confidence.
    • Hackathon Tech Solutions is a legitimate Bitcoin and Ethereum recovery company that specializes in helping individuals who have lost access to their cryptocurrency wallets or have been victims of hacking or scams. Our team of experts uses advanced technology and techniques to recover lost funds and secure your assets. We understand the importance of protecting your investments and work tirelessly to ensure that you regain control of your digital assets. Trust Hackathon Tech Solutions to provide you with reliable and efficient recovery services for your Ethereum and Bitcoin holdings.Reach out to HACKATHON TECH SOLUTIONS via below contact details Email: info  (@)  hackathon   tech  solution (.)  com Telegram:  (@)  hackathon  tech  solutions
    • When you are placing a minecart on Powered Rail, it is starting shaking and dismounting a player, instead of speeding up. (Without forge everything is ok, no mods installed)
    • why when I launch the forge version of minecraft in full screen and turn on screen recording, only the first static image of the launch is recorded and the gameplay itself is not recorded
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.