Need help with how to do some things in java

Discussion in 'Plugin Development' started by Qwertyness_, Apr 16, 2013.

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

    Qwertyness_

    I just started making Bukkit plugins this morning and started programming about a month ago so I a VERY new at this. I am having trouble finding how to do some things in plugin programming. My first issue is getting a player name from a command. I want to have /(command) (player_name) and want to resolve player_name to a variable. I then want to write that name to a yml file under a specific category. Then I also want to be able to resolve these thing back to a variable later. How do I do these things?

    I appreciate all of the help!
     
  2. Offline

    kreashenz

    Make sure it's if(args.length == 1){ and then use Player target = Bukkit.getPlayer(args[0]);
     
  3. Offline

    soulofw0lf

    Player p = Bukkit.getPlayer(args[1]);
    args[1] references the second argument after the command since they start with 0.
    p is the variable the player name is assigned to. hope that helps you get started with what you're trying to do!
     
  4. Offline

    stirante

    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    if (cmd.getName().equalsIgnoreCase("yourCommand") && args.length >= 1){
    Player player = Bukkit.getPlayerExact(args[0]);
    if (player == null){
    sender.sendMessage("Player not found");
    return true;
    }
    else {
    getConfig().set("path.to.var.in.yml", player.getName());
    }
    }
    return false;
    }
     
  5. Offline

    Qwertyness_

    stirante,
    When using the getConfig line you posted, will the path.to.var.in.yml post to plugin.yml or do I have to change that statement to the name of my yml file?
     
  6. Offline

    kreashenz

    Qwertyness_ No, it will go to the config.yml. Change path.to.var.in.yml to Test or something and check if it saves to the config. Also, if you're needing to reply to someone, hit the tahg button, or simply stirante :)
     
    stirante likes this.
  7. Offline

    Terradominik

    i am a very lazy server administrator, so i prefer the following way...:
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
        if (args.length == 0) return false;
        OfflinePlayer player = Bukkit.getPlayer(args[0]);
        if (player == null) player = Bukkit.getOfflinePlayer(args[0]);
        plugin.getConfig().set("Some.path",player.getName());
        return true
    }
     
  8. Offline

    Qwertyness_

    Okay, another problem. The methods getConfig() and getDescription() that I use for my logger are not showing up on the list of importable methods. All it gives me for an option is to create the methods myself.

    Thanks for all the help so far!
     
  9. Offline

    stirante

    Maybe you haven't extended JavaPlugin class?
     
  10. Offline

    Qwertyness_

    Oh yeah, I forgot about that :p

    I am testing the write to config command at the moment. If that works, how would I read from the config into a variable?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  11. Offline

    stirante

    Also, you again forgot to tag me or reply :p

    EDIT:
    String configVar = getConfig().getString("path.to.var.in.yml");
     
  12. Offline

    kreashenz

    Qwertyness_ You would read it, by using getConfig().get("The.path"); or getConfig().getString("the.path");
     
  13. Offline

    Qwertyness_

    Everyone

    I'm still having trouble writing to the config file.
    I have been using this line of code:
    Code:
    getConfig().set("path.to.wizards", player.getName());
     
    With the yml line of:

    # This is the configuration file.

    wizards:

    [Usernames]


    When the line of java code shown above runs, it does not write anything to the config. Am I using the wrong code?

    Would a hashmap maybe work for that? If so how would I use it?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  14. Offline

    kreashenz

    There's your problem, change "path.to.wizards", to just "wizards".
     
  15. Offline

    Qwertyness_

    kreashenz I already tried that. It doesn't work either......
     
  16. Offline

    kreashenz

    Qwertyness_ You are clearly doing something wrong then. Check your brackets, or post the whole code you are using now, and I will fix it.
     
  17. Offline

    Qwertyness_

    Here is my code. It is a little off topic in some places because I am just using it to learn.
    Code:
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.logging.Logger;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.block.Block;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    public class WizardStatus extends JavaPlugin {
       public final Logger logger = Logger.getLogger("Minecraft");
       public static WizardStatus plugin;
       public final HashMap<Player, Location> hashmap = new HashMap<Player, Location>();
       public void onEnable() {
        PluginDescriptionFile file = this.getDescription();
        this.logger.info(file.getName() + " Version " + file.getVersion() + " Has Been Enabled!");
        getConfig().options().copyDefaults(true);
        saveConfig();
       }
       
       public void onDisable() {
        PluginDescriptionFile  file = this.getDescription();
        this.logger.info(file.getName() + " Has Been Disabled!");
       }
       public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
        Player player = (Player) sender;       
        if (commandLabel.equalsIgnoreCase("setwizard") || commandLabel.equalsIgnoreCase("sw")) {
         
         if (args.length == 0){
          getConfig().set("wizards", player.getName());
          player.sendMessage("You are now a Wizard!");
            }
         Player tplayer = player.getServer().getPlayer(args[0]);
         if (args.length == 1) {
          getConfig().set("wizards", player.getName());
          tplayer.sendMessage("You are now a Wizard!");
         }
         
         if (tplayer == null){
         player.sendMessage("Player us offline or does not exist!");
        }
        }
       
        
        if (commandLabel.equalsIgnoreCase("removewizard") || commandLabel.equalsIgnoreCase("rw")) {  
         
         if (args.length == 0){
          getConfig().set("path.to.wizards", player.getName());
          player.sendMessage("You are now a Wizard!");
            }
         
         else if (args.length == 1) {
          getConfig().set("path.to.wizards", player.getName());
          player.sendMessage("You are now a Wizard!");
         }
        }
        
        if (commandLabel.equalsIgnoreCase("wizards") || commandLabel.equalsIgnoreCase("w")) {
         player.sendMessage(getConfig().getString("wizards"));
        }
        
        if (commandLabel.equalsIgnoreCase("hw")) {
         player.sendMessage("----" + ChatColor.GREEN + "Wizard Class Help" + ChatColor.WHITE + "----");
         player.sendMessage("/setwizard <PLAYER>" + ":" + ChatColor.YELLOW + "Makes defined person a wizard!");
         player.sendMessage("/sw" + ":" + ChatColor.YELLOW + "This is an alias for /setwizard ^");
         player.sendMessage("/wizards" + ":" + ChatColor.YELLOW + "Lists all current wizards.");
         player.sendMessage("/w" + ":" + ChatColor.YELLOW + "This is an alias for /wizards ^");
         player.sendMessage("/hw" + ":" + ChatColor.YELLOW + "Shows this help page!");
        }
        
        if (commandLabel.equalsIgnoreCase("setlocation") || commandLabel.equalsIgnoreCase("sl")) {
         if (args.length == 0) {
          player.sendMessage("/setlocation <location_name>)");
         }
         
         else if (args.length == 1) {
          
         String name = args[0];
         Location location = player.getLocation();
         hashmap.put(player, location);
         }
        }
        
        return false;
       }
    }
    
     
  18. Offline

    gomeow

    Completely untrue.

    /commandLabel args[0] args[1]
     
    kreashenz likes this.
  19. Offline

    kreashenz

    Qwertyness_ Make sure you tag me!! What is it meant to store though, I can't see.. :/ But maybe try this code, and tell me if it works
    Code:java
    1. import java.util.ArrayList;
    2. import java.io.File;
    3. import java.util.HashMap;
    4. import java.util.logging.Logger;
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Location;
    7. import org.bukkit.block.Block;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.plugin.PluginDescriptionFile;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13. public class WizardStatus extends JavaPlugin {
    14. public final Logger logger = Logger.getLogger("Minecraft");
    15. public static WizardStatus plugin;
    16. public final HashMap<Player, Location> hashmap = new HashMap<Player, Location>();
    17. public void onEnable() {
    18. PluginDescriptionFile file = this.getDescription();
    19. this.logger.info(file.getName() + " Version " + file.getVersion() + " Has Been Enabled!");
    20. getConfig().options().copyDefaults(true);
    21. saveConfig();
    22. if(!(new File("plugins/pluginName/config.yml")).exists()){
    23. saveConfig();
    24. }
    25. }
    26.  
    27. public void onDisable() {
    28. PluginDescriptionFile file = this.getDescription();
    29. this.logger.info(file.getName() + " Has Been Disabled!");
    30. }
    31. @Override
    32. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    33. Player player = (Player) sender;
    34. if (commandLabel.equalsIgnoreCase("setwizard") || commandLabel.equalsIgnoreCase("sw")) {
    35. if (args.length == 0){
    36. getConfig().set("wizards", sender.getName());
    37. player.sendMessage("You are now a Wizard!");
    38. }
    39. if (args.length == 1) {
    40. Player tplayer = Bukkit.getPlayer(args[0]);
    41. getConfig().set("wizards", tplayer.getName());
    42. tplayer.sendMessage("You are now a Wizard!");
    43. } else sender.sendMessage("§cThat player is offline and cannot be found.");
    44. }
    45.  
    46.  
    47. if (commandLabel.equalsIgnoreCase("removewizard") || commandLabel.equalsIgnoreCase("rw")) {
    48. // Another little tip, you can set aliases in plugin.yml :)
    49. if (args.length == 0){
    50. getConfig().set("wizards", player.getName());
    51. player.sendMessage("You are now a Wizard!");
    52. }
    53. else if (args.length == 1) {
    54. // For this, try getting the players and setting them to a null.
    55. // Try using a StringList in the config.
    56. Player tplayer = Bukkit.getPlayer(args[0]);
    57. getConfig().set("wizards", tplayer.getName());
    58. player.sendMessage("You are now a Wizard!");
    59. }
    60. }
    61.  
    62. if (commandLabel.equalsIgnoreCase("wizards") || commandLabel.equalsIgnoreCase("w")) {
    63. player.sendMessage(getConfig().getString("wizards"));
    64. }
    65.  
    66. if (commandLabel.equalsIgnoreCase("hw")) {
    67. player.sendMessage("----" + ChatColor.GREEN + "Wizard Class Help" + ChatColor.WHITE + "----");
    68. player.sendMessage("/setwizard <PLAYER>" + ":" + ChatColor.YELLOW + "Makes defined person a wizard!");
    69. player.sendMessage("/sw" + ":" + ChatColor.YELLOW + "This is an alias for /setwizard ^");
    70. player.sendMessage("/wizards" + ":" + ChatColor.YELLOW + "Lists all current wizards.");
    71. player.sendMessage("/w" + ":" + ChatColor.YELLOW + "This is an alias for /wizards ^");
    72. player.sendMessage("/hw" + ":" + ChatColor.YELLOW + "Shows this help page!");
    73. }
    74. // Just a little guess, you MIGHT wanna add in colours :) I suggest using § instead of ChatColor.
    75.  
    76. if (commandLabel.equalsIgnoreCase("setlocation") || commandLabel.equalsIgnoreCase("sl")) {
    77. if (args.length == 0) {
    78. player.sendMessage("/setlocation <location name>)");
    79. }
    80. else if (args.length == 1) {
    81. String name = args[0];
    82. Location location = player.getLocation();
    83. hashmap.put(player, location);
    84. }
    85. }
    86.  
    87. return true;
    88. }
    89. }
     
  20. Offline

    Qwertyness_

    kreashenz
    The config is meant to store player names under the category "wizards:"

    I did find a few errors with you're changes..


    else sender.sendMessage("§cThat player is offline and cannot be found.");
    That line is run if args.length = 0 as well so the offline message is displayed when making yourself a wizard. I tried to fix this with:

    else if (args.length > 1) {
    player.sendMessage("Too many arguments!");
    }

    But I still don't have the online detector. The online status can't be defined by number of arguments.

    Also, the sender.getName() did not fix the logging problem.


    EDIT: I made some changes and fixed a few of the problems I have been having. I fixed the "online detector" by putting :



    Code:
                    if (args.length == 0){
                        getConfig().set("wizards", sender.getName());
                        player.sendMessage("You are now a Wizard!");
                        saveConfig();
                    }
                    else if (args.length == 1) {
                        Player tplayer = Bukkit.getPlayer(args[0]);
                        if (tplayer == null) {
                            player.sendMessage(ChatColor.RED + "That player is offline or does not exist!");
                        }
                        else {
                            getConfig().set("wizards", tplayer.getName());
                            tplayer.sendMessage("You are now a Wizard!");
                            player.sendMessage("You made " + tplayer.getName() + " a wizard!");
                        }
                        saveConfig();
                    }

    Which brings me to my next point.......
    I made a sort of fix to my logging issue. As far as I can tell, it was not logging because the config was not saved after the code was executed. I am pretty sure it is logging fine now. I am not sure yet because I have to test this with another player, but I think that the config may overwrite any previous entries made when a new wizard is defined because when I made myself a wizard, it deleted a default that I had made.
     
  21. Offline

    Qwertyness_

    A new extension to my plugin code......
    What is wrong with this?


    Code:
    do {
                        int y = player.getLocation().getBlockY();
     
                        int x = player.getLocation().getBlockX();
     
                        int z = player.getLocation().getBlockZ();
     
                        y = y - 1;
     
                        Location waterLocation = new Location(player.getWorld(), x, y, z);
     
                        Block b = waterLocation.getBlock();
     
                        b.setType(Material.WATER);
     
                        b.setType(Material.AIR);
                        }while (waterjet == true);
    waterjet is a Boolean defined earlier in the code in order to toggle the state of the command.
    I can't figure out what is wrong. There are no eclipse errors but when the command is executed, there is an internal error in the console: Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
    Thanks for the continued help!
     
  22. Offline

    Tirelessly

    Don't store players in a map. You're probably using args[0] when it doesn't exist.
     
Thread Status:
Not open for further replies.

Share This Page