[MiniGame] Need help with Region.

Discussion in 'Plugin Development' started by xxmobkiller, Oct 5, 2013.

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

    xxmobkiller

    I am needing help with getting the region from the config and checking to see if a player is in it and if the player is then send them a msg.

    EXAMPLE:
    int p1x = getConfig().getInt("test.p1.x")
    int p1z = getConfig().getInt("test.p1.z")
    int p1y = getConfig().getInt("test.p1.y")

    int p2x = getConfig().getInt("test.p2.x")
    int p2z = getConfig().getInt("test.p2.z")
    int p2y = getConfig().getInt("test.p2.y")

    Thats how my code looks like for right now. I tried doing this. But it spams the users in the region.

    Code:
            for(Player player : Bukkit.getOnlinePlayers()){
    Set<String> config = getConfig().getKeys(false);
            for(String name : config){
     
                int p1y = getConfig().getInt(name+".P1.y");
                int p1z = getConfig().getInt(name+".P1.z");
                int p1x = getConfig().getInt(name+".P1.x");
     
                int p2y = getConfig().getInt(name+".P2.y");
                int p2z = getConfig().getInt(name+".P2.z");
                int p2x = getConfig().getInt(name+".P2.x");
     
                    int incrX = p2x - p1x > 0 ? 0 : 0;
                    int incrY = p2y - p1y > 0 ? 0 : 0;
                    int incrZ = p2z - p1z > 0 ? 0 : 0;
     
                    for (int x = p1x; x < p2x; x += incrX)
                        for (int y = p1y; y < p2y; y += incrY)
                            for (int z = p1z; z < p2z; z += incrZ){
     
                                if(player.getLocation().getX() > x && player.getLocation().getY() > y && player.getLocation().getZ() > z){
                                    player.sendMessage(prefix+"§7You are in the region of: " + name + "");
                                }
            }
     
  2. Offline

    xTrollxDudex

    xxmobkiller
    No dur. You're looping and sending the player a message.
     
  3. Offline

    adam753

    Of course it does. The check you're doing inside the 3 for-loops will be true lots of times for any player inside the region.
    Do this instead:
    Code:java
    1.  
    2. for(Player player : Bukkit.getOnlinePlayers()){
    3. Set<String> config = getConfig().getKeys(false);
    4. for(String name : config){
    5.  
    6. int p1y = getConfig().getInt(name+".P1.y");
    7. int p1z = getConfig().getInt(name+".P1.z");
    8. int p1x = getConfig().getInt(name+".P1.x");
    9.  
    10. int p2y = getConfig().getInt(name+".P2.y");
    11. int p2z = getConfig().getInt(name+".P2.z");
    12. int p2x = getConfig().getInt(name+".P2.x");
    13.  
    14. if(player.getBlockX() >= p1x && player.getBlockX() <=p2x && player.getBlockY() >= p1y && player.getBlockY() <=p2y && player.getBlockZ() >= p1z && player.getBlockZ() <=p2z ) {
    15. player.sendMessage(prefix+"§7You are in the region of: " + name + "");
    16. }
    17. }
    18.  
     
  4. Offline

    xxmobkiller

    Ok. I tried that but its not doing any thing.
     
  5. Offline

    adam753

    Make sure the p1 values in your config are all lower than the p2 values. p1 should be the lowest corner in every axis, and p2 should be the opposite, highest corner.

    Or if you're feeling adventurous, you could write code to order them correctly after reading them from the config, which would remove that caveat.
     
  6. Offline

    xxmobkiller

    Ok. This what my config looks like.
    Test:
    P1:
    z: 7
    x: 23
    y: 55
    P2:
    z: 12
    x: 18
    y: 60

    So yes p1 is lower then p2. And it still not working I am staning the the region for every and the timer is set to 20 is it should be spamming me and there are no lag and i check to see if its startingthe timer and it is. So i don't know what iswrong.
     
  7. Offline

    adam753

    xxmobkiller
    Nope, the X values are the wrong way round.
     
  8. Offline

    xxmobkiller

    Wait what do you mean wrong way round?
     
Thread Status:
Not open for further replies.

Share This Page