Posted February 11, 201411 yr 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!
February 11, 201411 yr Author 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,
February 11, 201411 yr Wrong event bus. ItemCraftedEvent is an FML event, you need to use FMLCommonHandler. EDIT: Sigh. Diesieben replied already. lol
February 11, 201411 yr Author 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,
February 11, 201411 yr Author 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.