Hello I was following a plugin tutorial by Codename_B for remote TNT but I noticed you can remotely blow up the TNT if its not in clear view. I was wondering could someone tell me how to remotely explode the TNT with blocks in the way. Here the tutorial: Heres the code(I added somethings): Code (Text): @EventHandler public void RemoteTNT(PlayerInteractEvent e) { if(e.getAction() == Action.LEFT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_BLOCK) { if(e.getPlayer().getItemInHand() !=null && e.getPlayer().getItemInHand().getType() == Material.LEVER) { if(e.getItem().getItemMeta().getDisplayName().equals(ChatColor.AQUA + "Detonator")) { Block b = e.getPlayer().getTargetBlock(null, 20); if(b.getType() == Material.TNT) { b.setType(Material.AIR); b.getWorld().spawn(b.getLocation().add(0.5, 0.5, 0.5), TNTPrimed.class); e.getPlayer().sendMessage(ChatColor.RED + "Fire in the hole!!"); }else { e.getPlayer().sendMessage(ChatColor.DARK_RED + "Nothing to go boom :(!"); } } } } }
Don't use Player#getTargetBlock, but instead use a BlockIterator. So you can iterate through the whole line of sight, ignoring all but tnt blocks and activating them.
Correct me if I'm wrong, but I believe he's suggesting you do like so.. Code (Text): Iterator<Block> blocks = e.getPlayer().getTargetBlock(null, 20).iterator(); while (blocks.hasNext()) { Block b = blocks.next(); if(b.getType() == Material.TNT) { b.setType(Material.AIR); b.getWorld().spawn(b.getLocation().add(0.5, 0.5, 0.5), TNTPrimed.class); e.getPlayer().sendMessage(ChatColor.RED + "Fire in the hole!!"); }else { e.getPlayer().sendMessage(ChatColor.DARK_RED + "Nothing to go boom :(!"); } }
Multiple problems: - Player#getTargetBlock returns a block, not an Iterator<Block> - for each block in the line of sight this would spam a message "Nothing to boom" - but the idea is more or less correct BlockIterator's javadocs: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/util/BlockIterator.html
Well, just saying "I am not able to do sb." doesn't make somebody willing to help you. Did you read the link? Did you try something? What did you tried? What is your exact problem? Don't expect spoonfeeding.
Maybe this is what @Coder is suggesting? Code (Text): Iterator<Block> blocks = BlockIterator(player, 20); while (blocks.hasNext()) { Block b = blocks.next(); if (b.getType() == Material.TNT) { // found tnt } }
I still use deprecated methods without any issues so here's what I would do. However many blocks you want to check forward (it goes to 50) do : for(int i = 1; i <= 50; i++) { Block block...getTargetBlock(i,null); If (block = TNT) { ignite TNT return; This is obviously not actual code although you get the point. If this is what you want it will get the first TNT within the player's line of sight, ignite it and then stop
Also after the for statement you would put your "sorry, no TNT!" Thing because if no TNT was in their line of sight then the method never returned so it continues through
I can't ads syntax on my phone that's the right code if you get what I'm saying but its not all in java lol I just wrote what went at certain spots and this method is much better than the other methods because it requires less ramm since the java VM dumps each block after it checks it unlike storing all blocks in a list which builds up ramm