Save Locations

Discussion in 'Plugin Development' started by Koningpeter, Jul 5, 2015.

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

    Koningpeter

    I'm making a plugin that if you use a command you claimed a area where nobody can build, this works perfectly but if I reload the server everyone can build in this area. Can anyone help me?
    This my code:
    Code:
    public ArrayList<Location> Array = new ArrayList<Location>();
    Player player = (Player) sender;
    Location pl = player.getLocation();
    if(label.equalsIgnoreCase("Claim")){
            for (int x = 0; x < 45; x++) {
                     for (int y = 0; y < 10; y++) {
                            for (int z = 0; z < 45; z++) {
                                    int tx = pl.getBlockX() -22;
                                    int ty = pl.getBlockY() -1;
                                    int tz = pl.getBlockZ() -22;
                                    Location Loc = new Location(player.getWorld(), tx + x, ty + y, tz + z);
                                    Array.add(Loc);
                            }
                    }
            }
         @EventHandler
         public void onBlockBreak(BlockBreakEvent event){
             Player player = event.getPlayer();
             if(Tower11111.contains(event.getBlock().getLocation())){
                 player.sendMessage(ChatColor.RED + "You can't break blocks here.");
                 event.setCancelled(true);
             }
         }
         @EventHandler
         public void onBlockBreak(BlockPlaceEvent event){
             Player player = event.getPlayer();
             if(Tower11111.contains(event.getBlock().getLocation())){
                 player.sendMessage(ChatColor.RED + "You can't place blocks here.");
                 event.setCancelled(true);
             }
         }
    }
    
     
    1. Java naming conventions
    2. Fields in your plugin are discarded when your plugin is diabled, when your plugin disabled, serialize your list and put it in the config, and when the plugin enables deserialize the stuff.
     
  2. Offline

    Koningpeter

    @megamichiel
    Yes, but how do I that?
    Can you give an example?
     
  3. Offline

    Larsn

  4. @Koningpeter
    In your onDisable method, create a new String list, then iterator over the Tower1111111111111111 field (Why so many 1's?), and for each location create a string where you store in the data separated by a comma or something. (world + "," + location.getBlockX() + "," + y + "," + z); Then in your config call set("somePath", <your string list>); When your plugin enables, get the string list from the config, iterate over it and create a location from it, something like this would do:
    Code:
    //The 'string' variable is an element of the string list
    String[] split = string.split(",");
    World w = Bukkit.getWorld(split[0]);
    int x = Integer.parseInt(split[1]);
    int y = Integer.parseInt(split[2]);
    int z = Integer.parseInt(split[3]);
    Location loc = new Location(x, y, z);
    Then add the location to that list
     
    Koningpeter likes this.
  5. Offline

    Koningpeter

    @megamichiel
    This doesn't work too....
    Code:
    public void onEnable(){
            getLogger().info("has been enabled without errors!");
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            getConfig().options().copyDefaults(true);
            saveDefaultConfig();
            String string = (String) getConfig().get("TowerP");
            String[] split = string.split(",");
            World w = Bukkit.getWorld(split[0]);
            int x = Integer.parseInt(split[1]);
            int y = Integer.parseInt(split[2]);
            int z = Integer.parseInt(split[3]);
            Location loc = new Location(w, x, y, z);
            Tower11111.add(loc);
        }
        public void onDisable(){
            getLogger().info("has been disabled without errors!");
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
    for (int x = 0; x < 45; x++) {
                               for (int y = 0; y < 11; y++) {
                                for (int z = 0; z < 45; z++) {
                                    int tx = pl.getBlockX() -22;
                                    int ty = pl.getBlockY() -2;
                                    int tz = pl.getBlockZ() -22;
                                    Location Tower1111 = new Location(world, tx + x, ty + y, tz + z);
                                     Tower11111.add(Tower1111);
                                     getConfig().set("TowerP", player.getWorld() + ", " + tx + x + ", " + ty + y + ", " + tz + z);
                                     saveConfig();
                                }
                               }
                         }
     
  6. Offline

    WesJD

  7. Offline

    Koningpeter

    @megamichiel
    What do you mean with iterator?
    Sorry I'am noob....
     
  8. @Koningpeter
    Woops I ment iterate there. Basicly you call a 'for' statement, you can look it up on google.
     
  9. Offline

    Koningpeter

    Can you help my?
    I don't know how it works...
    Here's my code:
    Code:
    public ArrayList<Location> Protect = new ArrayList<Location>();
        public void onEnable(){
            getLogger().info("has been enabled without errors!");
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            getConfig().options().copyDefaults(true);
            saveDefaultConfig();
            String string = (String) getConfig().get(".TowerP");
            String[] split = string.split(",");
            int x = Integer.parseInt(split[1]);
            int y = Integer.parseInt(split[2]);
            int z = Integer.parseInt(split[3]);
            Location loc = new Location(Bukkit.getServer().getWorld("world"), x, y, z);
            Protect.add(loc);
        }
        public void onDisable(){
            getLogger().info("has been disabled without errors!");
            getConfig().options().copyDefaults(true);
            Iterator<Location> iterator = Protect.iterator();
            while(iterator.hasNext());
            Location loc1 = iterator.next();
            getConfig().set("Protect", loc1);
        }
     
  10. Offline

    Koningpeter

    @megamichiel
    My server chrast if I use the command, can you explain me why?
    Code:
    List<String> test = new ArrayList<String>();
    if(sender instanceof Player){
                if(label.equalsIgnoreCase("test")){
                    test.add("test1");
                    test.add("test2");
                    test.add("test3");
                    test.add("test4");
                }
            }
            if(sender instanceof Player){
                if (label.equalsIgnoreCase("testset")){
                    Iterator<String> iterator = test.iterator();
                    while(iterator.hasNext());
                        p.sendMessage(iterator.next());
                }
            }
     
    Last edited: Jul 7, 2015
  11. @Koningpeter
    Code:
    while(iterator.hasNext());
    The semicolon ends the statement, that means it won't perform anything, and therefor next() isn't called in the while statement meaning that the loop goes on forever. You need to remove the semicolon
     
    Koningpeter likes this.
  12. Offline

    Koningpeter

    @megamichiel
    It does not work at all, one block is protect...
    How can I fix this?
    Code:
    public void onEnable(){
            getLogger().info("has been enabled without errors!");
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            getConfig().options().copyDefaults(true);
            saveDefaultConfig();
            for(int i=1; i<11; i++){
            String string = getConfig().getString(".Protect.Tower." + i);
            String[] split = string.split(", ");
            int x = Integer.parseInt(split[0]);
            int y = Integer.parseInt(split[1]);
            int z = Integer.parseInt(split[2]);
            for (int tx = 0; tx < 45; tx++) {
                  for (int ty = 0; ty < 11; ty++) {
                       for (int tz = 0; tz < 45; tz++) {
                       Location loc1 = new Location(Bukkit.getServer().getWorld("world"), x + tx, y + ty, z + tz);
                          ProtectNow.add(loc1);
                       return;
                      }
                  }
            }
            }
        }
        @SuppressWarnings("unused")
        public void onDisable(){
            getLogger().info("has been disabled without errors!");
            getConfig().options().copyDefaults(true);
                Iterator<String> iterator = Protect.iterator();
                while(iterator.hasNext())
                for(int i=1; i<11; i++){
                    getConfig().set(".Protect.Tower." + i, iterator.next());
                    saveConfig();
                    return;
                }
        }
        if(label.equalsIgnoreCase("test"{
             Player player = (Player) sender;
             Location pl  =  player.getLocation();
             for (int x = 0; x < 45; x++) {
                               for (int y = 0; y < 11; y++) {
                                for (int z = 0; z < 45; z++) {
                                    int tx = pl.getBlockX() -22;
                                    int ty = pl.getBlockY() -2;
                                    int tz = pl.getBlockZ() -22;
                                     Protect.add(tx + ", " + ty + ", " + tz);
                                    Location Protect = new Location(world, tx + x, ty + y, tz + z);
                                    ProtectNow.add(Protect);
     
Thread Status:
Not open for further replies.

Share This Page