Listing warps

Discussion in 'Plugin Development' started by Astrophylite, Aug 31, 2015.

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

    Astrophylite

    Hey all,

    I am trying to make a plugin that will warp players to the specified warp. I have everything working (list warps, set warps, delete warps, go to warps), but the list warps is a little complicated. I have got it to print out all the available warps, but I want to know how I can make it list the warps in a nicer looking way :p

    List Warp Code (open)

    Code:
    package me.zircon.zirconessentials.commands.teleport.warp;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.ConfigurationSection;
    
    import me.zircon.zirconessentials.other.SettingsManager;
    
    public class ListWarps implements CommandExecutor {
    
        SettingsManager settings = SettingsManager.getInstance();
        ConfigurationSection warps = settings.getData().getConfigurationSection("warps");
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("listwarps")) {
                if(!sender.hasPermission("zirconessentials.warp.list")) {
                    sender.sendMessage(ChatColor.RED + "You do not have permission to list warps!");
                    return false;
                } else {
                    sender.sendMessage(ChatColor.GREEN + "Available warps:");
                    sender.sendMessage(ChatColor.GREEN + "" + settings.getData().getConfigurationSection("warps").getKeys(false));
                    return true;
                }
            }
            return false;
        }
    }
    


    Any help will be appreciated,
    _Zircon_

    EDIT: It prints out like this: http://prntscr.com/8b367g but I want it to actually look like a list..
     
  2. Offline

    AdobeGFX

    You could replace [ and ] with nothing.. like this
    Code:
    settings.getData().getConfigurationSection("warps").getKeys(false).replace("[", "").replace("]", "");
    Or just loop through it and use string builder to make it look the way you want

    You can also add a "Total warps", Might be appreciated by some,
    Very simple aswell, i think you can do it by getting the configuration section, the same as when you get every warp, but getting the .size();
     
  3. Offline

    Astrophylite

    @AdobeGFX I have added the total warps, thanks for the suggestion.
    The code you put there just keeps putting me in a loop of quick fixes :p
    When I first put it in it told me to "add cast to method receiver", so I completed the quick fix then it told me "Cannot cast from Set<String> to String" ..

    Any help will be appreciated,
    _Zircon_.
     
  4. Offline

    Oxyorum

    @_zircon_ I'm assuming you want each destination on a separate line. You can loop through the list of warps and have send the player a separate message for each warp, similar to what's below.

    Code:
    for(String warp : warps){
       sender.sendMessage(warp);
    }
    
     
  5. Offline

    stefvanschie

  6. Offline

    Astrophylite

    @Oxyorum @stefvanschie I have fixed it to the point of satisfaction :p
    However, I have made it display commas in between each warp name, but it appends a comma to the end. How do I remove that?

    My code for the StringBuilder:
    Code:
    StringBuilder sb = new StringBuilder();
    for(int i = 0; i < warps.size(); i++) {
      sb.append(listWarps[i] + ", ");
    }
    String s = sb.toString();
     
  7. Offline

    Oxyorum

    @_zircon_ Add an if-statement inside your for loop to compensate, like so:

    Code:
    for(...){
       if(dealing with the last warp){
          //append to the stringbuilder without the comma
       }
       else{
        //append to stringbuilder WITH comma
       }
    }
    
    Edit: Fixed it.
     
  8. Offline

    Astrophylite

    @Oxyorum I done what you said, but it' s still happening.

    My IF statement:
    Code:
    if(i == warps.size()) {
      sb.append(listWarps[i] + "");
    } else {
      sb.append(listWarps[i] + ", ");
    }
    
     
  9. Offline

    Oxyorum

    @_zircon_ The condition on your first if statement is not correct, and doesn't execute. Variable i will never be equal to the size in your for loop.

    Edit: Look at your for loop condition and you'll see the problem.
     
  10. Offline

    Astrophylite

    Don't worry. I've fixed it now.
     
  11. Offline

    Astrophylite

    I'm opening this thread up again because I can't be bothered to make a new one on the same topic. I have just looked back through my list warp code, and I go to tidy it up. I finished doing all that, export the jar and reload the server. Bam, Console error for days :p
    Console Error (open)

    Code:
    java.lang.NullPointerException
            at me.zircon.zirconessentials.commands.teleport.warp.ListWarps.<init>(ListWarps.java:15) ~[?:?]
            at me.zircon.zirconessentials.ZirconEssentials.onEnable(ZirconEssentials.java:88) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:357) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:317) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.reload(CraftServer.java:741) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.Bukkit.reload(Bukkit.java:535) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_51]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_51]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:714) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:653) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:556) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_51]


    From what I can understand from that, is that: it cannot find the warps data config section... Even though, when I do "/delwarp *" (which deletes all warps), I make sure it re-sets up the warps config section?

    ListWarps.java (open)

    Code:
    package me.zircon.zirconessentials.commands.teleport.warp;
    
    import java.util.Set;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    
    import me.zircon.zirconessentials.other.SettingsManager;
    
    public class ListWarps implements CommandExecutor {
    
        SettingsManager settings = SettingsManager.getInstance();
        Set<String> warps = settings.getData().getConfigurationSection("warps").getKeys(false);
        Object[] listWarps = warps.toArray();
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("listwarps")) {
                if(!sender.hasPermission("zirconessentials.warp.list")) {
                    sender.sendMessage(ChatColor.RED + "You do not have permission to list warps!");
                    return false;
                } else {
                    if(settings.getData().getConfigurationSection("warps") == null) {
                        sender.sendMessage(ChatColor.GREEN + "There are no available warps to display!");
                        return false;
                    } else {
                        StringBuilder sb = new StringBuilder();
                        for(int i = 0; i < warps.size(); i++) {
                            if(i == warps.size() - 1) {
                                sb.append(listWarps[i] + " ");
                            } else {
                                sb.append(listWarps[i] + ", ");
                            }
                        }
                        String s = sb.toString();
                        sender.sendMessage(ChatColor.GREEN + "Available warps:");
                        sender.sendMessage(ChatColor.GREEN + "" + s);
                        sender.sendMessage(ChatColor.GREEN + "Total Warps: " + warps.size());
                        return true;
                    }
                }
            }
            return false;
        }
    }
    


    DeleteWarp.java (open)

    Code:
    package me.zircon.zirconessentials.commands.teleport.warp;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    
    import me.zircon.zirconessentials.other.SettingsManager;
    
    public class DeleteWarp implements CommandExecutor {
    
        SettingsManager settings = SettingsManager.getInstance();
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {       
            if(cmd.getName().equalsIgnoreCase("deletewarp")) {
                if(!sender.hasPermission("zirconessentials.warp.delete")) {
                    sender.sendMessage(ChatColor.RED + "You do not have permission to delete warps!");
                    Bukkit.getServer().getLogger().info(sender.getName() + " attempted to run " + label + " but didn't have enough permissions!");
                    return false;
                } else {
                    if(args.length == 0) {
                        sender.sendMessage(ChatColor.RED + "Usage: " + label + " <warp>");
                        return false;
                    } else {
                        if(args[0].equalsIgnoreCase("*")) {
                            if(!sender.hasPermission("zirconessentials.warp.delete.all")) {
                                sender.sendMessage(ChatColor.RED + "You do not have permission to delete all warps!");
                                Bukkit.getLogger().info(sender.getName() + " attempted to delete all the warps but did not have enough permissions!");
                                return false;
                            } else {
                                settings.getData().set("warps", null);
                                settings.getData().set("warps", "");
                                settings.saveData();
                                sender.sendMessage(ChatColor.GREEN + "All warps have been deleted!");
                                Bukkit.getLogger().info(sender.getName() + " deleted all the warps successfully!");
                                return true;
                            }
                        }
                        if(settings.getData().getConfigurationSection("warps." + args[0]) == null) {
                            sender.sendMessage(ChatColor.RED + "The warp \"" + args[0] + "\" cannot be found!");
                            Bukkit.getLogger().info(sender.getName() + " attempted to delete the warp \"" + args[0] + "\" but it doesn't exist!");
                            return false;
                        } else {
                            if(!sender.hasPermission("zirconessentials.warp.delete." + args[0]) || !sender.hasPermission("zirconessentials.warp.delete.*")) {
                                sender.sendMessage(ChatColor.RED + "You do not have permission to delete the warp " + args[0] + "!");
                                Bukkit.getLogger().info(sender.getName() + " attempted to delete the warp \"" + args[0] + "\" but did not have enough permissions!");
                            } else {
                                settings.getData().set("warps." + args[0], null);
                                settings.saveData();
                                sender.sendMessage(ChatColor.GREEN + "The warp " + args[0] + " has been deleted!");
                                return true;
                            }
                        }
                    }
                }
            }
            return false;
        }
    }
    


    Any help will be appreciated,
    _Zircon_
     
  12. Offline

    Oxyorum

Thread Status:
Not open for further replies.

Share This Page