Jump to content

[1.14.4] Loot Tables - item gives player random loot


Recommended Posts

Posted (edited)

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
Posted (edited)

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
Posted

 

I did wonder about that. I was literally converting.

 

But, how do I set the parameters and what are they? The class in MC just asks for a LootContext, which would be builder?  It doesn't seem to specify the parameters anywhere

Posted (edited)

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
Posted (edited)

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
Posted

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);
}
Posted

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?

 

 

Posted (edited)

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

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



×
×
  • Create New...

Important Information

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