Jump to content

[1.7.2] Adding enchanted books to chests?


SureenInk

Recommended Posts

Okay, so, I have my coding for my treasure chests, and I can get anything I want into it, but I can't figure out enchanted books. I can add books with enchants on them, but I can't get Enchanted Books (that you can put into an anvil and put the enchantment on your item) into them.

 

TileEntityChest chest20 = (TileEntityChest) world.getTileEntity(i + 46, j + 19, k + 13);
        if (chest20 == null) {
            System.err.printf("TileEntityChest20 is null!?!\n");
        }
        else {
            for (int slot = 0; slot < chest20.getSizeInventory(); slot++) {
                int slotran = random1.nextInt(30);
                int itemran = random2.nextInt(3);
                if (slotran == 20){
                    ItemStack stack = new ItemStack(Items.enchanted_book);
                    stack.addEnchantment(Enchantment.featherFalling, itemran+1); //this just adds a book with an enchant, not an enchanted book.
                    chest21.setInventorySlotContents(slot, stack);
                }
                if (slotran == 25){
                   ItemStack stack = new ItemStack(Items.enchanted_book, 1, 0);
                    WeightedRandomChestContent tmp = new WeightedRandomChestContent(stack, 1, 1, 1); //This adds an "Enchanted Book" that has no enchants.
                    chest21.setInventorySlotContents(slot, stack);
                }
            }
        }

Link to comment
Share on other sites

Are you giving example code to help others, or are you having an error? What is the actual problem if there is one?

Link to comment
Share on other sites

You should use

Items.enchanted_book.addEnchantment(stack, enchantment)

instead of

stack.addEnchantment(enchantment)

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

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



×
×
  • Create New...

Important Information

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