Solved Listener To See if a Player is in a Region Won't Work

Discussion in 'Plugin Help/Development/Requests' started by TheRCPanda, Mar 5, 2015.

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

    TheRCPanda

    So I am trying to make a plugin and in order for this plugin to work I need to make it so a player can place anywhere within a region, but if they try and place outside the region it won't let them, kind of like OCTC. I have a custom data type coded in and such but I am going to show you the parts that I think are relevant:

    Code:
    public class BlockListener implements Listener {
    
        public void onBlockPlace(BlockPlaceEvent event) {
    
            Player player = event.getPlayer();
            Block block = event.getBlock();
    
            Region rg = getRegionHere(block.getLocation());
    
            if (rg == null && !player.hasPermission("map.edit")) {
                player.sendMessage(CMGCore.TAG + "§4You do not have permission to break here!");
            }
    
        }
        public void onBlockBreak(BlockBreakEvent event) {
    
            Player player = event.getPlayer();
            Block block = event.getBlock();
    
            Region rg = getRegionHere(block.getLocation());
    
            if (rg == null && !player.hasPermission("map.edit")) {
                player.sendMessage(CMGCore.TAG + "§4You do not have permission to break here!");
            }
    
        }
    
    
        private static Region getRegionHere(Location loc) {
    
            List<Region> regionList = CMGCore.getRegions();
    
            for(Region rg : regionList) {
    
                Location p1 = rg.getP1();
                Location p2 = rg.getP2();
    
                int minX = p1.getBlockX() < p2.getBlockX() ? p1.getBlockX() : p2.getBlockX();
                int minY = p1.getBlockY() < p2.getBlockY() ? p1.getBlockY() : p2.getBlockY();
                int minZ = p1.getBlockZ() < p2.getBlockZ() ? p1.getBlockZ() : p2.getBlockZ();
    
                int maxX = p1.getBlockX() > p2.getBlockX() ? p1.getBlockX() : p2.getBlockX();
                int maxY = p1.getBlockY() > p2.getBlockY() ? p1.getBlockY() : p2.getBlockY();
                int maxZ = p1.getBlockZ() > p2.getBlockZ() ? p1.getBlockZ() : p2.getBlockZ();
    
                if(loc.getBlockX() >= minX && loc.getBlockX() <= maxX) {
    
                    if(loc.getBlockY() >= minY && loc.getBlockY() <= maxY) {
    
                        if(loc.getBlockZ() >= minZ && loc.getBlockZ() <= maxZ) {
    
                            return rg;
    
                        }
    
                    }
    
                }
    
    
            }
            return null;
        }
    
    }
    
    The code above is the listener to see if they placed outside of the region. And yes, it is registered in the main class.

    My problem is that the "If" statement will not work, I've tested when I'm opped and when I'm not etc, it just won't stop a player from placing outside the region. Help is appreciated, thanks :)

    P.S. If you need any other bit of code I can share it :p
     
    Last edited: Mar 5, 2015
  2. Offline

    timtower Moderator Moderator

  3. Offline

    TheRCPanda

    Yeah, just noticed that. Out of all my looking, that is the thing I forgot, that is just embarrassing, *facepalm*
     
  4. Offline

    timtower Moderator Moderator

    Making mistakes makes us humans ;) Please mark the thread as solved, top right corner, thread tools, edit thread.
     
Thread Status:
Not open for further replies.

Share This Page