Solved Make separate warp pages.

Discussion in 'Plugin Development' started by AdamDev, Sep 23, 2017.

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

    AdamDev

    Hello, everyone!

    I need some help with a plugin I'm making. I want to make pages of warps. So something like:

    -=-Warps 1/1-=-

    - Blah1
    - Blah2
    - Blah3
    - Blah4
    - Blah5
    -=-Warps 1/1-=-

    I tried on my own but it didn't work. So that's why I'm here.

    Code:
    package me.hype.testing.warps;
    
    import java.io.File;
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Core extends JavaPlugin {
      
        private File configfile;
        private String prefix = format("&9[&b&lWarps&9] &r");
      private List<String> warps = new ArrayList<String>();
      
        public void onEnable() {
            setUpConfig();
        }
      
        public void onDisable() {
          
        }
      
        public void setUpConfig() {
            configfile = new File(getDataFolder(), "config.yml");
          
            if(!configfile.exists()) {
                configfile.getParentFile().mkdirs();
                copy(getResource("config.yml"), configfile);
                getConfig().createSection("Warps");
                getConfig().createSection("Warp_Names");
                saveConfig();
            } else if(configfile.exists()) {
                return;
            }
        }
      
        private void copy(InputStream resource, File configfile2) {
            return;
        }
      
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
          
            if(sender instanceof Player) {
                final Player p = (Player) sender;
                if(label.equalsIgnoreCase("setwarp")) {
                    if(!p.hasPermission("warps.command.setwarp")) {
                        p.sendMessage(format(prefix + "&cYou do not have the permission to perform this command!"));
                        return true;
                    } else {
                        if(args.length == 0) {
                            p.sendMessage(format(prefix + "&cUsage: /setwarp <Name>"));
                            return true;
                        } else if(args.length == 1) {
                            if(getConfig().contains("Warps." + args[0])) {
                                p.sendMessage(format(prefix + "&cThe warp &e" + args[0] + " &calready exists!"));
                                return true;
                            } else if(!getConfig().contains("Warps." + args[0])) {
                                getConfig().set("Warps." + args[0] + ".World", p.getWorld().getName());
                                getConfig().set("Warps." + args[0] + ".X", p.getLocation().getX());
                                getConfig().set("Warps." + args[0] + ".Y", p.getLocation().getY());
                                getConfig().set("Warps." + args[0] + ".Z", p.getLocation().getZ());
                                getConfig().set("Warps." + args[0] + ".Yaw", p.getLocation().getYaw());
                                getConfig().set("Warps." + args[0] + ".Pitch", p.getLocation().getPitch());
                                getConfig().set("Warp_Names." + args[0], "");
                                saveConfig();
                                p.sendMessage(format(prefix + "&aYou have successfully created the warp &b" + args[0] + "&a!"));
                                warps.add(args[0]);
                                return true;
                            }
                        }
                    }
                } else if(label.equalsIgnoreCase("warp")) {
                    if(!p.hasPermission("warps.command.warp")) {
                        p.sendMessage(format(prefix + "&cYou do not have permission to perform this command!"));
                        return true;
                    } else {
                        if(args.length == 0) {
                            p.sendMessage(format(prefix + "&cUsage: /warp <Name>"));
                        } else if(args.length == 1) {
                            if(!getConfig().contains("Warps." + args[0])) {
                                p.sendMessage(format(prefix + "&cThe warp &e" + args[0] + " &cdoes not exist!"));
                                return true;
                            } else if(getConfig().contains("Warps." + args[0])) {
                                Location warp = new Location(Bukkit.getServer().getWorld(getConfig().getString("Warps." + args[0] + ".World")), getConfig().getDouble("Warps." + args[0] + ".X"), getConfig().getDouble("Warps." + args[0] + ".Y"), getConfig().getDouble("Warps." + args[0] + ".Z"), (float) getConfig().getDouble("Warps." + args[0] + ".Yaw"), (float) getConfig().getDouble("Warps." + args[0] + ".Pitch"));
                                p.teleport(warp);
                                p.sendMessage(format(prefix + "&aYou have been warped to &b" + args[0] + "&a!"));
                                return true;
                            }
                        }
                    }
                } else if(label.equalsIgnoreCase("warps")) {
                                  // RIGHT HERE IS WHERE IT LISTS THE WARPS
                    if(!p.hasPermission("warps.command.warps")) {
                        p.sendMessage(format(prefix + "&cYou do not have permission to perform this command!"));
                        return true;
                    } else {
                        if (args.length == 0) {
                              // Here it displays only 5 with -=-Warps 1/1-=-
                        } else if (args.length == 1) {
                            // Here it displays when over 5 warps with -=-Warps 1/number-=-
                            // 5 Warps per page.
                        }
                      
                    }
                } else if(label.equalsIgnoreCase("delwarp")) {
                    if(!p.hasPermission("warps.command.delwarp")) {
                        p.sendMessage(format(prefix + "&cYou do not have permission to perform this command!"));
                        return true;
                    } else {
                        if(args.length == 0) {
                            p.sendMessage(format(prefix + "&cUsage: /delwarp <Name>"));
                            return true;
                        } else if(args.length == 1) {
                            if(!getConfig().contains("Warps." + args[0])) {
                                p.sendMessage(format(prefix + "&cThe warp &e" + args[0] + " &cdoes not exist!"));
                                return true;
                            } else if(getConfig().contains("Warps." + args[0])) {
                                getConfig().set("Warps." + args[0], null);
                                getConfig().set("Warp_Names." + args[0], null);
                                saveConfig();
                                p.sendMessage(format(prefix + "&aYou have successfully deleted the warp &b" + args[0] + "&a!"));
                                return true;
                            }
                        }
                    }
                } else if(label.equalsIgnoreCase("clearwarps")) {
                    if (args.length == 0) {
                        getConfig().set("Warps_Names", null);
                        saveConfig();
                        warps.clear();
                } else if (args.length >= 1) {
                    p.sendMessage(format(prefix + "&cUsage: /clearwarps"));
                    return true;
                    }
                } // Add another command here
            }  
          
            return true;
        }
        private String format(String string) {
            return ChatColor.translateAlternateColorCodes('&', string);
        }
    
    }
    
    Other than that I hope this is possible because it will help me with another plugin also.

    ~ AdamDev
     
  2. Offline

    Caderape2

    @AdamDev Of course it's possible. You can get the configurationSection 'Warps' and retrieve the size.
    getConfigurationSection("Warps").getKeys(false).size();

    Let say the default page is 0.
    int start = page * 5;
    int end = start + 5;

    Then you can loop the configurationSection, add a count inside that will increase every loop, and check if the count is lower or equal to the start, and lower than the end. If yes, send the name of the warp. Once done, check if the end is lower than the size of the configurationSection. And if yes, send a message for the next page.
     
  3. Offline

    AdamDev

    @Caderape2
    So first:
    Code:
    int warpsSize = getConfig().getConfigurationSection("Warp_Names").getKeys(false).size();
    
    then ?:
    Code:
    int start = 1 * 5;
    int end = start + 5;
    
    Ok, Seems to not so much click
    Code:
    for (int i = 0;i<warpsSize;i++) {
          if (i <= start && i < end) {
              p.sendMessage("What do I send?");
         }
         if  (end < warpsSize) {
              p.sendMessage(ChatColor.GRAY+"/warps <page> - List warps to the required <page>");
         }
    }
    
    Am I close?

    Edit: can i do something like: int start = a[0] * 5?
     
  4. Offline

    Caderape2

    @AdamDev more something like this.
    You have to get the argument in the command and cast it to an int. but be sure it's really an int with a try catch
    Code:
            int page = argument of the page in the command
            page--; // for the method, the  first page is 0, but for a player, it's 1
            if (page < 0) page = 0; // just to be sure the player don't do shit
       
            int warpsSize = getConfig().getConfigurationSection("Warp_Names").getKeys(false).size();
       
            int start = page * 5;
            int end = start + 5;
            int count = 0;
       
            for (String warp : getConfig().getConfigurationSection("Warp_Names").getKeys(false))
            {
                if (count >= start && count < end)
                {
                    sender.sendMessage(warp);
                }
                count++;
            }
       
            if (end < warpsSize)
            {
                sender.sendMessage("/page "+(page+1) +" for the next page");
            }
     
  5. Offline

    AdamDev

    @Caderape2
    Thanks, it works! Now time to add this to the other plugin I was making.
     
Thread Status:
Not open for further replies.

Share This Page