Snowball Launching Stick Thingy - Tutorial

Discussion in 'Resources' started by JTGaming2012, Oct 12, 2014.

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

    JTGaming2012

    Hello all! JTGaming2012 here, back with another Bukkit tutorial for beginners!

    Summary:
    So in this tutorial, I will be showing you guys how to create a stick that launches snowballs hence the title, "Snowball Launching Stick Thingy".

    Basics:
    Right, I am assuming you know how to set up your project, if not, ask Mr. Google, otherwise, let's get started!

    1. First, extend 'JavaPlugin'
    Code:java
    1. public class SnowballThingy extends JavaPlugin {
    2.  
    3. }


    2. In the later stages, we will have event listeners so we have to implement 'Listener'.
    Code:java
    1. public class SnowballThingy extends JavaPlugin implements Listener {
    2.  
    3. }


    3. Thirdly, let's create our 'onEnable' and 'onDisable' methods. Now if you didn't know already, the 'onEnable' method gets triggered when the server first runs your plugins. We can use this method to load in config files and other variables.

    The 'onDisable' method is triggered when the server disables the plugins. So in here, we can save data and stuff!

    Both these methods must be included in the main class (even if they are blank) otherwise the plugin will explode...

    Code:java
    1. package me.jack.server;
    2.  
    3. import org.bukkit.plugin.java.JavaPlugin;
    4.  
    5. public class SnowballThingy extends JavaPlugin implements Listener {
    6.  
    7. public void onEnable() {
    8.  
    9. }
    10.  
    11. public void onDisable() {
    12.  
    13. }
    14.  
    15. }


    4. We are also need to tell the plugin that our listeners will be in this class - another important thing. This must go in our, 'onEnable' method, because we want to register these listeners when the server starts.
    Code:java
    1. public void onEnable() {
    2. //The first, 'this' is, "Where is the listener class?".
    3. //The second, 'this' is, "Which is the main class?".
    4. //They both just happen to be the same thing. Nothing consufing about that right?
    5. getServer().getPluginManager().registerEvents(this, this);
    6. }
    7.  


    Interact Event:

    Right, I think it's about time we actually started coding some of the plugin's features.

    So when I code, I plan out very specifically what I want it to do:
    When we interact...
    Check if we clicked the stick...
    Throw a snowball...
    Maybe play a cool sound like a ding...

    Now let's see if we can translate that to code.

    1. Let's create our event listener (this doesn't go inside the 'onEnable' or 'onDisable' methods).
    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent event) {
    3.  
    4. }


    This may seem a lil' bit confuddling...

    So the 'onPlayerInteract' is just a variable for the listener. It can be absolutely anything!

    The, 'PlayerInteractEvent' is the actual event we are going to be listening too.

    And finally, 'event' is where all the variables for this event are stored - stuff like the item they clicked.

    2. Next let's just create a variable to store the player that interacted with something.
    Code:java
    1. Player player = event.getPlayer();


    If you don't understand this, 'Player' creates a variable which stores player relates stuff.

    The second word, 'player' is the variable name. So when we want to recall this player later on, we will refer to it by saying, 'player'.

    Finally, 'event.getPlayer()' gets the player that interacted. Remember when I earlier said about the 'event' storing data? Well here's how that's used!

    3. Now we need to check if they have a stick in their hand. Here's how.
    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent event) {
    3. Player player = event.getPlayer();
    4. if(player.getItemInHand().getType() == Material.STICK) {
    5. }
    6. }


    Now, if the player has a stick in his hand, the code will do everything inside the '{' and '}'.

    4. Finally, let's launch a snowball. All we do here is spawn a snowball where the player is and then set it's velocity so it will fly forward!
    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent event) {
    3. Player player = event.getPlayer();
    4. if(player.getItemInHand().getType() == Material.STICK) {
    5. Snowball snowball = player.getWorld().spawn(player.getLocation(), Snowball.class);
    6. snowball.setVelocity(player.getLocation().getDirection().multiply(3));
    7. }
    8. }


    Once again, we are creating a 'Snowball' variable this time and calling it, 'snowball'. (The variable name should never be the same as the type of variable it is). Then we are assigning a newly spawned snowball to it!

    Next, we simply set the velocity (where it goes and how fast) to the players direction multiplied by 3 (which is actually quite powerful so you might wanna tone it down a bit).

    5. Lastly, let's make it pling!
    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent event) {
    3. Player player = event.getPlayer();
    4. if(player.getItemInHand().getType() == Material.STICK) {
    5. Snowball snowball = player.getWorld().spawn(player.getLocation(), Snowball.class);
    6. snowball.setVelocity(player.getLocation().getDirection().multiply(3));
    7.  
    8. player.playSound(player.getLocation(), Sound.NOTE_PLING, 5, 5);
    9. }
    10. }


    So that concludes the end of this Bukkit Plugin tutorial!
    If you have any questions, just comment them below!

    Here's the final code:
    Code:java
    1. package me.jack.server;
    2.  
    3. import org.bukkit.Material;
    4. import org.bukkit.Sound;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.entity.Snowball;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.player.PlayerInteractEvent;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class SnowballThingy extends JavaPlugin implements Listener {
    13.  
    14. public void onEnable() {
    15.  
    16. getServer().getPluginManager().registerEvents(this, this);
    17. }
    18.  
    19. public void onDisable() {
    20.  
    21. }
    22.  
    23. @EventHandler
    24. public void onPlayerInteract(PlayerInteractEvent event) {
    25. Player player = event.getPlayer();
    26. if(player.getItemInHand().getType() == Material.STICK) {
    27. Snowball snowball = player.getWorld().spawn(player.getLocation(), Snowball.class);
    28. snowball.setVelocity(player.getLocation().getDirection().multiply(3));
    29. player.playSound(player.getLocation(), Sound.NOTE_PLING, 5, 5);
    30. }
    31. }
    32.  
    33. }
     
  2. Offline

    rbrick

  3. Offline

    zombiekiller753

    +1

    Nicely described. No "Here's-the-code-now-copy-paste-it".
     
    GrandmaJam likes this.
  4. This isn't actually true.

    Also, a Snowball is a Projectile, so you can (and probably should) just use the launchProjectile() method. :)
     
  5. Offline

    teej107

    Going off of AdamQpzm statement,
    The onEnable() and onDisable() methods are overridden methods. The original method is located in the Plugin interface (which JavaPlugin implements). You don't need to have them at all in your plugin if you don't need to. Those methods get called regardless if there is anything inside of them or if you override them at all. Since they are overridden methods, it would be extremely nice if you used the @Override annotation for the overridden methods.

    Nice tutorial btw!
     
    AdamQpzm likes this.
  6. Offline

    MineStein

    launchProjectile ;)
     
  7.  
  8. Offline

    JTGaming2012

    @MineStei , AdamQpzm . Ah yeah, didn't really consider 'launchProjectile'. Ah well, at least people will now know how to launch any kind of entity though!
     
  9. Offline

    chasechocolate

    JTGaming2012 only call player.getLocation() once as it creates a new object each time.
     
    ChipDev likes this.
  10. Offline

    croc122

    If you had used launchProjectile, where exactly would you put it? I'm new to bukkit plugin developement, so if you could show an example, that would be great! JTGaming2012
     
  11. Offline

    The Fancy Whale

    Not bad, but this tutorial isn't only about the stick launcher. Next time I suggest that you assume people know how to make a plugin, they just need help making a stick launch a projectile.
     
Thread Status:
Not open for further replies.

Share This Page