Torch arrows of doom?

Discussion in 'Plugin Development' started by suckycomedian, Apr 16, 2012.

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

    suckycomedian

    I'm making a plugin where you place a torch and then it shoots an arrow.. and then the arrow explodes. im just making random plugins to learn cause im new to this. how to i make the arrow that's fired when i place the torch explode when it hits the ground?

    @EventHandler(priority = EventPriority.NORMAL)
    public void onBlockPlace(BlockPlaceEvent event){
    Player player = event.getPlayer();
    Block block = event.getBlockPlaced();


    if(block.getType() == Material.TORCH);
    player.shootArrow();

    }

    public void onProjectileHit(ProjectileHitEvent event){
    Entity entity = event.getEntity();

    if (entity instanceof Arrow){
    Arrow arrow = (Arrow) entity;
    Entity shooter = arrow.getShooter();

    if(shooter instanceof Player){
    Player player = (Player) shooter;

    if(plugin.enabledPlayers.contains(player.getName()));
    player.getWorld().createExplosion(arrow.getLocation(), 5F);
     
  2. Your doing it right! Does your listener works right? (Maybe add a System.out.print("Hail to the king!") or something like that to check if your listener works.)

    Also don't forget the @EventHandler before your onProjectileHit. If it's still not working please place your full code and preferably between code tags ([ code ] and [ /code ] for the format)
     
  3. Offline

    suckycomedian

    It worked!! All I had to do was put the event handler... lol I forgot about that. thanks!

    okay, so i just realized that anytime i place any block down it shoots an arrow and a blow up... do you see the error that's causing that? it would have to be in the first event

    nvm! i realized that I didn't bracket the command. the fix was

    if(block.getType() == Material.TORCH){
    player.shootArrow();
    }

    if anyone feels like telling me (i haven't even started looking at it yet, gonna do it tomorrow morning), i want to make it so that you can do something like /BlowTorch_add_<playername> and it would add them to this plugin.. how would i do that? I was going to set up the array tomorrow as well

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

    Lolmewn

    For the command, register it in the plugin.yml.
    Then in your main class (extending JavaPlugin), add a new method, the onCommand method (it's a boolean btw).

    There are many tutorials available on how to do commands, but I recommend adding the player to a HashSet, then in your listener check if the player is in the HashSet.
     
  5. Offline

    suckycomedian

    huh?

    okay so what I'm wanting to do now is have it so when someone places a torch, they shoot an arrow which explodes. Right now, when they place a torch it shoots an arrow and explodes, but if they shoot an arrow with a bow that arrow also explodes. I'm wondering if there's a way to tie the block place event with the projectile hit event so they both have to happen in order for the arrow to explode. here's my code

    Code:java
    1.  
    2.  
    3. @EventHandler(priority = EventPriority.NORMAL)
    4. public void onBlockPlace(BlockPlaceEvent event){
    5. Player player = event.getPlayer();
    6. Block block = event.getBlockPlaced();
    7.  
    8.  
    9. if((block.getType() == Material.TORCH) &&
    10. plugin.torchEnabledPlayers.contains(player.getName())){
    11. player.shootArrow();
    12. }
    13. }
    14. @EventHandler
    15. public void onProjectileHit(ProjectileHitEvent event){
    16. Entity entity = event.getEntity();
    17.  
    18. if (entity instanceof Arrow){
    19. Arrow arrow = (Arrow) entity;
    20. Entity shooter = arrow.getShooter();
    21.  
    22. if(shooter instanceof Player){
    23. Player player = (Player) shooter;
    24.  
    25. if(plugin.torchEnabledPlayers.contains(player.getName()));
    26. player.getWorld().createExplosion(arrow.getLocation(), 5F);
    27.  
    28. }
    29.  
    30.  
    31.  
    32. }
    33. }
    34. }
    35.  


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

    Njol

    Create a static HashSet<Arrow> where you put all arrows spawned in block place events, then on projectile hit only create an explosion if the event's arrow is in the set (and remove it from the set).
     
    suckycomedian likes this.
  7. Offline

    suckycomedian

    hmm... How would I do that? when I put a hashset into the block place event, it got mad at the Player player thing. this is what i did, it's probably wrong.. and im not sure where it goes in the projectile hit event. sorry lol. really new to this.

    Code:java
    1.  
    2. @EventHandler(priority = EventPriority.NORMAL)
    3. public void onBlockPlace(BlockPlaceEvent event){
    4. public static HashSet<Arrow>
    5.  
    6. Player player = event.getPlayer();
    7. Block block = event.getBlockPlaced();
    8.  
    9.  
    10. if((block.getType() == Material.TORCH) &&
    11. plugin.torchEnabledPlayers.contains(player.getName())){
    12.  
    13. }
    14. }
    15. @EventHandler
    16. public void onProjectileHit(ProjectileHitEvent event){
    17. Entity entity = event.getEntity();
    18.  
    19. if (entity instanceof Arrow){
    20. Arrow arrow = (Arrow) entity;
    21. Entity shooter = arrow.getShooter();
    22.  
    23. if(shooter instanceof Player){
    24. Player player = (Player) shooter;
    25.  
    26. if(plugin.torchEnabledPlayers.contains(player.getName()));
    27. player.getWorld().createExplosion(arrow.getLocation(), 5F);
    28. arrow.remove();
    29.  
    30. }
    31.  
    32.  
    33.  
    34. }
    35. }
    36.  
     
  8. Offline

    Njol

    You have to define it as a variable of course, i.e. it has to have a name. And since it's static you must put it outside of any functions (which is the only way two different functions can access the same variable - local variables are only accessible to the function they're defined in)
     
  9. Offline

    suckycomedian

    ok thanks! also, do you know what entity.doesBounce means? i want to make an arrow bounce, but when that alone is active it does nothing. if i put entity.setBounce(true) nothing happens and the event it is currently in this

    Code:java
    1.  
    2. @EventHandler
    3. public void onProjectileHit(ProjectileHitEvent event){
    4. Entity entity = event.getEntity();
    5.  
    6.  
    7.  
    8. if (entity instanceof Arrow){
    9. Arrow arrow = (Arrow) entity;
    10. Entity shooter = arrow.getShooter();
    11.  
    12. if(shooter instanceof Player){
    13. Player player = (Player) shooter;
    14.  
    15. if(plugin.bounceEnabledPlayers.contains(player.getName())){
    16. arrow.doesBounce();
    17.  
    18.  
    19. }
    20.  
    21.  
    22.  
    23. }
    24. }
    25. }
    26. }
    27.  


    but i don't think it's supposed to be in a projectile hit event

    this is what I have right now. idk what to do with the HashSet, though. I need to create a variable for it but idk what to put for that or where to put it in the code.​
    Code:java
    1. public class TorchListener implements Listener{
    Code:java
    1.  
    2. [LEFT] [/LEFT]
    3. [LEFT] [/LEFT]
    4. [LEFT]private BlowTorch plugin;[/LEFT]
    5. [LEFT]public TorchListener(BlowTorch plugin){[/LEFT]
    6. [LEFT]this.plugin = plugin;[/LEFT]
    7. [LEFT]}[/LEFT]
    8. [LEFT] [/LEFT]
    9. [LEFT]@EventHandler(priority = EventPriority.NORMAL)[/LEFT]
    10. [LEFT]public void onBlockPlace(BlockPlaceEvent event){[/LEFT]
    11. [LEFT] [/LEFT]
    12. [LEFT]Player player = event.getPlayer();[/LEFT]
    13. [LEFT]Block block = event.getBlockPlaced();[/LEFT]
    14. [LEFT] [/LEFT]
    15. [LEFT]if((block.getType() == Material.TORCH) &&[/LEFT]
    16. [LEFT]plugin.torchEnabledPlayers.contains(player.getName())){[/LEFT]
    17. [LEFT]player.shootArrow();[/LEFT]
    18. [LEFT]HashSet.put(Arrow);[/LEFT]
    19. [LEFT] [/LEFT]
    20. [LEFT]}[/LEFT]
    21. [LEFT]}[/LEFT]
    22. [LEFT]@EventHandler[/LEFT]
    23. [LEFT]public void onProjectileHit(ProjectileHitEvent event){[/LEFT]
    24. [LEFT]Entity entity = event.getEntity();[/LEFT]
    25. [LEFT]if (HashSet.contains(Arrow)){[/LEFT]
    26. [LEFT]if (entity instanceof Arrow){[/LEFT]
    27. [LEFT]Arrow arrow = (Arrow) entity;[/LEFT]
    28. [LEFT]Entity shooter = arrow.getShooter();[/LEFT]
    29. [LEFT] [/LEFT]
    30. [LEFT]if(shooter instanceof Player){[/LEFT]
    31. [LEFT]Player player = (Player) shooter;[/LEFT]
    32. [LEFT] [/LEFT]
    33. [LEFT]if(plugin.torchEnabledPlayers.contains(player.getName()));[/LEFT]
    34. [LEFT]player.getWorld().createExplosion(arrow.getLocation(), 5F);[/LEFT]
    35. [LEFT]arrow.remove();[/LEFT]
    36. [LEFT] [/LEFT]
    37. [LEFT]}[/LEFT]
    38. [LEFT]}[/LEFT]
    39. [LEFT]}[/LEFT]
    40. [LEFT]}[/LEFT]
    41. [LEFT]}[/LEFT]
    42. [LEFT][/LEFT]


    idk what's up with the left thing

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

    suckycomedian

    So I abandoned the arrow thing lol. anyway, I'm making it so that you can add a play to this event. I have it add null[0] to the array and I know that it successfully adds and removes them from the array, however it won't read the listener now. can anyone see what is wrong?

    main:
    Code:java
    1.  
    2. public class BlowTorch extends JavaPlugin{
    3.  
    4. protected ArrayList<String> torchEnabledPlayers;
    5.  
    6. public void onEnable(){
    7.  
    8. this.torchEnabledPlayers = new ArrayList<String>();
    9.  
    10. this.getCommand("trolltorch").setExecutor(new TorchExecutor(this));
    11.  
    12. PluginManager manager = this.getServer().getPluginManager();
    13.  
    14. manager.registerEvents( new TorchListener(this), this);
    15.  
    16. }
    17. public void onDisable(){
    18. }
    19. }
    20.  

    Listener:
    Code:java
    1.  
    2. public class TorchListener implements Listener{
    3. private BlowTorch plugin;
    4. public TorchListener(BlowTorch plugin){
    5. this.plugin = plugin;
    6. }
    7.  
    8. @EventHandler(priority = EventPriority.NORMAL)
    9. public void onBlockPlace(BlockPlaceEvent event){
    10.  
    11. Player player = event.getPlayer();
    12. Block block = event.getBlockPlaced();
    13.  
    14. if((block.getType() == Material.TORCH) &&
    15. plugin.torchEnabledPlayers.contains(player.getName())){
    16. player.getWorld().createExplosion(player.getLocation(), 15F);
    17. player.sendMessage("this should do something");
    18.  
    19. }
    20. }
    21. }

    Executor:
    Code:java
    1.  
    2. public class TorchExecutor implements CommandExecutor{
    3. private BlowTorch plugin;
    4. public TorchExecutor(BlowTorch plugin){
    5. this.plugin = plugin;
    6. }
    7.  
    8. public boolean onCommand(CommandSender sender, Command command, String label, String[] args){
    9.  
    10. Player player = (Player) sender;
    11. if(!(player.isOp())){
    12. player.sendMessage(ChatColor.RED + "You don't have access to that command!");
    13.  
    14. }else{
    15.  
    16. if(plugin.torchEnabledPlayers.contains(args[0])){
    17. plugin.torchEnabledPlayers.remove(args[0]);
    18. Bukkit.getServer().broadcastMessage(ChatColor.RED + "" + plugin.torchEnabledPlayers);
    19. player.sendMessage(ChatColor.RED + "Troll Torch Disabled for " + args [0]);
    20.  
    21. }else{
    22.  
    23. plugin.torchEnabledPlayers.add(args[0]);
    24. Bukkit.getServer().broadcastMessage("" + plugin.torchEnabledPlayers);
    25. player.sendMessage(ChatColor.GREEN + "Troll Torch Enabled for " + args[0]);
    26. }
    27. }
    28. return true;
    29. }
    30. }


    I guess it technically does read the listener, but I don't think it get's the player's name from the array cause it won't cause the event anymore
     
Thread Status:
Not open for further replies.

Share This Page