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: Spoiler (Move your mouse to the spoiler area to reveal the content) Show Spoiler Hide Spoiler Code:java package com.bugs3.cracmac.XPAccount; import java.util.logging.Logger; import org.bukkit.ChatColor;import org.bukkit.command.Command;import org.bukkit.command.CommandSender;import org.bukkit.entity.Player;import org.bukkit.event.EventHandler;import org.bukkit.event.Listener;import org.bukkit.event.player.PlayerJoinEvent;import org.bukkit.plugin.PluginDescriptionFile;import org.bukkit.plugin.java.JavaPlugin; public class XPAccount extends JavaPlugin implements Listener { public Logger log; public final Logger logger = Logger.getLogger("Minecraft"); public void onEnable() { log = getLogger(); PluginDescriptionFile pdfFile = this.getDescription(); this.logger.info("["+pdfFile.getName()+ "] "+pdfFile.getName()+" "+ pdfFile.getVersion() + " has been Enabled"); getServer().getPluginManager().registerEvents(this, this); saveConfig(); } public void onDisable(){ PluginDescriptionFile pdfFile = this.getDescription(); this.logger.info("["+pdfFile.getName()+ "] "+pdfFile.getName()+" "+ pdfFile.getVersion() + " has been Disabled"); saveConfig(); } @EventHandler public void onPlayerJoin(PlayerJoinEvent event){ Player player = event.getPlayer(); String p = player.getName(); int pl = player.getLevel(); float pxp = player.getExp(); getConfig().set("XPA."+p+".LVL", pl); player.sendMessage(ChatColor.DARK_GREEN + "[XP Account] Saved your XP Levels: " + ChatColor.DARK_GREEN + pl + " Points: " +pxp); } public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args){ Player player = (Player) sender; String p = player.getName(); if(command.getName().equalsIgnoreCase("xpa")) { if(args[0].equalsIgnoreCase("")){ int pl = getConfig().getInt("XPA."+p+".LVL"); float pxp = player.getExp(); player.sendMessage(ChatColor.DARK_GREEN + "[XP Account] Levels: " + ChatColor.DARK_GREEN + pl + " Points: " + pxp); } } return false; } } And this is my plugin.yml Spoiler (Move your mouse to the spoiler area to reveal the content) Show Spoiler Hide 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
you could also just use Code:java Player player = (Player) sender;sender.sendMessage( //text you want + player.getExp + " " + player.getLevel)
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 public void storeXP(String username){ Player player = Bukkit.getPlayer(username); int xp = player.getLevel(); this.getConfig().set("xp."+username+".lvl"); player.setLevel(0); // Reset XP after storing}
That's what I planned to do, but the problem is, I can't tell players how many xp they have on their bank...
Well, it's as simple as this; Code:java public int getBankXP(String username){return this.getConfig().getInt("xp."+username+".lvl");} Then somewhere in your onCommand, add this; Code:java sender.sendMessage("Your Bank XP is "+this.getBankXP(sender.getName()));
How you can't tell? you just use: Code:java sender.sendMessage("You have "+this.getConfig().get("xp."+sender.getName().+".lvl")+" levels!");
OK some updated info: Source: Spoiler (Move your mouse to the spoiler area to reveal the content) Show Spoiler Hide Spoiler Code:java package com.bugs3.cracmac.XPAccount; import java.util.logging.Logger; import org.bukkit.ChatColor;import org.bukkit.command.Command;import org.bukkit.command.CommandSender;import org.bukkit.entity.Player;import org.bukkit.event.EventHandler;import org.bukkit.event.Listener;import org.bukkit.event.player.PlayerJoinEvent;import org.bukkit.plugin.PluginDescriptionFile;import org.bukkit.plugin.java.JavaPlugin; public class XPAccount extends JavaPlugin implements Listener { public Logger log; public final Logger logger = Logger.getLogger("Minecraft"); public void onEnable() { log = getLogger(); PluginDescriptionFile pdfFile = this.getDescription(); this.logger.info("["+pdfFile.getName()+ "] "+pdfFile.getName()+" "+ pdfFile.getVersion() + " has been Enabled"); getServer().getPluginManager().registerEvents(this, this); saveConfig(); } public void onDisable(){ PluginDescriptionFile pdfFile = this.getDescription(); this.logger.info("["+pdfFile.getName()+ "] "+pdfFile.getName()+" "+ pdfFile.getVersion() + " has been Disabled"); saveConfig(); } @EventHandler public void onPlayerJoin(PlayerJoinEvent event){ Player player = event.getPlayer(); String p = player.getName(); getConfig().set("XPA."+p+".LVL", player.getLevel()); getConfig().set("XPA."+p+".XP", player.getExp()); player.sendMessage(ChatColor.DARK_GREEN + "[XP Account] Saved your XP Levels: " + ChatColor.DARK_GREEN + player.getLevel() + " Points: " +player.getExp()); } public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args){ Player player = (Player) sender; String p = player.getName(); if(command.getName().equalsIgnoreCase("xpa")) { if(args[0].equalsIgnoreCase("")){ 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")); } } return false; } } My plugin.yml: Spoiler (Move your mouse to the spoiler area to reveal the content) Show Spoiler Hide 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: Spoiler (Move your mouse to the spoiler area to reveal the content) Show Spoiler Hide Spoiler Code: XPA: rob1998: XP: 0.29411757 LVL: 10 And this is the error: Spoiler (Move your mouse to the spoiler area to reveal the content) Show Spoiler Hide 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
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].
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.)
I have some commands: /xpa [add|take] [amount] /xpa show /xpa player [player] how can I do something on command /xpa ?