Solved Between 2 locations check bug

Discussion in 'Plugin Development' started by ArthurHoeke, Jun 25, 2015.

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

    ArthurHoeke

    Hi!
    I want to create a system, that checks if a player is between 2 locations that have been set in the config.
    The problem with my code is, is that if the config has multiple locations set it doesn't work.
    Code:
        @EventHandler
        public void Titles(PlayerMoveEvent event){
            final Player p = event.getPlayer();
            for(String regionName : Main.instance.getConfig().getKeys(false)) {
                int x = Main.instance.getConfig().getInt(regionName + ".X");
                int y = Main.instance.getConfig().getInt(regionName + ".Y");
                int z = Main.instance.getConfig().getInt(regionName + ".Z");
                int x2 = Main.instance.getConfig().getInt(regionName + ".X2");
                int y2 = Main.instance.getConfig().getInt(regionName + ".Y2");
                int z2 = Main.instance.getConfig().getInt(regionName + ".Z2");
                if(isInside(p.getLocation(), new Location(p.getWorld(), x, y, z), new Location(p.getWorld(), x2, y2, z2)) == true){
                    p.sendMessage("Inside");
                }else{
                    p.sendMessage("Not inside");
                }
            }
        }
    The not inside message is send all the time. It doesn't matter if the player is in or out of a region, it just sends it.
    If I only have 1 location set, it works good.
    My config:
    Code:
    Economy:
      TravelCosts: 1000
    TestNation:
      X: -186
      Y: 0
      Z: -761
      X2: -197
      Y2: 256
      Z2: -772
      HomeX: -192
      HomeY: 4
      HomeZ: -767
      Players:
      - ArthurDev
      PvP: false
      MobSpawn: false
      Regen: slow
      Effect: jump,haste
      GreetingMessage: '&aWelcome to the nation!'
      ExitMessage: '&aYou''re leaving the nation!'
    Nation2:
      X: -197
      Y: 0
      Z: -754
      X2: -186
      Y2: 256
      Z2: -743
      HomeX: -184
      HomeY: 4
      HomeZ: -743
      PvP: true
      MobSpawn: true
      Regen: medium
      Effect: ''
      GreetingMessage: '&aWelcome to the nation!'
      ExitMessage: '&aYou''re leaving the nation!'
      Players:
      - ArthurDev
    
     
  2. Offline

    NathanWolf

    The way you've written that, it seems like it's going to check all players versus all regions.

    Unless your regions overlap, that's always going to produce a lot of "not inside" messages, and possibly one "inside".
     
  3. Offline

    ArthurHoeke

    So how to fix this issue?
     
  4. Offline

    MCMatters

    @ArthurHoeke are your regions overlapping?

    @ArthurHoeke The way you've written it you have to be in both regions at one time for it not to send "not inside"

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  5. Offline

    ArthurHoeke

    How to make a check per region?
     
  6. Offline

    NathanWolf

    It'd be better to have an "inside region" method that returns true/false.

    Then change your loop to return true instead of printing "Inside", and ignore the "not inside" case.

    All that said, this is going to lag the living heck out of your server. You will need to do a *lot* of optimizing- starting with ignoring PlayerMoveEvent's where the player hasn't changed blocks, at the very least.
     
  7. Offline

    ArthurHoeke

    I understand, but I don't get it how to check if the player isn't in a region. Really need that working, if I got that I can work on the system so it will not lagg
     
  8. Offline

    NathanWolf

    Ok- well then do what I said with one very minor change.

    Instead of printing "Inside", return false.
     
  9. Offline

    ArthurHoeke

    Right now I have this:
    Code:
        @EventHandler
        public void onDeathzone(PlayerMoveEvent event){
            final Player p = event.getPlayer();
            for(String regionName : Main.instance.getConfig().getKeys(false)) {
                if(isInside(p.getLocation(), new Location(p.getWorld(), Main.instance.getConfig().getInt(regionName + ".X"), Main.instance.getConfig().getInt(regionName + ".Y"), Main.instance.getConfig().getInt(regionName + ".Z")), new Location(p.getWorld(), Main.instance.getConfig().getInt(regionName + ".X2"), Main.instance.getConfig().getInt(regionName + ".Y2"), Main.instance.getConfig().getInt(regionName + ".Z2")))){
                    return;
                }else{
                    p.sendMessage("Outside");
                }
            }
        }
    But that still gives the Outside message all the time no matter if I'm in or out a zone.
     
  10. Offline

    MCMatters

    @ArthurHoeke
    Code:
        @EventHandler
        public void onDeathzone(PlayerMoveEvent event){
            final Player p = event.getPlayer();
            for(String regionName : Main.instance.getConfig().getKeys(false)) {
                if(isInside(p.getLocation(), new Location(p.getWorld(), Main.instance.getConfig().getInt(regionName + ".X"), Main.instance.getConfig().getInt(regionName + ".Y"), Main.instance.getConfig().getInt(regionName + ".Z")), new Location(p.getWorld(), Main.instance.getConfig().getInt(regionName + ".X2"), Main.instance.getConfig().getInt(regionName + ".Y2"), Main.instance.getConfig().getInt(regionName + ".Z2")))){
                    return;
                   }
                   p.sendMessage("Outside");
      
            }
        }
     
  11. Offline

    ArthurHoeke

    Doesn't work for some reason, still get the Outside message all the time
     
  12. Offline

    MCMatters

    Code:
        @EventHandler
        public void onDeathzone(PlayerMoveEvent event){
            final Player p = event.getPlayer();
              boolean o =  true;
            for(String regionName : Main.instance.getConfig().getKeys(false)) {
                if(isInside(p.getLocation(), new Location(p.getWorld(), Main.instance.getConfig().getInt(regionName + ".X"), Main.instance.getConfig().getInt(regionName + ".Y"), Main.instance.getConfig().getInt(regionName + ".Z")), new Location(p.getWorld(), Main.instance.getConfig().getInt(regionName + ".X2"), Main.instance.getConfig().getInt(regionName + ".Y2"), Main.instance.getConfig().getInt(regionName + ".Z2")))){
                o = false;
                }
            }
           if(o){
            p.sendMessage("Outside");
           }
        }
    @ArthurHoeke

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  13. Offline

    ArthurHoeke

    Works! Thanks ALOT!
     
Thread Status:
Not open for further replies.

Share This Page