Check dropped item position?

Discussion in 'Plugin Development' started by mateo226, Sep 1, 2015.

Thread Status:
Not open for further replies.
  1. Hello everyone!
    I was just wondering, how would I check a dropped item position?

    I am making a wishing well plugin and when someone throws a coin into the wishing well it should give him a reward. I just want to check if the dropped item coordinates match the well's cordinates..
    Thanks!
     
  2. Offline

    Oxyorum

    @mateo226 You can listen for a PlayerDropItem event and see if the location of the dropped item is inside your well.
     
  3. Ok thanks! I'll give it a go and if I am stuck I will report the problem back to you :)

    @Oxyorum Ok I am now stuck with this ANNOYING bug that I can seem to fix...

    Code:
    @EventHandler
        public void PlayerDropItemEvent(PlayerDropItemEvent e){
            Player player = e.getPlayer();
            Item item = e.getItemDrop();
            player.sendMessage("You dropped an item!");
            player.sendMessage(item.getItemStack().getItemMeta().getLore().toString());
            player.sendMessage(lores.toString());
            if(item.getItemStack().getItemMeta().getLore().toString().equalsIgnoreCase(lores.toString())) {
                player.sendMessage("You dropped the coin!");
                if(item.getLocation().getX() == 18 && item.getLocation().getY() == 57 && item.getLocation().getX() == -25) {
                    player.sendMessage(ChatColor.GOLD + "You made a wish!");
                }
            }
       
        }
    
    My code never gets to the "You dropped the coin message". My lores are the same though. I know that because I print them both out when I drop the item? What's the problem?

    Thanks!
    Also Sorry that I didn't format the code, I used to know how to do it now I just forgot :p

    EDIT: Okay formated the code.

    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than double posting.>
     
    Last edited by a moderator: Sep 1, 2015
  4. Use thingy.getLocation().getBlock().getLocation().getX() since this returns either a rounded double or an int i forgot xD :) but just getLocation().getX() returns like 5.5932582309582
     
  5. I'll try it out, 1 min :)

    @TheGamesHawk2001 Nope that doesn't work... It still doesn't trigger the code inside the brackets...

    Code:
    if(item.getLocation().getBlock().getLocation().getX() == 18 && item.getLocation().getBlock().getLocation().getY() == 57 && item.getLocation().getBlock().getLocation().getZ() == -25) {
                    player.sendMessage(ChatColor.GOLD + "You made a wish!");
                    item.remove();
                }
    
    EDIT: I also tried:

    Code:
    if(item.getLocation().getBlockX == 18 && item.getLocation().getBlockYX() == 57 && item.getLocation().getBlock().getLocation().getX() == -25) {
    
    EDIT: Sorry I am really stupid, I am updating the code, I accedentaly put getX everywhere. Still doesn't work though!

    Okay, this is the updated code (non working):

    Code:
    @EventHandler
        public void PlayerDropItemEvent(PlayerDropItemEvent e){
            Player player = e.getPlayer();
            Item item = e.getItemDrop();
           
            if(item.getItemStack().getItemMeta().getLore().toString().contains("the Wishing Well!")) {
               
                player.sendMessage("You dropped the coin!");
                if(item.getLocation().getBlock().getLocation().getX() == 18 && item.getLocation().getBlock().getLocation().getY() == 57 && item.getLocation().getBlock().getLocation().getZ() == -25) {
                    player.sendMessage(ChatColor.GOLD + "You made a wish!");
                    item.remove();
                }
            }
           
        }
    
    @Oxyorum I am just linking you to this thread so that you can see if you can find any problems. Thanks1

    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than quadruple posting.>
     
    Last edited by a moderator: Sep 1, 2015
  6. Offline

    Oxyorum

    @mateo226 Like @TheGamesHawk2001 said don't use the getX() method for the block, since that returns a double (and since one of your ifs compares that to an int, that condition will never be true unless the x value you get from the getX() method is exactly 18 and not something more realistic like 18.02345 or similar). Use getBlockX() instead. Other than that, I dont see any issues. As long as the lores for both your items actually match, the code should run. Just change the methods like I said.
     
  7. @Oxyorum Well I found an issue! lol! When I drop the coin, the coin has the same position as I do. Well, +1 Y. Is there a way to check the location when the coin is on the ground? I tried using a while(!item.isOnGround()) loop but It went on for infinity...
     
  8. Offline

    Oxyorum

    @mateo226 It should, because you dropped it. xD

    Also, I saw an issue in your code when checking the location of the item. Just use item.getLocation().getBlockX() instead of what you wrote. The code that you were writing for that before was redundant.
     
  9. @Oxyorum Haha ye that makes sense, but is there a way to for example check when the item HITS the ground and THEN check the location?? Thanks!
     
  10. Offline

    Oxyorum

    @mateo226 Good question. The PlayerDropItem event will be fired when a player drops an item from their inventory, but there is no guarantee that it will reach the ground immediately after (dropping an item while flying above the ground, etc.). I believe that if you check to see if the item is on the ground before doing anything else, that will be sufficient. Like below:

    Code:
    @EventHander
    public void PlayerDropEvent(PlayerDropEvent e){
           Item it = e.getitemDrop();
          boolean isCorrectItem = it.getItemStack().getItemMeta().getLore().toString().contains("the Wishing Well!");
           //You are going to want to check if its the right item first, so you don't
           //spend time looking at any PlayerDropEvent except the ones
          //that concern you (ones involving the correct item)
          if(isCorrectItem && it.isOnGround()){
             //do things
           }
    }
    
    Edit: Improve the code a bit.
     
    Last edited: Sep 1, 2015
  11. @Oxyorum That unfortunately doesn't work. Infact, it doesn't even trigger the rest of the code. I believe that is because the onDrop event is only called once, and when I first drop it, it isn't on the ground..

    @Oxyorum Unfortunately, once again, the code doesn't even get triggered. I still think this might be because of the onPlayerDrop beinig called once....

    Hopefully someone will know a workaround!
    Without this, my plugin is useless :(

    @TheGamesHawk2001 Anything? :)

    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than triple posting.>
     
    Last edited by a moderator: Sep 1, 2015
  12. Offline

    Oxyorum

    @mateo226 Hmm. Another thing you can try is creating a task that runs each time this event is fired for an item you want. Look below:

    Code:
    //Le code
    @EventHandler
    public void onPlayerDrop(PlayerDropEvent e){
       //check if the item is the one we want
       Item it = e.getItemDrop();
       boolean isCorrectItem = it.getItemStack().getItemMeta().getLore().toString().contains("the Wishing Well!");
        if(isCorrectItem){
            getScheduler().runTask(this, new MyTask(it);
       }
    }
    
    class MyTask extends BukkitRunnable{
         private Item it;
        MyTask(Item i){
          it = i;
       }
       
    
      @Override
       public void run(){
           //wait until item is on the ground
           //when it is, do everything else
       }
    }
    
     
  13. Offline

    Forge_User_45064837 Retired Staff

    @Oxyorum That code wont work if it doesn't have a item meta or lore. Please if you are going to spoonfeed, do it correctly.

    @mateo226 When someone drops an item, check if it has an item meta and lore, then check where it is and go from there.
     
  14. @Oxyorum Thanks for the code, will try it out even thought it might not work.
    @bwfcwalshy Will do!
     
  15. Offline

    Oxyorum

    @bwfcwalshy True. I'm just trying to help, no need to be rude about it. I don't get this "spoonfeeding" concept some of you guys have on this forum. Sometimes, people learn better when they have an example to look at (and other times they don't, I know), but w/e.

    @mateo226 Try it and see what happens. Let me know if you need more help.
     
  16. @Oxyorum Nope, doesn't seem to do it:
    Code:
    @EventHandler
        public void PlayerDropItemEvent(PlayerDropItemEvent e) {
            Player player = e.getPlayer();
            Item item = e.getItemDrop();
            if (item.getItemStack().hasItemMeta()) {
                player.sendMessage(item.getLocation().getBlockX() + ", " + item.getLocation().getBlockY() + ", "
                        + item.getLocation().getBlockZ());
                Bukkit.getServer().getScheduler().runTask(this, new MyTask(item,player));
             
            }
        }
    
    }
    
    class MyTask extends BukkitRunnable {
        private Item item;
        private Player p;
    
        MyTask(Item i, Player player) {
            item = i;
            p = player;
        }
    
        @Override
        public void run() {
            if(item.isOnGround()) {
                if (item.getItemStack().getItemMeta().getLore().toString().contains("the Wishing Well!")) {
    
                    p.sendMessage("You dropped the coin!");
                    if (item.getLocation().getBlockX() == 18 && item.getLocation().getBlockY() == 58
                            && item.getLocation().getBlockZ() == -26) {
                        p.sendMessage(ChatColor.GOLD + "You made a wish!");
                        item.remove();
                    }
                }
            }
        }
    }
    
    
    EDIT: Since this might be hard to do, is there any other good ways to check if a coin is dropped in a certain area, maybe water? (already googled it but found nothing).. I guess my plugin just isn't possible to be made...

    @Oxyorum Well, I can't seem to find any other techniques of checking if an item is somewhere. I'll continue looking...
     
    Last edited: Sep 1, 2015
  17. Offline

    Oxyorum

    @mateo226 You need to check if the item has a lore as well, in addition what you already did, as said above. You are only checking to see if it has an ItemMeta.

    Also, I would suggest putting a while loop in your task that runs while the item is not on the ground, and then having your other code run afterwards, but I suspect that something like that would cause serious issues. Like you said, there has to be a more straightforward way of achieving this, and hopefully someone who has more experience with this can help you.
     
  18. @Oxyorum I think I will just give up now. Thank you very much for helping me! :)
     
Thread Status:
Not open for further replies.

Share This Page