ThrowTNT

Discussion in 'Plugin Development' started by Mr_Squidels, Nov 25, 2013.

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

    Mr_Squidels

    Hello all, if you are reading this, I need help on "throwing tnt". I've tried many things to fix it but it is still not working!

    If you could just please make a code for me from scratch and take notes off this one? Thanks!

    My current code:

    Code:
    import org.bukkit.*;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.TNTPrimed;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.util.Vector;
     
    public class Main extends JavaPlugin
        implements Listener
    {
     
        public Main()
        {
        }
     
        public void onEnable()
        {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
     
        @EventHandler
        public void onPlayerInteractEvent(PlayerInteractEvent event)
        {
            Player player = event.getPlayer();
            if(player.getInventory().getItemInHand().getType() == Material.TNT && event.getAction() == Action.RIGHT_CLICK_AIR)
                if(player.hasPermission("tnt.use"))
                {
                    TNTPrimed tnt = (TNTPrimed)player.getWorld().spawn(player.getEyeLocation(), EntityType.PRIMED_TNT);
                    tnt.setVelocity(player.getEyeLocation().getDirection().multiply(2));
                    tnt.setFuseTicks(40);
                    int amount = player.getInventory().getItemInHand().getAmount();
                    if(--amount == 0)
                        player.getInventory().removeItem(new ItemStack[] {
                            new ItemStack(Material.TNT)
                        });
                    else
                        player.getInventory().getItemInHand().setAmount(amount);
                } else
                {
                    player.sendMessage((new StringBuilder()).append(ChatColor.RED).append("You are not allowed to throw TNT.").toString());
                }
        }
    }
    Hopefully you can help me, thanks.
     
  2. Offline

    Deleted user

  3. Offline

    Garris0n

    You need an @EventHandler annotation above your listener.
     
  4. Offline

    Mr_Squidels

    No, it's org/bukkit/entity/TNTPrimed. It just gives an error, I thought it would work because I saw this bit from other peoples throwing TNT thing. I've tried TNTPrimed.class to but that works but it makes an error somewhere else, sorry, I'm on my ipad I can't check it now, got to go to school! Thanks
     
  5. Offline

    xTrollxDudex

    Mr_Squidels
    You spawn TNTPrimed.class, not the package name :p
     
  6. Offline

    Conarnar

    Code:java
    1. TNTPrimed tnt = (TNTPrimed)player.getWorld().spawn(player.getEyeLocation(), org/bukkit/entity/TNTPrimed);

    should be
    Code:java
    1. TNTPrimed tnt = player.getWorld().spawn(player.getEyeLocation(), TNTPrimed.class);

    or
    Code:java
    1. TNTPrimed tnt = (TNTPrimed)player.getWorld().spawnEntity(player.getEyeLocation(), EntityType.PRIMED_TNT);

    NINJA'D :'(
     
  7. Offline

    Mr_Squidels

    Awesome, it's all working but I just want to add one more thing, a 3 second cool down to use the tnt. I've seen the Schedular programming page and stuff, but I can't seem to set it up with this so could you please help me? :D
     
  8. Offline

    GreatMinerZ

  9. Offline

    NathanWolf

    Don't use schedulers for cooldowns (for goodness sake, this is such a common problem- and now we get to watch a 7 minute long video to tell us the wrong way to do it?)

    Just store the last time each player used the command in a hashmap. Check the time when using the command to make sure they've used it long enough ago. Pretty simple really, and not broken like it will be if you use a scheduled task.
     
  10. Offline

    Mr_Squidels

    Hmm, that didn't really work. Can someone else please help me?

    bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  11. Offline

    maxben34

    Your problem should be solved by now. The only way we could help you is if you give you insight into what your current problem is.
     
  12. Offline

    BungeeTheCookie

    He wants to know how to make a cooldown.
     
  13. Offline

    NathanWolf

    What about it didn't work? I explained how to handle cool downs for this, did you try it? Get an error? Have any code or stack traces to share?
     
  14. Offline

    Mr_Squidels

    I attempted to try it but I couldn't find out a way to. So someone please help me.:'(
     
  15. Offline

    BungeeTheCookie

  16. Offline

    Mr_Squidels

    BungeeTheCookie I've tried to implement it but it didn't work. Can you just try to implement it to my code above. thanks
     
  17. Offline

    xTrollxDudex

    Mr_Squidels
    This isn't a "code for you" section

    Go to plugin requests if that is your only intent of coming here.
     
Thread Status:
Not open for further replies.

Share This Page