Standing on a Certain Block + having an certain item stack in their inv help

Discussion in 'Plugin Development' started by Mortal_Wombat, Aug 10, 2014.

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

    Mortal_Wombat

    I know the title is long but eh :p.

    So i was trying to make it where when a player stands on a certain block AND if they have a certain item in their inc it would do something! But it isnt checking or something and ignores that part :/.

    Note: I get No errors


    Code:java
    1. @EventHandler
    2. public void onPlayerMove(PlayerMoveEvent e) {
    3.  
    4. ItemStack snow = new ItemStack(Material.SNOW_BALL);
    5.  
    6.  
    7.  
    8. if(e.getPlayer().getInventory().contains(snow)); {
    9. if (e.getTo().getBlock().getRelative(BlockFace.DOWN).getType() == Material.GOLD_BLOCK) {
    10. e.getPlayer().sendMessage("YAYAYAYA");
    11.  
    12. }
    13.  
    14. else {
    15.  
    16.  
    17.  
    18. e.isCancelled();
    19.  
    20.  
    21.  
    22.  
    23.  
    24.  
    25.  
    26. }
    27.  
    28.  
    29.  
    30.  
    31.  
    32.  
    33.  
    34. }
    35.  


    Thanks in advance <3
     
  2. did you register the events?
     
  3. Offline

    Mortal_Wombat

  4. Offline

    hintss

    what is the point of the e.isCancelled()? >_>
     
  5. Offline

    ZodiacTheories

    Mortal_Wombat

    e.isCancelled() doesn't do anything, I think you mean e.setCancelled(true);
     
  6. Offline

    Mortal_Wombat

    ZodiacTheories I tried that but all it did was cancel all players movement o_o
     
  7. Offline

    bennie3211

    Mortal_Wombat then just remove the else statement? You don't have to cancel the event when nothing has to happen here.

    EDIT:

    Did you try to debug it with debug messages?
     
  8. Offline

    ZodiacTheories

    Mortal_Wombat

    What I do, instead of using the blockface.down, I do this:

    Code:java
    1. if(e.getPlayer().getLocation().subtract(0, 1, 0).getBlock().getType() == Material.GOLD_BLOCK) {
     
  9. Offline

    stormneo7

    You need to loop through all the items in the player's inventory. .contains() as I recall only checks if the player has a specific item type in their inventory, not an item stack.
     
  10. e.isCancelled() returns if the event is cancelled or not (true or false)
     
  11. Offline

    DevSock

    Here is how I would go about doing something like this, I've commented it to explain it to you as good as possible.
    A word of wisdom however, you should always use debug messages when you run into something that gives you a headache! All you need to do is add a System.out.println(); method or something else that will display text to a player. Before you use my code, you should try putting in some debug messages. Put one right after you check if the inventory has a snowball in it, if the message displays then it does! If it doesn't display, then you know that that's the line that it is failing at.




    Code:java
    1. //First we listen for the PlayerMoveEvent.
    2. @EventHandler
    3. public void onPlayerMove(PlayerMoveEvent event)
    4. {
    5. //We make a player variable here for shorthand usage.
    6. Player player = event.getPlayer();
    7. //We check to make sure the block they're standing on is the correct block.
    8. if(player.getLocation().getBlock().getRelative(BlockFace.DOWN).getType().equals(Material.GOLD_BLOCK))
    9. {
    10. //Now we loop through every slot in the player's inventory.
    11. for(ItemStack item : player.getInventory())
    12. {
    13. //If the item is there, and it is a snow ball then we send the player a message.
    14. if(item != null && item.getType().equals(Material.SNOW_BALL))
    15. {
    16. player.sendMessage("YAYAYAYAYA");
    17. //Now, since we've hit the correct item and we've done all we need to do, we break out of the loop.
    18. break;
    19. }
    20. }
    21. }
    22. }
     
  12. add some debug messages in the event, for example:
    Code:Java
    1.  
    2. if(player.getLocation().getBlock().getRelative(BlockFace.DOWN).getType().equals(Material.GOLD_BLOCK))
    3. {
    4. Bukkit.broadcastMessage("Is gold block");
    5.  

    and then say me where it doesnt execute the code (when no message is showing up)
     
  13. Offline

    hintss

    I know what it is, but what was its use here? It was just running e.isCancelled() without using its returned value
     
Thread Status:
Not open for further replies.

Share This Page