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

I'm getting a null pointer exception in this function:

 

public void processCommand(ICommandSender sender, String[] args) {

        if(args.length == 0){

            PlayerWallet wallet = PlayerWallet.get((EntityPlayer) sender);

            ((EntityPlayer) sender).addChatComponentMessage(new ChatComponentText("£" + wallet.getWallet()));

 

        }

   

i know the error occurs at "wallet.getWallet()". i just can't work out what is causing the error :/

 

suggestions?

 

the Player wallet class:

 

public class PlayerWallet implements IExtendedEntityProperties {

 

    public final static String EXT_PROP_NAME = "PlayerWallet";

    private double funds;

 

 

    public void init(Entity entity, World world){

 

        funds = 0;

 

    }

 

 

 

    public void loadNBTData(NBTTagCompound nbt){

 

        NBTTagCompound properties = (NBTTagCompound) nbt.getTag(EXT_PROP_NAME);

        properties.getDouble("funds");

    }

 

 

    public void saveNBTData(NBTTagCompound nbt){

        NBTTagCompound properties = new NBTTagCompound();

        properties.setDouble("funds", funds);

        nbt.setTag(EXT_PROP_NAME, properties);

    }

 

 

    public static final PlayerWallet get(EntityPlayer player)

    {

        return (PlayerWallet) player.getExtendedProperties(EXT_PROP_NAME);

    }

 

 

    public static final void register(EntityPlayer player)

    {

        player.registerExtendedProperties(PlayerWallet.EXT_PROP_NAME, new PlayerWallet());

    }

 

    public double getWallet(){

 

        return funds;

    }

    public double addFunds(double f){

 

        funds += f;

        return getWallet();

    }

    public double setFunds(double f){

        funds = f;

        return funds;

    }

    public double removeFunds(double f){

        funds -=f;

        return funds;

    }

 

}

 

The Event handler class:

 

public class MconomyEventHandler {

 

    @EventHandler

    public void onEntityConstructing(EntityEvent.EntityConstructing event)

    {

/*

Be sure to check if the entity being constructed is the correct type for the extended properties you're about to add! The null check may not be necessary - I only use it to make sure properties are only registered once per entity

*/

        if (event.entity instanceof EntityPlayer && PlayerWallet.get((EntityPlayer) event.entity) == null)

// This is how extended properties are registered using our convenient method from earlier

            PlayerWallet.register((EntityPlayer) event.entity);

// That will call the constructor as well as cause the init() method

// to be called automatically

    }

 

 

}

 

the event is registered here:

 

@EventHandler

    public void init(FMLInitializationEvent event)

    {

// some example code

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

    }

 

 

i would really appreciate if anyone can solve this problem for me :)

I added those 2 bits of code to the end of the first post

@EventHandler is only for the FML events involved in the mod loading process - all other events use @SubscribeEvent.

 

Next time something doesn't seem to be working, put a println statement inside and watch your console - if it doesn't print, you then have more knowledge of what the problem might be.

 

E.g.

@EventHandler
public void onEntityConstructing(EntityConstructing event) {
System.out.println("Entity is constructing."); // Doesn't print, so you know the problem has to do with your event
}

 

Also, for code, either use [ code ]code here[ / code ] tags (without spaces) or a service like pastebin - it makes it far more readable.

  • Author

oh so that's how i format code haha... i tried using the button at the top of the textbox (picture of a hash tag) but it didnt do anything lol.

 

thanks :)

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.