Exploding Arrows help!

Discussion in 'Plugin Development' started by JMSPTGammer, Feb 11, 2014.

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

    JMSPTGammer

    Hey! I'm new to bukkit plugin coding and im trying to make a plugin! Its not really exploding arrows, thats just what im using to learn how to do it. I want so that if you craft a special arrow with an arrow and tnt, it will create an exploding arrow! Then if i use it, it will explode when it hits the ground!

    This is what i have so far:

    Main class:
    Code:java
    1. package me.r0xoW.explodingarrows;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.Material;
    6. import org.bukkit.inventory.ItemStack;
    7. import org.bukkit.inventory.ShapelessRecipe;
    8. import org.bukkit.plugin.PluginDescriptionFile;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. public class Main extends JavaPlugin {
    12.  
    13. public static Main plugin;
    14. public final Logger logger = Logger.getLogger("Minecraft");
    15. public final Listeners ls = new Listeners();
    16.  
    17. @Override
    18. public void onEnable() {
    19. PluginDescriptionFile pdffile = this.getDescription();
    20. this.logger.info(pdffile.getName() + " Version " + pdffile.getVersion() + " Has been Enabled!");
    21. this.getServer().getPluginManager().registerEvents(ls, this);
    22.  
    23. ItemStack is = new ItemStack(Material.ARROW);
    24. ShapelessRecipe sr = new ShapelessRecipe(is).addIngredient(Material.ARROW).addIngredient(Material.TNT);
    25. this.getServer().addRecipe(sr);
    26. }
    27.  
    28. @Override
    29. public void onDisable() {
    30. PluginDescriptionFile pdffile = this.getDescription();
    31. this.logger.info(pdffile.getName() + " Version " + pdffile.getVersion() + " Has been Enabled!");
    32. this.getServer().clearRecipes();
    33. }
    34. }
    35.  


    Listeners Class:
    Code:java
    1. package me.r0xoW.explodingarrows;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5.  
    6. import org.bukkit.Location;
    7. import org.bukkit.Material;
    8. import org.bukkit.entity.Arrow;
    9. import org.bukkit.entity.LivingEntity;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.entity.Projectile;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.Listener;
    14. import org.bukkit.event.entity.ProjectileHitEvent;
    15. import org.bukkit.event.entity.ProjectileLaunchEvent;
    16. import org.bukkit.inventory.ItemStack;
    17. import org.bukkit.metadata.FixedMetadataValue;
    18.  
    19. public class Listeners implements Listener {
    20. public static Main plugin;
    21. public static final List<String> players = new ArrayList<String>();
    22. ItemStack is = new ItemStack(Material.ARROW);
    23.  
    24. @EventHandler
    25. public void onProjectileLaunch(ProjectileLaunchEvent event) {
    26. Projectile projectile = event.getEntity();
    27. LivingEntity shooter = projectile.getShooter();
    28. if (shooter instanceof Player && projectile instanceof Arrow && players.contains(((Player)shooter).getName())) {
    29. projectile.setMetadata("Explosive", new FixedMetadataValue(plugin, true));
    30. }
    31. }
    32.  
    33. @SuppressWarnings("unused")
    34. @EventHandler
    35. public void onProjectileHit(ProjectileHitEvent event) {
    36. Projectile projectile = event.getEntity();
    37. LivingEntity shooter = projectile.getShooter();
    38. if (projectile.hasMetadata("Explosive")){
    39. Location loc = projectile.getLocation();
    40. projectile.getWorld().createExplosion(loc, 10, true);
    41. projectile.remove();
    42. }
    43. }
    44. }
    45.  
     
  2. Offline

    adam753

    Okay. What's the problem? Is it not working, or is there something you don't know how to do?

    Edit: I notice you aren't setting the "plugin" variable in your Listeners class. That's probably causing some problems for you.
     
  3. Offline

    JMSPTGammer

    It doesnt work everytime i launch an arrow! and i want to learn to use recipes for these type of events like one special recipe makes a special item ( a normal item, but changes the name) and only that item makes an explosion
     
  4. Offline

    Timbals

    JMSPTGammer
    Maybe you have to register the listeners?

    Add this to you Listeners class:
    Code:java
    1. public Listeners(Main plugin) {
    2. this.plugin = plugin;
    3. Bukkit.getPluginManager().registerEvents(this, plugin);
    4. }


    And change this in your main class
    Code:java
    1. public final Listeners ls = new Listeners();


    To:
    Code:java
    1. public final Listeners ls;


    and add this in your onEnable
    Code:java
    1. ls = new Listeners(this);
     
  5. Offline

    JMSPTGammer

    gives an error :
     
  6. Offline

    Ethanol2906

    Everything looks to be in order except for one part in your onEnable(). Change the
    Code:java
    1. this.getServer().getPluginManager().registerEvents(ls, this);

    To:
    Code:java
    1. Bukkit.getServer().getPluginManager().registerEvents(this.ls, this);


    I believe that would fix the problem but I may not be sure. It also removes the need to have the:
    Code:java
    1. public Listners(Main plugin) {
    2. this.plugin = plugin;
    3. Bukkit.getServer().getPluginManager().registerEvents(this, plugin);
    4. }
    5.  
     
Thread Status:
Not open for further replies.

Share This Page