Objective
For this lab, you will be implementing various Minecraft item objects. Recall that items
can be wielded, but are not blocks in the Minecraft world. You can throw ('q
')
your items in the world, but you will mostly focus on what you can do with items in your
inventory.
Notes
To see if the current item stack is a sand item:
String itemName = itemStack.getItem().getUnlocalizedName(); if (itemName.equals(Blocks.sand.getUnlocalizedName())) { ... }
Getting the players inventory:
ItemStack[] inventory = player.inventory.mainInventory;
Adding a new item to the item registry:
parentMod.itemRegistry.newInstance(textureName, new ItemClass(), inGameName);
Tasks
Part 1 — Creating a Mod Module
- Inside the
tealsmc.mods.items
package, open theRockSifter
class. Create a default constructor for theRockSifter
class if one was notcreated for you. Inside the default constructor, set the max stack size to 1 (using thesetMaxStackSize()
function), and set the creative tab toCreativeTabs.tabTools
(using thesetCreativeTab()
function). - Inside your Module class (
ItemsModule
)onLoad()
method, register your item to the mod loader. You may do so by callingparentMod.itemRegistry.newInstance()
, which takes parametersString texture_name, Item item
, andString in_game_name
. For the texture name, use"rock_sifter"
and for the display name call it"Rock Sifter"
. (See the notes section above for an example.) - Go in game, open your creative inventory, browse to the tools tab, and verify that a green-looking item shows up near the bottom of the inventory.
Part 2 — Creating a Rock Sifter
- Go back to the RockSifter class. Override the
onItemRightClick()
method like so:public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player) { ... }
- See the notes section above for help with the following items.
- Iterate through each item in the player's inventory, and check to see if the item is
sand. Note: some items in the inventory may be null! If the item is sand, remove it by
setting that inventory entry to null. Finally, at the bottom of the
onItemRightClick()
method, return the stack that was passed in. Verify that the rock sifter works as expected (obliterates all sand blocks in your inventory). - Now change the
onItemRightClick()
method to convert all sand items to gold nuggets (Items.gold_nugget
). Again, verify that the new code works as expected. - Finally, change the behavior of the rock sifter so that for each sand item in an item stack, there is a 10% chance that it will contain a gold nugget. You will need to "roll the dice" (hint: Math.random()) the same number of times as the size of each sand item's stack. For each roll, if the number is less than 10% (0.10), then you've found gold! — increase the count of gold nuggets for that stack. Replace the current inventory entry with a stack of gold nuggets, as many as were found in the prior loop. If no gold nuggets were found, then you'll have to empty (null) the inventory slot. Verify that your code works as expected: each sand block is converted to a gold nugget 10% of the time.