Jump to content

[1.7.2] Registering Events


Glorfindel22

Recommended Posts

I am working on updating my Minecraft mod, the Recipe Expansion Pack, to 1.7 and I can't figure out how to register events.

 

I have a ModEvents class with the following code

package net.richardsprojects.recipexpack.main;

//Cut out the imports here for this post to save space so none of the issues come from the imports

public class ModEvents
{

@SubscribeEvent
public void shovelHandler(PlayerInteractEvent event) {

	System.out.println("Testttttttttttttttttttttttttt!!!!!!!!!!!!!!!!!!!!!!!!!!");

	ItemStack itemstack = event.entityPlayer.inventory.getCurrentItem();

	//Check to see if the player right clicked
	if(event.action.equals(event.action.RIGHT_CLICK_BLOCK)) {

		//Check if player has nothing in their hand
		//if(event.entityPlayer.getCurrentArmor(0) != null) {

			if(itemstack.getDisplayName() == "Wooden Shovel" || itemstack.getDisplayName() == "Stone Shovel" || itemstack.getDisplayName() == "Iron Shovel" || itemstack.getDisplayName() == "Gold Shovel" || itemstack.getDisplayName() == "Diamond Shovel") {

				//Change Grass to dirt
				if(event.entityPlayer.worldObj.getBlock(event.x, event.y, event.z) == Block.getBlockFromName("Grass")) {
					event.entityPlayer.worldObj.setBlock(event.x, event.y, event.z, Blocks.dirt, 3, 0);

					//Drop "Grass Seeds" in front of player
					event.entityPlayer.dropItem(ItemGrassSeeds.grassSeeds, 1);

					//Reduce the durability
					itemstack.damageItem(1, event.entityPlayer);
				}
			//}
		}
	}
}
}

 

I used the SubscribeEvent annotation because I read that that was the new one. Below my PreInit event in my main class:

    @EventHandler 
    public void preInit(FMLPreInitializationEvent event) {

    	ItemGrassSeeds.mainRegistry();
    	FMLCommonHandler.instance().bus().register(new ModEvents());
    }

 

I do not get any errors just nothing happens when I right click with my shovel and no "Test" line appears in my console. This is making me believe that I am not registering the event right. Any help would be greatly appreciated! :)

Creator of the Recipe Expansion Pack mod.

http://www.minecraftforum.net/topic/1983421-172-forge-recipe-expansion-pack-version-012/

Updated to 1.7.2!

Link to comment
Share on other sites

PlayerInteractEvent is posted on the MinecraftForge bus not the FML bus.

 

FML Bus = FMLCommonHandler.instance().bus()

MinecraftForge Bus = MinecraftForge.EVENT_BUS

 

In other words, use the MinecraftForge EventBus.

 

Okay, that seemed to solve it, the event is now registered and seems prints out the "test" text to the log when I interact. However, the event code still doesn't work. In my event I am trying to detect if the player has a shovel (of any type in their hand). Under the old system I simply check using the item id but since that doesn't seem to work in 1.7 I am checking using the item name (downside won't work if the item is renamed on an anvil). The problem is it is not working at all it doesn't detect I have a shovel in my hand and print out test 2. Is there a better way to check the type of item a player has in their hand? Below is my code:

	@SubscribeEvent
public void shovelHandler(PlayerInteractEvent event) {

	System.out.println("Testttttttttttttttttttttttttt!!!!!!!!!!!!!!!!!!!!!!!!!!");

	ItemStack itemstack = event.entityPlayer.inventory.getCurrentItem();

	//Check to see if the player right clicked
	if(event.action.equals(event.action.RIGHT_CLICK_BLOCK)) {

		//Check if player has nothing in their hand
		//if(event.entityPlayer.getCurrentArmor(0) != null) {

			if(itemstack.getDisplayName() == "Wooden Shovel" || itemstack.getDisplayName() == "Stone Shovel" || itemstack.getDisplayName() == "Iron Shovel" || itemstack.getDisplayName() == "Gold Shovel" || itemstack.getDisplayName() == "Diamond Shovel") {

				System.out.println("Test2");

				//Change Grass to dirt
				if(event.entityPlayer.worldObj.getBlock(event.x, event.y, event.z) == Block.getBlockFromName("Grass")) {
					event.entityPlayer.worldObj.setBlock(event.x, event.y, event.z, Blocks.dirt, 3, 0);

					//Drop "Grass Seeds" in front of player
					event.entityPlayer.dropItem(ItemGrassSeeds.grassSeeds, 1);

					//Reduce the durability
					itemstack.damageItem(1, event.entityPlayer);
				}
			//}
		}
	}
}

Creator of the Recipe Expansion Pack mod.

http://www.minecraftforum.net/topic/1983421-172-forge-recipe-expansion-pack-version-012/

Updated to 1.7.2!

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

Announcements



×
×
  • Create New...

Important Information

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