Jump to content

[1.7.10] Controlling Enchantments of Enchanted Books when added to Loot Chests


Recommended Posts

Posted

I have some items that function as loot bags, where the bag, when used, gives random items to the player.  The bags come in tiers, with different items for each tier of bag.  I'm using ChestGenHooks for the bag content.

 

I'd like to add enchanted books whose level increases with the tier of loot bag.  I've got a bag generating enchanted books, but the enchantments on the book are random.

 

Here's the code I'm using.  The top two lines are taken directly from the ChestGenHooks file and are used for adding randomly enchanted books to various generated chests.  I'd like to control the level of the enchantments, but I'm not sure how to do this.  Does anyone know how to do this, if it's possible?

 

ItemStack book = new ItemStack(Items.enchanted_book, 1, 0);
WeightedRandomChestContent tmp = new WeightedRandomChestContent(book, 1, 1, 1);
loot_bag_rare.addItem(tmp);

Posted

If I read your post correctly, you have an ITEM that gets USED, and when used it gives an enchanted book? If so, then there is absolutely no need for WeightedRandomChestContent - that's for randomly generating loot inside of inventories, but you just need a single enchanted book generating onItemRightClick.

 

Instead, you can use what ItemEnchantedBook uses when it is determining random enchantments for said loot:

ItemStack itemstack = new ItemStack(Items.book, 1, 0); // will become an Enchanted Book after next method
EnchantmentHelper.addRandomEnchantment(rand, itemstack, 30); // last parameter is how powerful of an enchantment you want, roughly

For more or less powerful enchantments, on average, increase or decrease the last parameter.

Posted

Sorry, I didn't include the rest of the items with that bag that are available.  The enchanted book is only one possibility, and I have other items that can be generated by the bag.

 

Also, I've now got a new problem: where to get the Random for that method from.  I could make a new one, but I'm hesitant to do so.

 

This is the code I have.  I haven't added the modified book to the chest hook yet  I'd like to include a pre-made Random variable where I'm making the new one, but not sure where to get it from.

 

@EventHandler
public void preInit(FMLPreInitializationEvent event)
{
	ItemInit.init();
	ItemInit.register();
	ChestHooks.init();
}

