Jump to content

using extended player properties in command


jamiemac262

Recommended Posts

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 :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.