Developers help

Discussion in 'Plugin Development' started by raymart23, Jun 2, 2015.

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

    raymart23

    First of all this is my code :
    Code:
    package Commands;
    
    import me.raymart.Warp;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    import Configs.Config;
    
    public class CommandWarp implements CommandExecutor {
        public static Warp plugin;
        Config conf = Config.getInstance();
        public CommandWarp(Warp instance) {
            plugin = instance;
        }
    
          @Override
          public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
              if(!(sender instanceof Player)) {
                System.out.println("You must be a player to do that");
                return true;
            }
              String warp = ChatColor.BLACK + "[" + ChatColor.AQUA + "EasyWarp" + ChatColor.BLACK + "]" + " " + ChatColor.RESET;
              Player user = (Player) sender;
              if(commandLabel.equalsIgnoreCase("setwarp")) {
                      if(user.hasPermission("easywarp.setwarp")) {
                  if(args.length == 0) {
                      user.sendMessage(warp + ChatColor.GRAY + "/setwarp <name>");
                      return true;
                  }
                  if(this.conf.getData().contains("warps." + args[0])) {
                      user.sendMessage(warp + ChatColor.GOLD + args[0] + ChatColor.GRAY + " Warp is already existed");
                      return true;
                  }
                  this.conf.getData().set("warps." + args[0] + ".world", user.getLocation().getWorld().getName());
                  this.conf.getData().set("warps." + args[0] + ".x", user.getLocation().getX());
                  this.conf.getData().set("warps." + args[0] + ".y", user.getLocation().getY());
                  this.conf.getData().set("warps." + args[0] + ".z", user.getLocation().getZ());
                  this.conf.getData().set("warps." + args[0] + ".yaw", user.getLocation().getYaw());
                  this.conf.getData().set("warps." + args[0] + ".pitch", user.getLocation().getPitch());
                  this.conf.saveData();
                  user.sendMessage(warp + ChatColor.GOLD +  args[0] + ChatColor.GRAY + " has been saved");
              }else {
                  user.sendMessage(warp + ChatColor.GRAY + "You have no permission to do that");
              }
              }
             
                if (commandLabel.equalsIgnoreCase("warp")) {
                    if (args.length == 0) {
                      user.sendMessage(warp + ChatColor.GRAY + "/warp <name>");
                      return true;
                    }
                    if (this.conf.getData().getConfigurationSection("warps." + args[0]) == null) {
                      user.sendMessage(warp + ChatColor.GRAY + "warp " + ChatColor.GOLD + args[0] + ChatColor.GRAY + " doesn't exists");
                      return true;
                    }
                    World w = Bukkit.getServer().getWorld(this.conf.getData().getString("warps." + args[0] + ".world"));
                    double x = this.conf.getData().getDouble("warps." + args[0] + ".x");
                    double y = this.conf.getData().getDouble("warps." + args[0] + ".y");
                    double z = this.conf.getData().getDouble("warps." + args[0] + ".z");
                    double yaw = this.conf.getData().getDouble("warps." + args[0] + ".yaw");
                    double pitch = this.conf.getData().getDouble("warps." + args[0] + ".pitch");
                    user.teleport(new Location(w, x, y, z, (float)yaw, (float)pitch));
                    user.sendMessage(warp + ChatColor.GRAY + "Teleported to " + ChatColor.GOLD + args[0]);
                  }
                if (commandLabel.equalsIgnoreCase("delwarp")) {
                    if (user.hasPermission("easywarp.delwarp")) {
                      if (args.length == 0)
                      {
                        user.sendMessage(warp + ChatColor.GRAY + "/delwarp <name>");
                        return true;
                      }
                      if (this.conf.getData().getConfigurationSection("warps." + args[0]) == null) {
                        user.sendMessage(warp + ChatColor.GOLD + args[0] + ChatColor.GRAY + " doesn't exists");
                        return true;
                      }
                      this.conf.getData().set("warps." + args[0], null);
                      this.conf.saveData();
                      user.sendMessage(warp + ChatColor.GOLD + args[0] + ChatColor.GRAY + " has been deleted");
                    } else {
                      user.sendMessage(warp + ChatColor.GRAY + "You have no permission to do that");
                    }
    
                  }
            return false;
        }
    
    }
    
    can someone help me how to add listwarps command ?
     
  2. Offline

    caderape

    @raymart23 config.ConfigurationSection is what you need, you can loops all the values of "warps" with this.
     
  3. Offline

    mine-care

    @raymart23
    Add a if condition to check if the command is listwarps.
    There you have it, you added the command....
    Please be more specific on what the problem is and where you need help, we dont have all of your code to know exacly how you have implemented it and therefore be in a position to sugest stuff, generally speaking yes it is what @caderape said, and you can refer here for more, but there are other possible ways such as loading the list on a list/map (depents on the way you want it).
     
  4. Offline

    raymart23

    @mine-care I mean what code should I use to add /listwarp command
     
  5. Offline

    Just_Jitse

    @raymart23 I think they aren't going to do that, then they would be spooning feed :/
     
  6. Offline

    mine-care

    @Just_Jitse Exacly.
    So @raymart23 try yourself with the resources above and when you reach a dead end we will be here to assist you :D
     
  7. Offline

    Just_Jitse

  8. Offline

    raymart23

    @mine-care @Just_Jitse please guys need your help I just want to add /listwarp command thats only my concern please
    Code:
    if(commandLabel.equalsIgnorecase("listwarp")) {
    //help me here to complete
    }
     
  9. Offline

    caderape

    @raymart23 How will you do if an error is coming ? You will not be able to correct it and you will come here again.
    This is really not difficult configurationSection. And it's helpfull. Learn it.
     
    mine-care likes this.
  10. Offline

    ForsakenRealmz

    @raymart23 I'm going to assume your config file looks something like this...?
    Code:
    warps:
      warp1:
        - String/Ints/And more
      warp2:
        - String/Ints/And more
      warp3:
        - String/Ints/And more
    If so, you will use
    Code:
    Set<String> variableName = getConfig().getConfigurationSection("path").getKeys(false);
    What the above line of those does is get the name every node under warps. So, in the Set you will find [warp1, warp2, warp3]. As you can see... This is like having each node individually now. It's like as if you didn't even have the warps: to begin with.

    You can iterate (cycle) through these by using an enhanced for-loop
    Code:
    for( String string : variableName ) {
        // Code Here
        // The variable string will be populated one at a time with the warp1, warp2, warp3
        // This can be used to list the warps as simple as...
        player.sendMessage( ChatColor.GOLD + string );
        // That will send the player a message in gold warp1 \n warp2 \n warp3
    }
    The enchanted for-loop does not require a counter, unlike the standard for-loops which would make use of array lengths to know when to stop. This just continues to populate until there are no more elements left in the array( list, set, etc..). If you are testing for a result in the enhanced for-loop and want to stop there then use a break; to jump out of the loop. Hopefully that helps you out?
     
  11. Offline

    raymart23

    @ForsakenRealmz how bout for
    Code:
    ( String warpName : this.conf.getConfigurationSecntion("Path").getKeys(false);
     
  12. Offline

    ForsakenRealmz

    @raymart23 You are correct in doing that, but I personally like to store it in the Set for later use. I'm writing a plugin currently and I cycle through the Set filled with the configuration section very often. And it would be a pain to type that long line out every time, and make it rather sloppy.

    Did what I post help you though?
     
  13. Offline

    raymart23

Thread Status:
Not open for further replies.

Share This Page