Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.16.5] [Solved] Get loot_tables from datapacks


Erfurt
 Share

Recommended Posts

Hey guys

So I have this class, where I get the chance value from a loot_table, it works perfectly, however I would like to add datapack functionality to this. And I'm pretty lost here, I know I have to add datapacks in my getManager(@Nullable World world) method, and I have no idea how to do that atm, so I could use some help achieving this

It is possible that there is a vanilla/forge method where they are already stored, but it's something I haven't been able to find myself

 

The class in question

Spoiler

public class LootTableUtil
{
    public static List<LootPool> getPools(LootTable table)
    {
        // public net.minecraft.loot.LootTable field_186466_c # pools
        return ObfuscationReflectionHelper.getPrivateValue(LootTable.class, table, "field_186466_c");
    }
 
    public static List<ILootCondition> getLootConditions(LootPool pool)
    {
        // public net.minecraft.loot.LootPool field_186454_b # conditions
        return ObfuscationReflectionHelper.getPrivateValue(LootPool.class, pool, "field_186454_b");
    }
 
    public static float getRandomChanceValue(RandomChance condition)
    {
        // public net.minecraft.loot.conditions.RandomChance field_186630_a # chance
        return ObfuscationReflectionHelper.getPrivateValue(RandomChance.class, condition, "field_186630_a");
    }
 
    public static float getRandomChanceWithLootingValue(RandomChanceWithLooting condition)
    {
        // public net.minecraft.loot.conditions.RandomChanceWithLooting field_186627_a # chance
        return ObfuscationReflectionHelper.getPrivateValue(RandomChanceWithLooting.class, condition, "field_186627_a");
    }
 
    public static float getLootingMultiplierValue(RandomChanceWithLooting condition)
    {
        // public net.minecraft.loot.conditions.RandomChanceWithLooting field_186628_b # lootingMultiplier
        return ObfuscationReflectionHelper.getPrivateValue(RandomChanceWithLooting.class, condition, "field_186628_b");
    }
 
    public static float getChance(ResourceLocation resource)
    {
        final Float[] chance = {null};
        final LootTableManager manager = getManager();
 
        LootTable table = manager.getLootTableFromLocation(resource);
 
        getPools(table).forEach(
                pool -> {
                    final List<ILootCondition> poolConditions = getLootConditions(pool);
                    poolConditions.forEach(condition -> {
                            if(condition instanceof RandomChance)
                            {
                                chance[0] = getRandomChanceValue((RandomChance) condition);
                            }
                            else if(condition instanceof RandomChanceWithLooting)
                            {
                                chance[0] = getRandomChanceWithLootingValue((RandomChanceWithLooting) condition);
                            }
                        }
                    );
                }
        );
 
        return chance[0];
    }
 
    public static float getLootingMultiplier(ResourceLocation resource)
    {
        final Float[] lootingMultiplier = {null};
        final LootTableManager manager = getManager();
 
        LootTable table = manager.getLootTableFromLocation(resource);
 
        getPools(table).forEach(
                pool -> {
                    final List<ILootCondition> poolConditions = getLootConditions(pool);
                    poolConditions.forEach(condition -> {
                            if(condition instanceof RandomChanceWithLooting)
                            {
                                lootingMultiplier[0] = getLootingMultiplierValue((RandomChanceWithLooting) condition);
                            }
                        }
                    );
                }
        );
 
        return lootingMultiplier[0];
    }
 
    private static LootTableManager manager;
 
