MinecraftScripterAI
Discord ↗
Link Copied to Clipboard
×
"Show me how to make an item that drops other random items on right click using a loot table."
/* To create an item that drops random items from a loot table when right clicked, you first need to create a new item class that extends Item. Override the onItemRightClick method to generate loot from a specified loot table and spawn it in the world. */ public class RandomLootItem extends Item { public RandomLootItem(Properties properties) { super(properties); } @Override public ActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) { if (!world.isClientSide) { LootTable lootTable = world.getServer().getLootTables().get(new ResourceLocation("your_mod_id", "your_loot_table")); LootContext.Builder builder = (new LootContext.Builder((ServerWorld) world)) .withParameter(LootParameters.ORIGIN, player.position()) .withParameter(LootParameters.THIS_ENTITY, player) .withRandom(world.random); List<ItemStack> loot = lootTable.getRandomItems(builder.create(LootParameterSets.GIFT)); for (ItemStack stack : loot) { ItemEntity itemEntity = new ItemEntity(world, player.getX(), player.getY(), player.getZ(), stack); world.addFreshEntity(itemEntity); } } return ActionResult.success(player.getItemInHand(hand)); } } /* Don't forget to register your item and loot table in your mod setup, and replace "your_mod_id" and "your_loot_table" with your actual mod ID and loot table name. */