Solved Getting entity from a location, then casting it to ItemFrame

Discussion in 'Plugin Development' started by Kassestral, Feb 15, 2015.

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

    Kassestral

    So I am trying to make it so when players click the the 'buy' sign for my shop, it will get the item in the itemframe below the sign, I keep getting the following error on line 68:

    Code:
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_7_R3.block.Cr
    aftBlock cannot be cast to org.bukkit.entity.Entity
            at com.kassestral.plugins.imperium.events.Shop.onShopInteract(Shop.java:
    68) ~[?:?]
    Event Code
    Code:
        @EventHandler
        public void onShopInteract(PlayerInteractEvent event) throws IOException {
            Player player = event.getPlayer();
           
            if(event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
                player.sendMessage("1");
                player.sendMessage("" + event.getClickedBlock().getType());
                if(event.getClickedBlock().getType().equals(Material.WALL_SIGN) || event.getClickedBlock().getType().equals(Material.SIGN_POST)){
                    Sign sign = (Sign) event.getClickedBlock().getState();
                    player.sendMessage("2");
                    if(sign.getLine(1).equalsIgnoreCase(ChatColor.BLUE + "Buy")) {
                        player.sendMessage("3");
                        Location location = sign.getLocation();
                        World world = location.getWorld();
                        double X = location.getX();
                        double Y = location.getY() -1;
                        double Z = location.getZ();
                        Location frame_location = new Location(world,X,Y,Z);
              //line 68 Entity entity_itemframe = (Entity) world.getBlockAt(frame_location);
                        ItemFrame itemframe = (ItemFrame) entity_itemframe;
                        player.sendMessage("" + itemframe.getItem());
                    }
                }
            }
        }
    Any help appreciated
     
  2. Offline

    crolemol

    @Kassestral
    You arE casting a block to an entity which is not possible. Try ItemFrame frame = (ItemFrame) block.getState
     
  3. Offline

    Kassestral

    @crolemol
    Code:
    28) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_7_R3.block.Cr
    aftBlockState cannot be cast to org.bukkit.entity.ItemFrame
    Cannot cast a block state either
     
  4. Offline

    1Rogue

    That's because an ItemFrame is an entity.
     
    Konato_K likes this.
  5. Offline

    Kassestral

    @1Rogue
    I know, do you by any chance know how to get it from a location?
     
  6. Offline

    1Rogue

  7. Offline

    Kassestral

    @1Rogue
    Because the event should only be triggered when a player clicks a Sign, not the ItemFrame :p
     
  8. Offline

    1Rogue

    Oh I see, in that case, iterate through the world's entities and find the closest itemframe within an appropriate maximum radius (say, 2 blocks).
     
  9. Offline

    Kassestral

  10. Offline

    caderape

    @Kassestral This is not thesame problem you posted 2 weeks ago ?

    I will tell you exactly the same thing as before :p

    Code:
    if (e.getClickedBlock() != null && e.getClickedBlock().getType() == Material.WALL_SIGN) {
                Location loc = e.getClickedBlock().getLocation();
                loc.setY(loc.getY()-1);
                for (Entity ent : loc.getChunk().getEntities()) {
                    if (ent.getLocation().getBlock().getLocation().equals(loc)) {
                        ItemFrame item = (ItemFrame) ent;
                        Bukkit.broadcastMessage(item.getItem().getType().toString());
                    }
                }
            }
     
  11. Offline

    Kassestral

    @caderape
    A few weeks ago I tried doing it and it wasn't working, just got back round to working on the code, and for some reason it doesn't get the right entity :p
     
  12. Offline

    caderape

    @Kassestral I tested it with this code, and it works for me.
    That should be good for you too ;)
     
  13. Offline

    Kassestral

    @caderape
    Did you test with multiple itemframes?
     
  14. Offline

    caderape

    @Kassestral ha, i see.
    It works for me if you don't put more than one itemframe by location. But if u wanna do it, check the direction where the sign is attached to the block and check in the location if the itemframe has the same direction
     
Thread Status:
Not open for further replies.

Share This Page