Save Location

Discussion in 'Plugin Development' started by ItsBlockFighter, Jun 23, 2017.

Thread Status:
Not open for further replies.
  1. Hi!
    I did one that if a player steals then he saves the coordinate and the world, but does not do it how can I do it?

    My Code:
    Code:
    public class QuitSave implements Listener{
    
        Core plugin;
        public void QuitSave(Core plugin) {
            this.plugin = plugin;
        }
    
        public void onPlayerQuit(PlayerQuitEvent e) {
            Player p = e.getPlayer();
            double x = plugin.getConfig().getDouble("Locations.yourlocation.x");
            double y = plugin.getConfig().getDouble("Locations.yourlocation.y");
            double z = plugin.getConfig().getDouble("Locations.yourlocation.z");
            String world = plugin.getConfig().getString("Locations.yourlocation.world");
            Location myLoc = new Location(Bukkit.getServer().getWorld(world), x, y, z);
        }
    }
    
     
  2. Dont save every coordinate by itself. Save Whole Location and then load it by doing getConfig().get(path.to.location);

    Dont forget this.saveConfig(); ;)

    Sent from my Xperia M4 Aqua using Tapatalk
     
  3. @MGlolenstine
    Now he does not even save a file.

    Updated Code:
    Code:
    public class QuitSave implements Listener{
    
        Core plugin;
        public void QuitSave(Core plugin) {
            this.plugin = plugin;
        }
    
        public void onPlayerQuit(PlayerQuitEvent e) {
            Player p = e.getPlayer();
            plugin.getConfig().set("loc.world", p.getLocation().getWorld().getName());
            plugin.getConfig().set("loc.x", p.getLocation().getX());
            plugin.getConfig().set("loc.y", p.getLocation().getY());
            plugin.getConfig().set("loc.z", p.getLocation().getZ());
            plugin.saveConfig();
        }
    }
    
     
  4. Offline

    timtower Administrator Administrator Moderator

  5. Nice catch

    Sent from my Xperia M4 Aqua using Tapatalk
     
  6. @timtower
    You do not even file

    @MGlolenstine
    Now this is the letter "e" bad but I do not know why the problem may be?

    Code:
    Code:
        @EventHandler
        public void onPlayerQuit(PlayerQuitEvent e) {
            Player player = e.getPlayer();
            File file = new File(Plugin.core.getDataFolder(), "spawn.yml");
            YamlConfiguration config = YamlConfiguration.loadConfiguration(file);
    
            config.set("world", player.getLocation().getWorld().getName());
            config.set("x", player.getLocation().getX());
            config.set("y", player.getLocation().getY());
            config.set("z", player.getLocation().getZ());
            config.set("yaw", player.getLocation().getYaw());
            config.set("pitch", player.getLocation().getPitch());
            try {
                config.save(file);
            } catch (IOException /* ERROR "e"*/ e) {
                e.printStackTrace();
            }
        }
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 23, 2017
  7. Its this.saveConfig();

    Sent from my Xperia M4 Aqua using Tapatalk
     
  8. Offline

    timtower Administrator Administrator Moderator

    @ItsBlockFighter Hover your mouse over it, it will tell you that a variable with that name already exists (the event)
     
  9. @timtower
    It's just the problem that I want everyone to have a place on the outside instead of one. How can I do this?
     
    Last edited: Jun 24, 2017
  10. Offline

    Caderape2

  11. @Caderape2
    And how do I know? Because I want all players to have a file and save it. Then I'll load it in the command.
     
  12. Offline

    Caderape2

    @ItsBlockFighter a separate file for each player ? create a file with the playerUUID as name so.
    then you can get the file when a player join or leave with his uuid
     
  13. @Caderape2
    Do it this way? If so then how can I teleport it to the coordinate?

    Code:
    Code:
            String playerName = p.getName();
            File userdata = new File(Bukkit.getServer().getPluginManager().getPlugin("GhostRealms_RPJoin").getDataFolder(), File.separator + "PlayerDatabase");
            File f = new File(userdata, File.separator + playerName + ".yml");
            FileConfiguration playerData = YamlConfiguration.loadConfiguration(f);
    
            if (!f.exists()) {
                try {
    
                    playerData.createSection("world");
                    playerData.set(p.getLocation().getWorld().getName(), 1);
    
                    playerData.createSection("x");
                    playerData.set(String.valueOf(p.getLocation().getX()), 1);
    
                    playerData.createSection("y");
                    playerData.set(String.valueOf(p.getLocation().getY()), 1);
    
                    playerData.createSection("z");
                    playerData.set(String.valueOf(p.getLocation().getZ()), 1);
    
                    playerData.createSection("yaw");
                    playerData.set(String.valueOf(p.getLocation().getYaw()), 1);
    
                    playerData.createSection("pitch");
                    playerData.set(String.valueOf(p.getLocation().getPitch()), 1);
    
                    playerData.save(f);
                } catch (IOException exception) {
    
                    exception.printStackTrace();
                }
            }
        }
    }
    
     
  14. Offline

    Caderape2

    @ItsBlockFighter
    File f = new File(userdata, playerName + ".yml");
    userdata will be the parent folder, you just have to put the file name after the ','

    Bukkit can store location.
    config.set(pathyouwant, loc);
    Location loc = (Location) config.get(pathyouwant);

    You have to create the file if it doesn't exist. file.createNewFile();
     
Thread Status:
Not open for further replies.

Share This Page