Can't getPlayerStats() from another class?

Discussion in 'Plugin Development' started by nehuskerfan2, Jul 26, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    nehuskerfan2

    I've been having this problem and I can't really fix it.
    RPGPerks.java
    Code:java
    1.  
    2. package me.nehuskerfan2.RPGPerks;
    3.  
    4. import java.io.File;
    5. import java.io.IOException;
    6. import java.io.InputStream;
    7. import java.util.logging.Level;
    8.  
    9. import me.nehuskerfan2.RPGPerks.commands.Help;
    10. import me.nehuskerfan2.RPGPerks.commands.Powerup;
    11.  
    12. import org.bukkit.configuration.file.FileConfiguration;
    13. import org.bukkit.configuration.file.YamlConfiguration;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15.  
    16. public class RPGPerks extends JavaPlugin {
    17.  
    18. protected RPGPerksLogger log;
    19. public FileConfiguration playerStats = null;
    20. public File playerStatsFile = null;
    21.  
    22. public void reloadPlayerStats() {
    23. if (playerStatsFile == null) {
    24. playerStatsFile = new File(getDataFolder(), "playerStats.yml");
    25. }
    26. playerStats = YamlConfiguration.loadConfiguration(playerStatsFile);
    27.  
    28. // Look for defaults in the jar
    29. InputStream defConfigStream = this.getResource("playerStats.yml");
    30. if (defConfigStream != null) {
    31. YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
    32. playerStats.setDefaults(defConfig);
    33. }
    34. }
    35. public FileConfiguration getPlayerStats() {
    36. if (playerStats == null) {
    37. this.reloadPlayerStats();
    38. }
    39. return playerStats;
    40. }
    41. public void savePlayerStats() {
    42. if (playerStats == null || playerStatsFile == null) {
    43. return;
    44. }
    45. try {
    46. getPlayerStats().save(playerStatsFile);
    47. } catch (IOException ex) {
    48. this.getLogger().log(Level.SEVERE, "Could not save config to " + playerStatsFile, ex);
    49. }
    50. }
    51.  
    52.  
    53. public RPGPerks(RPGPerks rpgPerks) {
    54. // TODO Auto-generated constructor stub
    55. }
    56.  
    57. public void onEnable() {
    58. this.log = new RPGPerksLogger(this);
    59. this.getCommand("rpgperks").setExecutor(new Help(this));
    60. this.getCommand("powerup").setExecutor(new Powerup(this));
    61. }
    62.  
    63. public void onDisable() {
    64.  
    65. }
    66.  
    67. }
    68.  


    commands/powerup.java
    Code:java
    1.  
    2. package me.nehuskerfan2.RPGPerks.commands;
    3.  
    4. import java.io.File;
    5. import java.io.IOException;
    6. import java.io.InputStream;
    7. import java.util.logging.Level;
    8.  
    9. import me.nehuskerfan2.RPGPerks.RPGPerks;
    10. import me.nehuskerfan2.RPGPerks.RPGPerksLogger;
    11.  
    12. import org.bukkit.ChatColor;
    13. import org.bukkit.command.Command;
    14. import org.bukkit.command.CommandExecutor;
    15. import org.bukkit.command.CommandSender;
    16. import org.bukkit.configuration.file.FileConfiguration;
    17. import org.bukkit.configuration.file.YamlConfiguration;
    18.  
    19. public class Powerup implements CommandExecutor {
    20.  
    21.  
    22.  
    23.  
    24. public Powerup(RPGPerks rpgPerks) {
    25. // TODO Auto-generated constructor stub
    26. }
    27. //a-explode
    28. //a-poison
    29. //a-fire
    30. //m-smite
    31. //m-tank
    32. //m-pstrike
    33. //w-firebolt
    34. //w-barrage
    35. //w-freeze
    36. @Override
    37. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    38. String rpgclass = getPlayerStats().getString(sender + ".class");
    39. if(label.equalsIgnoreCase("powerup")){
    40. if(args.length == 0){
    41. sender.sendMessage(ChatColor.BLUE + "Here is a list of your skills");
    42. }
    43. }
    44. return true;
    45. }
    46.  
    47. }
    48.  


    Can you just tell me how this would work? getPlayerStats() and this.getPlayerStats() wouldn't work.
     
  2. Offline

    r0306

    nehuskerfan2
    The method is not declared as static, so it is not a class method. You need to create a new instance of your class to reference it. You can either extend the main class in your commands class or create a new object like this:
    Code:
    RPGPerks perks = new RPGPerks();
    String rpgclass = perks.getPlayerStats().getString(sender + ".class");
    
     
  3. Offline

    nehuskerfan2

    I always overlook the easiest of things.
     
Thread Status:
Not open for further replies.

Share This Page