Solved My warps won't save

Discussion in 'Plugin Development' started by IkBenHarm, Apr 1, 2013.

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

    IkBenHarm

    Hello,
    i'm making a plugin and in this plugin you will be able to warp. All the warp stuff is now working, but when i quit the server and start it over again it won't save the warps.

    This is my code:
    Code:
    package me.ikbenharm.hcommands;
     
    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 Main extends JavaPlugin {
       
        public final Location[] warplocations = new Location[100];
        public final String[] warpnames = new String[100];
        public int warpcounter = 0;
       
        public void onEnable(){
            System.out.println("HCommands Enabled");
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
       
        public void onDisable(){
            System.out.println("HCommands Disabled");
        }
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            Player player = (Player)sender;
     
            if(cmd.getName().equalsIgnoreCase("setwarp")){
                if(args.length == 0){
                    player.sendMessage(ChatColor.RED + "Please use: " + ChatColor.GREEN + "/setwarp <warpname>");
                }else{
                    Location location = player.getLocation();
                    if(!(warpcounter > 100)){
                        warplocations[warpcounter] = location;
                        warpnames[warpcounter] = args[0];
                        warpcounter++;
                        player.sendMessage(ChatColor.GREEN + "Warp set as " + args[0]);
                    }else{
                        player.sendMessage(ChatColor.RED + "Maximum of warps is only 100!");
                    }
                }
                return true;
               
            }if(cmd.getName().equalsIgnoreCase("warp")){
                for(int i = 0; i < warpnames.length; i++){
                    String warpname = warpnames [i];
                    if(args[0].equalsIgnoreCase(warpname)){
                        Location warplocation = warplocations[i];
                        player.teleport(warplocation);
                        player.sendMessage(ChatColor.RED + "Teleported to " + ChatColor.GREEN + warpname);
                        break;
                    }
                }
                return true;
               
            }if(cmd.getName().equalsIgnoreCase("warps")){
                String warps = "";
                for(int i = 0; i < warpnames.length; i++){
                    if(i !=warpnames.length-1){
                        warps+= warpnames[i] + ", ".replace("null" , " ");
                    }else{
                        player.sendMessage(ChatColor.GOLD + "Showing all warps: " + ChatColor.BLUE + warps);
                    }
                }
                return true;
            }
            return false;
        }
    }
    If someone knows it and want's to help me i'd really appriciate it!
     
  2. Offline

    teunie75

    You are better of storing the names into a file because you now store it in something temporary, and that way it will get deleted after shutdown.
     
  3. Offline

    IkBenHarm

    teunie75 How do i do that? (sorry, i'm new to this)
     
  4. Offline

    KoffiePatje

    I recommend you to take a look at this tutorial, you have to store all your variables, if this doesn't work out for you, you can contact me personally ( dutch aswell ) so i can explain it to you if you want ;)

    http://wiki.bukkit.org/Configuration_API_Reference
     
  5. Offline

    kreashenz

    IkBenHarm Use config.. Lol, uhm, if you need help with configs, you can use
    Code:
     
            File file = new File("plugins/PluginName/warps.yml");
            YamlConfiguration yml = YamlConfiguration.loadConfiguration(file);
                            Player player = (Player)sender;
                            String Warp = args[0];
                            Location loc = player.getLocation();
                            double x = loc.getX();
                            double y = loc.getY();
                            double z = loc.getZ();
                            double yaw = loc.getYaw();
                            double pitch = loc.getPitch();
                            World world = loc.getWorld();
                            String warpName = args[0];
                            yml.set("warps." + warpName.toLowerCase() + ".x", Double.valueOf(x));
                            yml.set("warps." + warpName.toLowerCase() + ".y", Double.valueOf(y));
                            yml.set("warps." + warpName.toLowerCase() + ".z", Double.valueOf(z));
                            yml.set("warps." + warpName.toLowerCase() + ".yaw", Double.valueOf(yaw));
                            yml.set("warps." + warpName.toLowerCase() + ".pitch", Double.valueOf(pitch));
                            yml.set("warps." + warpName.toLowerCase() + ".world", world.getName());
                            try {
                                yml.save(file);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
     
  6. Offline

    IkBenHarm

    I will have a look at that thank you. ( bedoelde je dat jij ook nederlands was? :p)

    Don't understand what you just typed there, so i will have a look at what koffiepatje said to me, but thanks anyway!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  7. Offline

    kreashenz

    Uhh, that's basically a whole setwarp command.
     
  8. Offline

    KoffiePatje

  9. Offline

    IkBenHarm

    Ik heb ernaar gekeken, maar ik snap het niet xd. Ik ben nogal een noob in java...

    I now have this:

    Code:
            if(cmd.getName().equalsIgnoreCase("setwarp")){
                if(args.length != 1){
                    player.sendMessage(ChatColor.RED + "Invalid arguments");
                        return false;
                }
                Location location = player.getLocation();
                if(!this.getConfig().contains("warps." + args[0].toLowerCase())){
                    this.getConfig().set("warps." + args[0].toLowerCase(), location);
                    this.saveConfig();
                    player.sendMessage(ChatColor.GREEN + "Warp set as" + ChatColor.BLUE + args[0]);
                }else {
                    player.sendMessage(ChatColor.RED + "This warp already exists!");
                }
                return true;
            }
           
            if(cmd.getName().equalsIgnoreCase("warp")){
                if(args.length != 1){
                    player.sendMessage("Invalid arguments");
                    return false;
                }
                try{
                    Location location = (Location) this.getConfig().get("warps.") + args[0].toLowerCase();
                    player.teleport(location);
                    player.sendMessage(ChatColor.GREEN + "You have been teleported to: " + ChatColor.BLUE + args[0]);
                } catch (Exception e){
                    player.sendMessage(ChatColor.BLUE +args[0] + "does not exist!") ;
                }
                return true;
            }
    But for some reason it gives an error by this line:
    Location location = (Location) this.getConfig().get("warps.") + args[0].toLowerCase();
    Somebody knows why, or how to fix it?

    Nevermind, found it

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  10. Offline

    KoffiePatje

    I think that you can not convert a string into a location.

    As far as i know setting your YML warps.name to (Location)location sets random stuf or the name of the class.

    If you want to store the location you should store the paramaters of the location which is probably, x, y, z, pitch, yaw & world.

    then you could get the values x, y, z, pitch, yaw & world from each warp and recreate an exact copy of the previous stored location.

    This of course means that you also have to store the location as x,y,z,pitch,yaw&world

    Does it work? Your way? ( never tested it, would make thing quite easy )

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

    IkBenHarm

    Solves the whole problem! thanks everyone!
     
Thread Status:
Not open for further replies.

Share This Page