Get All TNT Drops

Discussion in 'Spigot Plugin Development' started by Jaffaaaa, Aug 10, 2018.

  1. I'm making a mineable TNT plugin, how do I make it so the player throwing the TNT get all of the blocks in his inventory?

    ThrowAbleTNT Class:
    Code (Java):
    package xyz.theducc.play.TNT;

    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.block.Block;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;

    import net.md_5.bungee.api.ChatColor;

    public class ThrowableTNT implements Listener {

        @EventHandler
        public boolean onInteract(final PlayerInteractEvent event) {
            Player player = event.getPlayer();
            World world = player.getWorld();
            ItemStack item = player.getInventory().getItemInHand();
           
            if (player.getItemInHand().getType() == Material.TNT) {

            if (!(player.getLocation().getWorld().getName().equals("Mines"))) {
                player.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "Ducc" + ChatColor.DARK_RED + "" + ChatColor.BOLD + "TNT >>" + ChatColor.RED + " You are not in a mine.");
                return false;
            }
           
            if (!player.hasPermission("DuccTNT.throw")) {
                player.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "Ducc" + ChatColor.DARK_RED + "" + ChatColor.BOLD + "TNT >>" + ChatColor.RED + " You do not have permission to do this.");
                return false;
            }

                if ((event.getAction() == Action.RIGHT_CLICK_BLOCK || event.getAction() == Action.LEFT_CLICK_BLOCK)) {
                    event.setCancelled(true);
                    if (item.getAmount() == 1) {
                        player.getInventory().remove(item);
                    }
                    else {
                        item.setAmount(item.getAmount() - 1);
                    }
                   
                    Block block = (Block) event.getClickedBlock();
                    Location tntloc = block.getLocation();
                   
                    world.spawnEntity(tntloc, EntityType.PRIMED_TNT);
                    world.spawnEntity(tntloc, EntityType.PRIMED_TNT);
                    world.spawnEntity(tntloc, EntityType.PRIMED_TNT);
                    world.spawnEntity(tntloc, EntityType.PRIMED_TNT);
                    world.spawnEntity(tntloc, EntityType.PRIMED_TNT);
                   
                    return false;
            }
            }
        return true;
        }
    }
     
     
  2. Not entirely sure, but you could make an event handler for EntityExplodeEvent. I think there's a getBlocks() method in it. I don't know if it really is what you're looking for however.
     
  3. You should most likely save those tnt entities you spawn in a hashmap or something. on EntityExplodeEvent check if the explosion is from a tnt and whether these explosions belong to X user. If true then get the drops using #getDrops and give them to the player.
     
  4. Use this please:
    Code (Java):
    player.getInventory().getItemInMainHand();
    The method you are using is deprecated because now there are 2 hands.
     
  5. Apart from posting something out of the topic the Inventory#getItemInHand method is fine in versions 1.8X and below.
    The OP is most likely using older versions of the API and not 1.9X+. However, your statement is correct in later versions as mentioned.
     
    • Like Like x 1