Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

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!

  • 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,

Wrong event bus. ItemCraftedEvent is an FML event, you need to use FMLCommonHandler.

 

EDIT:

Sigh. Diesieben replied already. lol

  • 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,

  • 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.

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.