user13009431
user13009431

Reputation:

Accessing config.yml from another file (Bukkit)

I have created an Economy Plugin for my server, and it works perfectly. Players can type in /bal and see their balance. My server also has a GUI Plugin, and one of these options is to see their currency. I keep player's currencies stored in a config.yml file in my Economy Plugin. How could I access variables in my Economy config.yml, if I wanted to access them from another plugin? I have scoured the whole internet and have found nothing. If anyone has any idea on how to do this, please tell me. Thanks.

Upvotes: 0

Views: 871

Answers (2)

Kiip
Kiip

Reputation: 37

You can access a configuration file from another plugin.

File configFile = new File(<JavaPlugin Object>.getServer().getWorldContainer().getAbsolutePath() + "/plugins/<Your Plugin Name>/config.yml"); // First we will load the file.
FileConfiguration config = YamlConfiguration.loadConfiguration(configFile); // Now we will load the file into a FileConfiguration.

After this we can get values from the config.

boolean randomValue = config.getBoolean("randomValue"); // Gets a boolean value.

Congratulations! You now can read values from another plugin!

Upvotes: 0

Matrzak
Matrzak

Reputation: 104

There is several ways to do this but in my opinion best for you is to make Player class. It will be storing player data (like currency) and then during development of another plugin import your Economy resource as a dependency.

In Economy Plugin:

  1. Store player currency in Player object
  2. Store all Player objects in collection
  3. Make public function which will be returning player object from your collection, for example as a function parameter pass player nick

In all another plugins:

  1. Import your Economy plugin as a dependency
  2. Use your function (for example getPlayer('YourNick');)
  3. If player exists in collection this code will return object of your entity
  4. Get player currency via getter

Remember to synchronize object data every time you making some changes (for example when player is buying something)

Upvotes: 1

Related Questions