Firework explode when impact

Discussion in 'Plugin Development' started by stimoze, Jul 14, 2016.

Thread Status:
Not open for further replies.
  1. I was making a plugin what adds a gun (diamond hoe) , and this gun shoots a firework.
    So what I want:
    I want to detonate the firework if it hits a block.

    It works perfectly if i shoot it to the sky, but it disappears if i shoot it to the block.

    My code:
    Code:
        @EventHandler
        public void FreezeGunData(PlayerInteractEvent e){
            ItemStack hoe = new ItemStack(Material.DIAMOND_HOE);
            ItemMeta meta = hoe.getItemMeta();
            meta.setDisplayName("§f§lFirework gun");
            List<String> lore = new ArrayList();
            lore.add("§eWith this gun you can fire fireworks!");
            meta.setLore(lore);
            hoe.setItemMeta(meta);
            if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK)
            {
                if (e.getItem().equals(null))
                {
                    return;
                }
                if (e.getPlayer().getInventory().getItemInHand().isSimilar(hoe))
                {
                    final Location loc = e.getPlayer().getEyeLocation();
                    final Vector v = loc.getDirection();
                    Player p = e.getPlayer();
                    p.sendMessage("fired successfully");
                    Firework f = (Firework) p.getWorld().spawnEntity(p.getLocation(), EntityType.FIREWORK);
                    FireworkMeta fm = f.getFireworkMeta();
                    fm.clearEffects();
                    double distance = getConfig().getDouble("distance");
                    v.normalize().multiply(distance);
                    loc.add(v);
                    InstantFirework.createFireworkEffect(FireworkEffect.builder().flicker(false).trail(true).with(FireworkEffect.Type.BALL).withColor(Color.AQUA).withFade(Color.BLACK).build(), loc);
                    f.setFireworkMeta(fm);
                }
            }
        }
    What would be the best solution for my problem?
     
  2. Offline

    Zombie_Striker

    @stimoze
    To do that, you will need to create a runnable that repeats every tick. What it will do is check if the firework is near a block, and if so, detonate. If you want it so it will only detonate if it "hits" a block it is going towards, then get the fireworks location, and add the firework's velocity to the fireworks location. If that new location is a block, then you would detonate.
     
  3. Can you give me an example of your 2nd method?

    Is this a good solution?
    Code:
                    new BukkitRunnable()
                    {
    
                        @Override
                        public void run() {
                            if(f.isOnGround() == true)
                            {
                                f.getLocation().add(f.getVelocity().add(v));
                            }
                           
                        }
                       
                    }.runTaskTimer(this, 1, 1);
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 15, 2016
  4. @stimoze Remove the == true and look at some Java tutorials.
     
  5. Offline

    Zombie_Striker

    @stimoze
    Not really. First, you can remove the "== true" from the if statement, since f.isOnGround already returns a boolean. After that, you are not only adding the velocity onto the location, but you seem to be doubling the velocity each time this runs. Finally,you are not checking if the block at that new location is a block.

    To do what you want, follow these steps.
    1. Get the velocity of the firework. Velocity is an instance of Vector, so I will be referring to this instance as the vector,
    2. Find the highest value between the X,Y, and Z.
    3. Divide the X,Y, and Z by that highest number.
    4. Add the modified vector to the firework's current location. Get the block at that location
    5. If the block is not equal to air (I.e, is a block)
    6. Use the detonate method for the firework to detonate.
     
  6. I don't really get the 2. step What do you mean with "the highest value between the X,Y, and Z."?
     
    Last edited: Jul 15, 2016
  7. Offline

    Zombie_Striker

    @stimoze
    Use Math.max(first int, second int) to compare two integers and return the highest. Do this for the X and Y, and then the highest between those two and the Z. In the end, you will get the highest value for all X,Y, and Z.
     
Thread Status:
Not open for further replies.

Share This Page