    public static LootTableManager getManager(@Nullable World world)
    {
        if(world == null || world.getServer() == null)
        {
            if(manager == null)
            {
                manager = new LootTableManager(new LootPredicateManager());
 
                SimpleReloadableResourceManager serverResourceManger = new SimpleReloadableResourceManager(ResourcePackType.SERVER_DATA);
                List<IResourcePack> packs = new LinkedList<>();
                packs.add(new VanillaPack("minecraft"));
                for(ModFileInfo mod : ModList.get().getModFiles())
                {
                    if(mod.getFile().getFileName().contains(MagicalJewelry.MOD_ID) || mod.getFile().getFileName().equals("main"))
                    {
                        packs.add(new ModFileResourcePack(mod.getFile()));
                    }
                }
                packs.forEach(serverResourceManger::addResourcePack);
                serverResourceManger.addReloadListener(manager);
                CompletableFuture<Unit> completableFuture = serverResourceManger.reloadResourcesAndThen(Util.getServerExecutor(), Minecraft.getInstance(), packs, CompletableFuture.completedFuture(Unit.INSTANCE));
                Minecraft.getInstance().driveUntil(completableFuture::isDone);
            }
            return manager;
        }
        return world.getServer().getLootTableManager();
    }
 
    public static LootTableManager getManager()
    {
        World world = Minecraft.getInstance().world;
        return getManager(world);
    }
}

 

 

Link to comment
Share on other sites

  • You should use ObfuscationReflectionHelper.getField and store that in a static final field. getPrivateValue is slow and therefore should be used only for one-off lookups.
  • Your code in getManager(World) makes zero sense. Do not try to make your own LootTableManager... Why are you doing that? MinecraftServer#getLootTables gives it to you.
  • Your code in getManager() makes no sense either. This will hard-crash on servers (the server has no Minecraft class) and in generate data pack things are server side, so using Minecraft#world for it doesn't work.
Link to comment
Share on other sites

12 hours ago, diesieben07 said:
  • You should use ObfuscationReflectionHelper.getField and store that in a static final field. getPrivateValue is slow and therefore should be used only for one-off lookups.
  • Your code in getManager(World) makes zero sense. Do not try to make your own LootTableManager... Why are you doing that? MinecraftServer#getLootTables gives it to you.
  • Your code in getManager() makes no sense either. This will hard-crash on servers (the server has no Minecraft class) and in generate data pack things are server side, so using Minecraft#world for it doesn't work.

I don't see those two methods, getField and getLootTables, I assume it's findField and getLootTableManager?

 

It's my first time doing anything with reflection, so will that method still get the value?

Link to comment
Share on other sites

8 minutes ago, Erfurt said:

I don't see those two methods, getField and getLootTables, I assume it's findField and getLootTableManager?

yes

9 minutes ago, Erfurt said:

It's my first time doing anything with reflection, so will that method still get the value?

if you store the value in a static final field?
yes this is basic java

Link to comment
Share on other sites

8 hours ago, diesieben07 said:

MinecraftServer#getLootTableManager, just call it.

I would love to, but non-static, my methods are used within some commands that are static

