Solved Need help with saving multiple locations in config file

Discussion in 'Plugin Development' started by Urag, Jul 15, 2017.

Thread Status:
Not open for further replies.
  1. I need to save multiple locations in config file and then i want to check if specific block is in that list
    Any help needed :)
     
  2. Offline

    Machine Maker

    @Urag you can find some good location serializers if you just google location serializer, but you should just save the world uuid and the block X, Y, and Z to the config of you don't want to use a serializer.
     
    Urag likes this.
  3. Code:
    public class Main extends JavaPlugin implements Listener {
       
        @Override
        public void onEnable() {
            registerConfig();
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            System.out.println("[TARTAK] Plugin dziala :)");
        }
       
        @Override
        public void onDisable() {
            System.out.println("[TARTAK] Plugin zostal wylaczony.");
        }
       
        private void registerConfig(){
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
       
       
       
        public List<String> locations = getConfig().getStringList("locations");
       
        public static String getLiteStringFromLocation(Location loc) { 
            if (loc == null) {
            return "";
            }
            return loc.getWorld().getName() + ":" + loc.getBlockX() + ":" + loc.getBlockY() + ":" + loc.getBlockZ() ; 
           }
       
        public static Location getLiteLocationFromString(String s) {
              if (s == null || s.trim() == "") {
              return null;
              }
              final String[] parts = s.split(":");
              if (parts.length == 4) {
              World w = Bukkit.getServer().getWorld(parts[0]);
              double x = Double.parseDouble(parts[1]);
              double y = Double.parseDouble(parts[2]);
              double z = Double.parseDouble(parts[3]);
              return new Location(w, x, y, z);
              }
              return null;
              }
    
       
        @EventHandler
        public void OnBlockPlace(BlockPlaceEvent e) {
            if (e.getPlayer().getName().equalsIgnoreCase("uzurpator")){
                locations.add(getLiteStringFromLocation(e.getBlock().getLocation()));
                saveConfig();
                reloadConfig();
                e.getPlayer().sendMessage("lol");
            }
        }
       
        @EventHandler
        public void OnBlockBreakEvent(BlockBreakEvent event) {
            if (){
                final BlockState b = event.getBlock().getState();
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
                    public void run(){
                        event.getPlayer().sendMessage("lol");
                        b.update(true);
                    }
                },15);
            }
        }
    } 
    I have found location serialier and i think i made something wrong
    An idea in my code is to save placed block location to config and on break check if that block is in config list.
    Could you help me and explain like to noob ? :D
     
  4. Offline

    Machine Maker

    @Urag so you are adding the location to the list of location objects, but you must set the config to that list. After you add the location, add this line.
    Code:
    getConfig().set("locations", locations);
    Then you save the config.
     
    Urag likes this.
  5. Offline

    Horsey

    Location implements ConfigurationSerializable, so you can just create a set of locations, and use getConfig().set("path", YourListOfLocations);

    And get the list by casting getConfig().get("path"); to Set<Location>
     
  6. @Horsey
    @X1machinemaker1X

    I have made:

    Code:
    List<String> locations = getConfig().getStringList("locations");
    locations.add(e.getBlock().getLocation().toString());
    getConfig().set("locations", locations);
    And if:
    Code:
    if (locations.contains(event.getBlock().getLocation().toString()))
    And it works
    Is there anything to improve?
     
  7. Offline

    Horsey

    I guess that works but I don't see why you dont just use a list of Locations.
     
    Urag likes this.
  8. How would that look?
    Could you show me all the code?
     
  9. Offline

    Zombie_Striker

    @Urag
    Just take your code, replace "<String>" with "<Location>", and remove the "toString"
     
    Urag likes this.
  10. @Horsey

    So what should I now get from config?
    public List<Location> locations = getConfig().get???("locations");
     
  11. Offline

    Zombie_Striker

    @Urag
    Just use the .get method and cast it to a List of locations.
     
    Urag likes this.
  12. @Zombie_Striker
    how? ;_;
    Code:
    public List<Location> locations = (List<Location>) getConfig().get("locations");
    locations.add(e.getBlock().getLocation()
    makes error on placement
     
  13. Offline

    Machine Maker

    @Urag whats on line 82 of your Main class?

    I'm guessing the code is throwing a NPE because the list is null for some reason.
     
  14. @X1machinemaker1X
    Line 82 is locations.add(e.getBlock().getLocation());

    And break event:
    Code:
        @EventHandler
        public void OnBlockBreakEvent(BlockBreakEvent event) {
            if (locations.contains(event.getBlock().getLocation())){
                final BlockState b = event.getBlock().getState();
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
                    public void run(){
                        b.update(true);
                    }
                },15);
            }
        }
    Makes:
    line 92 is:
    Code:
    if (locations.contains(event.getBlock().getLocation())){
     
  15. Offline

    Machine Maker

    @Urag yeah ok, so the problem is that the locations variable is null. Where are you setting the location variable?
     
    Urag likes this.
  16. @X1machinemaker1X

    Nowhere, earlier with string and toString it worked :p
    Code:
    public class Main extends JavaPlugin implements Listener {
       
        @Override
        public void onEnable() {
            registerConfig();
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            System.out.println("[Tartak] Plugin dziala :)");
        }
       
        @Override
        public void onDisable() {
            System.out.println("[Tartak] Plugin zostal wylaczony.");
        }
       
        private void registerConfig(){
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
       
        public boolean tartak = false;
       
        public List<Location> locations = (List<Location>) getConfig().get("locations");
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("tartak")) { // If the player typed /basic then do the following, note: If you only registered this executor for one command, you don't need this
                if(args.length==0){
                    if(tartak==true){
                        tartak=false;
                        sender.sendMessage("[Tartak] Opcja stawiania nieskonczonych bloków wylaczona");
                        return true;
                    }
                    else{
                        tartak=true;
                        sender.sendMessage("[Tartak] Opcja stawiania nieskonczonych bloków aktywowana");
                        return true;
                    }
                }
                if (args.length==1 && args[0].equalsIgnoreCase("reset")){
                    getConfig().set("locations", null);;
                    locations.clear();
                    saveConfig();
                    reloadConfig();
                    sender.sendMessage("[Tartak] Wyczyszczono lokalizacje blokow");
                    return true;
                }
                if (args.length>1 || !args[0].equalsIgnoreCase("reset")){
                    sender.sendMessage("[Tartak] Poprawne uzycie: /tartak [reset]");
                    return true;
                }
            } //If this has happened the function will return true.
                // If this hasn't happened the value of false will be returned.
            return false;
        }
       
        @EventHandler
        public void OnBlockPlace(BlockPlaceEvent e) {
            if (tartak==true){
                if (e.getPlayer().getName().equalsIgnoreCase("Firenvis") || e.getPlayer().getName().equalsIgnoreCase("Uzurpator")){
                    locations.add(e.getBlock().getLocation());
                    getConfig().set("locations", locations);
                    saveConfig();
                    reloadConfig();
                }
            }
        }
       
        @EventHandler
        public void OnBlockBreakEvent(BlockBreakEvent event) {
            if (locations.contains(event.getBlock().getLocation())){
                final BlockState b = event.getBlock().getState();
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
                    public void run(){
                        b.update(true);
                    }
                },15);
            }
        }
    } 
     
  17. Offline

    Machine Maker

    @Urag you need to put
    Code:
    public List<Location> locations;
    before your onEnable() method and put
    Code:
    locations = (List<Location>) getConfig().get("locations")
    in your onEnable() method.
     
    Urag likes this.
  18. Works, thanks :)
     
  19. Offline

    Machine Maker

    Urag likes this.
Thread Status:
Not open for further replies.

Share This Page