Who placed TNT?

Discussion in 'Plugin Development' started by Meatiex, Dec 28, 2014.

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

    Meatiex

    How could I tell who placed tnt with EntityExplodeEvent?

    Heres the code I use to put the TNT down...
    Code:
        @EventHandler
        public void place(BlockPlaceEvent event) {
            if (list.contains(event.getPlayer()))
                if (!event.getPlayer().isSneaking())
            if (event.getBlock().getType() == Material.TNT) {
              event.getBlock().setType(Material.AIR);
              ((TNTPrimed)event.getPlayer().getWorld().spawn(event.getBlock().getLocation(), TNTPrimed.class)).setFuseTicks(40);
            }
        }
    Heres my EntityExplodeEvent right now
    Code:
        @EventHandler
        public void onEntityExplode(EntityExplodeEvent event) {
            if (event.getEntity().getType() == EntityType.PRIMED_TNT) {
          Iterator it = event.blockList().iterator();
          int count = 0;
          while (it.hasNext()) {
           count = count + 1;
           it.next();
          }
          Bukkit.broadcastMessage("Debug Score: " + count);
        }
        }
    I want to add "count" to a HashMap<Player, Integer>, how would I do this?

    http://bukkit.org/threads/tnt-blow-up-specific-blocks.205236/

    Any help is Awesome :D
     
  2. Offline

    mythbusterma

    @Meatiex

    You'd be better off with a BlockPlaceEvent.
     
  3. Offline

    Meatiex

    Cant count blocks exploded by tnt on block place event...
     
  4. Offline

    mythbusterma

    @Meatiex

    You know you can listen for more than one event...
     
  5. Offline

    Meatiex

    Code:
    @EventHandler
        public void place(BlockPlaceEvent event) {
    Code:
    @EventHandler
        public void onEntityExplode(EntityExplodeEvent event) {
     
  6. Offline

    mythbusterma

  7. Offline

    Meatiex

    What do you mean by this? I don't think I understand.
     
  8. Offline

    mythbusterma

    @Meatiex

    You can have two event handlers.....one for each event.
     
  9. Offline

    1Rogue

    He's saying to keep track of who placed TNT where, and when one goes off, refer back to your tracking. Using a Map would benefit you here.
     
    Meatiex likes this.
  10. Offline

    Meatiex

    I made a HashMap<Location, String> for loc and player.getName(). It works when I place 1 tnt at a time, but when I place 2 tnt at a time the hashmap dosn't take the 2nd location... I get the error from my code "No Loc" for all the tnt that blow up except the first one...
    Code:
        static ArrayList<String> list = new ArrayList<String>();
        HashMap<String, Integer> score = new HashMap<String, Integer>();
        HashMap<Location, String> placed = new HashMap<Location, String>();
    Code:
        @EventHandler
        public void place(BlockPlaceEvent event) {
            Player player = event.getPlayer();
            if (list.contains(player.getName()))
                if (!player.isSneaking())
            if (event.getBlock().getType() == Material.TNT) {
              event.getBlock().setType(Material.AIR);
              ((TNTPrimed)player.getWorld().spawn(event.getBlock().getLocation(), TNTPrimed.class)).setFuseTicks(40);
              placed.put(event.getBlock().getLocation(), player.getName());
            }
        }
    Code:
        @SuppressWarnings("rawtypes")
        @EventHandler
        public void onEntityExplode(EntityExplodeEvent event) {
            if (event.getEntity().getType() == EntityType.PRIMED_TNT) {
          Iterator it = event.blockList().iterator();
          int count = 0;
          while (it.hasNext()) {
           count = count + 1;
           it.next();
          }
          Location loc = new Location(event.getLocation().getWorld(), Math.round(event.getLocation().getX()), Math.round(event.getLocation().getY()), Math.round(event.getLocation().getZ()));
          if (placed.get(loc) != null) {
              if (score.get(placed.get(loc)) == null)
                  score.put(placed.get(loc), 0);
        score.put(placed.get(loc), score.get(placed.get(loc)) + count);
        Bukkit.broadcastMessage(" " + score.get(placed.get(loc)));
        placed.remove(loc);
          } else Bukkit.broadcastMessage(ChatColor.RED + "No loc");
        }
        }
     
Thread Status:
Not open for further replies.

Share This Page