TNT Wand

Discussion in 'Plugin Development' started by AnicraftPlayz, Mar 6, 2021.

  1. Offline

    AnicraftPlayz

    Hi, so recently I was making a plugin and its basically just a bunch of different wands that do cool things. The first one I decided to make was a TNT Wand, the idea I had in mind, is that if the player right clicks the TNT Wand it launches a piece of TNT in the direction they were facing. So far I have the item created and a command to get it. I've spent the past hour racking my brain and googling but I couldn't find anything that could help me. Any help would be much appreciated, thanks!
     
  2. Offline

    Legendary_zotar

    For spawning you can use World#spawnEntity(Location loc, EntityType.PRIMED_TNT)
    then you'd have to go about setting its direction, you can use the Entity#setVelocity to set the direction and speed in that direction, causing it to launch.

    If your new to bukkit, Primed TNT is considered an Entity, and i believe you can access the class by casting the returned entity of World#spawnEntity

    TNTPrimed tnt = (TNTPrimed)World#spawnEntity(loc, EntityType.PRIMED_TNT);
    tnt.setVelocity(...)
     
  3. Offline

    8el

    Hey, here is some of the code that I used in my TNTWands plugin. The TNTWandEvents is what happens when you use the wand itself, and you have to make sure that you setup an ItemManager.tntWand or change it's name in my code that way the wand works for you. The CooldownManager is using a HashMap to set a cooldown for players which will go in it's own Java Class. Last, you will need to put
    getServer().getPluginManager().registerEvents(new TNTWandEvents(), this); and
    CooldownManager.setupCooldown().; in your Main Class so the wand events and the cooldowns get loaded!


    Code:
    public class TNTWandEvents implements Listener {
    
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            ItemStack i = e.getItem();
            if (!(e.getAction() == Action.RIGHT_CLICK_AIR) && !(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
            boolean fireShot = false;
            if (e.getItem() != null) {
                if(CooldownManager.checkCooldown(e.getPlayer())) {
                    if (e.getItem().getItemMeta().equals(ItemManager.tntWand.getItemMeta())) {
                        e.getPlayer().sendMessage("§c§lTNT wand has been used!");
                        CooldownManager.setCooldown(e.getPlayer(), 30);
                        fireShot = true;
                    }
                } else {
                    if (e.getItem().getItemMeta().equals(ItemManager.tntWand.getItemMeta())) {
                        fireShot = false;
                        e.getPlayer().sendMessage("§4§lPlease wait before using this wand again!");
                    }
                }
                if (e.getItem().getItemMeta().equals(ItemManager.tntWand.getItemMeta())) {
                    if (fireShot == true) {
                        Entity tnt = e.getPlayer().getWorld().spawn(e.getPlayer().getLocation().add(0, 1, 0), TNTPrimed.class);
                        ((TNTPrimed) tnt).setFuseTicks(40);
                        tnt.setVelocity(e.getPlayer().getLocation().getDirection().multiply(2));
                    }
                }
            }
        }

    Code:
    public class CooldownManager {
    
        public static HashMap<UUID, Double> cooldowns;
    
        public static void setupCooldown(){
            cooldowns = new HashMap<>();
        }
    
        //setting up the cooldown
        public static void setCooldown(Player player, int seconds){
            double delay = System.currentTimeMillis() + (seconds*1000);
            cooldowns.put(player.getUniqueId(), delay);
        }
    
        //get the cooldown
        public static int getCooldown(Player player){
            return Math.toIntExact(Math.round(cooldowns.get(player.getUniqueId()) - System.currentTimeMillis()/1000));
        }
    
        //checking the cooldown
        public static boolean checkCooldown(Player player){
            if(!cooldowns.containsKey(player.getUniqueId()) || cooldowns.get(player.getUniqueId()) <= System.currentTimeMillis()){
                return true;
            }
            return false;
        }
    
    }
    
     

Share This Page