Advanced Peripherals

Advanced Peripherals

29M Downloads

Player Detector getPlayersInCoords dont work correctly (for me)

jajajafeti opened this issue · 7 comments

commented

Descripe

I tried to use playerDetector.getPlayersInCoords(c1, c2) and it always returns an empty table (not nil).
So I switched the Coordiantes and it worked. Therefore I conclude that the first Argument c1 must be the Top corner of the cuboid. I think it should work regardless of the order of the Arguments.

edit:
the values {x,z} of c1 must be bigger or equal to c2 or it doesnt work...
Uh oh and for the y value its kinda wired it just uses the y value from c2 as the start and ends at 190 which is the hight of the clouds. I could not see how c1.y changed anything. ... edit again... it is also the same for x, y it starts to check from c2 but it dont stops at c1. It is realy wired...

Steps to reproduce

I used this Code to make sure I am in the coords but it only works when u switch c1 and c2.

local pD = peripheral.find("playerDetector")
local c1 = {x=-937, y=62 , z=422}
local c2 = {x=-930, y=68, z=417}
local tPlayers = {test = 1}
local tPlayerC
 
while true do
 tPlayerC = pD.getPlayerPos("jajajafeti")
 c1 = {x = tPlayerC.x-2,
       y = tPlayerC.y-2,
       z = tPlayerC.z-2}
       
 c2 = {x = tPlayerC.x+2,
       y = tPlayerC.y+2,
       z = tPlayerC.z+2}
 
 tPlayers = {test = " text"}
 tPlayers = pD.getPlayersInCoords(c1, c2)
 print(tPlayers) 
 for k, v in pairs(tPlayers) do
   print(k,v)
   print("debugg") 
 end 
 
-- cB.sendMessage()
 sleep(2)
 
end

Multiplayer?

Yes

Version

1.18.1-0.7.9r (Latest 1.18)

Minecraft, Forge and maybe other related mods versions

forge 39.0.88 with advancedperipherals-1.18.1-0.7.12b

Screenshots or Videos

No response

Crashlog/log

No response

commented

I can confirm this function is most likely not working correctly.

Turning around B,A or A,B either results in

a) not showing anything
b) giving players position outside the grid too.
Example:
A={x=37007,y=240,z=-17729,dimension="minecraft:overworld"}
B={x=36912,y=0,z=-17824,dimension="minecraft:overworld"}

Player detected at position {x=64778,y=6,z=-17082,dimension="minecraft:overworld"} which is completely out of the bounds of this cube. At the same time this was able to track myself inside that cube under b).

Example code: https://pastebin.com/MNZ8ezZj

commented

@jajajafeti
This code is probably what we expect, no?
Works for me at least.

function isPlayerInCoords(A, B, player)
  per = peripheral.find("playerDetector")
  cube = {}
  if A.x > B.x then
    cube.minx = B.x cube.maxx = A.x
  elseif A.x < B.x then
    cube.minx = A.x cube.maxx = B.x
  else -- identical
    cube.minx = A.x cube.maxx = A.x
  end
  
  if A.y > B.y then
    cube.miny = B.y cube.maxy = A.y
  elseif A.y < B.y then
    cube.miny = A.y cube.maxy = B.y
  else -- they are the same
    cube.miny = A.y cube.maxy = A.y
  end
   
  if A.z > B.z then
    cube.minz = B.z cube.maxz = A.z
  elseif A.z < B.z then
    cube.minz = A.z cube.maxz = B.z
  else
    cube.minz = A.z cube.maxz = A.z
  end
  
  playerpos = per.getPlayerPos(player)
  
  if not A.dimension == B.dimension then error("Dimension not matching") 
  else cube.dimension = A.dimension end

  if not playerpos.dimension == cube.dimension then
    return false
  end
  
  if playerpos.x <= cube.maxx
  and playerpos.x >= cube.minx
  and playerpos.y <= cube.maxy
  and playerpos.y >= cube.miny
  and playerpos.z <= cube.maxz
  and playerpos.z >= cube.minz then
    return true
  end
  
  return false
end
commented

@0rbiter
Yes that looks good

Here you can find the Code for the original function.

 public final List<String> getPlayersInCoords(Map<?, ?> firstCoord, Map<?, ?> secondCoord) throws LuaException {
        List<String> playersName = new ArrayList<>();
        BlockPos firstPos = LuaConverter.convertToBlockPos(firstCoord);
        BlockPos secondPos = LuaConverter.convertToBlockPos(secondCoord);
        for (ServerPlayer player : getPlayers()) {
            if (CoordUtil.isInRange(player, getLevel(), firstPos, secondPos))
                playersName.add(player.getName().getString());
        }
        return playersName;
    }

I investigated some more and found the mistake in

    public static boolean isInRange(Player player, Level world, BlockPos firstPos, BlockPos secondPos) {
        int xOffset = Math.min(Math.abs(firstPos.getX())+Math.abs(secondPos.getX()), APConfig.PERIPHERALS_CONFIG.PLAYER_DET_MAX_RANGE.get());
        int yOffset = Math.min(Math.abs(firstPos.getY())+Math.abs(secondPos.getY()), APConfig.PERIPHERALS_CONFIG.PLAYER_DET_MAX_RANGE.get());
        int zOffset = Math.min(Math.abs(firstPos.getZ())+Math.abs(secondPos.getZ()), APConfig.PERIPHERALS_CONFIG.PLAYER_DET_MAX_RANGE.get());

        return world.getNearbyPlayers(TargetingConditions.forNonCombat(),
                null, new AABB(firstPos.offset(xOffset, yOffset, zOffset), secondPos)).contains(player);
    }

}

you can ignore Math.min right now and just focus on the addition of the absolute values of the postions. These offset values need to be the differenc of the postions and not the summ which explains why firstPos and therefore the "Box" gets huge in all directions.

commented

LoL true

My solution would be to replace this
Math.abs(firstPos.getX())+Math.abs(secondPos.getX())
with
Math.abs(firstPos.getX()-secondPos.getX())
for every axis (obv.)
Then define the needed reference corner (not familiar with js, but maybe like that?)
int xValue = firstPos.getX() < secondPos.getX() ? firstPos.getX() : secondPos.getX()
for every axis (obv. again)
This will give us the corner of the cuboid which has the smallest coordinates.
Then you can add the offsets to get the second Corner with the biggest coords.
I think that is what it needs to function properly. Not a Pro dough.

commented

I don't understand the logic though.

firstPos.offset(xOffset, yOffset, zOffset), secondPos)).contains(player);

What does this achieve even if the offset is the difference between pos1/pos2?
It would just move pos1->pos2 and reduce the cube to zero - no?

commented

LoL true

My solution would be to replace this Math.abs(firstPos.getX())+Math.abs(secondPos.getX()) with Math.abs(firstPos.getX()-secondPos.getX()) for every axis (obv.) Then define the needed reference corner (not familiar with js, but maybe like that?) int xValue = firstPos.getX() < secondPos.getX() ? firstPos.getX() : secondPos.getX() for every axis (obv. again) This will give us the corner of the cuboid which has the smallest coordinates. Then you can add the offsets to get the second Corner with the biggest coords. I think that is what it needs to function properly. Not a Pro dough.

Thank you both for your hints 😉
But there was a bit simpler way

commented

Fixed for 1.16 in 0.7.8r
Will be fixed soon for 1.18