[Tutorial] Simple Guns

Discussion in 'Resources' started by negative_codezZ, Oct 25, 2013.

?

Was this helpful?

  1. Yes

    16 vote(s)
    47.1%
  2. No

    10 vote(s)
    29.4%
  3. Kind of

    8 vote(s)
    23.5%
Thread Status:
Not open for further replies.
  1. Offline

    negative_codezZ

    Hey guys!

    I decided to make a tutorial today on simple guns. I will make it so that when you right click a blaze rod, it shoots a snowball. When that snowball hits something, it will have some effects! First, we need to create our main class in which we will be registering our events:

    Code:java
    1. package me.negative_codezZ.blazegun;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.event.Listener;
    5. import org.bukkit.plugin.java.JavaPlugin;
    6.  
    7. public class Main extends JavaPlugin {
    8.  
    9. public void onEnable() {
    10. Bukkit.getServer().getPluginManager().registerEvents(new Events(), this);
    11. }
    12. }
    13.  


    Now that we've registered our events, let's use them! First, we will make it so that when a player right clicks the air with a blaze rod, it will launch a snowball:

    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(final PlayerInteractEvent e) {
    3. if(!(e.getAction() == Action.RIGHT_CLICK_AIR)) return; //if the player does not right click the air, it will stop the code right there.
    4. if(!(e.getItem().getType() == Material.BLAZE_ROD)) return; //if the item that the player right clicked with is not a Blaze Rod, it will stop the code right there.
    5. Snowball s = e.getPlayer().launchProjectile(Snowball.class); //here we launch the snowball
    6. s.getWorld().playEffect(e.getPlayer().getLocation(), Effect.SMOKE, 10); //We will play a smoke effect at the shooter's location
    7. }


    *Remember this is inside our Events class.

    Now that we've done that, we want to make it do something different when the snowball hits an object so we will create another event:

    Code:java
    1. @EventHandler
    2. public void onProjectileHit(ProjectileHitEvent e) {
    3. Projectile p = e.getEntity(); // states that we will now be able to call the projectile "p"
    4. if(!(p instanceof Snowball)) return; // if the projectile is not a snowball, the code will stop
    5. Snowball s = (Snowball) p; // states that the snowball will not be called "s"
    6. s.getWorld().createExplosion(s.getLocation(), 5.0F); //we create a 10 block explosion where the snowball lands
    7. for(Entity en : s.getNearbyEntities(5, 30, 5)) { //we get the entities in a 5 block radius of where the snowball hit and call them "en"
    8. if(en instanceof Player) { // we check that "en" are players
    9. Player pl = (Player) en; // we call the player "ens" "pl"
    10. if(!(pl == e.getEntity().getShooter())) pl.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 100, 0)); // we add blindness to all of these "pls" for 5 seconds. In minecraft, 20 ticks is one second. Therefore 20*5 = 100. No effect will be added to the shooter.
    11. }
    12. }
    13.  


    Now, we just need to create the "plugin.yml":

    Code:
    name: BlazeGun
    main: me.negative_codezZ.blazegun.Main
    version: 1.0
    description: Make a blaze rod a gun!
    We are done! Main class:

    Code:java
    1. package me.negative_codezZ.blazegun;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.plugin.java.JavaPlugin;
    5.  
    6. public class Main extends JavaPlugin {
    7.  
    8. public void onEnable() {
    9. Bukkit.getServer().getPluginManager().registerEvents(new Events(), this);
    10. }
    11. }
    12.  


    Events class:

    Code:java
    1. package me.negative_codezZ.blazegun;
    2.  
    3. import org.bukkit.Effect;
    4. import org.bukkit.Material;
    5. import org.bukkit.entity.Entity;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.entity.Projectile;
    8. import org.bukkit.entity.Snowball;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.block.Action;
    12. import org.bukkit.event.entity.ProjectileHitEvent;
    13. import org.bukkit.event.player.PlayerInteractEvent;
    14. import org.bukkit.potion.PotionEffect;
    15. import org.bukkit.potion.PotionEffectType;
    16.  
    17. public class Events implements Listener {
    18.  
    19. @EventHandler
    20. public void onPlayerInteract(final PlayerInteractEvent e) {
    21. if(!(e.getAction() == Action.RIGHT_CLICK_AIR)) return;
    22. if(!(e.getItem().getType() == Material.BLAZE_ROD)) return;
    23. Snowball s = e.getPlayer().launchProjectile(Snowball.class);
    24. s.getWorld().playEffect(e.getPlayer().getLocation(), Effect.SMOKE, 10);
    25. }
    26.  
    27. @EventHandler
    28. public void onProjectileHit(ProjectileHitEvent e) {
    29. Projectile p = e.getEntity();
    30. if(!(p instanceof Snowball)) {
    31. return;
    32. }
    33. Snowball s = (Snowball) p;
    34. s.getWorld().createExplosion(s.getLocation(), 5.0F);
    35. for(Entity en : s.getNearbyEntities(5, 30, 5)) {
    36. if(en instanceof Player) {
    37. Player pl = (Player) en;
    38. if(!(pl == e.getEntity().getShooter())) pl.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 100, 0));
    39. }
    40. }
    41. }
    42.  
    43. }
    44.  


    plugin.yml:

    Code:
    name: BlazeGun
    main: me.negative_codezZ.blazegun.Main
    version: 1.0
    description: Make a blaze rod a gun!
    We are done! Thanks! Drop a like if it helped! Main
     
    MarinD99, user_90854156 and JPG2000 like this.
  2. Offline

    negative_codezZ

    Thanks for pointing that out! Un-needed import.
     
  3. Offline

    DevRosemberg

    negative_codezZ I dont understand why are you actually created this "Tutorial" for one of the simplest ways to do this.
     
    Divinity Realms and JPG2000 like this.
  4. Offline

    negative_codezZ

    Because tutorials are to teach people. People are learning Java and this is a simple lesson.
     
  5. Offline

    DevRosemberg

    negative_codezZ I know, i appreciate that you are helping people but i really think you should help them with harder stuff. Thats why you are not getting replies and views.

    Kind Regards
     
  6. Offline

    SacredWaste

    DevRosemberg The point is negative_codezZ is trying to teach people how to shoot projectiles out of items. If you know how to do this, bravo to you. This is to teach those that don't know how to. If you already know, you can either say you think its a good tutorial or not; or just leave.
     
    JPG2000 likes this.
  7. Offline

    DevRosemberg

  8. Offline

    Ultimate_n00b

    This is a fine tutorial. I suggest you fix your indentation so I can read the code easier, but it isn't bad to post stuff like this. Resources isn't a "Sacred Section" only for difficult stuff. Simple things like this can exist here as well.
     
  9. Offline

    SacredWaste

    DevRosemberg I'm talking to you. You think negative_codezZ should make harder tutorials, and I was replying to you that the tutorial is fine. You should not simply be telling him to make harder tutorials because this is easy for you.
     
    negative_codezZ likes this.
  10. Offline

    negative_codezZ

    Thanks. I did post it with correct indentation. Weird...
    Thank you.
     
  11. Offline

    Ultimate_n00b

    The indentation gets ruined on editing.
     
  12. Offline

    negative_codezZ

    Oh, thanks for pointing that out.
     
  13. Offline

    PieMan456

    negative_codezZ
    Hey when I am adding the potion effect .addPotionEffect(PotionEffectType.Harm, 20, 2) it says addPotionEffect is wrong and it cant have what I typed inside the parentheses after it. Do you know what is wrong?
     
  14. Offline

    negative_codezZ

    This is the way you add potion effects:

    Code:java
    1. p.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, 8000000, 1));
    2.  


    That's an infinite Night Vision effect.
     
  15. Offline

    PieMan456

  16. Offline

    negative_codezZ

    You're welcome! Any time!
     
  17. Offline

    dmoney12321

    This was a good tutorial. It helped me (A beginner) A lot. Keep making these please.
    negative_codezZ
     
  18. Offline

    negative_codezZ

    Thank you very much. I am glad. I will, soon.
     
  19. Offline

    MarinD99

    Awesome tutorial! Thanks dude! Have you ever thought about creating youtube videos? You seem like an excellent teacher :)
     
  20. Offline

    viper_monster

  21. Offline

    MarinD99

  22. Offline

    xTrollxDudex

    negative_codezZ
    Title should be "Simple plugin tutorial" cause you know. This teaches you how to make an entire plugin
     
  23. Offline

    beaudigi

    This was a great tut it helped me ( java noob) learn some new things.Howver can you add that your guns have sounds and run of ammo>?
     
  24. Offline

    negative_codezZ

    Thank you!

    To play a sound, get an object, for example:

    Code:java
    1.  
    2. Snowball s = (Snowball) p; // states that the snowball will not be called "s"


    Then, in order to play the sound, do the following:

    Code:java
    1. s.playSound(LOCATION - Example: s.getLocation(), SOUND - Example: Sound.NOTE_PLING, FLOAT - Recommended: 10F, FLOAT - Recommended: 10F); //Plays the sound "NOTE_PLING" at the snowballs location.


    As of for running out of ammo, do something like this at the top of the class:
    Code:java
    1. private int ammo = (AMOUNT);


    And in the PlayerInteractEvent, make sure the ammo is not less than the ammo stated at the top of the class, for example:

    Code:
    @EventHandler
    public void onPlayerInteract(final PlayerInteractEvent e) {
    if(!(this.ammo < integer that you put)) { //makes sure the ammo isn't less than the amount needed
    e.getPlayer().sendMessage("§cYou ran out of ammo!");
    return;
    }
    if(!(e.getAction() == Action.RIGHT_CLICK_AIR)) return;
    if(!(e.getItem().getType() == Material.BLAZE_ROD)) return;
    Snowball s = e.getPlayer().launchProjectile(Snowball.class);
    this.ammo--; //reduces the ammo by 1
    s.getWorld().playEffect(e.getPlayer().getLocation(), Effect.SMOKE, 10);
    }
    There you go!

    <Removed>

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

    PieMan456

    negative_codezZ
    Hey my snowball will not shoot. Here is my class:
    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent e){
    3. if(e.getAction() != Action.RIGHT_CLICK_AIR || e.getAction() != Action.RIGHT_CLICK_BLOCK) return;
    4. if(e.getItem().getType() != Material.STICK) return;
    5. if(e.getPlayer().getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.AQUA + "Snowball Shooter")){
    6. Snowball s = e.getPlayer().launchProjectile(Snowball.class);
    7. s.setMetadata("bullet", new FixedMetadataValue(this, true));
    8. }
    9. }
     
  26. Offline

    negative_codezZ

    You are setting Metadata to a projectile which is now turning into an entity.
     
  27. Offline

    beaudigi

    One more thing:
    can you make the gun emmit 1 heart of damage on impact (and also 1./2 heart damage to the user) and have a coldown?

    Can you please tell me why this dosent work
    Show Spoiler

    Code:
    @EventHandler
        public void onPlayerInteract1(PlayerInteractEvent e) {
            //called every time a player interacts
            if (!(e.getAction() == Action.RIGHT_CLICK_AIR)) return;
            //checks if the player right clicked air
            if (!(e.getItem().getType() == Material.WOOD_HOE)) return;
            //checks if the player holds a furnace
            Snowball s = e.getPlayer().launchProjectile(Snowball.class);
                s.getWorld().playEffect(e.getPlayer().getLocation(), Effect.SMOKE, 10);
                s.getWorld().playSound(e.getPlayer().getLocation(), Sound.GHAST_SCREAM, 10, 0);
                }
    The actual gun works (the last bit of it is below this (i can post it if you want). the sound and effect dont work.

    Heres the other part of the code :D (decided to post it )
    Code:
        @EventHandler
        public void onEntityDamage1(EntityDamageByEntityEvent e) {
                //called every time something takes damage
                if (e.getDamager() instanceof Snowball) {
                    //checks if the damage is from a verbal
                    Snowball s = (Snowball) e.getDamager();
                    //sets a variable equal to the entity that caused the damage
                    if (s.getShooter() instanceof Player) {
                        //checks if the damager is a player
                        Player shooter = (Player) s.getShooter();
                        //sets a variable equal to the causer of damage player
                        if (shooter.getItemInHand().getType() == Material.WOOD_HOE) {
                            //checks if that player holds a furnace
                            e.setDamage(4.0);
                            //sets damage received to 4



    So heres the first problem, but using the example you showed and some modifications tis gun dosent work at all:
    Show Spoiler

    Code:
    @EventHandler
    public void onPlayerInteract11(final PlayerInteractEvent e) {
        if(!(e.getAction() == Action.RIGHT_CLICK_AIR)) return;
        if(!(e.getItem().getType() == Material.GOLD_INGOT)) return;
        Snowball t = e.getPlayer().launchProjectile(Snowball.class);
        t.getWorld().playEffect(e.getPlayer().getLocation(), Effect.SMOKE, 10);
        t.getWorld().playSound(e.getPlayer().getLocation(), Sound.GHAST_SCREAM, 10, 0);
        }
     
    @EventHandler
    public void onProjectileHit(ProjectileHitEvent e) {
        Projectile p = e.getEntity();
        if(!(p instanceof Snowball)) {
            return;
            }
        Snowball t = (Snowball) p;
        t.getWorld().createExplosion(t.getLocation(), 5.0F);
        for(Entity en : t.getNearbyEntities(5, 30, 5)) {
            if(en instanceof Player) {
                Player pl = (Player) en;
                if(!(pl == e.getEntity().getShooter())) pl.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 100, 2));


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

    PieMan456

    negative_codezZ
    So how would I set the metadata of the snowball that shoots?
     
  29. Offline

    negative_codezZ

    I do not see an edit on the second one.
     
Thread Status:
Not open for further replies.

Share This Page