How to give a Player money using Economy API

Hello, So first off thank you so far everyone for helping out. I’m sure if I knew where to look and how to read JavaDocs to a fault I wouldn’t nessecarily have too many questions. However I don’t, so My question today is using the below logic how would i add an ammount to the exising player’s balance?

this.economyService.getOrCreateAccount(player2).get().deposit();

???so i’m having difficutly with the deposit portion…
I have

("$", 50, ???) 

however at cause(???) i’m drawing a blank…what would be a good cause?

PS, this is being done on an entityDeath event…however is says “The method deposit(Currency, BigDecimal, Cause) in the type Account is not applicable for the arguments (String, int, DestructEntityEvent.Death)”
Thanks

A real easy cause is to get your PluginContainer and then just call Cause.source(container).build(). But check your other two arguments; they need to be a Currency and a BigDecimal, not a String and an int (just like the error says).

1 Like

For the currency you should be able to get the default currency using the API

1 Like
		@Listener
public void MobDeath(DestructEntityEvent.Death event, @First EntityDamageSource src, @Before(PluginContainer.class) Cause cause){
	Entity killer = src.getSource();
	if ((killer instanceof Player)){
		Player player = (Player)killer;
		String player2 = player.getUniqueId().toString();
		
		if(event.getTargetEntity().getType().equals(EntityTypes.BAT)){
			player.sendMessage(Text.of("You Killed a chicken & earned some Money"));
			Cause.source(cause).build();
			BigDecimal bd = new BigDecimal("50");
			this.economyService.getOrCreateAccount(player2).get().deposit(economyService.getDefaultCurrency(), bd, cause);
		}
	}
}
}

…so what i’m trying to do is get the dependancy set up for EconomyLite…so how would i set this up from here…mind you there is no other code yet written besides fore this. So there is no PluginContainer besides for the id name and version. Because i’m not understanding it. A little help would b appreiciated. I know some of the rest of the code mainly at the deposit isn’t finished. But that’s cause i’m trying to figure out how to set the cause up.

… I’m not sure what you’re attempting to do here. Regardless, the PluginContainer isn’t something you get from a Cause, it’s something you inject. Or you could get it from the PluginManager.

K…then I’m trying to use the getPlugin…how to I make sure that that is the economy plugin’s name?

Do I need to declare it somehow?

What i’m trying to do is hook into EconomyLite and run these methods through it…

	private static final String EconomyLite = PluginManager.SPONGE_PLUGIN_ID;

Would this be good enough for the cause?

@Listener
public void MobDeath(DestructEntityEvent.Death event, @First EntityDamageSource src){
	Entity killer = src.getSource();
	if ((killer instanceof Player)){
		Player player = (Player)killer;
		String player2 = player.getUniqueId().toString();
		
		if(event.getTargetEntity().getType().equals(EntityTypes.BAT)){
			Sponge.getGame().getPluginManager().getPlugin(EconomyLite);
			player.sendMessage(Text.of("You Killed a chicken & earned some Money"));
			Cause.source(EconomyLite).build();
			BigDecimal bd = new BigDecimal("50");
			this.economyService.getOrCreateAccount(player2).get().deposit(economyService.getDefaultCurrency(), bd, Cause.source(EconomyLite).build());
		}
	}
}
}

You don’t hook into an Economy plugin directly, you only use the economy service and the plugin that implements the economy api takes care of the rest.

For the Cause you need to provide your plugin’s PluginContainer which could be done as below in your main plugin class:

@Inject private PluginContainer pluginContainer;

public PluginContainer getPluginContainer() {
   return this.pluginContainer;
}

Also you’re not exactly building your cause correctly.
The first thing you need is your PluginContainer but you should always add more causes if available, this can be just about any object such as an entity, a block, an item, etc.
With your example, here’s how I would construct the Cause.

Cause cause = Cause.of(NamedCause.source(Plugin.getInstance().getPluginContainer()), NamedCause.owner(event.getTargetEntity()));

EDIT: I highly recommend giving this a read to learn more about causes and how they work.

1 Like

May I ask how you came up with the Plugin.getInstance()?

That’s just an instance of your plugin class, done as so:

@Plugin()
public class Plugin {
    private static Plugin instance;

   public void onPreInit(GamePreInitializationEvent event) {
      instance = this;
   }

    public static Plugin getInstance() {
        return instance;
    }
}

EDIT: stupid me forgot to actually add in what initialized the instance. /facepalm

1 Like

here’s what What I have The error log is inside…it’s telling me that (or what I understand) that the Cause is null… so My question is where did I not set this correctly? please help thank you.

@Inject public static PluginContainer pluginContainer;

Here’s the problem, you can’t @Inject using static. That’s the whole reason why i gave the example using Plugin.getInstance() in the first place.

Ok but if I take out the static then in the Eco class i gent an error over the HOB.getPluginContainer() Portion of the text. The error becomes:
So how would i then go about calling the non static inject into the static class of EcoRewards?

You need to actually call getInstance first, then you’ll have your main plugin class and be able to call getPluginContainer.

1 Like
Cause cause = Cause.of(NamedCause.source(((HOB) HOB.getInstance()).getPluginContainer()))

Like so?

Right.

1 Like

Ok however I’m still getting an error when i do that and actually do kill the bat…mind you it’s giving the message like it should. however then it still states this Error log

This means that at EcoRewards.java line 36, something is null.

which is the Cause cause = Cause.of(NamedCause.source(((HOB) HOB.getInstance()).getPluginContainer()));

Well, the only two things that you have defined on that line are HOB.getInstance and HOB#getPluginContainer. And your class is the first stack frame, not second or third, meaning it can’t be HOB#getPluginContainer because otherwise Cause would be throwing the NPE. Therefore, HOB.getInstance() is returning null.

Ok…but I did what i was told and there are now these in the main class

private static Plugin instance;

public static Plugin getInstance(){
return instance;
}