ericgrandt / TotalEconomy

All in one economy plugin for Minecraft.
https://ericgrandt.github.io/TotalEconomy/
31 stars 33 forks source link

[QUESTION] Interaction with Total Economy through Javascript #356

Closed SirFancyBacon closed 4 years ago

SirFancyBacon commented 4 years ago

Sponge Version: 1.12.2-2838-7.1.7-RC3928 Forge Version: 14.23.5.2828 Total Economy Version: 1.8.1

Hello, Sorry if you have a discord where it would be better to ask this question; i didn't see one.

I was wondering how one would interact with this plugin from Javascript? I am using CustomNPCs.

What i am looking for is: How to import TotalEconomy. How to check a balance How to deduct a balance How to add to a balance

MarkL4YG commented 4 years ago

That integration is totally up to the JS-engine used by CustomNPCs.
TotalEconomy implements the built-in economy API of Sponge and anything that wants to interact with TE should use the economy API of Sponge and not TE itself.
As CustomNPCs appears to be a forge mod, that means that there would have to be some sort of bridge between the mod and Sponge as the Sponge API is exclusive to Sponge and thus unavailable to plain forge mods.
You could ask the author of the mod (or - if different - the JS engine) to provide integration with the Sponge API to access it in your custom script.

SirFancyBacon commented 4 years ago

I see, well I know I can import other Java packages.

Do you happen to know what I would need to import? And also you know what sponge code I'd need to use?

MarkL4YG commented 4 years ago

Well... I am not really sure whether or not that would work in the JS sandbox but you may try using the Sponge API starting from the global singleton Sponge class of org.spongepowered.api.
Way could be to:

  1. Get the ServiceManager
  2. Request the EconomyService instance (package is org.spongepowered.api.service.economy)
  3. From there you could try using the getOrCreateAccount methods to obtain the account and subsequently, its balance.
SirFancyBacon commented 4 years ago

Looking at the java docs; it appears the actual functions of paying out the economy is provided entirely by the plugin https://jd.spongepowered.org/7.1.0/

MarkL4YG commented 4 years ago

That is correct. Sponge offers the contracts and interfaces while the implementation is left to the economy plugin of the administrators choice.
TotalEconomy provides such an economy service and registers it with Sponge.
You should use the interfaces provided by Sponge so that your code does not directly depend on TE.

SirFancyBacon commented 4 years ago

Do you happen to know what I'd be looking for? I'd like to add a number to an account; possibly deduct from an account as well.

https://jd.spongepowered.org/7.1.0/org/spongepowered/api/service/economy/EconomyService.html Based on this i don't see anything that allows me to add or subtract to an account directly.

MarkL4YG commented 4 years ago

Use #getOrCreateAccount. You'll be able to deposit, set or withdraw money from the returned account.

SirFancyBacon commented 4 years ago

Would that be org.spongepowered.api.service.economy.getOrCreateAccount?

MarkL4YG commented 4 years ago

getOrCreateAccount is a method that can be called on the EconomyService.
The Java package is org.spongepowered.api.service.economy, the class is EconomyService and getOrCreateAccount is the method.
However, you will need to get the instance of the EconomyService from Sponge. See my comment from before on how to get the service instance.

SirFancyBacon commented 4 years ago

I missed that somehow and I'm sorry about that :sweat_smile: So i got it working up until the point of needing either a UUID or an identifier.. the UUID method (using the UUID string) does not seem to be working as using getBalance returns to me an undefined

EDIT: Okay, so since TE uses VirtualAccounts i think that my issues are i am not using the correct identifier

SirFancyBacon commented 4 years ago

Figured it out; thanks for the direction!

MarkL4YG commented 4 years ago

You're welcome!