Check if player is between two locations (Cuboid)

Discussion in 'Spigot Plugin Development' started by Snowmite, Jun 17, 2018.

  1. How would I check if a player is between two locations?

    My code currently:
    Code (Text):
    Code (Text):
        public static boolean isInRegion(Player player, Location loc1, Location loc2)
        {
            boolean yes = false;
            List<Block> blocks = new ArrayList<>();
            int topBlockX = (loc1.getBlockX() < loc2.getBlockX() ? loc2.getBlockX() : loc1.getBlockX());
            int bottomBlockX = (loc1.getBlockX() > loc2.getBlockX() ? loc2.getBlockX() : loc1.getBlockX());

            int topBlockY = (loc1.getBlockY() < loc2.getBlockY() ? loc2.getBlockY() : loc1.getBlockY());
            int bottomBlockY = (loc1.getBlockY() > loc2.getBlockY() ? loc2.getBlockY() : loc1.getBlockY());

            int topBlockZ = (loc1.getBlockZ() < loc2.getBlockZ() ? loc2.getBlockZ() : loc1.getBlockZ());
            int bottomBlockZ = (loc1.getBlockZ() > loc2.getBlockZ() ? loc2.getBlockZ() : loc1.getBlockZ());

            for(int x = bottomBlockX; x <= topBlockX; x++)
            {
                for(int z = bottomBlockZ; z <= topBlockZ; z++)
                {
                    for(int y = bottomBlockY; y <= topBlockY; y++)
                    {
                        if(player.getLocation().equals(new Location(player.getWorld(), x, y, z))){
                            yes = true;
                        }
                    }
                }
            }
            return yes;
        }
    Event code:
    Code (Text):
    Code (Text):
        @EventHandler
        public void onDeath(EntityDamageByEntityEvent event){
            if(event.getDamager() instanceof Player && event.getEntity() instanceof Player){
                Player pl = (Player) event.getEntity();
                Player da = (Player) event.getDamager();

                if(PitArenaManager.getArena(pl)!=null && PitArenaManager.getArena(pl).getPlayers().contains(da)){
                    PitArena arena = PitArenaManager.getArena(pl);
                    PitPlayer pitPlayer = PlayerManager.getPlayer(pl.getUniqueId());
                    PitPlayer damager = PlayerManager.getPlayer(da.getUniqueId());
                    if(LocationUtils.isInRegion(pl, arena.getLobbyPosition1(), arena.getLobbyPosition2())&&
                            LocationUtils.isInRegion(da, arena.getLobbyPosition1(), arena.getLobbyPosition2())){
                        Bukkit.broadcastMessage("hey");
                        event.setCancelled(true);
                        TitleAPI.sendFullTitle(da, 0, 60, 0, "", ThePit.getMessage("regionDamage"));
                    }
                }
            }
        }
    But it isn't working
     
  2. Just do something where it compares the player's x position to see if it is inbetween loc1.x and loc2.x, and repeat it for the y and z values. That should work.
     
  3. JanTuck

    Supporter

    So I suck at math, but this worked for me.
    Code (Java):
        public boolean inCuboid(Location origin, Location l1, Location l2){
            return new IntRange(l1.getX(), l2.getX()).containsDouble(origin.getX())
                    && new IntRange(l1.getY(), l2.getY()).containsDouble(origin.getY())
                    &&  new IntRange(l1.getZ(), l2.getZ()).containsDouble(origin.getZ());
        }
     
    • Informative Informative x 1
  4. Never heard of IntRange
    EDIT: seems to come from external library of some sort.
     
  5. JanTuck

    Supporter

    • Like Like x 1