[Gun] Cooldowns....

Discussion in 'Plugin Development' started by ASHninja1997, Sep 7, 2013.

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

    ASHninja1997

    So i am trying to make it to where when a player interacts with a wood_hoe it shoots a arrow. Now i have that down pat, but the only problem is that players can spam the arrows because there is no cool down. Now i looked up Cool down's and tried putting in the code myself but it still didn't work. So, the code is down below......I would want the cool down to be 1.5 seconds. I am not asking you to make the code for me i am just asking on how you can achieve this.
    Code:java
    1. @EventHandler
    2. public void onInteract(PlayerInteractEvent e) {
    3. Player p = e.getPlayer();
    4. if (e.getAction() == Action.RIGHT_CLICK_AIR) {
    5. if (p.getItemInHand().getType() == Material.WOOD_HOE) {
    6. if (!p.getInventory().containsAtLeast(
    7. new ItemStack(Material.FLINT), 1)) {
    8. p.sendMessage(ChatColor.DARK_AQUA
    9. + "You need flint to shoot!");
    10. p.playSound(p.getLocation() , Sound.ANVIL_LAND, 1f,3f);
    11. e.setCancelled(true);
    12. } else {
    13. p.getInventory().removeItem(
    14. new ItemStack(Material.FLINT, 1));
    15. Entity arrow = p.launchProjectile(Arrow.class);
    16. arrow.setVelocity(p.getLocation().getDirection()
    17. .multiply(1.5f));
    18. arrow.getWorld().playEffect(arrow.getLocation(), Effect.SMOKE, 25);
    19. arrow.getWorld().playEffect(arrow.getLocation(), Effect.BOW_FIRE, 25);
    20. p.updateInventory();
    21. }
    22. } else {
    23. return;
    24. }
    25. }
    26. }


    I have to go for a bit brb in a couple of minutes.

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

    DevRosemberg

    Simply use Bukkit Runables and make an arraylist of players cooled:

    Code:java
    1. public ArrayList<String> cooled = new ArrayList<>();


    So when they shoot add them to it and then do:

    Code:java
    1. this.plugin.getServer().getScheduler().scheduleSyncDelayedTask(this.plugin, new Runnable() {
    2. public void run() {
    3. this.cooled.remove(player.getName());
    4. }
    5. }
    6. , 1.5 * 20L);


    Have fun with this.
     
    McMhz likes this.
  3. Offline

    XgXXSnipz

    i get an error on this.plugin
     
  4. Offline

    Gopaintman

    Don't use "this" because its in a separate Runnable class within your plugin class. Just use cooled.whatever in the runnable method.
     
  5. Offline

    XgXXSnipz

    and where would i add this?

    Code:java
    1. cooled.getScheduler().scheduleSyncDelayedTask(cooled, new Runnable() {
    2. public void run() {
    3. cooled.remove(p.getName());

    I get an error on: getScheduler() and cooled.remove(p.getName());

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

    Hellborn5456

    XgXXSnipz 1.you should learn basic java first.
    2. cooled.remove(p,getName()); is a arraylist string
    3. make a arraylist called cooled
     
  7. Offline

    XgXXSnipz

    Bump

    Code:java
    1. public ArrayList<String> Player = new ArrayList<>();

    Like this?

    Code:java
    1. public class Gunz extends JavaPlugin implements Listener {
    2.  
    3. public void onEnable(){
    4. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    5. }
    6.  
    7. public ArrayList<String> cooled = new ArrayList<>();
    8.  
    9. @EventHandler
    10. public void onInteract(PlayerInteractEvent e) {
    11. Player p = e.getPlayer();
    12. if (e.getAction() == Action.RIGHT_CLICK_AIR) {
    13. if (p.getItemInHand().getType() == Material.STICK) {
    14. if (!p.getInventory().containsAtLeast(
    15. new ItemStack(Material.FLINT), 1)) {
    16. p.sendMessage(ChatColor.RED
    17. + "You need bullets to shoot!");
    18. p.playSound(p.getLocation() , Sound.ANVIL_LAND, 1f,3f);
    19. e.setCancelled(true);
    20. } else {
    21. p.getInventory().removeItem(
    22. new ItemStack(Material.FLINT, 1));
    23. Entity arrow = p.launchProjectile(Snowball.class);
    24. arrow.setVelocity(p.getLocation().getDirection()
    25. .multiply(1.5f));
    26. arrow.getWorld().playSound(p.getLocation(), Sound.FIREWORK_BLAST2, 1.0F, 2.0F);
    27. }
    28. }
    29. } else {
    30. return;
    31. }
    32.  
    33. }
    34. @EventHandler
    35. public void onEntityDamage(EntityDamageByEntityEvent e) {
    36. if (e.getDamager() instanceof Snowball) {
    37. Snowball f = (Snowball) e.getDamager();
    38. if (f.getShooter() instanceof Player) {
    39. Player shooter = (Player) f.getShooter();
    40. if (shooter.getItemInHand().getType() == Material.BLAZE_ROD);
    41. e.setDamage(5.5);
    42. f.getWorld().playEffect(f.getLocation(), Effect.SMOKE, 100);
    43.  
    44. }
    45. }
    46. }
    47. }


    Here is my full code, someone please help im using this for a minigame!

    Code:java
    1. public ArrayList<Player> cooled = new ArrayList<Player>();


    here is my arraylist

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

    Ironraptor3

    XgXXSnipz

    *sorry for caps but* NO DONT SAVE PLAYERS TO ARRAY LISTS (OR ANY KIND OF LIST)
    this causes memory leaks, crashes, etc...

    save them by their name

    use: public static ArrayList<String> cooling = new ArrayList<String>();

    and add players with: cooling.add(player.getName());
     
  9. Offline

    XgXXSnipz

    Sorry for caps: CAN SOME PLEASE TELL ME WHIY I GET AN ERROR ON THIS PART
    Code:java
    1. cooling.getScheduler()
    2. player.getName());


    Code:java
    1. package me.JoeyLangston.Gunz;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Effect;
    8. import org.bukkit.Material;
    9. import org.bukkit.Sound;
    10. import org.bukkit.craftbukkit.v1_7_R1.entity.CraftPlayer;
    11. import org.bukkit.entity.Entity;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.entity.Snowball;
    14. import org.bukkit.event.EventHandler;
    15. import org.bukkit.event.Listener;
    16. import org.bukkit.event.block.Action;
    17. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    18. import org.bukkit.event.player.PlayerInteractEvent;
    19. import org.bukkit.inventory.ItemStack;
    20. import org.bukkit.plugin.java.JavaPlugin;
    21.  
    22. public class Gunz extends JavaPlugin implements Listener {
    23.  
    24. public static ArrayList<String> cooling = new ArrayList<String>();
    25.  
    26. public void onEnable(){
    27. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    28. }
    29.  
    30.  
    31.  
    32. @EventHandler
    33. public void onInteract(PlayerInteractEvent e) {
    34. Player p = e.getPlayer();
    35. if (e.getAction() == Action.RIGHT_CLICK_AIR) {
    36. if (p.getItemInHand().getType() == Material.STICK) {
    37. if (!p.getInventory().containsAtLeast(
    38. new ItemStack(Material.FLINT), 1)) {
    39. p.sendMessage(ChatColor.RED
    40. + "Your outta bullets!");
    41. p.playSound(p.getLocation() , Sound.ANVIL_LAND, 1f,3f);
    42. e.setCancelled(true);
    43. } else {
    44. p.getInventory().removeItem(
    45. new ItemStack(Material.FLINT, 1));
    46. Entity arrow = p.launchProjectile(Snowball.class);
    47. arrow.setVelocity(p.getLocation().getDirection()
    48. .multiply(4.5f));
    49. arrow.getWorld().playEffect(arrow.getLocation(), Effect.SMOKE, 100);
    50. arrow.getWorld().playSound(p.getLocation(), Sound.FIREWORK_BLAST2, 5.0F, 5.0F);
    51. ((CraftPlayer)p).getHandle().updateInventory(((CraftPlayer)p).getHandle().activeContainer);
    52. }
    53. }
    54. } else {
    55. return;
    56. }
    57.  
    58.  
    59. }
    60. @EventHandler
    61. public void onEntityDamage(EntityDamageByEntityEvent e) {
    62. if (e.getDamager() instanceof Snowball) {
    63. Snowball f = (Snowball) e.getDamager();
    64. if (f.getShooter() instanceof Player) {
    65. Player shooter = (Player) f.getShooter();
    66. if (shooter.getItemInHand().getType() == Material.BLAZE_ROD);
    67. e.setDamage(5.5);
    68.  
    69.  
    70. }
    71. }
    72. }
    73. }
    Here is my full code where do i insert the cooling thing

    BUMP

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

    Jaaakee224

    You can only bump every 24 hours.
     
  11. Offline

    XgXXSnipz

  12. Offline

    DeGambler

    XgXXSnipz
    Please...stop spamming...

    Seriously though, YOU NEED TO LEARN JAVA, if you can't even get a simple Runnable working.
    Show Spoiler

    Fufu.. now I feel bad heres a basic runnable!
    Code:java
    1. new BukkitRunnable() {
    2.  
    3. @Override
    4. public void run() {
    5. // Stuffs to run (Aka remove them from from the array)
    6. }
    7. }.runTaskLater(this, 20L); // 20L Ticks = 1 second

     
    bobacadodl likes this.
  13. Offline

    mattibijnens

    Great tutorial here!

     
  14. Offline

    XgXXSnipz

    Code:java
    1. public class Gunz extends JavaPlugin implements Listener {
    2.  
    3. public static ArrayList<String> cooling = new ArrayList<String>();
    4.  
    5.  
    6.  
    7.  
    8. public void onEnable(){
    9. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    10.  
    11. new BukkitRunnable() {
    12.  
    13. @Override
    14. public void run() {
    15. // Stuffs to run (Aka remove them from from the array)
    16. }
    17. }.runTaskLater(this, 50L); } // 20L Ticks = 1 second
    18.  
    19.  
    20.  
    21. @EventHandler
    22. public void onInteract(PlayerInteractEvent e) {
    23. Player p = e.getPlayer();
    24. if (e.getAction() == Action.RIGHT_CLICK_AIR) {
    25. if (p.getItemInHand().getType() == Material.STICK) {
    26. if (!p.getInventory().containsAtLeast(
    27. new ItemStack(Material.FLINT), 1)) {
    28. p.sendMessage(ChatColor.RED
    29. + "Your outta bullets!");
    30. p.playSound(p.getLocation() , Sound.ANVIL_LAND, 1f,3f);
    31. e.setCancelled(true);
    32. } else {
    33. p.getInventory().removeItem(
    34. new ItemStack(Material.FLINT, 1));
    35. Entity arrow = p.launchProjectile(Snowball.class);
    36. arrow.setVelocity(p.getLocation().getDirection()
    37. .multiply(4.5f));
    38. arrow.getWorld().playEffect(arrow.getLocation(), Effect.SMOKE, 100);
    39. arrow.getWorld().playSound(p.getLocation(), Sound.FIREWORK_BLAST2, 5.0F, 5.0F);
    40. ((CraftPlayer)p).getHandle().updateInventory(((CraftPlayer)p).getHandle().activeContainer);
    41.  
    42.  
    43. }
    44.  
    45. cooling.getScheduler().scheduleSyncDelayedTask(cooling, new Runnable() {
    46. public void run() {
    47. cooling.remove(player.getName());
    48.  
    49.  
    50.  
    51. }
    52. } else {
    53. return;
    54. }
    55.  
    56.  
    57. }
    58. @EventHandler
    59. public void onEntityDamage(EntityDamageByEntityEvent e) {
    60. if (e.getDamager() instanceof Snowball) {
    61. Snowball f = (Snowball) e.getDamager();
    62. if (f.getShooter() instanceof Player) {
    63. Player shooter = (Player) f.getShooter();
    64. if (shooter.getItemInHand().getType() == Material.BLAZE_ROD);
    65. e.setDamage(5.5);
    66.  
    67.  
    68. }
    69. }
    70. }
    71. }


    why do i get an error on player.getName()); the error message is :syntax error, insert ; to complete statment
     
  15. Offline

    DrTURTLE2


    Oh goodness..
     
  16. Offline

    XgXXSnipz

    I know, Dude im fairly decent at java, just having a massive brain cloud covering my java knowledge it seems

    Can someone just redo my code and repost it for me, im not begging but this seems to be really hard to do for me at the moment

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

    DrTURTLE2

    If you were decent at Java you would know what that meant.
     
  18. Offline

    Superckl1

    Insert a semicolon ( ; ) to complete the statement...
     
    McMhz likes this.
  19. Offline

    XgXXSnipz

    Code:java
    1. package me.JoeyLangston.Gunz;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Effect;
    8. import org.bukkit.Material;
    9. import org.bukkit.Server;
    10. import org.bukkit.Sound;
    11. import org.bukkit.craftbukkit.v1_7_R1.entity.CraftPlayer;
    12. import org.bukkit.entity.Entity;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.entity.Snowball;
    15. import org.bukkit.event.EventHandler;
    16. import org.bukkit.event.Listener;
    17. import org.bukkit.event.block.Action;
    18. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    19. import org.bukkit.event.player.PlayerInteractEvent;
    20. import org.bukkit.inventory.ItemStack;
    21. import org.bukkit.plugin.java.JavaPlugin;
    22. import org.bukkit.scheduler.BukkitRunnable;
    23.  
    24. public class Gunz extends JavaPlugin implements Listener {
    25.  
    26. public static ArrayList<String> cooling = new ArrayList<String>();
    27.  
    28. public void onEnable(){
    29. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    30.  
    31. 2.}
    32.  
    33. @EventHandler
    34. public void onInteract(PlayerInteractEvent e) {
    35. Player p = e.getPlayer();
    36. if (e.getAction() == Action.RIGHT_CLICK_AIR) {
    37. if (p.getItemInHand().getType() == Material.STICK) {
    38. if (!p.getInventory().containsAtLeast(
    39. new ItemStack(Material.FLINT), 1)) {
    40. p.sendMessage(ChatColor.RED
    41. + "Your outta bullets!");
    42. p.playSound(p.getLocation() , Sound.ANVIL_LAND, 1f,3f);
    43. e.setCancelled(true);
    44. } else {
    45. p.getInventory().removeItem(
    46. new ItemStack(Material.FLINT, 1));
    47. Entity arrow = p.launchProjectile(Snowball.class);
    48. arrow.setVelocity(p.getLocation().getDirection()
    49. .multiply(4.5f));
    50. arrow.getWorld().playEffect(arrow.getLocation(), Effect.SMOKE, 100);
    51. arrow.getWorld().playSound(p.getLocation(), Sound.FIREWORK_BLAST2, 5.0F, 5.0F);
    52. ((CraftPlayer)p).getHandle().updateInventory(((CraftPlayer)p).getHandle().activeContainer); }}}
    53.  
    54.  
    55. new BukkitRunnable() {
    56.  
    57. @Override
    58. public void run() {
    59.  
    60. ((Server) cooling).getScheduler().scheduleSyncDelayedTask(cooling, new Runnable(){
    61. public void run() {
    62. cooling.remove(p.getName());
    63. }
    64. }.runTaskLater(this, 50L));}
    65.  
    66.  
    67.  
    68. @EventHandler
    69. public void onEntityDamage(EntityDamageByEntityEvent e) {
    70. if (e.getDamager() instanceof Snowball) {
    71. Snowball f = (Snowball) e.getDamager();
    72. if (f.getShooter() instanceof Player) {
    73. Player shooter = (Player) f.getShooter();
    74. if (shooter.getItemInHand().getType() == Material.BLAZE_ROD);
    75. e.setDamage(5.5);
    76.  
    77.  
    78. }
    79. }
    80. 1,}

    I hate java, stupid bracket errors i get an error on these ones

    Thats the problem, i did and it still wont work

    Code:java
    1. package me.JoeyLangston.Gunz;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Effect;
    8. import org.bukkit.Material;
    9. import org.bukkit.Sound;
    10. import org.bukkit.craftbukkit.v1_7_R1.entity.CraftPlayer;
    11. import org.bukkit.entity.Entity;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.entity.Snowball;
    14. import org.bukkit.event.EventHandler;
    15. import org.bukkit.event.Listener;
    16. import org.bukkit.event.block.Action;
    17. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    18. import org.bukkit.event.player.PlayerInteractEvent;
    19. import org.bukkit.inventory.ItemStack;
    20. import org.bukkit.plugin.java.JavaPlugin;
    21. import org.bukkit.scheduler.BukkitRunnable;
    22.  
    23. public class Gunz extends JavaPlugin implements Listener {
    24.  
    25. public static ArrayList<String> cooling = new ArrayList<String>();
    26.  
    27.  
    28.  
    29.  
    30. public void onEnable(){
    31. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    32.  
    33. new BukkitRunnable() {
    34.  
    35. @Override
    36. public void run() {
    37. // Stuffs to run (Aka remove them from from the array)
    38. }
    39. }.runTaskLater(this, 50L); } // 20L Ticks = 1 second
    40.  
    41.  
    42.  
    43. @EventHandler
    44. public void onInteract(PlayerInteractEvent e) {
    45. Player p = e.getPlayer();
    46. if (e.getAction() == Action.RIGHT_CLICK_AIR) {
    47. if (p.getItemInHand().getType() == Material.STICK) {
    48. if (!p.getInventory().containsAtLeast(
    49. new ItemStack(Material.FLINT), 1)) {
    50. p.sendMessage(ChatColor.RED
    51. + "Your outta bullets!");
    52. p.playSound(p.getLocation() , Sound.ANVIL_LAND, 1f,3f);
    53. e.setCancelled(true);
    54. } else {
    55. p.getInventory().removeItem(
    56. new ItemStack(Material.FLINT, 1));
    57. Entity arrow = p.launchProjectile(Snowball.class);
    58. arrow.setVelocity(p.getLocation().getDirection()
    59. .multiply(4.5f));
    60. arrow.getWorld().playEffect(arrow.getLocation(), Effect.SMOKE, 100);
    61. arrow.getWorld().playSound(p.getLocation(), Sound.FIREWORK_BLAST2, 5.0F, 5.0F);
    62. ((CraftPlayer)p).getHandle().updateInventory(((CraftPlayer)p).getHandle().activeContainer);
    63.  
    64.  
    65. }
    66.  
    67. cooling.getScheduler().scheduleSyncDelayedTask(cooling, new Runnable() {
    68. public void run() {
    69. cooling.remove(player.getName());
    70.  
    71.  
    72.  
    73. }
    74. } else {
    75. return;
    76. }
    77.  
    78.  
    79. }
    80. @EventHandler
    81. public void onEntityDamage(EntityDamageByEntityEvent e) {
    82. if (e.getDamager() instanceof Snowball) {
    83. Snowball f = (Snowball) e.getDamager();
    84. if (f.getShooter() instanceof Player) {
    85. Player shooter = (Player) f.getShooter();
    86. if (shooter.getItemInHand().getType() == Material.BLAZE_ROD);
    87. e.setDamage(5.5);
    88.  
    89.  
    90. }
    91. }
    92. }
    93. }


    here is my new code, it still wont work, i keep getting the same error

    Aloha?????

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

    XgXXSnipz

  21. Offline

    phildachil

    ... Hover over your error and eclipse will tell you what to do >_>
    Code:
     final Player p = e.getPlayer();
    And really stop spamming no one is gonna give you your full code. I'm gonna give you a tip to use something else than p.launchprojectile.Arrow.class

    You seem to have no java knowledge what-so-ever if you can't fix a very simple error.
     
  22. Offline

    XgXXSnipz

    Well, Im trying to learn! So stop being so rude about it, bucky tuts. are no good
     
    McMhz likes this.
  23. Offline

    McMhz

    Make an arraylist, like so
    Code:java
    1. List<String> cplayers = new ArrayList<String>();


    Then before shooting the arrow and checking if they have a bullet, Check if he is in cplayers and if not then
    Code:java
    1. if(cplayers.contains(p.getName()){
    2. //dont shoot, and maybe make some kind of sound?
    3.  
    4. }else{
    5. //Check for ammo, shoot and all that and then make a runnable.
    6.  
    7. new BukkitRunnable() {
    8.  
    9. @Override
    10. public void run() {
    11. cplayers.remove(p.getName());
    12. }
    13. }.runTaskTimer(this, 40, 40);
    14.  
    15. }


    Should work.

    We were all new to this once, no need to be so rude.

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

    XgXXSnipz

    Thanks bro, your the only one that is nice to me on this forum :D

    Code:java
    1. package me.JoeyLangston.Gunz;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.Effect;
    9. import org.bukkit.Material;
    10. import org.bukkit.Sound;
    11. import org.bukkit.craftbukkit.v1_7_R1.entity.CraftPlayer;
    12. import org.bukkit.entity.Entity;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.entity.Snowball;
    15. import org.bukkit.event.EventHandler;
    16. import org.bukkit.event.Listener;
    17. import org.bukkit.event.block.Action;
    18. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    19. import org.bukkit.event.player.PlayerInteractEvent;
    20. import org.bukkit.inventory.ItemStack;
    21. import org.bukkit.plugin.java.JavaPlugin;
    22. import org.bukkit.scheduler.BukkitRunnable;
    23.  
    24. public class Gunz extends JavaPlugin implements Listener {
    25.  
    26. List<String> cplayers = new ArrayList<String>();
    27.  
    28.  
    29. public void onEnable(){
    30. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    31. }
    32.  
    33.  
    34.  
    35.  
    36.  
    37. @EventHandler
    38. public void onInteract(PlayerInteractEvent e) {
    39. final Player p = e.getPlayer();
    40.  
    41. if (e.getAction() == Action.RIGHT_CLICK_AIR) {
    42.  
    43. if (p.getItemInHand().getType() == Material.STICK) {
    44.  
    45. if (!p.getInventory().containsAtLeast(
    46. new ItemStack(Material.FLINT), 1)) {
    47. p.sendMessage(ChatColor.RED
    48. + "Your outta bullets!");
    49. p.playSound(p.getLocation() , Sound.ANVIL_LAND, 1f,3f);
    50. e.setCancelled(true);
    51. } else {
    52.  
    53. p.getInventory().removeItem(
    54. new ItemStack(Material.FLINT, 1));
    55. if(cplayers.contains(p.getName()));
    56. //dont shoot, and maybe make some kind of sound?
    57.  
    58. //Check for ammo, shoot and all that and then make a runnable.
    59.  
    60. new BukkitRunnable() {
    61.  
    62. @Override
    63. public void run() {
    64. cplayers.remove(p.getName());
    65. }
    66. }.runTaskTimer(this, 40, 40);
    67.  
    68. }
    69. Entity arrow = p.launchProjectile(Snowball.class);
    70. arrow.setVelocity(p.getLocation().getDirection()
    71. .multiply(4.5f));
    72. arrow.getWorld().playEffect(arrow.getLocation(), Effect.SMOKE, 100);
    73. arrow.getWorld().playSound(p.getLocation(), Sound.FIREWORK_BLAST2, 5.0F, 5.0F);
    74. ((CraftPlayer)p).getHandle().updateInventory(((CraftPlayer)p).getHandle().activeContainer); }}}
    75.  
    76.  
    77. @EventHandler
    78. public void onEntityDamage(EntityDamageByEntityEvent e) {
    79. if (e.getDamager() instanceof Snowball) {
    80. Snowball f = (Snowball) e.getDamager();
    81. if (f.getShooter() instanceof Player) {
    82. Player shooter = (Player) f.getShooter();
    83. if (shooter.getItemInHand().getType() == Material.STICK);
    84. e.setDamage(5.5);
    85.  
    86.  
    87. }
    88. }
    89. }
    90. }

    like this? please dont get mad :(

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

    McMhz

    No problem, If you need any more help just tag me.

    I think you got it nearly right, Except for that one ; you put instead of a { at
    Code:java
    1. if(cplayers.contains(p.getName()));
    2.  

    It should be
    Code:java
    1. if(cplayers.contains(p.getName())){

    Just remove the ; and put the { instead.

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

    XgXXSnipz

    ah ok, going to test :D
     
  27. Offline

    McMhz

    Also, Before the BukkitRunnable you have to add the player to the cplayers list.
     
  28. Offline

    XgXXSnipz

    on a unrelated note, please check this out for me http://forums.bukkit.org/threads/shop.226883/#post-2222046

    didnt work, oh wait no im reading your new post

    didnt work :( here is my full code :
    Code:java
    1. package me.JoeyLangston.Gunz;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.Effect;
    9. import org.bukkit.Material;
    10. import org.bukkit.Sound;
    11. import org.bukkit.craftbukkit.v1_7_R1.entity.CraftPlayer;
    12. import org.bukkit.entity.Entity;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.entity.Snowball;
    15. import org.bukkit.event.EventHandler;
    16. import org.bukkit.event.Listener;
    17. import org.bukkit.event.block.Action;
    18. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    19. import org.bukkit.event.player.PlayerInteractEvent;
    20. import org.bukkit.inventory.ItemStack;
    21. import org.bukkit.plugin.java.JavaPlugin;
    22. import org.bukkit.scheduler.BukkitRunnable;
    23.  
    24. public class Gunz extends JavaPlugin implements Listener {
    25.  
    26. List<String> cplayers = new ArrayList<String>();
    27.  
    28.  
    29. public void onEnable(){
    30. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    31. }
    32.  
    33.  
    34.  
    35.  
    36.  
    37. @EventHandler
    38. public void onInteract(PlayerInteractEvent e) {
    39. final Player p = e.getPlayer();
    40.  
    41. if (e.getAction() == Action.RIGHT_CLICK_AIR) {
    42.  
    43. if (p.getItemInHand().getType() == Material.STICK) {
    44.  
    45. if (!p.getInventory().containsAtLeast(
    46. new ItemStack(Material.FLINT), 1)) {
    47. p.sendMessage(ChatColor.RED
    48. + "Your outta bullets!");
    49. p.playSound(p.getLocation() , Sound.ANVIL_LAND, 1f,3f);
    50. e.setCancelled(true);
    51. } else {
    52.  
    53. p.getInventory().removeItem(
    54. new ItemStack(Material.FLINT, 1));
    55. if(cplayers.contains(p.getName())){
    56.  
    57. cplayers.add(p.getName());
    58. new BukkitRunnable() {
    59.  
    60. @Override
    61. public void run() {
    62. cplayers.remove(p.getName());
    63. }
    64. }.runTaskTimer(this, 40, 40);
    65.  
    66. }
    67. Entity arrow = p.launchProjectile(Snowball.class);
    68. arrow.setVelocity(p.getLocation().getDirection()
    69. .multiply(4.5f));
    70. arrow.getWorld().playEffect(arrow.getLocation(), Effect.SMOKE, 100);
    71. arrow.getWorld().playSound(p.getLocation(), Sound.FIREWORK_BLAST2, 5.0F, 5.0F);
    72. ((CraftPlayer)p).getHandle().updateInventory(((CraftPlayer)p).getHandle().activeContainer); }}}}
    73.  
    74.  
    75. @EventHandler
    76. public void onEntityDamage(EntityDamageByEntityEvent e) {
    77. if (e.getDamager() instanceof Snowball) {
    78. Snowball f = (Snowball) e.getDamager();
    79. if (f.getShooter() instanceof Player) {
    80. Player shooter = (Player) f.getShooter();
    81. if (shooter.getItemInHand().getType() == Material.STICK);
    82. e.setDamage(5.5);
    83.  
    84.  
    85. }
    86. }
    87. }
    88. }


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

    McMhz

    I don't know what's wrong then, Probably something in your code.
     
  30. Offline

    itzrobotix

    It says List<String> cooling = new ArrayList<String>(); ....
    Should it not be;
    ArrayList<String> cooling = new ArrayList<String>(); ?
    Also you're checking wether they are in the ArrayList before adding at line 56 type return; and on line 57 type }
     
Thread Status:
Not open for further replies.

Share This Page