"Throw" TNT?

Discussion in 'Plugin Development' started by youngbawss22, Nov 29, 2012.

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

    youngbawss22

    I am trying to make the player throw (launch in the direction he is facing) primed tnt. It doesn't have to go on forever, but pretty far. I'm guessing it has to do with velocity? So far this is the direction i'm going with my event:
    Code:
    @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event2){
        Player p = event2.getPlayer();
        if(event2.getPlayer().getItemInHand().getType() == Material.TNT);
            // insert code here
        }
    }
    Thanks for the help.
     
  2. Offline

    fireblast709

    remove TNT, spawn TNTPrimed and tntentity.setVelocity(Vector)
     
  3. Offline

    youngbawss22

    Could you help me out with vectors, i have never really experimented with them before.
     
  4. Offline

    fireblast709

    To get the direction the player is looking at (afaik) player.getLocation().getDirection(). I go as far as that, as I do not fully get minecrafts physics myself
     
  5. Offline

    Woobie

    Easier and better way would be to cancel tnt placement, then drop tnt block entity with world.dropItem, set the velocity, once it hits something, get the entity location and spawn primed tnt.

    Pseudo alot? o_o

    EDIT: Maybe just check if player right clicks with tnt in his/her hand, and do rest of the stuff, instead of cancelling tnt placement... it would just fail.
     
    MrBluebear3 likes this.
  6. Offline

    youngbawss22

    Ok so i have a bit of the code down... i just need help with the simplest part. I need to reference the tnt.
    Code:
    TNTPrimed tnt = (TNTPrimed) // what to put here?
              tnt.setVelocity(p.getLocation().getDirection().normalize().multiply(2));
     
    zzienzz likes this.
  7. Offline

    lol768

  8. Offline

    youngbawss22

    Oh wait, i figured it out. Unfortunately, it fires tnt every time i right click... anything. When i tried putting "else" in the code it said to delete the token :X
    Code:
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event2){
        Player p = event2.getPlayer();
        if(event2.getPlayer().getItemInHand().getType() == Material.TNT);
            p.getInventory().removeItem(new ItemStack(Material.TNT, 1));
            TNTPrimed tnt = (TNTPrimed) p.getWorld().spawn(p.getLocation(), TNTPrimed.class);
            tnt.setVelocity(p.getLocation().getDirection().normalize().multiply(2));
            }
            else { //says delete token
                p.sendMessage(ChatColor.RED + "You need to be holding TNT");
              }
        }
    }
     
  9. Offline

    fireblast709

    world.spawnEntity(EntityType)?
     
  10. Offline

    youngbawss22

    I fixed it, but now a new problem appears (yippie!). Whenever i right or left click, it performs the event twice.. so it throws two tnt and takes 2 tnt. Also, if you don't have tnt then it sends you two messages saying You need to be holding TNT. :/

    P.S and i only want it on left clicks, not right clicks...
     
  11. Offline

    Woobie

    Im on my phone now, so I cant show code, but before you check item in hand, do
    Code:
    if(event2.getAction().equals(Action.RIGHT_CLICK_AIR)){ 
    Also when you check item in hand, it end with ;
    Replace it with {

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

    youngbawss22

    That's better, can i make it left click though?

    Anybody know why the event is performed twice?

    Yep, got that :)
     
  13. Offline

    Woobie

    Just change RIGHT to LEFT to make it shoot when left clicked (DUHH!)
    Show me your code, it shouldnt be launching twice.
     
  14. Offline

    youngbawss22

    Awesome! On to the next thing, a cooldown! Can anybody help with this? I am trying to prohibit the player from rapidly left clicking.
     
  15. Offline

    fireblast709

    ArrayLists and scedulers. wiki.bukkit.org/Scheduler_Programming
    Code:java
    1. ArrayList<String> cooling = new ArrayList<String>();
    2. // When letting them cool down
    3. final String name = event.getPlayer().getName();
    4. cooling.add(name);
    5. Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable()
    6. {
    7. public void run()
    8. {
    9. cooling.remove(name);
    10. }
    11. }, cooldown_in_seconds*20L);
     
  16. Offline

    EnvisionRed

    Well, you ended your if statement with a semicolon, so that won't work.
     
  17. Offline

    youngbawss22

    I also ended me and woobie's conversation by saying " Yep, got that" :p
     
  18. Offline

    CeramicTitan

    I have the perfect code for this:

    HERE YOU GO:
    Code:JAVA
    1. if(event.getAction() == Action.RIGHT_CLICK_AIR){
    2. if (player.getItemInHand().getType() == Material.TNT) {
    3. //Frag Grenades
    4. final Item grenade = event.getPlayer().getWorld().dropItem(player.getEyeLocation(), new ItemStack(Material.TNT));
    5. grenade.setVelocity(event.getPlayer().getEyeLocation().getDirection());
    6. player.getInventory().removeItem(grenade.getItemStack());
    7. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    8. @Override
    9. public void run() {
    10. grenade.getWorld().createExplosion(grenade.getLocation(), 4.0F, false);
    11. }
    12. grenade.remove();
    13. }
    14. }, 60L);
    15. event.setCancelled(true);
    16. }
    17. }
    18.  
    19. //So the explosion doesn't break blocks but still damages the player:
    20.  
    21. @EventHandler
    22. public void onGrenadeExplode(EntityExplodeEvent event){
    23. Entity entity = event.getEntity();
    24. if (entity == null) {
    25. event.blockList().clear();
    26. }
    27. }
    28.  
    29.  
    30.  
    31.  
    32.  


    youngbawss22
     
  19. Offline

    Cammy_the_block

    WAIT FOR IT.................
     
  20. Offline

    youngbawss22

    The codes perfect, except for one thing. The remove statement on line 12 requires an "identifier" and i have not idea what it's talking about :p
     
  21. Offline

    CeramicTitan

    it just removes the entity so players can pick it up again.
     
  22. Offline

    youngbawss22

    I know, but it has that notorious red squiggly line beneath it giving me an error. Also, the tnt does not explode D: I must say it throws alot better and smoother.
     
  23. Offline

    CeramicTitan

    what error?
    Also are you using the scheduler right? is this in you main class or separate?
     
  24. Offline

    chaseoes

    It would be better to set to it to air rather than cancel the placement.
     
  25. Offline

    youngbawss22

    The error says " Syntax error on token "remove", Identifier expected after this token "
    Separate class. Not sure how i would put it in the Main.
     
  26. Offline

    CeramicTitan

    Then in the scheduler change "plugin" to "this"

    Also did you ad the "();" to the end of "remove"?
     
  27. Offline

    youngbawss22

    AHH more errors :X For the first thing, i changed it to "this" and then another error appeared on the scheduleSyncDelayedTask. And for the second thing, yes there is a () after the remove.

    Heres the error i am getting in the console.
    Code:
    012-11-29 20:42:09 [SEVERE] Could not pass event PlayerInteractEvent to SuicideBomberExtension v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:341)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
        at org.bukkit.craftbukkit.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:177)
        at org.bukkit.craftbukkit.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:147)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:1024)
        at net.minecraft.server.Packet18ArmAnimation.handle(SourceFile:41)
        at net.minecraft.server.NetworkManager.b(NetworkManager.java:290)
        at net.minecraft.server.NetServerHandler.d(NetServerHandler.java:113)
        at net.minecraft.server.ServerConnection.b(SourceFile:39)
        at net.minecraft.server.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.MinecraftServer.r(MinecraftServer.java:595)
        at net.minecraft.server.DedicatedServer.r(DedicatedServer.java:222)
        at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:493)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:426)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:856)
    Caused by: java.lang.IllegalArgumentException: Plugin cannot be null
        at org.apache.commons.lang.Validate.notNull(Validate.java:203)
        at org.bukkit.craftbukkit.scheduler.CraftScheduler.validate(CraftScheduler.java:391)
        at org.bukkit.craftbukkit.scheduler.CraftScheduler.runTaskTimer(CraftScheduler.java:119)
        at org.bukkit.craftbukkit.scheduler.CraftScheduler.scheduleSyncRepeatingTask(CraftScheduler.java:115)
        at org.bukkit.craftbukkit.scheduler.CraftScheduler.scheduleSyncDelayedTask(CraftScheduler.java:99)
        at com.youngbawss22.bukkit.PlayerInteract.onPlayerInteract(PlayerInteract.java:26)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:339)
        ... 16 more
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  28. Offline

    zRations

    After seeing this, I was intrigued so I decided to test this bit of code out. I used the code below but I did not get any results, no errors, no effects.

    Any ideas on a fix?

    Code:JAVA
    1. @EventHandler
    2. public void TntShoot(PlayerInteractEvent e){
    3. Player p = e.getPlayer();
    4. if(!p.isOp()) {
    5. if(e.getAction().equals(Action.RIGHT_CLICK_AIR)) {
    6. if(e.getPlayer().getItemInHand().getType() == Material.STICK) {
    7. TNTPrimed tnt = (TNTPrimed) p.getWorld().spawn(p.getLocation(), TNTPrimed.class);
    8. tnt.setVelocity(p.getLocation().getDirection().normalize().multiply(2));
    9. }
    10. else {
    11. p.sendMessage(ChatColor.RED + "Error: stick not held");
    12. }
    13. }
    14. }
    15. }
     
  29. Offline

    CeramicTitan

    Could you post you code or if you don't want it public, pm me?
     
  30. Offline

    youngbawss22

    Sure thing (btw it's basically your code :p)
    Code:
    package com.youngbawss22.bukkit;
     
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Item;
    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.entity.EntityExplodeEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.Plugin;
     
    public class PlayerInteract implements Listener {
        public static Main plugin;
     
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event){
        Player p = event.getPlayer();
        if(event.getAction() == Action.LEFT_CLICK_AIR){ //<--- changed to left click
            if (p.getItemInHand().getType() == Material.TNT) {
            final Item grenade = event.getPlayer().getWorld().dropItem(p.getEyeLocation(), new ItemStack(Material.TNT));
            grenade.setVelocity(event.getPlayer().getEyeLocation().getDirection());
            p.getInventory().removeItem(grenade.getItemStack());
            Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() { // "this" doesn't seem to work
       
                @Override
                public void run() {
                    grenade.getWorld().createExplosion(grenade.getLocation(), 4.0F, false);
                    }
                grenade.remove(); //<---- error on remove
                }, 60L);
            event.setCancelled(true);
            }
        }
    }
            @EventHandler
            public void onGrenadeExplode(EntityExplodeEvent event){
            Entity entity = event.getEntity();
            if (entity == null) {
            event.blockList().clear();
            }
        }
    }
    edit: Wait did you mean the whole .jar?
     
Thread Status:
Not open for further replies.

Share This Page