Jump to content

1.12 getting active item


Recommended Posts

Im trying to improve my java skills  with modding some minecraft and currently im trying to do something like a chopping Tree mod.

Im using tutorials and the MinecraftForge Docu but I still have trouble.


I wanted to go like this.

  1.  an event triggered through players destroying a block
  2.  2 conditions: active item == axe and destroyed block == wood
  3. conditions == true -> destroy tree



Could somebody give me a little push or perhaps a tutorial about this problem.

most tutorials i find are about creating items ^^


this is my method i was trying to get active item.


    public static boolean isItemAxe(EntityPlayer theplayer){
    	if(theplayer.getActiveItemStack().isEmpty()== true){
    	 	return false;
		} else if(theplayer.getActiveItemStack().isItemEqual(Items.IRON_PICKAXE)== false){
			return false;
return true;

Exception: The method isItemEqual(ItemStack) in the type ItemStack is not applicable for the arguments (Item)




public static boolean isItemAxe(EntityPlayer theplayer){
        if(theplayer == null || theplayer.getActiveItemStack().getItem() == null ){
             return false;
        } else if(theplayer.getActiveItemStack().getItem() == Items.IRON_PICKAXE){
            return true;
            return false;

this should work.

later i try to do a condition with all pickaxes.

Now  im having trouble creating an event triggered through players destroying a block


thanks in advance :)


Edited by suyama
Link to comment
Share on other sites

Hey, always nice to see new people getting into modding. Also, it is really nice to see that you actually tried something before asking -- there are so many lazy people who want to get all the answers without learning for themselves.


So based on the code you posted, I have a few suggestions.


For the exception, the description of the problem is pretty clear. The method isItemEqual() needs an ItemStack instance passed to it, but you passed an Item. This brings up a few things I want to teach you:


  1) In Minecraft some classes are "singletons" meaning there is only one instance of them in the whole game. Each item (also block) is actually a singleton. They appear to be multiple in game because each item is handled in an ItemStack which associates the single Item instance with a quantity as well as data that is unique to that stack (damage/meta data as well as NBT data). For blocks there is actually a different system where a map where every block position (BlockPos) has a block state (IBlockState) that associates the single block instance with data that is unique to that block location (meta data). The concept of which things in Minecraft are singletons is important and impacts how you compare things (see my tutorial on which comparison methods to use here http://jabelarminecraft.blogspot.com/p/minecraft-modding-singletons-instances.html).


2) In modding we're working with other people's code and sometimes it is disorganized or misleading. In this case, the method name talks about the item so it might be tempting to think you should pass an item, but you must look closely at the functionality of every method (use the Open Declaration feature in Eclipse to find the code for the method) to make sure it does what you think it does.


3) You need to use Eclipse to help you avoid errors, and it can even help you by giving you possible fixes. In this case Eclipse should have warned you that the parameter didn't match.


With all that being said, you need to pass an item stack to the method. It is okay to create a temporary ItemStack for the purpose of the test so you can go isItemEqual(new ItemStack(Items.IRON_PICKAXE)) to fix the problem. Alternatively, you could get the item from the active item stack and compare it.


Now regarding your plan to "create an event', you're thinking about it wrong. There is already events available for most things modders might want to do, so what you want to do is "subscribe an event handling method".


I have a tutorial on event handling here: http://jabelarminecraft.blogspot.com/p/minecraft-forge-172-event-handling.html


In any case you should look at subscribing to events like BlockEvent.BreakEvent which are fired when a block is broken. There is also a harvest event, and I think there is tool use events as well. 





Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

BlockEvent.BreakEvent is called before the block is actually finished breaking. It's also called on the server only, I believe. I have found that if I want to do something as a result of blocks breaking, I usually need BlockEvent.HarvestDropsEvent since it fires on the server AND client, and fires after the block is broken, but before the block drops anything.


There are a few different threads about making tree-choppers, so you could benefit from doing a search here on the forum.

Edited by Daeruin
Link to comment
Share on other sites

thanks guys. this was a nice push for me. :)


and jabelar  your blog is awesome.

The Documention of forge is nice but im looking for something like this https://docs.oracle.com/javase/7/docs/api/

    public void BlockHarvested(BlockEvent.BreakEvent event) {
        if (event.getState().getBlock().isWood(event.getWorld(), event.getPos())) {

                System.out.println("It Works!");



this is my event. it works fine now.

Now I just need a method to delete everything around the harvested block.


Could someone correct me if i have the wrong idea.

I was thinking about a method which has 2 or 3 for loops (for x,y and perhaps z) with a max_radius of maybe 4 which

has 2 conditions .

if leaves || if wood -> break. block.


And around the for loops i need my condition isAxe.


If im correct I just need to find out how the Syntax works to do something like this :D

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.

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.

  • Create New...

Important Information

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