Getting an Item Frame from a Location

Discussion in 'Plugin Development' started by Cryptite, Jun 28, 2014.

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

    Cryptite

    I have an item frame whose location I 'should' know. Given that ItemFrames are entities, I'm forced to search through the chunk to retrieve the Item Frame. However, the item frame's location does not appear to be where it visibly is.

    Code:
    Code:java
    1. for (Entity e : location.getChunk().getEntities()) {
    2. if (e instanceof CraftItemFrame && e.getLocation().equals(location)) {
    3. itemFrame = (ItemFrame) e;
    4. }
    5. }


    Presuming item frames work like, say, Torches, I should be able to provide said location and have the item frame properly found with the above code. However, upon printing the location of the entity(itemframe), it reports to be off by 1X and 1Z... In this case, the repoted location of the itemframe is, in my case, a plank of wood... Why is this, and how can I more... accurately... retrieve an Item Frame given a location (that it should be at, anyway...)
     
  2. Offline

    Gerov

    So.... Are you trying to do it from an event, command, or just in your onEnable() method?
     
  3. Offline

    RingOfStorms

    You can't use equals between Location objects like that as they will rarely ever return true. Note that locations also contain a yaw and pitch on them and a couple of other things. I would make a function to compare the x,y,z only, or do something like getBlock().equals(getBlock())

    This is what I use:
    Code:java
    1.  
    2. public static boolean locBlockSame (Location l1, Location l2) {
    3. return l1.getBlockX() == l2.getBlockX() &&
    4. l1.getBlockY() == l2.getBlockY() &&
    5. l1.getBlockZ() == l2.getBlockZ();
    6. }
    7.  
     
  4. Offline

    NathanWolf

    I believe they report the location of the block they're attached to, not where it seems like they really are... Annoyingly enough.
     
  5. Offline

    Cryptite

    Simply through a function called during a load that is called at the end of an onEnable().
    It's not reporting the block it's even attached to either, it's reporting a block that's diagonal from it.
     
  6. Offline

    Gerov

    As NathanWolf said, I don't think they tell the server their exact location.
     
  7. Offline

    Cryptite

    Suppose I can just check via distance which one is closest to my location and use that.
     
Thread Status:
Not open for further replies.

Share This Page