Detect TNT Minecart Placement

Discussion in 'Plugin Development' started by Musician101, Dec 11, 2013.

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

    Musician101

    I'm trying to broadcast a message when a player places a TNT Minecart. I've got it to work but for some reason the message is broadcast twice. I'm currently using PlayerInteractEvent to run the necessary code. Any ideas on how I can fix this?

    Here's my code.
    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent event)
    3. {
    4. Action action = event.getAction();
    5. Block block = event.getClickedBlock();
    6. ItemStack item = event.getItem();
    7. Player player = event.getPlayer();
    8. block.getState();
    9.  
    10. List<Material> rails = new ArrayList<Material>(Arrays.asList(Material.ACTIVATOR_RAIL, Material.DETECTOR_RAIL, Material.POWERED_RAIL, Material.RAILS));
    11.  
    12. if (action == Action.RIGHT_CLICK_BLOCK && player.getGameMode() == GameMode.CREATIVE)
    13. {
    14. if (item.getType() == Material.EXPLOSIVE_MINECART && rails.contains(block.getType()))
    15. {
    16. plugin.getLogger().info(player.getName() + " has placed an " + item.getType().toString() + " at X: " + player.getLocation.getBlockX() + ", Y: " + player.getLocation.getBlockY() + ", Z: " + player.getLocation.getBlockZ() + ".");
    17. }
    18. }
    19. }


    EDIT: The code I provided was only a snippet of the full class. If you wish to see the full class you can find it here.
     
  2. Offline

    JRL1004

    Musician101 Make sure you are not registering your events more than once. Each time you register events for a class adds 1 to the number of times that classes events are called.
     
  3. Offline

    Musician101

    JRL1004 I don't think that's the problem. I have more things that happen within in that method and the TNT Minecart placement is the only thing that double posts. I will, however test to see if it is a possibility that I managed to register it twice.

    EDIT: Disabled all of my event listeners and tried it again with the same result.
     
  4. Offline

    JRL1004

    Musician101 Hmm, not sure. That seems like it should work (at least to me).
     
  5. Offline

    Musician101

    JRL1004 I've had this problem for a while but I think it's an issue with Bukkit or Vanilla Minecraft :/ Thanks for the help anyway.
     
  6. Offline

    Razoul05

    Hi Musician,
    I was writing my own plugin to block the TnT Placement and TnT MineCart based on a specific permission and encounterd the same double status message for the cart as you did but was able to resolve it by using the ignoreCanceled=ture flag on the Event.
    Code:java
    1. @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled=true)
    2. public void onPlayerInteract(PlayerInteractEvent event)
    3. {
    4. // Do Stuff
    5. }

    I'm no expert but what I THINK is happening is the following:
    1. Player triggers event
    2. Bukkit Parses through ALL plugins and passes this event
    3. Your plugin then cancels the event & Logs
    4. Bukkit Re-Parses through ALL plugins and passes the UPDATED event
    5. Your plugin cancels (no change) & Logs
    By adding the ignoreCancelled flag your method won't be called on the second iteration. This will be fine unless you plan to Un-Cancel an event.
     
  7. Offline

    Musician101

    Razoul05 Sorry to say that it hasn't fixed my issue. If what you say is true about how the event works, it's a bit strange.
     
Thread Status:
Not open for further replies.

Share This Page