Experience plugin

Discussion in 'Plugin Development' started by rob1998@, Jun 24, 2013.

Thread Status:
Not open for further replies.
  1. I need some help,
    I'm trying to save the current xp lvl and xp points to a config and I want to load them with the command /xpa

    this is my source:
    Show Spoiler

    Code:java
    1. package com.bugs3.cracmac.XPAccount;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.player.PlayerJoinEvent;
    12. import org.bukkit.plugin.PluginDescriptionFile;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14.  
    15. public class XPAccount extends JavaPlugin implements Listener {
    16.  
    17. public Logger log;
    18. public final Logger logger = Logger.getLogger("Minecraft");
    19. public void onEnable()
    20. {
    21.  
    22. log = getLogger();
    23. PluginDescriptionFile pdfFile = this.getDescription();
    24. this.logger.info("["+pdfFile.getName()+ "] "+pdfFile.getName()+" "+ pdfFile.getVersion() + " has been Enabled");
    25. getServer().getPluginManager().registerEvents(this, this);
    26. saveConfig();
    27. }
    28. public void onDisable(){
    29. PluginDescriptionFile pdfFile = this.getDescription();
    30. this.logger.info("["+pdfFile.getName()+ "] "+pdfFile.getName()+" "+ pdfFile.getVersion() + " has been Disabled");
    31. saveConfig();
    32. }
    33.  
    34.  
    35. @EventHandler
    36. public void onPlayerJoin(PlayerJoinEvent event){
    37. Player player = event.getPlayer();
    38. String p = player.getName();
    39. int pl = player.getLevel();
    40. float pxp = player.getExp();
    41. getConfig().set("XPA."+p+".LVL", pl);
    42. player.sendMessage(ChatColor.DARK_GREEN + "[XP Account] Saved your XP Levels: " + ChatColor.DARK_GREEN + pl + " Points: " +pxp);
    43. }
    44.  
    45.  
    46. public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args){
    47. Player player = (Player) sender;
    48. String p = player.getName();
    49. if(command.getName().equalsIgnoreCase("xpa"))
    50. {
    51. if(args[0].equalsIgnoreCase("")){
    52. int pl = getConfig().getInt("XPA."+p+".LVL");
    53. float pxp = player.getExp();
    54. player.sendMessage(ChatColor.DARK_GREEN + "[XP Account] Levels: " + ChatColor.DARK_GREEN + pl + " Points: " + pxp);
    55. }
    56. }
    57. return false;
    58. }
    59.  
    60. }
    61.  



    And this is my plugin.yml
    Show Spoiler

    Code:
    name: XPAccount
    main: com.bugs3.cracmac.XPAccount.XPAccount
    version: 1.0
    authors:
      - rob1998
    commands:
      xpa:
        description: Show your current XP info.
        usage: /xpa
     
  2. you could also just use

    Code:java
    1. Player player = (Player) sender;
    2. sender.sendMessage( //text you want + player.getExp + " " + player.getLevel)
     
  3. No, I'm making a xpbank plugin, so I need them to store the xp.
     
  4. Offline

    jackwilsdon

    I'd reccomend making the plugin save the player's XP using a command instead of on join. This method can be called and will store player XP to the config.
    Code:java
    1. public void storeXP(String username)
    2. {
    3. Player player = Bukkit.getPlayer(username);
    4. int xp = player.getLevel();
    5. this.getConfig().set("xp."+username+".lvl");
    6. player.setLevel(0); // Reset XP after storing
    7. }
     
  5. That's what I planned to do, but the problem is, I can't tell players how many xp they have on their bank...
     
  6. Offline

    jackwilsdon

    Well, it's as simple as this;
    Code:java
    1. public int getBankXP(String username)
    2. {
    3. return this.getConfig().getInt("xp."+username+".lvl");
    4. }
    Then somewhere in your onCommand, add this;
    Code:java
    1. sender.sendMessage("Your Bank XP is "+this.getBankXP(sender.getName()));
     
  7. Offline

    Zethariel

    How you can't tell? you just use:

    Code:java
    1. sender.sendMessage("You have "+this.getConfig().get("xp."+sender.getName().+".lvl")+" levels!");
     
  8. OK some updated info:

    Source:
    Show Spoiler

    Code:java
    1. package com.bugs3.cracmac.XPAccount;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.player.PlayerJoinEvent;
    12. import org.bukkit.plugin.PluginDescriptionFile;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14.  
    15. public class XPAccount extends JavaPlugin implements Listener {
    16.  
    17. public Logger log;
    18. public final Logger logger = Logger.getLogger("Minecraft");
    19. public void onEnable()
    20. {
    21.  
    22. log = getLogger();
    23. PluginDescriptionFile pdfFile = this.getDescription();
    24. this.logger.info("["+pdfFile.getName()+ "] "+pdfFile.getName()+" "+ pdfFile.getVersion() + " has been Enabled");
    25. getServer().getPluginManager().registerEvents(this, this);
    26. saveConfig();
    27. }
    28. public void onDisable(){
    29. PluginDescriptionFile pdfFile = this.getDescription();
    30. this.logger.info("["+pdfFile.getName()+ "] "+pdfFile.getName()+" "+ pdfFile.getVersion() + " has been Disabled");
    31. saveConfig();
    32. }
    33.  
    34.  
    35. @EventHandler
    36. public void onPlayerJoin(PlayerJoinEvent event){
    37. Player player = event.getPlayer();
    38. String p = player.getName();
    39. getConfig().set("XPA."+p+".LVL", player.getLevel());
    40. getConfig().set("XPA."+p+".XP", player.getExp());
    41. player.sendMessage(ChatColor.DARK_GREEN + "[XP Account] Saved your XP Levels: " + ChatColor.DARK_GREEN + player.getLevel() + " Points: " +player.getExp());
    42. }
    43.  
    44.  
    45. public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args){
    46. Player player = (Player) sender;
    47. String p = player.getName();
    48. if(command.getName().equalsIgnoreCase("xpa"))
    49. {
    50. if(args[0].equalsIgnoreCase("")){
    51. player.sendMessage(ChatColor.DARK_GREEN + "[XP Account] Levels: " + ChatColor.GREEN + getConfig().getInt("XPA."+p+".LVL") + ChatColor.DARK_GREEN + " Points: " + ChatColor.GREEN + getConfig().getInt("XPA."+p+".XP"));
    52. }
    53. }
    54. return false;
    55. }
    56.  
    57. }
    58.  



    My plugin.yml:
    Show Spoiler

    Code:
    name: XPAccount
    main: com.bugs3.cracmac.XPAccount.XPAccount
    version: 1.0
    authors:
      - rob1998
    commands:
      xpa:
        description: Show your current XP info.
        usage: /xpa


    The config.yml created when I join:
    Show Spoiler

    Code:
    XPA:
      rob1998:
        XP: 0.29411757
        LVL: 10
    


    And this is the error:
    Show Spoiler

    Code:
    2013-06-24 15:58:58 [INFO] rob1998 issued server command: /xpa
    2013-06-24 15:58:58 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'xpa' in plugin XPAccount v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:189)
        at org.bukkit.craftbukkit.v1_5_R3.CraftServer.dispatchCommand(CraftServer.java:523)
        at net.minecraft.server.v1_5_R3.PlayerConnection.handleCommand(PlayerConnection.java:971)
        at net.minecraft.server.v1_5_R3.PlayerConnection.chat(PlayerConnection.java:889)
        at net.minecraft.server.v1_5_R3.PlayerConnection.a(PlayerConnection.java:846)
        at net.minecraft.server.v1_5_R3.Packet3Chat.handle(Packet3Chat.java:44)
        at net.minecraft.server.v1_5_R3.NetworkManager.b(NetworkManager.java:292)
        at net.minecraft.server.v1_5_R3.PlayerConnection.d(PlayerConnection.java:115)
        at net.minecraft.server.v1_5_R3.ServerConnection.b(SourceFile:35)
        at net.minecraft.server.v1_5_R3.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.v1_5_R3.MinecraftServer.r(MinecraftServer.java:581)
        at net.minecraft.server.v1_5_R3.DedicatedServer.r(DedicatedServer.java:226)
        at net.minecraft.server.v1_5_R3.MinecraftServer.q(MinecraftServer.java:477)
        at net.minecraft.server.v1_5_R3.MinecraftServer.run(MinecraftServer.java:410)
        at net.minecraft.server.v1_5_R3.ThreadServerApplication.run(SourceFile:573)
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
        at com.bugs3.cracmac.XPAccount.XPAccount.onCommand(XPAccount.java:55)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
        ... 15 more
    
     
  9. Offline

    jackwilsdon

    You are using args[0] on line 50, and the args variable is empty, therefore [0] is out of bounds. Just remove the whole if statement that includes args[0].
     
  10. ok but how can I do something when for example args[2] doesn't exists?
     
  11. Offline

    CubieX

    Check the length of args with "if args.length == 1" for example, before accessing the index.
    (depending how much arguments you are expecting for a command.)
     
  12. I have some commands:
    /xpa [add|take] [amount]
    /xpa show
    /xpa player [player]
    how can I do something on command /xpa ?
     
Thread Status:
Not open for further replies.

Share This Page