Solved Properly launching TNT on left click..

Discussion in 'Plugin Development' started by kreashenz, Apr 6, 2013.

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

    kreashenz

    I can't seem to get the launching of the TNT correct, it spawns, and launches a TINY bit forward, but that's all I can get in the code.. Should I set the velocity to like, 20 or something? Anyone help a little on this? Also the removing of the ammo, wont work, I have debugged it, ( shown ) and its still not removing or updating the inventory.
    Code:java
    1. @SuppressWarnings("deprecation")
    2. @EventHandler
    3. public void onTnTThrow(PlayerInteractEvent e){
    4. if(e.getPlayer().getItemInHand().getType() == Material.TNT
    5. && e.getAction() == Action.LEFT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_BLOCK){
    6. e.getPlayer().sendMessage("1");
    7. ItemStack ammo = new ItemStack(Material.TNT, 2);
    8. if(e.getPlayer().getItemInHand().hasItemMeta()){
    9. e.getPlayer().sendMessage("2");
    10. if(e.getPlayer().getItemInHand().getItemMeta().getDisplayName().equals("§rBombs")){
    11. e.getPlayer().sendMessage("3");
    12. Player p = e.getPlayer();
    13. Entity tnt = p.getWorld().spawn(p.getLocation().add(0.65,0, 0), TNTPrimed.class);
    14. ((TNTPrimed)tnt).setVelocity(tnt.getVelocity().multiply(3));
    15. ((TNTPrimed)tnt).setFuseTicks(40);
    16. p.getInventory().remove(ammo);
    17. e.getPlayer().sendMessage("4");
    18. // this line doesnt work \/\/\/
    19. p.updateInventory();
    20. e.getPlayer().sendMessage("5");
    21. }
    22. }
    23. }
    24. }
     
  2. Offline

    chasechocolate

    If you want to launch it where they are looking, use this:
    Code:java
    1. ((TNTPrimed)tnt).tnt.setVelocity(player.getLocation().getDirection().multiply(1.5));
     
  3. Offline

    kreashenz

    chasechocolate It only shooting outwards when a player is flying, and when they are on the ground, it spawns on their feet..
     
  4. I m using this in my TF2 plugin here the code, just remove cooldown.get and cooldown.add cos thats just something im using for reloading :)
    also play around with the package, cos right now its my TF2 package ;) hope this helps

    code:

    package me.Shadow.TF2;

    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.TNTPrimed;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    public class GLauncher {
    @SuppressWarnings("deprecation")
    public static void shoot(Player player){
    if(Cooldown.get(player)==false){
    ItemStack grenade = new ItemStack(Material.TNT,1);
    ItemMeta meta=grenade.getItemMeta();
    meta.setDisplayName("Grenades");
    grenade.setItemMeta(meta);
    if(player.getInventory().containsAtLeast(grenade,1)){
    TNTPrimed tnt = player.getWorld().spawn(player.getEyeLocation(), TNTPrimed.class);
    tnt.setVelocity(player.getLocation().getDirection().multiply(1.5));
    tnt.setFuseTicks(30);
    player.getInventory().removeItem(grenade);
    player.updateInventory();
    Cooldown.add(player, "reloading");
    }else{
    player.sendMessage(ChatColor.DARK_GRAY+"You need 'Grenades'");
    }
    }else{
    player.sendMessage("Your "+player.getItemInHand().getItemMeta().getDisplayName()+" is still reloading ("+Numbers.round(Cooldown.time)+")");
    }
    }
    }
     
  5. Offline

    kreashenz

    XxShadow_NinjaxxX That kinda just screwed it up, some how, now it is not shooting at all..
    Code:java
    1. @SuppressWarnings("deprecation")
    2. @EventHandler
    3. public void onTnTThrow(PlayerInteractEvent e){
    4. Player p = e.getPlayer();
    5. if(e.getAction() == Action.LEFT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_BLOCK &&
    6. p.getItemInHand().getType() == Material.TNT){
    7. p.sendMessage("1");
    8. ItemStack ammo = new ItemStack(Material.TNT, 2);
    9. if(p.getInventory().containsAtLeast(ammo, 1)){
    10. p.sendMessage("2");
    11. if(p.getItemInHand().equals(ammo)){
    12. if(p.getItemInHand().getItemMeta().getDisplayName().equals("§rBombs")){
    13. p.sendMessage("3");
    14. Entity tnt = p.getWorld().spawn(p.getLocation().add(0.65, 0, 0), TNTPrimed.class);
    15. tnt.setVelocity(p.getLocation().getDirection().multiply(1.5));
    16. ((TNTPrimed) tnt).setFuseTicks(40);
    17. p.getInventory().removeItem(ammo);
    18. p.sendMessage("4");
    19. p.updateInventory();
    20. p.sendMessage("5");
    21. }
    22. }
    23. }
    24. }
    25. }

    Only the first debug message comes up.
     
  6. Offline

    Hoolean

    kreashenz
    1. What number does it get up to?
    2. Your code will only currently work if the player has exactly 2 TNT items in their hand :)
     
  7. Offline

    kreashenz

    MrBluebear3 It only goes up to the first debug message, and how do I fix the second problem?
     
  8. Offline

    Hoolean

    Code:
     @SuppressWarnings("deprecation")
        @EventHandler
        public void onTnTThrow(PlayerInteractEvent e){
            Player p = e.getPlayer();
            if(e.getAction() == Action.LEFT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_BLOCK &&
                    p.getItemInHand().getType() == Material.TNT){
                p.sendMessage("1");
                ItemStack ammo = new ItemStack(Material.TNT, 2);
                if(p.getItemInHand().getAmount()>=2) {
                        if(p.getItemInHand().getItemMeta().getDisplayName().equals("§rBombs")){
                            p.sendMessage("3");
                            Entity tnt = p.getWorld().spawn(p.getLocation().add(0.65, 0, 0), TNTPrimed.class);
                            tnt.setVelocity(p.getLocation().getDirection().multiply(1.5));
                            ((TNTPrimed) tnt).setFuseTicks(40);
                            p.getInventory().removeItem(ammo);
                            p.sendMessage("4");
                            p.updateInventory();
                            p.sendMessage("5");
                        }
                    }
            }
        }
    That should work :)

    Actually wait, found another problem!

    Code:
    @SuppressWarnings("deprecation")
        @EventHandler
        public void onTnTThrow(PlayerInteractEvent e){
            Player p = e.getPlayer();
            if(e.getAction() == Action.LEFT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_BLOCK &&
                    p.getItemInHand().getType() == Material.TNT){
                p.sendMessage("1");
                ItemStack ammo = new ItemStack(Material.TNT, 2);
                if(p.getItemInHand().getAmount()>=2) {
                        if(p.getItemInHand().getItemMeta().getDisplayName().equals("§rBombs")){
                            p.sendMessage("3");
                            Entity tnt = p.getWorld().spawn(p.getLocation().add(0.65, 0, 0), TNTPrimed.class);
                            tnt.setVelocity(p.getLocation().getDirection().multiply(1.5));
                            ((TNTPrimed) tnt).setFuseTicks(40);
                            if(p.getItemInHand().getAmount()>2) {p.getItemInHand().setAmount(p.getItemInHand().getAmount()-2)}else{p.setItemInHand(new ItemStack(Material.AIR, 0))}
                            p.sendMessage("4");
                            p.updateInventory();
                            p.sendMessage("5");
                        }
                    }
            }
        }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  9. Offline

    kreashenz

    MrBluebear3 It's not removing the items, now :/ (Causing an infinite bug).. This time its sending all the debug messages.
    [EDIT] When I posted this, your comment popped up. Thanks for this :)
     
  10. Offline

    ohtwo

    Then remove the items...?
     
  11. Offline

    kreashenz

    MrBluebear3 When the item gets to 2, it doesn't remove anything. Should I add an if statement, for when the player has 2 of the item, then set the slot to air?

    ohtwo
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  12. Offline

    Hoolean

    Code:
    @SuppressWarnings("deprecation")
        @EventHandler
        public void onTnTThrow(PlayerInteractEvent e){
            Player p = e.getPlayer();
            if(e.getAction() == Action.LEFT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_BLOCK &&
                    p.getItemInHand().getType() == Material.TNT){
                p.sendMessage("1");
                ItemStack ammo = new ItemStack(Material.TNT, 2);
                if(p.getItemInHand().getAmount()>=2) {
                        if(p.getItemInHand().getItemMeta().getDisplayName().equals("§rBombs")){
                            p.sendMessage("3");
                            Entity tnt = p.getWorld().spawn(p.getLocation().add(0.65, 0, 0), TNTPrimed.class);
                            tnt.setVelocity(p.getLocation().getDirection().multiply(1.5));
                            ((TNTPrimed) tnt).setFuseTicks(40);
                            if(p.getItemInHand().getAmount()>2) {p.getItemInHand().setAmount(p.getItemInHand().getAmount()-2)}else{p.setItemInHand(new ItemStack(Material.AIR, 0))}
                            p.sendMessage("4");
                            p.updateInventory();
                            p.sendMessage("5");
                        }
                    }
            }
        }
    Try changing it to this :)
     
  13. ya you need to change somethings like you can remove if(p.getItemInHand.getItemMeta,getDisplayName.equals(Bombs))
    bcause i have custom names for my ammo and stuff
     
Thread Status:
Not open for further replies.

Share This Page