GTA:

Discussion in 'Plugin Development' started by beaudigi, Nov 29, 2013.

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

    beaudigi

    Hi guys Ive been working on my 1st Java plugin, however I dont really know how to code....
    I watched a video to do this, but I dont undertsand how it works, so I dont know how to do more:
    Code:java
    1. package me.simplerocks.uhs;
    2.  
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.Material;
    6. import org.bukkit.entity.Fireball;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.block.Action;
    11. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    12. import org.bukkit.event.player.PlayerInteractEvent;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14.  
    15. public class UHS extends JavaPlugin implements Listener {
    16.  
    17. public void onEnable() {
    18. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    19. }
    20.  
    21. @EventHandler
    22. public void onPlayerInteract(PlayerInteractEvent e) {
    23. if (!(e.getAction() == Action.RIGHT_CLICK_AIR)) return;
    24.  
    25. if (!(e.getItem().getType() == Material.FURNACE)) return;
    26.  
    27. Fireball f = e.getPlayer().launchProjectile(Fireball.class);
    28. f.setIsIncendiary(false);
    29. f.setYield(0);
    30. }
    31.  
    32. @EventHandler
    33. public void onEntityDamage(EntityDamageByEntityEvent e) {
    34. if (e.getDamager() instanceof Fireball) {
    35. Fireball f = (Fireball) e.getDamager();
    36. if (f.getShooter() instanceof Player) {
    37. Player shooter = (Player) f.getShooter();
    38. if (shooter.getItemInHand().getType() == Material.FURNACE) {
    39. e.setDamage(10.0);
    40. }
    41. }
    42. }
    43. }
    44. }



    I know that that this shoots a fireball from a dispenser, ddoes 10 hearts nad dosent damage the environment, but can someone explain how it does this specifically.

    As well as this if I wanted to another gun underneath this, how would i do it.
    And how would I make it run of ammo with slight cooldowns.

    If possible, could you explain this like im an idiot as Im quite a java noob.
     
  2. Offline

    skilcox

    You can use http://jd.bukkit.org/rb/apidocs/ to read up on all of the code you used.
    For example to read about your onPlayerInteract method ctrl+f for PlayerInteractEvent.

    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent e) {
    3. //called every time a player interacts
    4. if (!(e.getAction() == Action.RIGHT_CLICK_AIR)) return;
    5. //checks if the player right clicked air
    6. if (!(e.getItem().getType() == Material.FURNACE)) return;
    7. //checks if the player holds a furnace
    8. Fireball f = e.getPlayer().launchProjectile(Fireball.class);
    9. //creates a fireball
    10. f.setIsIncendiary(false);
    11. //sets the fireball to not cause fire
    12. f.setYield(0);
    13. //sets the fireball to have no explosion
    14. }
    15.  
    16. @EventHandler
    17. public void onEntityDamage(EntityDamageByEntityEvent e) {
    18. //called every time something takes damage
    19. if (e.getDamager() instanceof Fireball) {
    20. //checks if the damage is from a fireball
    21. Fireball f = (Fireball) e.getDamager();
    22. //sets a variable equal to the entity that caused the damage
    23. if (f.getShooter() instanceof Player) {
    24. //checks if the damager is a player
    25. Player shooter = (Player) f.getShooter();
    26. //sets a variable equal to the causer of damage player
    27. if (shooter.getItemInHand().getType() == Material.FURNACE) {
    28. //checks if that player holds a furnace
    29. e.setDamage(10.0);
    30. //sets damage received to 10
    31. }
    32. }
    33. }
    34. }
    35. }
     
  3. Offline

    AoH_Ruthless

    beaudigi
    The Bukkit API is hard to learn if you don't have an understanding of Java. I would spend time learning Java first, THEN learning the Bukkit API and coding.

    But the easiest way to learn HOW something does what it does is to follow skilcox instructions
     
  4. Offline

    beaudigi

    Thankyou for all ypur support guys, I will use your methods :D
    Um but how do I Say start a new gun underneath the one posted above,, as it always comes up with an error..when I just copy paste that gun below it

    Code:java
    1. package me.simplerocks.uhs;
    2.  
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.Material;
    6. import org.bukkit.entity.Fireball;
    7. import org.bukkit.entity.Player;
    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.EntityDamageByEntityEvent;
    13. import org.bukkit.event.player.PlayerInteractEvent;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15.  
    16. public class UHS extends JavaPlugin implements Listener {
    17.  
    18. public void onEnable() {
    19. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    20. }
    21.  
    22. @EventHandler
    23. public void onPlayerInteract(PlayerInteractEvent e) {
    24. //called every time a player interacts
    25. if (!(e.getAction() == Action.RIGHT_CLICK_AIR)) return;
    26. //checks if the player right clicked air
    27. if (!(e.getItem().getType() == Material.FURNACE)) return;
    28. //checks if the player holds a furnace
    29. Fireball f = e.getPlayer().launchProjectile(Fireball.class);
    30. //creates a Verbal
    31. f.setIsIncendiary(false);
    32. //sets the Verbal to not cause fire
    33. f.setYield(0);
    34. //sets the Verbal to have no explosion
    35. }
    36.  
    37. @EventHandler
    38. public void onEntityDamage(EntityDamageByEntityEvent e) {
    39. //called every time something takes damage
    40. if (e.getDamager() instanceof Fireball) {
    41. //checks if the damage is from a verbal
    42. Fireball f = (Fireball) e.getDamager();
    43. //sets a variable equal to the entity that caused the damage
    44. if (f.getShooter() instanceof Player) {
    45. //checks if the damager is a player
    46. Player shooter = (Player) f.getShooter();
    47. //sets a variable equal to the causer of damage player
    48. if (shooter.getItemInHand().getType() == Material.FURNACE) {
    49. //checks if that player holds a furnace
    50. e.setDamage(10.0); [SIZE=5][FONT=arial black] (im Getting an error here: it says wrong syntax }. I have replaced it but it still comes up with this issue)[/FONT][/SIZE]
    51. //sets damage received to 10
    52.  
    53.  
    54. @EventHandler
    55. public void onPlayerInteract1(PlayerInteractEvent e) {
    56. //called every time a player interacts
    57. if (!(e.getAction() == Action.RIGHT_CLICK_AIR)) return;
    58. //checks if the player right clicked air
    59. if (!(e.getItem().getType() == Material.INK_SACK)) return;
    60. //checks if the player holds a furnace
    61. Snowball s = e.getPlayer().launchProjectile(Snowball.class);
    62.  
    63. }
    64.  
    65. @EventHandler
    66. public void onEntityDamage1(EntityDamageByEntityEvent e) {
    67. //called every time something takes damage
    68. if (e.getDamager() instanceof Snowball) {
    69. //checks if the damage is from a verbal
    70. Snowball s = (Snowball) e.getDamager();
    71. //sets a variable equal to the entity that caused the damage
    72. if (s.getShooter() instanceof Player) {
    73. //checks if the damager is a player
    74. Player shooter = (Player) s.getShooter();
    75. //sets a variable equal to the causer of damage player
    76. if (shooter.getItemInHand().getType() == Material.INK_SACK) {
    77. //checks if that player holds a furnace
    78. e.setDamage(2.0);
    79. //sets damage received to 10
    80. }
    81. }
    82. }
    83. }
    84. }
    '



    Btw How do I add ammo and a cooldown, ive been watching some more vids, but I think its easier when you guys explain it :p

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

    np98765

    Moved to Plugin Development.
     
  6. Offline

    UltiFix

    It's great that your learning how to code but, you also need to learn to be self sufficient. So

    asking whenever you get stuck isn't going to be so great in the future. Just saying. ;)
     
  7. Offline

    beaudigi

    Ok im still learning the lingo of the forums. i wish to be an active member :D
    Ive fixed the problem ive been having and added a pistol type weapon. Although it is very similar :D
    Im going to only post when needed now, as ill try watch a video on how to do it first.
     
  8. Offline

    UltiFix

    Thanks! Sorry if I sound like a jerk, I'm not trying to be. Just trying to keep peopling from getting bugged and hopefully this will help you learn to be self sufficient.
     
  9. Offline

    skilcox

    you could also do an else if in the same method, or make a method that takes input from the listener method to clean up and keep it small
     
Thread Status:
Not open for further replies.

Share This Page