Link to comment
Share on other sites

  • Erfurt changed the title to [1.16.5] [Solved] Get loot_tables from datapacks

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
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.

 Share



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • does anybody know how to fix this? (Im not too tech savy and dont know alot) basically i made anew modpack today. Tried to create a new world and it crashes every time with this as the crash exit code : ---- Minecraft Crash Report ---- // I let you down. Sorry Time: 24/09/21 18:12 Description: Starting integrated server java.lang.NullPointerException: Starting integrated server     at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:770) ~[guava-21.0.jar:?] {}     at com.google.common.collect.ImmutableCollection$ArrayBasedBuilder.add(ImmutableCollection.java:477) ~[guava-21.0.jar:?] {}     at com.google.common.collect.ImmutableList$Builder.add(ImmutableList.java:758) ~[guava-21.0.jar:?] {}     at com.google.common.collect.CollectCollectors$$Lambda$2173/998099820.accept(Unknown Source) ~[?:?] {}     at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169) ~[?:1.8.0_51] {}     at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) ~[?:1.8.0_51] {}     at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) ~[?:1.8.0_51] {}     at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1374) ~[?:1.8.0_51] {}     at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:512) ~[?:1.8.0_51] {}     at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:502) ~[?:1.8.0_51] {}     at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) ~[?:1.8.0_51] {}     at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[?:1.8.0_51] {}     at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499) ~[?:1.8.0_51] {}     at net.minecraft.world.biome.provider.BiomeProvider.<init>(SourceFile:38) ~[?:?] {re:mixin,pl:accesstransformer:B,re:classloading,pl:accesstransformer:B}     at org.infernalstudios.infernalexp.world.dimension.ModNetherBiomeProvider.<init>(ModNetherBiomeProvider.java:63) ~[?:2.2.0] {re:mixin,re:classloading}     at org.infernalstudios.infernalexp.util.NoiseChunkGeneratorUtil.lambda$null$0(NoiseChunkGeneratorUtil.java:39) ~[?:2.2.0] {re:mixin,re:classloading}     at org.infernalstudios.infernalexp.util.NoiseChunkGeneratorUtil$$Lambda$2595/1435857604.apply(Unknown Source) ~[?:?] {}     at com.mojang.serialization.codecs.RecordCodecBuilder$Instance.lambda$ap3$6(RecordCodecBuilder.java:286) ~[datafixerupper-4.0.26.jar:?] {re:classloading}     at com.mojang.serialization.codecs.RecordCodecBuilder$Instance$$Lambda$932/1229281613.apply(Unknown Source) ~[?:?] {}     at com.mojang.serialization.codecs.RecordCodecBuilder$2.encode(RecordCodecBuilder.java:112) ~[datafixerupper-4.0.26.jar:?] {}     at com.mojang.serialization.codecs.KeyDispatchCodec.encode(KeyDispatchCodec.java:92) ~[datafixerupper-4.0.26.jar:?] {}     at com.mojang.serialization.MapCodec$MapCodecCodec.encode(MapCodec.java:96) ~[datafixerupper-4.0.26.jar:?] {}     at com.mojang.serialization.Encoder.encodeStart(Encoder.java:14) ~[datafixerupper-4.0.26.jar:?] {re:classloading}     at com.mojang.serialization.codecs.FieldEncoder.encode(FieldEncoder.java:24) ~[datafixerupper-4.0.26.jar:?] {}     at com.mojang.serialization.MapCodec$1.encode(MapCodec.java:39) ~[datafixerupper-4.0.26.jar:?] {}     at com.mojang.serialization.codecs.RecordCodecBuilder$Instance$4.encode(RecordCodecBuilder.java:223) ~[datafixerupper-4.0.26.jar:?] {}     at com.mojang.serialization.codecs.RecordCodecBuilder$2.encode(RecordCodecBuilder.java:112) ~[datafixerupper-4.0.26.jar:?] {}     at com.mojang.serialization.MapCodec$MapCodecCodec.encode(MapCodec.java:96) ~[datafixerupper-4.0.26.jar:?] {}     at com.mojang.serialization.Encoder.encodeStart(Encoder.java:14) ~[datafixerupper-4.0.26.jar:?] {re:classloading}     at com.mojang.serialization.codecs.BaseMapCodec.encode(BaseMapCodec.java:52) ~[datafixerupper-4.0.26.jar:?] {re:classloading}     at net.minecraftforge.common.LenientUnboundedMapCodec.encode(LenientUnboundedMapCodec.java:75) ~[?:?] {re:classloading}     at net.minecraftforge.common.LenientUnboundedMapCodec.encode(LenientUnboundedMapCodec.java:22) ~[?:?] {re:classloading}     at com.mojang.serialization.Encoder$1.encode(Encoder.java:25) ~[datafixerupper-4.0.26.jar:?] {}     at com.mojang.serialization.Codec$2.encode(Codec.java:76) ~[datafixerupper-4.0.26.jar:?] {}     at net.minecraft.util.registry.SimpleRegistryCodec.encode(SourceFile:28) ~[?:?] {re:classloading}     at net.minecraft.util.registry.SimpleRegistryCodec.encode(SourceFile:11) ~[?:?] {re:classloading}     at com.mojang.serialization.Encoder$1.encode(Encoder.java:25) ~[datafixerupper-4.0.26.jar:?] {}     at com.mojang.serialization.Codec$2.encode(Codec.java:76) ~[datafixerupper-4.0.26.jar:?] {}     at com.mojang.serialization.Encoder.encodeStart(Encoder.java:14) ~[datafixerupper-4.0.26.jar:?] {re:classloading}     at com.mojang.serialization.codecs.FieldEncoder.encode(FieldEncoder.java:24) ~[datafixerupper-4.0.26.jar:?] {}     at com.mojang.serialization.MapCodec$1.encode(MapCodec.java:39) ~[datafixerupper-4.0.26.jar:?] {}     at com.mojang.serialization.codecs.RecordCodecBuilder$Instance$6.encode(RecordCodecBuilder.java:296) ~[datafixerupper-4.0.26.jar:?] {}     at com.mojang.serialization.codecs.RecordCodecBuilder$2.encode(RecordCodecBuilder.java:112) ~[datafixerupper-4.0.26.jar:?] {}     at com.mojang.serialization.MapCodec$MapCodecCodec.encode(MapCodec.java:96) ~[datafixerupper-4.0.26.jar:?] {}     at com.mojang.serialization.Encoder$1.encode(Encoder.java:25) ~[datafixerupper-4.0.26.jar:?] {}     at com.mojang.serialization.Codec$2.encode(Codec.java:76) ~[datafixerupper-4.0.26.jar:?] {}     at com.mojang.serialization.Encoder.encodeStart(Encoder.java:14) ~[datafixerupper-4.0.26.jar:?] {re:classloading}     at net.minecraft.world.storage.ServerWorldInfo.func_237370_a_(SourceFile:250) ~[?:?] {re:classloading,pl:accesstransformer:B}     at net.minecraft.world.storage.ServerWorldInfo.func_230411_a_(SourceFile:231) ~[?:?] {re:classloading,pl:accesstransformer:B}     at net.minecraft.world.storage.SaveFormat$LevelSave.func_237288_a_(SaveFormat.java:341) ~[?:?] {re:computing_frames,re:mixin,re:classloading}     at net.minecraft.world.storage.SaveFormat$LevelSave.func_237287_a_(SaveFormat.java:336) ~[?:?] {re:computing_frames,re:mixin,re:classloading}     at net.minecraft.client.Minecraft.loadWorld(Minecraft.java:1740) ~[?:?] {re:mixin,pl:accesstransformer:B,pl:runtimedistcleaner:A,re:classloading,pl:accesstransformer:B,pl:mixin:APP:abnormals_core.mixins.json:client.MinecraftMixin,pl:mixin:APP:outer_end.mixins.json:BackgroundMusicMixin,pl:mixin:APP:randompatches.mixins.json:client.MinecraftMixin,pl:mixin:APP:betterendforge.mixins.json:MinecraftMixin,pl:mixin:A,pl:runtimedistcleaner:A}     at net.minecraft.client.Minecraft.func_238192_a_(Minecraft.java:1685) ~[?:?] {re:mixin,pl:accesstransformer:B,pl:runtimedistcleaner:A,re:classloading,pl:accesstransformer:B,pl:mixin:APP:abnormals_core.mixins.json:client.MinecraftMixin,pl:mixin:APP:outer_end.mixins.json:BackgroundMusicMixin,pl:mixin:APP:randompatches.mixins.json:client.MinecraftMixin,pl:mixin:APP:betterendforge.mixins.json:MinecraftMixin,pl:mixin:A,pl:runtimedistcleaner:A}     at net.minecraft.client.gui.screen.CreateWorldScreen.func_195352_j(CreateWorldScreen.java:260) ~[?:?] {re:classloading,pl:runtimedistcleaner:A}     at net.minecraft.client.gui.screen.CreateWorldScreen.lambda$init$11(CreateWorldScreen.java:205) ~[?:?] {re:classloading,pl:runtimedistcleaner:A}     at net.minecraft.client.gui.screen.CreateWorldScreen$$Lambda$13249/653350518.onPress(Unknown Source) ~[?:?] {}     at net.minecraft.client.gui.widget.button.Button.func_230930_b_(SourceFile:33) ~[?:?] {re:classloading,pl:accesstransformer:B,re:mixin,pl:accesstransformer:B}     at net.minecraft.client.gui.widget.button.AbstractButton.func_230982_a_(SourceFile:16) ~[?:?] {re:classloading,re:mixin}     at net.minecraft.client.gui.widget.Widget.func_231044_a_(Widget.java:136) ~[?:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A,re:mixin,pl:accesstransformer:B,pl:runtimedistcleaner:A}     at net.minecraft.client.gui.INestedGuiEventHandler.func_231044_a_(SourceFile:27) ~[?:?] {re:computing_frames,re:mixin,re:classloading}     at net.minecraft.client.MouseHelper.func_198033_b(MouseHelper.java:87) ~[?:?] {re:mixin,pl:runtimedistcleaner:A,re:classloading,pl:mixin:APP:randompatches.mixins.json:client.keybindings.MouseHelperMixin,pl:mixin:A,pl:runtimedistcleaner:A}     at net.minecraft.client.MouseHelper$$Lambda$12852/788532761.run(Unknown Source) ~[?:?] {}     at net.minecraft.client.gui.screen.Screen.func_231153_a_(Screen.java:427) ~[?:?] {re:mixin,pl:accesstransformer:B,pl:runtimedistcleaner:A,re:computing_frames,pl:accesstransformer:B,pl:runtimedistcleaner:A,re:classloading,pl:accesstransformer:B,pl:mixin:APP:quark.mixins.json:client.ScreenMixin,pl:mixin:A,pl:runtimedistcleaner:A}     at net.minecraft.client.MouseHelper.func_198023_a(MouseHelper.java:85) ~[?:?] {re:mixin,pl:runtimedistcleaner:A,re:classloading,pl:mixin:APP:randompatches.mixins.json:client.keybindings.MouseHelperMixin,pl:mixin:A,pl:runtimedistcleaner:A}     at net.minecraft.client.MouseHelper.func_228030_c_(MouseHelper.java:181) ~[?:?] {re:mixin,pl:runtimedistcleaner:A,re:classloading,pl:mixin:APP:randompatches.mixins.json:client.keybindings.MouseHelperMixin,pl:mixin:A,pl:runtimedistcleaner:A}     at net.minecraft.client.MouseHelper$$Lambda$12851/2033249737.run(Unknown Source) ~[?:?] {}     at net.minecraft.util.concurrent.ThreadTaskExecutor.execute(ThreadTaskExecutor.java:111) ~[?:?] {re:mixin,pl:accesstransformer:B,xf:OptiFine:default,re:computing_frames,pl:accesstransformer:B,xf:OptiFine:default,re:classloading,pl:accesstransformer:B,xf:OptiFine:default}     at net.minecraft.client.MouseHelper.func_228028_b_(MouseHelper.java:180) ~[?:?] {re:mixin,pl:runtimedistcleaner:A,re:classloading,pl:mixin:APP:randompatches.mixins.json:client.keybindings.MouseHelperMixin,pl:mixin:A,pl:runtimedistcleaner:A}     at net.minecraft.client.MouseHelper$$Lambda$10760/310858279.invoke(Unknown Source) ~[?:?] {}     at org.lwjgl.glfw.GLFWMouseButtonCallbackI.callback(GLFWMouseButtonCallbackI.java:36) ~[lwjgl-glfw-3.2.2.jar:build 10] {}     at org.lwjgl.system.JNI.invokeV(Native Method) ~[lwjgl-3.2.2.jar:build 10] {}     at org.lwjgl.glfw.GLFW.glfwWaitEventsTimeout(GLFW.java:3174) ~[lwjgl-glfw-3.2.2.jar:build 10] {}     at com.mojang.blaze3d.systems.RenderSystem.limitDisplayFPS(SourceFile:127) ~[?:?] {re:mixin,re:classloading}     at net.minecraft.client.Minecraft.func_195542_b(Minecraft.java:999) [?:?] {re:mixin,pl:accesstransformer:B,pl:runtimedistcleaner:A,re:classloading,pl:accesstransformer:B,pl:mixin:APP:abnormals_core.mixins.json:client.MinecraftMixin,pl:mixin:APP:outer_end.mixins.json:BackgroundMusicMixin,pl:mixin:APP:randompatches.mixins.json:client.MinecraftMixin,pl:mixin:APP:betterendforge.mixins.json:MinecraftMixin,pl:mixin:A,pl:runtimedistcleaner:A}     at net.minecraft.client.Minecraft.func_99999_d(Minecraft.java:607) [?:?] {re:mixin,pl:accesstransformer:B,pl:runtimedistcleaner:A,re:classloading,pl:accesstransformer:B,pl:mixin:APP:abnormals_core.mixins.json:client.MinecraftMixin,pl:mixin:APP:outer_end.mixins.json:BackgroundMusicMixin,pl:mixin:APP:randompatches.mixins.json:client.MinecraftMixin,pl:mixin:APP:betterendforge.mixins.json:MinecraftMixin,pl:mixin:A,pl:runtimedistcleaner:A}     at net.minecraft.client.main.Main.main(Main.java:184) [?:?] {re:classloading,pl:runtimedistcleaner:A}     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_51] {}     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_51] {}     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_51] {}     at java.lang.reflect.Method.invoke(Method.java:497) ~[?:1.8.0_51] {}     at net.minecraftforge.fml.loading.FMLClientLaunchProvider.lambda$launchService$0(FMLClientLaunchProvider.java:51) [forge-1.16.5-36.2.5.jar:36.2] {}     at net.minecraftforge.fml.loading.FMLClientLaunchProvider$$Lambda$489/929856893.call(Unknown Source) [forge-1.16.5-36.2.5.jar:36.2] {}     at cpw.mods.modlauncher.LaunchServiceHandlerDecorator.launch(LaunchServiceHandlerDecorator.java:37) [modlauncher-8.0.9.jar:?] {}     at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:54) [modlauncher-8.0.9.jar:?] {}     at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:72) [modlauncher-8.0.9.jar:?] {}     at cpw.mods.modlauncher.Launcher.run(Launcher.java:82) [modlauncher-8.0.9.jar:?] {re:classloading}     at cpw.mods.modlauncher.Launcher.main(Launcher.java:66) [modlauncher-8.0.9.jar:?] {re:classloading}    
    • I think I've got it. Or at least a base to start working from. if (toolRef.getHarvestLevel(heldItems, ToolType.AXE, playerRef, blockState) <= 0) { event.setCanceled(true); } I've implemented it into my existing onBlockBreak event, because I was having issues to get it working for the HarvestCheck event. For a weird reason it wouldn't accept the BlockState reference inside the getHarvestLevel() method, when I was using it on HarvestCheck event. I'd still prefer any kind of reflection method though. Was way less complicated.
    • It's not only the tools. Let me explain. I remove some of the vanilla tools, primarily those made of wood, and then only need to check for tools in general. Ontop of that I've build some custom tools, which allows me to define what exactly they are allowed to harvest, and what not, in the specific custom tool class. Then I check against AIR(for having no item in my hands at all), entirely disallowing to harvest anything but for example, DIRT, CROPS, and stuff you'd need no tools to harvest in reality. Which leaves harvest with blocks in my hand open to be addressed. In the past I was just using the "setRequiresTool" reflection method, so I couldn't bypass my prohibition of harvesting anything with only AIR, or random blocks in my hand.
    • I have no idea what you are trying to do here. If you want to check for any tools, why do you have an if statement at all...?
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.