Check is entity at (x,y,z) is an item frame.

Discussion in 'Plugin Development' started by Nic2555, Jul 2, 2015.

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

    Nic2555

    Hi, the title basicly says it. I'm looking for a way to find out if the block or the entity or the items at the position x, y, z, is a item frame. Then get the item or the block in it.
    Thanks !

    * Did a misstake in my title. i'm sorry *
     
  2. @Nic2555 Get the block or entity at the coordinates, check if it's an item frame, cast the entity/block to an item frame. I'm not sure if there is a method that can return the itemstack in the item frame, though. I haven't dealt with item frames yet, so I wouldn't know. I'll do some research on it, though. ;)

    EDIT: Looked at the docs, there is a method that returns the itemstack. getItem(). Check out the ItemFrame document for more info.
     
  3. Offline

    BurnyDaKath

    It is easy. You loop through all entities and check if they have exact x, y, z and entity type.

    Code:
    World world = player.getWorld(); //Or put world you need to check entities here.
    double x = Math.round(***); //Round makes coord look like *.0, not *.31415
    double y = Math.round(***);
    double z = Math.round(***);
    for (Entity entity : world.getEntities()){
           if (Math.round(entity.getLocation().getX()) == x && Math.round(/*getY() here*/) == y && /*do Z*/ && entity.getType() == EntityType.ITEMFRAME){
                   //Here do what you want with your item frame.
           }
    }
     
  4. Offline

    _Error

    This is obv not going to work, But something like this if you want to check for an item frame at a location.
    It will check each time a player moves, You can make it check with other events, just an example

    Code:
    public void onMove(PlayerMoveEvent e) {
    Player p = e.getPlayer();
    World w = p.getWorld();
    Location l = new Location(w, x, y, z);
    if(l.getWorld().getEntities().equals(EntityType.ITEM_FRAME)){
    // do somthing
    }
    
    }
    
     
    Last edited: Jul 2, 2015
  5. @_Error @BurnyDaKath OP says that he already has the coordinates and just wants to check if the block/entity/item is an item frame. He also wanted to get the item in the frame. You two are checking the entire world for entities that matches an item frame. Also, PlayerMoveEvent isn't the best event to check for that. For one thing, that would check every player that is moving's world and get nearby entities that is equal to an item frame. Depending on how much RAM this server has, this could lag it a bit.

    @Nic2555 I suggest something like this:
    Code:
    public boolean isItemFrame(Location loc)
    if (loc.getWorld().getBlockAt(loc).getType() == Material.ITEM_FRAME) {
        return true;
    } else {
        return false;
    }
     
  6. Offline

    Nic2555

    i like all of your method, @CodePlaysMinecraft has a good point, i'll check it out and leave a feedback when i'm done, Thanks !
     
  7. Offline

    Hawktasard

    @CodePlaysMinecraft
    You could change it to this (friendly advice)
    Code:java
    1. public boolean isItemFrame(Location loc) {
    2. return loc.getBlock().getType() == Material.ITEM_FRAME;
    3. }
     
  8. Offline

    _Filip

    @_Error Learn to code before "helping" others with programming.
     
  9. Offline

    Nic2555

    I tried both @CodePlaysMinecraft , @_Error and @Hawktasard codes and they all don't want to work. here's what i got for now
    PHP:
    for(int i firstBlock.getBlockX(); secondBlock.getBlockX(); i++)
                    {
                        for(
    int j firstBlock.getBlockY(); secondBlock.getBlockY(); i++)
                        {
                            for (
    int k firstBlock.getBlockZ(); secondBlock.getBlockZ(); k++)
                            {
                                
    sender.sendMessage("ttes2t");
                                
    Location loc = new Location(main.getWorld(), ijk);
                                if(
    loc.getBlock().getType() == Material.ITEM_FRAME)
                                {
                                    
    Bukkit.getServer().getLogger().info("test2");
    }
    it prompt the first "ttes2t" message but not the "test2" one.
     
  10. Offline

    _Error

    You tried @BurnyDaKath code?

    @_Filip I was in a hurry and didn't have enough time to think a lot, And plus here in Plugin Development, It's to help the person make the plugin, not babyfeed them codes. I was just giving this code for him to modify it and improve it, and before judging others, judge yourself. You did not even give a code.

    People these days are just rude to each others.
     
  11. The only reason it wouldn't output "test2" would be because the block at that location isn't an ItemFrame.
     
  12. Offline

    _Filip

    Code:
    public void onMove(PlayerMoveEvent e) {
    Player p = e.getPlayer();
    World w = p.getWorld();
    Location l = new Location(w, x, y, z);
    if(l.getWorld().getEntities().equals(EntityType.ITEM_FRAME)){
    // do somthing}
    }
    
     
  13. Offline

    DevRosemberg

    @_Filip Lol no.

    Code:
    private static final public void if(Move) Player {
    Player PLAYER = Player;
    foreach (Entity as ent) {
      if (ent.getType == Item.Frame) {
        return true;
      }
    }
    }
     
  14. Offline

    Nic2555

    @CodePlaysMinecraft Thanks for the reply, well i've place down a four item frame and set the radius to 10, ( i was beside the frames) and still don't find them.
     
  15. Offline

    eyamaz

    Cleaned up the page. Let's keep it that way.
     
    bwfcwalshy likes this.
  16. Offline

    Nic2555

  17. 1.
    can be simplified to:
    Code:
    return someThingThatIsOrReturnsABoolean;
    Also ItemFrame is an ENTITY. Material.ITEM_FRAME is the item you can have in an inventory.
    This would work (not tested):
    Code:
    loc = loc.getBlock().getLocation().add(0.5, 0.5, 0.5); //Edit x,y,z to get the middle of the block
    for (Entity e : loc.getWorld().getEntitiesByClass(ItemFrame.class)) { //loop through all ItemFrames in the world
    if (e.getLocation().distance(loc) <= 0.5) { //Check if the distance between your location and the ItemFrame is less than a half block
    //Do something with it
    break; //break out of the loop or return
    }
    }
     
Thread Status:
Not open for further replies.

Share This Page