Jump to content

Recommended Posts

Posted

Hello Modders,

 

I'm trying to trigger an achievement when a particular item is crafted.

I have created a new class called "MyEventListener.java" and this is the code I have in there:

 

 

package mymod.handlers;

import mymod.Main;

import cpw.mods.fml.common.gameevent.PlayerEvent.ItemCraftedEvent;

 

public class MyEventListener

{

 

public void SomethingCrafted(ItemCraftedEvent event)

{

if (event.crafting.getItem() == Main.MySword_1)

{

event.player.addStat(Main.MyAchievement_1, 1);

}

}

 

}

 

Then, in my main Mod class, I register this class in the "init" section with this line:

 

MinecraftForge.EVENT_BUS.register(new MyEventListener());

 

I thought that, perhaps, this was being caused because I didn't have the "@ForgeSubscribe" annotation I used to have before the "SomethingCrafted" event, but when I try to add it, it claims to be an error.

 

Any ideas?

 

Thanks!

Posted

Hey CoolBoy,

 

Thanks for the quick response, but I'm afraid that the achievement is still not being triggered.

Now my MyEventListener.java file looks like this:

 

package mymod.handlers;

 

import mymod.Main;

import cpw.mods.fml.common.eventhandler.SubscribeEvent;

import cpw.mods.fml.common.gameevent.PlayerEvent.ItemCraftedEvent;

 

 

public class MyEventListener

{

 

@SubscribeEvent

public void SomethingCrafted(ItemCraftedEvent event)

{

if (event.crafting.getItem() == Main.MySword_1)

{

event.player.addStat(Main.MyAchievement_1, 1);

}

}

 

}

 

And in my Main mod file, I have in the init section:

 

MinecraftForge.EVENT_BUS.register(new MyEventListener());

 

 

Any ideas why this wouldn't be working?

 

Thanks,

Posted

Hey Diesenben and Coolboy,

 

I'm afraid to say that, even with your advice, I was not able to figure out what to replace the below line of code with:

    MinecraftForge.EVENT_BUS.register(new MyEventListener());

 

I couldn't find any forum entries on registering an event bus with FML events...

You mentioned I needed FMLCommonHandler - so would it start with FMLCommonHandler.(something)?

 

Any other help would be really appreciated!

 

 

Thanks so much,

Posted

In case anyone else was wondering, this is the line of code you need:

FMLCommonHandler.instance().bus().register(new MyEventListener());

 

"MyEventListener.java" is the file I made that holds the ItemCraftedEvent method.

 

 

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.