Solved Chunks

Discussion in 'Plugin Development' started by Coopah, Jan 14, 2015.

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

    Coopah

    Okay, so I've got a method where when a player is in a Clan and they type /c claim it will get the chunk they're in and add the world, x and z of the chunk to the clans claims.

    This works fine and what not but I'm stuck on how to check if a player is in a claimed chunk? I'm completely puzzled.

    Code so far:
    Code:
    Chunk chunk = p.getLocation().getChunk();
    
            if (plugin.getConfig().contains(cName)) {
    
                if (plugin.getConfig().getStringList(cName + ".Admins").contains(p.getName()) ||
                        plugin.getConfig().getString(cName + ".Leader").contains(p.getName())) {
    
                    if (plugin.getConfig().getInt(cName + ".Claims") == plugin.getConfig().getInt(cName + ".ClaimsAmount")) {
    
                        p.sendMessage(StringList.pref() + "You have too much land. Unclaim some to claim this chunk.");
    
                    } else {
    
                        if (plugin.getConfig().get(cName + ".ChunkClaim1") == "") {
    
                            plugin.getConfig().set(cName + ".ChunkClaim1" + ".World", chunk.getWorld().getName());
                            plugin.getConfig().set(cName + ".ChunkClaim1" + ".X", chunk.getX());
                            plugin.getConfig().set(cName + ".ChunkClaim1" + ".Z", chunk.getZ());
                            plugin.getConfig().set(cName + ".Claims", plugin.getConfig().getInt(cName + ".Claims") + 1);
                            plugin.saveConfig();
    
                            for (Player players : Bukkit.getOnlinePlayers()) {
    
                                if (plugin.getConfig().getStringList(cName + ".Members").contains(players.getName()) ||
                                        plugin.getConfig().getStringList(cName + ".Moderators").contains(players.getName()) ||
                                        plugin.getConfig().getStringList(cName + ".Admins").contains(players.getName()) ||
                                        plugin.getConfig().getString(cName + ".Leader").contains(players.getName())) {
    
                                    players.sendMessage(StringList.pref() + ChatColor.BLUE + p.getName() + ChatColor.YELLOW + " has claimed some land.");
                                    players.playSound(players.getLocation(), Sound.NOTE_BASS, 1.0F, 1.0F);
    
                                }
                            }
    So, basically how do I now check if a player is in this chunk that has been claimed?
     
  2. Offline

    dsouzamatt

    @Coopah Compare the world, x and z of the chunk that the player is on with the world, x and z of each of their clan's claimed chunks. If all three match, then they must be standing on a claimed chunk.
     
  3. Offline

    Coopah

    @dsouzamatt
    Okay, but how would I check if it's not their claimed land? Since there will be more claims from different clans.
     
  4. Offline

    dsouzamatt

    @Coopah Check against the claimed chunks of all clans.
     
  5. Offline

    Coopah

    @dsouzamatt
    Okay so how would I store them if I'm going to check all of them?
     
  6. Offline

    dsouzamatt

    @Coopah Something like this:

    Code:
    chunks:
      clan1:
        - world/23/15
        - world/19/10
      clan2:
        - world/-3/4
        - world/3/12
        - world/5/-10
    /\ I'd combine the world's name and chunk coordinates into a single string for cleaner storage, then separate them later.

    For each clan, you'd get the list of all of their claimed chunks and compare each of them with the chunk that the player is standing on.
     
  7. Offline

    Coopah

    @dsouzamatt
    Oh okay thank you. So how would I combine them for cleaner storage?
     
  8. Offline

    dsouzamatt

    @Coopah Just concatenate them, with slashes in between each variable.

    You can then use String.Split to separate the string back into an array, and from there get your world from it's name, and parse the two ints for the x and y coordinates.
     
  9. Offline

    Coopah

    @dsouzamatt
    Okay, so I've made progress.
    I've saved them by doing:
    Code:
     List<String> ClansClaims = plugin.getConfig().getStringList("Claims." + cName);
    
    Code:
     ClansClaims.add(chunk.getWorld().getName() + "/" + chunk.getX()  + "/" + chunk.getZ());                        plugin.getConfig().set("Claims." + cName, ClansClaims);
    So, I'm now just really stuck on checking them.
    Could you give me a example or a code snippet or something of how to check if a player is in one of the chunks on a PlayerMoveEvent? I'm not very good with parsing and replacing.
     
  10. Offline

    dsouzamatt

    @Coopah To split the string, you would do this:
    Code:
    String[] chunkProperties = chunk.split("/");
    ...where "chunk" is the string of the chunk's properties.

    This returns a string array with three arguments; the world, x coordinate and y coordinate, as strings.

    To get the world from this array, use:
    Code:
    World world = Bukkit.getServer().getWorld(chunkProperties[0]);
    ...then compare this to the player's world.

    To parse each of the x and y coordinates into integers:
    Code:
    int x = Integer.parseInt(chunkProperties[1]);
    int y = Integer.parseInt(chunkProperties[2]);
    ...then compare each of these to the player's x and y coordinates.
     
  11. Offline

    Coopah

    @dsouzamatt
    Oh okay, thank you so much :)
    So if I had like clan names then in there it has the claimed chunks how would I check each clan? I mean... how would I check for all the chunks? Because they will be in different clans?
    So it would be like:
    Claims:
    Clan1:
    Claim1:world,x,y
    Claim2:world,x,y
    Clan2
    Claim1:world,x,y
    Claim2:world,x,y
    Claim3:world,x,y
    and so on.
     
  12. Offline

    dsouzamatt

    @Coopah You can now use:
    Code:
    Set<String> clans = plugin.getConfig().getConfigurationSection("Claims").getKeys(false);
    ...described here and here, to get a list of all the clans under "Claims" in the config.

    Iterate over this list. For each clan, retrieve their claims (in a similar way as you did above), iterate over them and compare the world, x and y.
     
    Coopah likes this.
  13. Offline

    Coopah

    @dsouzamatt
    Thank you :) I will try this a bit later on.
     
    dsouzamatt likes this.
  14. Offline

    Coopah

    @dsouzamatt
    So you said to do chunk.split but chunk.split isn't a method for:
    Chunk chunk = p.getLocation().getChunk();
     
  15. Offline

    dsouzamatt

    "chunk" is the variable of the string that you created with the world, x and y coordinates. of the chunk

    The .split method splits a string at a given character into an array.
     
  16. Offline

    Coopah

    @dsouzamatt
    Oh okay, so am I getting the world, x and y of the chunk the player is standing in or the one from the config?
    What I have so far:
    Code:
        @EventHandler
        public void onPlayerMove(PlayerMoveEvent e) {
           
            Player p = e.getPlayer();
           
            Chunk chunk = p.getLocation().getChunk();
            Set<String> clans = plugin.getConfig().getConfigurationSection("Claims").getKeys(false);
            String chunk2 = chunk.getWorld().getName() + "/" + chunk.getX() + "/" + chunk.getZ();
            chunk2.split("/");
           
            if (clans.contains(chunk2)) {
               
                p.sendMessage("Okay good m8");
               
            } else {
               
                p.sendMessage("lolno");
           
            }
           
        }
    This is saying that the string is out of bounds of the set<String> clans list.
     
  17. Offline

    dsouzamatt

    @Coopah The string set that you have only contains the names of the clans.

    You need to iterate through that set, using the clan name in the path, to get each of the claim locations.
     
    Coopah likes this.
  18. Offline

    Coopah

    @dsouzamatt
    Oh I completely forgot about that!
    So I've got this:
    Code:
        @EventHandler
        public void onPlayerMove(PlayerMoveEvent e) {
           
            Player p = e.getPlayer();
           
            Chunk chunk = p.getLocation().getChunk();
            Set<String> clans = plugin.getConfig().getConfigurationSection("Claims").getKeys(false);
            String chunk2 = chunk.getWorld().getName() + "/" + chunk.getX() + "/" + chunk.getZ();
            chunk2.split("/");
           
            for (String s : clans) {
               
                if (s.contains(chunk2)) {
                   
                    p.sendMessage("Okay good m8");
                   
                } else {
                   
                    p.sendMessage("lolno");
               
                }   
               
            }
           
        }
    However, it always seems to go to "lolno". So I assume chunk2 is wrong?
     
  19. Offline

    dsouzamatt

    @Coopah You're using the wrong string set to check against.

    Your config is structured:
    Code:
    Claims:
      clan1:
        claim1: world/1/2
    This line:
    Code:
    Set<String> clans = plugin.getConfig().getConfigurationSection("Claims").getKeys(false);
    ...gives you a list of the clans. You then need to:

    Iterate over it and for each clan, get a string set of their claims
    Code:
    Set<String> clanClaims = plugin.getConfig().getConfigurationSection("Claims" + clanName).getKeys(false);
    ...then iterate over that to get each string then compare
    Code:
    String chunkProperties = plugin.getConfig().getString("Claims" + clanName + "." + claim);
     
    Coopah likes this.
  20. Offline

    Coopah

  21. Offline

    dsouzamatt

    A string of the set named "clans" which you are iterating over; the name of one of the clans.
     
    Coopah likes this.
Thread Status:
Not open for further replies.

Share This Page