Jump to content

[1.14.4] Loot Tables - item gives player random loot


FizixRichard

Recommended Posts

Hi all,

 

I'm creating a custom item, which when the player uses it, gives the player random items from a loot table.

 

I'm generating the custom item which appears in game, I have a loot table setup, and can use the item.

 

Where I'm struggling is getting random loot from the loot table and giving it to the player. When I look online I can see how to do it the 1.12 way, which uses loottable.generateLootForPools(). But, this doesn't work in 1.14, generateLootForPools() doesn't appear to exist.

 

Something like this in 1.12:

for (ItemStack itemstack : loottable.generateLootForPools(world.rand, builder.build())){    ItemEntity entityitem = new ItemEntity(world, player.posX, player.posY + 1.5D, player.posZ, itemstack);    world.spawnEntity(entityitem);}
	

 

Does anyone know how to do this in 1.14?

 

 

Here is the relevant code from my custom item's class...

 

I'm setting the loot table resource:

private static final ResourceLocation LOOT_TABLE = new ResourceLocation("mymod:mymod_loot");
	

 

 

I have an action for when  the player right clicks the item in their hand, which destroys the item and calls "generateLoot()"

@Overridepublic ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn){    LOGGER.debug("Right Clicked");    ItemStack itemstack = playerIn.getHeldItem(handIn);    // Remove item    itemstack.shrink(1);       if (!worldIn.isRemote)    {        generateLoot(worldIn, playerIn);    }    return new ActionResult<>(ActionResultType.SUCCESS, itemstack);}
	

 

 

The generateLoot method:

 

    private void generateLoot(World world,PlayerEntity player)    {        LootTable table = ServerLifecycleHooks.getCurrentServer().getLootTableManager().getLootTableFromLocation(LOOT_TABLE);        LootContext.Builder builder = new LootContext.Builder((ServerWorld) world);        // Here is where I need to generate random loot from loot table and give to player(???)           }
	

 

 

Any help would be greatly appreciated :)

 

Edited by FizixRichard
Link to comment
Share on other sites

Thanks for the reply.

 

I'm a beginner so I'm struggling a bit here. How do I use LootTable.generate()? I'm not finding any examples or documentation online.

 

Updated code to:

 

LootTable loottable = ServerLifecycleHooks.getCurrentServer().getLootTableManager().getLootTableFromLocation(LOOT_TABLE);
LootContext.Builder builder = new LootContext.Builder((ServerWorld) world);

for (ItemStack itemstack : loottable.generate(world.rand, builder.build()))

 

with:

for (ItemStack itemstack : loottable.generate(world.rand, builder.build()))

 

builder.build() isn't accepted.

Edited by FizixRichard
Link to comment
Share on other sites

But when I pass builder, or builder.build() (the LootContext I created) it says that it's incorrect. I know I'm probablty being really dumb here, but what am I doing wrong?

 

 

for (ItemStack itemstack : loottable.generate(builder.build()))
{

}

 

What does it want?

Edited by FizixRichard
Link to comment
Share on other sites

Thank you. I understand what you're saying now. But, I'm not finding any information anywhere.

 

I can't figure out how to set the LootParameterSet for this. I'm still learning, but I don't think this is something complicated. I'm clearly missing something obvious, but without any documentation or guides, I'm finding myself going around in circles. :/

 

 

Does anyone know how to declare the LootParameterSet for builder.build() to generate the ItemStack?

 

LootContext.Builder builder = new LootContext.Builder((ServerWorld) world);
LootTable loottable = ServerLifecycleHooks.getCurrentServer().getLootTableManager().getLootTableFromLocation(LOOT_TABLE);

for(ItemStack itemstack : loottable.generate(builder.build(???)))
{
    ItemEntity entityitem = new ItemEntity(world, player.posX, player.posY + 1.5D, player.posZ, itemstack);
    world.addEntity(entityitem);
}
Edited by FizixRichard
Link to comment
Share on other sites

Thanks. I have checked the classes, but I'm not sure what to set. Sorry.

 

When I try with EMPTY the for loop doesn't fire. It's just a standard loot table with a list of items.

 

for(ItemStack itemstack : loottable.generate(builder.build(LootParameterSets.EMPTY)))
{
    LOGGER.debug("Spawning Loot");

    ItemEntity entityitem = new ItemEntity(world, player.posX, player.posY + 1.5D, player.posZ, itemstack);
    world.addEntity(entityitem);
}
Link to comment
Share on other sites

I've checked the debugger and it's not throwing any errors.

 

If I check to see whether LOOT_TABLE is set, it's fine.

 

Check 1: reads "Loot table exists"

Check 2: reads "Item Stack List contains 0 records"

 

 

if (LOOT_TABLE == LootTables.EMPTY)
{
    LOGGER.debug("Loot table is empty");
}
else
{
    LOGGER.debug("Loot table exists");
}


LootContext.Builder builder = new LootContext.Builder((ServerWorld) world);
LootTable loottable = ServerLifecycleHooks.getCurrentServer().getLootTableManager().getLootTableFromLocation(LOOT_TABLE);
LootContext lootcontext = builder.withParameter(LootParameters.POSITION, player.getPosition()).withParameter(LootParameters.THIS_ENTITY, player).build(LootParameterSets.GIFT);

List<ItemStack> itemstacklist = loottable.generate(lootcontext);

LOGGER.debug("Item Stack List contains " + itemstacklist.size() + " records");

 

 

I've tried this with various parameter sets and it returns the same. If there is anything wrong with the loot table json, it would throw an error or warning wouldn't it?

 

 

Link to comment
Share on other sites

Done.

 

Could this have something to do with it?

 

lootcontext > lootTableManager > registeredLootTables > {ResourceLocation@18432} "minecraft:empty" -> {LootTable@15258}

lootcontext > lootTables = {LinkedHashSet@18751} size = 0

 

It's registered to minecraft:empty. "LootTable@15258" is the loottable, loot tables under lootcontext is 0, pools is empty, path is empty.

 

 

 

Edited by FizixRichard
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.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • I was just trying to play my modded world when i randomly got this crash for no reason. I sorted through like every mod and eventually I realized it was LLibrary but I can't seem to find a solution to fix the crashing. I can't lose the world that I have that uses this mod please help me. Here's the report: https://pastebin.com/0D00B79i If anyone has a solution please let me know.  
    • 🤑DAFTAR & LOGIN🤑 🤑DAFTAR & LOGIN🤑 🤑DAFTAR & LOGIN🤑   Daftar Slot Ligawin88 adalah bocoran slot rekomendasi gacor dari Ligawin88 yang bisa anda temukan di SLOT Ligawin88. Situs SLOT Ligawin88 hari ini yang kami bagikan di sini adalah yang terbaik dan bersiaplah untuk mengalami sensasi tak terlupakan dalam permainan slot online. Temukan game SLOT Ligawin88 terbaik dengan 100 pilihan provider ternama yang dipercaya akan memberikan kepuasan dan kemenangan hari ini untuk meraih x500. RTP SLOT Ligawin88 merupakan SLOT Ligawin88 hari ini yang telah menjadi pilihan utama bagi pemain judi online di seluruh Indonesia. Setiap harinya jutaan pemain memasuki dunia maya untuk memperoleh hiburan seru dan kemenangan besar dalam bermain slot dengan adanya bocoran RTP SLOT Ligawin88. Tidak ada yang lebih menyenangkan daripada mengungguli mesin slot dan meraih jackpot x500 yang menggiurkan di situs SLOT Ligawin88 hari ini yang telah disediakan SLOT Ligawin88. Menangkan jackpot besar x500 rajanya maxwin dari segala slot dan raih kemenangan spektakuler di situs Ligawin88 terbaik 2024 adalah tempat yang menyediakan mesin slot dengan peluang kemenangan lebih tinggi daripada situs slot lainnya. Bagi anda yang mencari pengalaman judi slot paling seru dan mendebarkan, situs bo SLOT Ligawin88 terbaik 2024 adalah pilihan yang tepat. Jelajahi dunia slot online melalui situs SLOT Ligawin88 di link SLOT Ligawin88.
    • 🤑DAFTAR & LOGIN🤑 🤑DAFTAR & LOGIN🤑 🤑DAFTAR & LOGIN🤑   Daftar Slot Asusslot adalah bocoran slot rekomendasi gacor dari Asusslot yang bisa anda temukan di SLOT Asusslot. Situs SLOT Asusslot hari ini yang kami bagikan di sini adalah yang terbaik dan bersiaplah untuk mengalami sensasi tak terlupakan dalam permainan slot online. Temukan game SLOT Asusslot terbaik dengan 100 pilihan provider ternama yang dipercaya akan memberikan kepuasan dan kemenangan hari ini untuk meraih x500. RTP SLOT Asusslot merupakan SLOT Asusslot hari ini yang telah menjadi pilihan utama bagi pemain judi online di seluruh Indonesia. Setiap harinya jutaan pemain memasuki dunia maya untuk memperoleh hiburan seru dan kemenangan besar dalam bermain slot dengan adanya bocoran RTP SLOT Asusslot. Tidak ada yang lebih menyenangkan daripada mengungguli mesin slot dan meraih jackpot x500 yang menggiurkan di situs SLOT Asusslot hari ini yang telah disediakan SLOT Asusslot. Menangkan jackpot besar x500 rajanya maxwin dari segala slot dan raih kemenangan spektakuler di situs Asusslot terbaik 2024 adalah tempat yang menyediakan mesin slot dengan peluang kemenangan lebih tinggi daripada situs slot lainnya. Bagi anda yang mencari pengalaman judi slot paling seru dan mendebarkan, situs bo SLOT Asusslot terbaik 2024 adalah pilihan yang tepat. Jelajahi dunia slot online melalui situs SLOT Asusslot di link SLOT Asusslot.
    • 🤑DAFTAR & LOGIN🤑 🤑DAFTAR & LOGIN🤑 🤑DAFTAR & LOGIN🤑 Daftar Slot Galeri555 adalah bocoran slot rekomendasi gacor dari Galeri555 yang bisa anda temukan di SLOT Galeri555. Situs SLOT Galeri555 hari ini yang kami bagikan di sini adalah yang terbaik dan bersiaplah untuk mengalami sensasi tak terlupakan dalam permainan slot online. Temukan game SLOT Galeri555 terbaik dengan 100 pilihan provider ternama yang dipercaya akan memberikan kepuasan dan kemenangan hari ini untuk meraih x500. RTP SLOT Galeri555 merupakan SLOT Galeri555 hari ini yang telah menjadi pilihan utama bagi pemain judi online di seluruh Indonesia. Setiap harinya jutaan pemain memasuki dunia maya untuk memperoleh hiburan seru dan kemenangan besar dalam bermain slot dengan adanya bocoran RTP SLOT Galeri555. Tidak ada yang lebih menyenangkan daripada mengungguli mesin slot dan meraih jackpot x500 yang menggiurkan di situs SLOT Galeri555 hari ini yang telah disediakan SLOT Galeri555. Menangkan jackpot besar x500 rajanya maxwin dari segala slot dan raih kemenangan spektakuler di situs Galeri555 terbaik 2024 adalah tempat yang menyediakan mesin slot dengan peluang kemenangan lebih tinggi daripada situs slot lainnya. Bagi anda yang mencari pengalaman judi slot paling seru dan mendebarkan, situs bo SLOT Galeri555 terbaik 2024 adalah pilihan yang tepat. Jelajahi dunia slot online melalui situs SLOT Galeri555 di link SLOT Galeri555.
    • 🤑DAFTAR & LOGIN🤑 🤑DAFTAR & LOGIN🤑 🤑DAFTAR & LOGIN🤑 Daftar Slot Kocok303 adalah bocoran slot rekomendasi gacor dari Kocok303 yang bisa anda temukan di SLOT Kocok303. Situs SLOT Kocok303 hari ini yang kami bagikan di sini adalah yang terbaik dan bersiaplah untuk mengalami sensasi tak terlupakan dalam permainan slot online. Temukan game SLOT Kocok303 terbaik dengan 100 pilihan provider ternama yang dipercaya akan memberikan kepuasan dan kemenangan hari ini untuk meraih x500. RTP SLOT Kocok303 merupakan SLOT Kocok303 hari ini yang telah menjadi pilihan utama bagi pemain judi online di seluruh Indonesia. Setiap harinya jutaan pemain memasuki dunia maya untuk memperoleh hiburan seru dan kemenangan besar dalam bermain slot dengan adanya bocoran RTP SLOT Kocok303. Tidak ada yang lebih menyenangkan daripada mengungguli mesin slot dan meraih jackpot x500 yang menggiurkan di situs SLOT Kocok303 hari ini yang telah disediakan SLOT Kocok303. Menangkan jackpot besar x500 rajanya maxwin dari segala slot dan raih kemenangan spektakuler di situs Kocok303 terbaik 2024 adalah tempat yang menyediakan mesin slot dengan peluang kemenangan lebih tinggi daripada situs slot lainnya. Bagi anda yang mencari pengalaman judi slot paling seru dan mendebarkan, situs bo SLOT Kocok303 terbaik 2024 adalah pilihan yang tepat. Jelajahi dunia slot online melalui situs SLOT Kocok303 di link SLOT Kocok303.
  • Topics

×
×
  • Create New...

Important Information

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