public class ChestHooks {
static ChestGenHooks loot_bag_common = ChestGenHooks.getInfo(VenturyThings.LOOT_BAG_COMMON);
static ChestGenHooks loot_bag_uncommon = ChestGenHooks.getInfo(VenturyThings.LOOT_BAG_UNCOMMON);
static ChestGenHooks loot_bag_rare = ChestGenHooks.getInfo(VenturyThings.LOOT_BAG_RARE);
static ChestGenHooks loot_bag_epic = ChestGenHooks.getInfo(VenturyThings.LOOT_BAG_EPIC);
static ChestGenHooks loot_bag_mythical = ChestGenHooks.getInfo(VenturyThings.LOOT_BAG_MYTHICAL);
static ChestGenHooks loot_bag_trash = ChestGenHooks.getInfo(VenturyThings.LOOT_BAG_TRASH);
static ChestGenHooks loot_trader = ChestGenHooks.getInfo(VenturyThings.LOOT_TRADER);

public static void init()
{
                // Loot Bag - Rare
	ItemStack lowbook = new ItemStack(Items.book, 1, 0);
	EnchantmentHelper.addRandomEnchantment(new Random(), lowbook, 10);

                loot_bag_rare.addItem(new WeightedRandomChestContent(new ItemStack(Item.getItemFromBlock(Blocks.anvil)), 1, 3, 20));
                loot_bag_rare.addItem(new WeightedRandomChestContent(new ItemStack(Item.getItemFromBlock(Blocks.coal_block)), 1, 3, 20));
                loot_bag_rare.addItem(new WeightedRandomChestContent(new ItemStack(Item.getItemFromBlock(Blocks.redstone_block)), 1, 3, 20));
                loot_bag_rare.addItem(new WeightedRandomChestContent(new ItemStack(Items.golden_boots), 1, 1, 20));
	loot_bag_rare.addItem(new WeightedRandomChestContent(new ItemStack(Items.golden_chestplate), 1, 1, 20));
	loot_bag_rare.addItem(new WeightedRandomChestContent(new ItemStack(Items.golden_leggings), 1, 1, 20));
	loot_bag_rare.addItem(new WeightedRandomChestContent(new ItemStack(Items.golden_helmet), 1, 1, 20));
	loot_bag_rare.addItem(new WeightedRandomChestContent(new ItemStack(Items.iron_boots), 1, 1, 20));
	loot_bag_rare.addItem(new WeightedRandomChestContent(new ItemStack(Items.iron_chestplate), 1, 1, 20));
	loot_bag_rare.addItem(new WeightedRandomChestContent(new ItemStack(Items.iron_leggings), 1, 1, 20));
	loot_bag_rare.addItem(new WeightedRandomChestContent(new ItemStack(Items.iron_helmet), 1, 1, 20));
	loot_bag_rare.addItem(new WeightedRandomChestContent(new ItemStack(Items.ender_pearl), 1, 4, 20));
	loot_bag_rare.addItem(new WeightedRandomChestContent(new ItemStack(Items.glowstone_dust), 3, 7, 20));
	loot_bag_rare.addItem(new WeightedRandomChestContent(new ItemStack(Items.iron_axe), 1, 1, 20));
	loot_bag_rare.addItem(new WeightedRandomChestContent(new ItemStack(Items.iron_shovel), 1, 1, 20));
	loot_bag_rare.addItem(new WeightedRandomChestContent(new ItemStack(Items.iron_sword), 1, 1, 20));
	loot_bag_rare.addItem(new WeightedRandomChestContent(new ItemStack(Items.iron_pickaxe), 1, 1, 20));
	loot_bag_rare.addItem(new WeightedRandomChestContent(new ItemStack(Items.iron_hoe), 1, 1, 20));
	loot_bag_rare.addItem(new WeightedRandomChestContent(new ItemStack(Items.golden_axe), 1, 1, 20));
	loot_bag_rare.addItem(new WeightedRandomChestContent(new ItemStack(Items.golden_shovel), 1, 1, 20));
	loot_bag_rare.addItem(new WeightedRandomChestContent(new ItemStack(Items.golden_sword), 1, 1, 20));
	loot_bag_rare.addItem(new WeightedRandomChestContent(new ItemStack(Items.golden_pickaxe), 1, 1, 20));
	loot_bag_rare.addItem(new WeightedRandomChestContent(new ItemStack(Items.golden_hoe), 1, 1, 20));
                loot_bag_rare.setMin(1);
	loot_bag_rare.setMax(4);
        }
[]

Posted

Ah, I see. You don't need to specify an actual enchantment for books when adding to ChestGenHooks - instead, add a regular Items.enchanted_book. When you generate your loot bag's loot, call ChestGenHooks#getItems, which in turn calls Item#getChestGenBase on each item.

 

Enchanted books override this method to add random enchantments, otherwise vanilla loot charts would have to add every single enchantment combination as a possibility in their loot lists. This way they can just add the enchanted book, and if one is generated, determine what it is at that time.

 

You won't need a new Random any more at that point in your code, but if you did, there's nothing wrong with making a new Random instance into a class field and using that. Usually you can get an instance of Random from the World object. Items and I think Blocks also have a Random class field, as well as some other classes and many methods.

Posted

That's what I was doing, but the problem is the level of the enchantments are still completely random (still the case with the earlier suggestion too).  I'm trying to make it so that three different bags have three different general levels of enchanted books, but that's not happening yet.

Posted

That's what I was doing, but the problem is the level of the enchantments are still completely random (still the case with the earlier suggestion too).  I'm trying to make it so that three different bags have three different general levels of enchanted books, but that's not happening yet.

Oh right, the 30 is hard-coded in there, and you can't generate an enchanted stack to add to the loot list initially because then only that ONE enchanted book will ever be in the loot list...

 

Well, the only viable option I can think of at the moment is to make a dummy item that extends ItemEnchantedBook and overrides the getChestGenBase method to use different values for the enchantment level based on some custom field, so you would need 3 instances of this item for the different loot levels. Obviously you would pass the regular Items.book in as the ItemStack, just like I showed in my first reply.

 

It's not very elegant, but it should work.

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.