Save locations to config file AKA Warp plugin

Discussion in 'Plugin Development' started by MiningDead, May 22, 2019.

  1. Offline

    MiningDead

    I cannot figure out how to use the config documentation efficiently. Scratching my head out on this stuff. NPE through the roof. Sorry for the args.length being out of order and the rest of the illogical stuff/formatting, I'm very new to Bukkit, I know some stuff about basic Java. The configuration API reference is for a seasoned pro, not someone learning about the config file. Any help appreciated. The switch I have with the "set" case doesn't seem to work at all, and only these work:
    /warp (Says: No warps available) normal
    /warp set (Says: Warp does not exist) normal (from args.length == 1)
    /warp bill (same as right above)
    /warp set set set genericThirdOr4thArgument (says: Try using /warp set <name>) also normal
    The NPE comes from: /warp set name
    or any args[1] in place of name. No idea if .createSection is the way to go for these sections, or if I'm goofing even at the initial part where I set "Warps" in the config in the first place.
    Code:
    if(getConfig().getString("Warps") == null) {
                getConfig().set("Warps", null);
                saveConfig();
            }
    (This^^ part is in onEnable in main class, the following is part of the main cmd switch)
    
                 case "warp":
                        if(args.length == 0) {
                            if(getConfig().getConfigurationSection("Warps") == null) {
                                player.sendMessage("No warps available.");
                                return true;
                            } else {
                            player.sendMessage("Available warps: " + getConfig().getConfigurationSection("Warps"));
                            return true;
                            }
                        } else if (args.length == 2) {
                            switch (args[0].toLowerCase()) {
                                case "set":
                                    if (args[1].toLowerCase() == "warps" || args[1].toLowerCase() == "set") {
                                        player.sendMessage("Bad name for a warp!");
                                        return true;
                                    } else {
                                        //this is happening when /warp set <name> fires
                                        Location loc43 = player.getLocation();
                                        getConfig().getConfigurationSection("Warps").createSection(args[1].toLowerCase());
                                        getConfig().getConfigurationSection(args[1].toLowerCase()).set("location.World", loc43.getWorld());
                                        getConfig().getConfigurationSection(args[1].toLowerCase()).set("location.X", loc43.getX());
                                        getConfig().getConfigurationSection(args[1].toLowerCase()).set("location.Y", loc43.getY());
                                        getConfig().getConfigurationSection(args[1].toLowerCase()).set("location.Yaw", loc43.getYaw());
                                        getConfig().getConfigurationSection(args[1].toLowerCase()).set("location.Pitch", loc43.getPitch());
                                        saveConfig();
                                        return true;
                                    }
                               
                               
                                default:
                                    player.sendMessage("Try using /warp set <name> or /warp <name>");
                                    return true;
                                }
                            } else if (args.length == 1) {
                                if (getConfig().contains("Warps."+args[0]) == true) {
                                    World w = Bukkit.getServer().getWorld(getConfig().getString("Warps."+args[0]+"location.World"));
                                    double x = getConfig().getDouble("Warps."+args[0]+"location.X");
                                    double y = getConfig().getDouble("Warps."+args[0]+"location.Y");
                                    double z = getConfig().getDouble("Warps."+args[0]+"location.Z");
                                    float yawe = getConfig().getInt("Warps."+args[0]+"location.Yaw");
                                    float pitche = getConfig().getInt("Warps."+args[0]+"location.Pitch");
                                    Location telePorter = new Location(w, x, y, z, yawe, pitche);
                                    //If config file under Warps section contains primary argument we teleport them.
                                    player.teleport(telePorter);
                                    return true;
                                } else {
                                    player.sendMessage("Warp does not exist!");
                                    return true;
                                }
                               
                            } else {
                                player.sendMessage("Try using /warp set <name>");
                                return true;
                            }
    
     
  2. Offline

    timtower Ninja on the waves Moderator

    @MiningDead String compare with == will always return false.
    Use .equalsIgnoreCase instead.
     
  3. I worked on something similar, here's a rough overlook:

    Code:
    //Your command
    if(args.length() == 0) {
    
    //Your code
    
    }
    if(args.length() == 1) {
    
    //Your code (too few arguments)
    
    }
    if(args.length() == 2)  {
    
    String locationName = args[1];
    
    //Check if warp doesn't already exist
    
                String world = p.getWorld().getName();
                double x = p.getLocation().getX();
                double y = p.getLocation().getY();
                double z = p.getLocation().getZ();
                double yaw = p.getLocation().getYaw();
                double pitch = p.getLocation().getPitch();
    
                this.locationscfg.set("Locations." + LocationName.toLowerCase() + ".World", world);
                this.locationscfg.set("Locations." + LocationName.toLowerCase() + ".X", x);
                this.locationscfg.set("Locations." + LocationName.toLowerCase() + ".Y", y);
                this.locationscfg.set("Locations." + LocationName.toLowerCase() + ".Z", z);
                this.locationscfg.set("Locations." + LocationName.toLowerCase() + ".Yaw", yaw);
                this.locationscfg.set("Locations." + LocationName.toLowerCase() + ".Pitch", pitch);
               
                saveLocations();
    
    
    }
    
    That's a simple way to save a location.
    Also you don't need to check if this is true

    Code:
    if (getConfig().contains("Warps."+args[0]) == true) {
    Just use

    Code:
    if(getConfig().contains("Warps." + args[0])) {}
    Also your code to get the existing warps will return a lot of useless stuff

    Code:
    player.sendMessage("Available warps: " + getConfig().getConfigurationSection("Warps"));
    Instead of using the code above, use something like



    Code:
    if(getConfig().getConfigurationSection("Warps").getKeys(false).size() > 0) { //Check if there's a least one warp
    
    
    
    
    
    String existing_warps = "Warps: ");
    
    
    
    
    
    for(String warps : getConfig().getConfigurationSection("Warps").getKeys(false)) {
    
    
    
    
    
    existing_warps = String.valueOf(existing_warps) + warps;
    
    
    
    
    
    }
    
    
    
    
    
    p.sendMessage(existing_warps);
    
    
    
    
    
    }
    
    
    else {
    
    
    
    
    
    p.sendMessage("There are no warps yet");
    
    
    
    
    
    }
     
  4. @Minesuchtiiii Use else ifs, yaw and pitch are floats, save the world UUID not name, instead of that for loop to list locations using a stream would look better.
    Also, I'd likely just cache the warp stuff in a Set, load during onEnable and save in onDisable.
     
  5. Does if / else if make a difference in this case? Also it doesn't matter too much wether you're using floats or doubles for the yaw and pitch. Using the for loop or the stream is kinda a personal preference I guess. What advantage would you have caching it and saving it when disabling?
     
  6. @Minesuchtiiii It isn't a case of it it "matters" it's just proper code. As for float and double, floats use less memory and then the type is actually consistent so you should use it regardless.
    As for caching, quicker and can much more easily manipulate them.
     

Share This Page