Need help making an object throw-able

Discussion in 'Plugin Development' started by WinX-9, Oct 3, 2012.

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

    WinX-9

    Hi,
    I making a Call Of Duty plugin for my server and I'm trying to make objects throw-able and then explode after a couple of seconds. Any help would be great!
     
  2. kind of like dropping a green dye on the ground and then it cause an explotion? should be easy, i think. won't get much range on it though
     
  3. Offline

    WinX-9

    Ok, I just wanted to make something like a snowball give you blindness if you are to close to where the snowball exploded.

    Could anyone please send me the code to do this? Sorry, I'm kind of a beginner :(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  4. ok, the only things that can be thrown in the game is snowball, enderpearl and egg, if i'm not mistaken. you get make it explode on impact, if that's what you want, but no one will give you code just like that, you're supposed to learn.
     
  5. Offline

    gomeow

    People would actually give him the code, but still he should learn
     
  6. Offline

    Loogeh

    Okay so, this is really easy. I spent a long time figuring this out even though it was super easy lol.

    My code for slimeball grenades

    Code:
        @EventHandler
        public void slimeGrenade(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            World world = player.getWorld();
            if (player.hasPermission("perm.here")) {
                if (player.getItemInHand().getType() == Material.SLIME_BALL) {
                    if (event.getAction() == Action.LEFT_CLICK_AIR) {
                        final Item grenade = world.dropItem(player.getEyeLocation(), new ItemStack(Material.SLIME_BALL));
                        grenade.setVelocity(player.getEyeLocation().getDirection());
                        plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable() {
     
                                    @Override
                                    public void run() {
                                        grenade.getWorld().createExplosion(grenade.getLocation(), 0.0F);
                                        grenade.remove();
                                    }
                                }, 40L);
                    }
                }
            }
        }
    to make any item throwable you just do
    Item grenade = world.dropItem(player.getEyeLocation() new ItemStack(Material.YOURMATERIAL);
    grenade.setVelocity(player.getEyeLocation().getDirection());

    code for snowball grenades

    Code:
        @EventHandler
        public void onHit(ProjectileHitEvent event)
        {
          if (event.getEntity() instanceof Snowball)
          {
            Entity e = event.getEntity();
            Location loc = event.getEntity().getLocation();
            World world = event.getEntity().getWorld();
            world.playEffect(loc, Effect.STEP_SOUND, 50);
            List<Entity> entities = e.getNearbyEntities(5, 5, 5);
            for (Entity entity : entities)
            {
                if (entity instanceof Player)
                {
                  ((Player)entity).addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 200, 0));
                  ((Player)entity).addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 200, 7));
                  ((Player)entity).addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 200, 1));
                    }
                }
            }
        }




    I'm that guy. :>

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
    fromgate, chasechocolate and WinX-9 like this.
  7. Offline

    WinX-9

    Thanks so much! I'll study the code and put it in my Call Of Duty plugin :)

    Also, would the code make an explosion effect? If not do you mind adding that please, thanks.

    PS, You've earned a like Loogeh!

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

    Loogeh

    The slimeball creates an explosion with a delay, it does not damage to players or blocks though because the power is set to 0.0F (F = float)
     
  9. Offline

    WinX-9

    Oh, one more thing, I was looking at you're code and the
    Code:
    if (event.getAction() == Action.LEFT_CLICK_AIR) {
    can I change the LEFT_CLICK_AIR to RIGHT_CLICK_AIR?
     
  10. Offline

    brord

    Id say try it
    Thats where you learn the most of
     
    TwistedMexi likes this.
  11. Offline

    Vandrake

    brord WinX-9 Loogeh gomeow
    Btw it is possible to throw any kind of items not just eggs/snowballs and... enderpearls.
    I did a slime ball throwing plugin just a few weeks ago, that created a fake explosion after a configurable amount of time and blinded nearby players(radius also configurable)
     
  12. Offline

    brord

    Didnt Loogeh made that a couple of posts up? 0.o
     
  13. Offline

    Vandrake

    oh he did o.o I didnt even notice
     
  14. Offline

    WinX-9

    Do you mind if you send me the code? If not I fully understand :)
     
  15. Offline

    Vandrake

    Loogeh did it up there. Go check it out o.o his code is actually a bit different than mine but does nearly the same o.o
     
  16. Offline

    WinX-9

    Thanks!
     
  17. Offline

    Deonasoto

    Cool guys!
     
  18. Offline

    WinX-9

    For Loogeh's code what would I have to import?
     
  19. Offline

    Royal_Soda

    WinX-9
    You have to set up your own Java Project. The only thing you should do is add Craftbukkit as an external jar. Other than that, just hover over the red lines and hit import. ;)
     
  20. Offline

    WinX-9

    Thanks Royal_Soda!

    Here is my main class code so far:
    Code:
    package com.grenade.semtex;
     
    import java.awt.Desktop.Action;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.entity.Item;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class GrenadeSemtex extends JavaPlugin {
     
        @EventHandler
        public void slimeGrenade(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            World world = player.getWorld();
            if (player.hasPermission("perm.here")) {
                if (player.getItemInHand().getType() == Material.SLIME_BALL) {
                    if (event.getAction() == Action.LEFT_CLICK_AIR) {
                        final Item grenade = world.dropItem(player.getEyeLocation(), new ItemStack(Material.SLIME_BALL));
                        grenade.setVelocity(player.getEyeLocation().getDirection());
                        plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable() {
     
                                    @Override
                                    public void run() {
                                        grenade.getWorld().createExplosion(grenade.getLocation(), 0.0F);
                                        grenade.remove();
                                    }
                                }, 40L);
                    }
                }
            }
        }
    }
    
    Does anyone see any problems with this?

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

    Royal_Soda

    WinX-9
    That won't work. You need to fix it up, here is what I did:

    MainClass:
    Code:
    package plugin.pluginhere;
     
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class MainClass extends JavaPlugin {
       
        public final ListenerClass Listener = new ListenerClass();
     
        public void onEnable() {
           
            PluginManager pm = getServer().getPluginManager();
           
            pm.registerEvents(this.Listener, this);
           
            getLogger().info("PLUGIN is now enabled.");
           
        }
       
        public void onDisable() {
           
            getLogger().info("PLUGIN is now disabled.");
           
        }
       
    }
    ListenerClass:
    Code:
    package plugin.pluginhere;
     
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.World;
    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.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.Plugin;
     
    public class ListenerClass implements Listener {
       
        @EventHandler
        public void slimeGrenade(PlayerInteractEvent event) {
           
            Player player = event.getPlayer();
            World world = player.getWorld();
            Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin("PLUGINNAME");
           
            if (player.hasPermission("perm.here")) {
               
                if (player.getItemInHand().getType() == Material.SLIME_BALL) {
                   
                    if (event.getAction() == Action.LEFT_CLICK_AIR) {
                       
                        final Item grenade = world.dropItem(player.getEyeLocation(), new ItemStack(Material.SLIME_BALL));
                        grenade.setVelocity(player.getEyeLocation().getDirection());
                        plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable() {
     
                                    @Override
                                    public void run() {
                                        grenade.getWorld().createExplosion(grenade.getLocation(), 0.0F);
                                        grenade.remove();
                                    }
                                }, 40L);
                    }
                }
            }
        }
     
    }
     
    WinX-9 likes this.
  22. Offline

    Loogeh

    You need the onEnable and onDisable methods in this class (as it is the main class) also, because you're using events. You need to implement Listener aswell.
     
    WinX-9 likes this.
  23. Offline

    Royal_Soda

    What Loogeh said, is what I did. Try using it.
     
    WinX-9 likes this.
  24. Offline

    Loogeh

  25. Offline

    WinX-9

    Thanks again guys!

    Ok, this now what I have for my MainClass:

    Code:
    package com.grenade.semtex;
     
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
         
    public class MainClass {
         
            public final ListenerClass Listener = new ListenerClass();
       
            public void onEnable() {
             
                PluginManager pm = getServer().getPluginManager();
             
                pm.registerEvents(this.Listener, this);
             
                getLogger().info("Semtex is now enabled.");
             
            }
         
            public void onDisable() {
             
                getLogger().info("Semtex is now disabled.");
             
            }
         
        }
    }
    
    Here is what I have for my ListenerClass:

    Code:
    package com.grenade.semtex;
     
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.World;
    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.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.Plugin;
     
    public class ListenerClass implements Listener {
     
          @EventHandler
          public void slimeGrenade(PlayerInteractEvent event) {
             
                Player player = event.getPlayer();
                World world = player.getWorld();
                Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin("PLUGINNAME");
             
                if (player.hasPermission("perm.here")) {
                 
                    if (player.getItemInHand().getType() == Material.SLIME_BALL) {
                     
                        if (event.getAction() == Action.LEFT_CLICK_AIR) {
                         
                            final Item grenade = world.dropItem(player.getEyeLocation(), new ItemStack(Material.SLIME_BALL));
                            grenade.setVelocity(player.getEyeLocation().getDirection());
                            plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable() {
       
                                        @Override
                                        public void run() {
                                            grenade.getWorld().createExplosion(grenade.getLocation(), 0.0F);
                                            grenade.remove();
                                        }
                                    }, 40L);
                        }
                    }
                }
            }
       
        }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  26. Offline

    Loogeh

    Does it work?
     
  27. Offline

    WinX-9

    Trying it right now :)

    LOL, I forgot to make Plugin.yml [coal]

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

    Loogeh

    Haha :>
     
  29. Offline

    WinX-9

    Hmmm, still doesn't work. Can it be in a file folder not a .jar?

    let me show you my plugin.yml file

    name: grenade.semtex
    main: com.grenade.semtex.MainClass
    version: 1

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

    Royal_Soda

Thread Status:
Not open for further replies.

Share This Page