Creating a gun cooldown?

Discussion in 'Plugin Development' started by kreashenz, Jun 6, 2013.

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

    kreashenz

    This might be a double post, I'm not sure. I already checked.

    So, I wanted to create a gun that has a delay with X ticks. If you don't know what I mean, I mean like a fire rate, eg Bolt action snipers, BANG *click-click(2.5 second delay)* BANG, and repeat. I had something like this, but it wasn't calling the delays, so I just removed it. Thanks!
     
  2. Offline

    Garris0n

    On the click run a scheduler that puts them in a "no shoot" arraylist for said gun(other ways could be used, maybe a hashmap with the gun name etc.) then after it ends remove them. On the click don't let them shoot.
     
  3. Offline

    kreashenz

    Garris0n I've done that, and it didn't work. I added to a Hashmap, then called a scheduler when they try shoot again, and that didn't work.
     
  4. Offline

    kaskoepoes112

    write something like this:

    Code:java
    1.  
    2.  
    3. public static List<String> noshoot = new ArrayList<String>();
    4.  
    5. @EventHandler
    6. public void onShoot(PlayerInterachtEvent evt){
    7. if (evt.getAction == Action.RIGHT_CLICK_AIR){
    8.  
    9. if (! noshoot.contains(evt.getPlayer().getName())) { // watch out! comparing two strings is kinda tricky
    10. // shoot
    11. noshoot.add(evt.getPlayer().getName())
    12. // start sheduler here
    13. }
    14. }
    15. }
    16.  
    17.  


    now just like Garrison said , just make a scheduler which removes the name from the list :p done :)

    code has not been writen in eclipse :p
     
  5. Offline

    kreashenz

  6. Offline

    Garris0n

    kreashenz Are you sure everything is firing properly? Like add debug messages after everything and see what does and doesn't work.
     
  7. Offline

    Jatoc


    I didn't look at all your code, but I took a very quick look and I didn't see any checks to cancel the event if the player already exists in the list...
    Here's a snippet of your code
    Code:java
    1.  
    2. if(dp.equalsIgnoreCase(b.getString("Wooden_Hoe.Name").replace('&', '§')) && a == Material.WOOD_HOE){
    3. if(hasAmmo(p)){
    4. launch(p, p.getLocation().add(0,1.62,0));
    5. gun.shotWH.add(p.getName());
    6. gun.shotWHDelay(p);
    7. } else Functions.tell(p, "§cYou need more ammo!");
    8. }
    9.  


    Unless I missed something, it should read:

    Code:java
    1.  
    2. if(dp.equalsIgnoreCase(b.getString("Wooden_Hoe.Name").replace('&', '§')) && a == Material.WOOD_HOE){
    3. if(!gun.shotWH.contains(p.getName()){
    4. if(hasAmmo(p)){
    5. launch(p, p.getLocation().add(0,1.62,0));
    6. gun.shotWH.add(p.getName());
    7. gun.shotWHDelay(p);
    8. } else Functions.tell(p, "§cYou need more ammo!");
    9. }
    10. }
    11.  
     
  8. Offline

    kreashenz

    Jatoc :O Ohhhhhhhh mah gawd, you are most likely right. Good think I kept these classes. Gimme a few minutes, I will tell you if that's the reason.
     
  9. Offline

    Jatoc

    aiight haha
     
  10. Offline

    kreashenz

    Jatoc Ok, it's calling an NPE now, but it worked, I think. Here's the stacktrace.
    PHP:
    [SEVERECould not pass event PlayerInteractEvent to CoD v1.0
    org
    .bukkit.event.EventException
    at org
    .bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
    at org.bukkit.craftbukkit.v1_5_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:186)
    at org.bukkit.craftbukkit.v1_5_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:156)
    at net.minecraft.server.v1_5_R3.PlayerConnection.a(PlayerConnection.java:611)
    at net.minecraft.server.v1_5_R3.Packet15Place.handle(SourceFile:58)
    at net.minecraft.server.v1_5_R3.NetworkManager.b(NetworkManager.java:292)
    at net.minecraft.server.v1_5_R3.PlayerConnection.d(PlayerConnection.java:109)
    at net.minecraft.server.v1_5_R3.ServerConnection.b(SourceFile:35)
    at net.minecraft.server.v1_5_R3.DedicatedServerConnection.b(SourceFile:30)
    at net.minecraft.server.v1_5_R3.MinecraftServer.r(MinecraftServer.java:581)
    at net.minecraft.server.v1_5_R3.DedicatedServer.r(DedicatedServer.java:226)
    at net.minecraft.server.v1_5_R3.MinecraftServer.q(MinecraftServer.java:477)
    at net.minecraft.server.v1_5_R3.MinecraftServer.run(MinecraftServer.java:410)
    at net.minecraft.server.v1_5_R3.ThreadServerApplication.run(SourceFile:573)
    Caused byjava.lang.NullPointerException
    at me
    .kreashenz.cod.listeners.Guns.shotWADelay(Guns.java:202)
    at me.kreashenz.cod.listeners.ShootListener.onPlayerShoot(ShootListener.java:70)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
    ... 
    16 more
    And here's the codes for that part.

    Code:java
    1.  
    2. // shotWADelay method.
    3. public void shotWADelay(final Player p){
    4. plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new BukkitRunnable(){ // Line 202
    5. @Override
    6. public void run() {
    7. if(shotWA.contains(p.getName())){
    8. shotWA.remove(p.getName());
    9. }
    10. }
    11. }, a.getInt("Wooden_Axe.Delay"));
    12. }
    13. // Line 70 of onPlayerShoot
    14. if(dp.equalsIgnoreCase(b.getString("Wooden_Axe.Name").replace('&', '§')) && a == Material.WOOD_AXE){
    15. if(!gun.shotWA.contains(p.getName())){
    16. if(hasAmmo(p)){
    17. launch(p, p.getLocation().add(0,1.62,0));
    18. gun.shotWA.add(p.getName());
    19. gun.shotWADelay(p); // This is line 70
    20. } else Functions.tell(p, "§cYou need more ammo!");
    21. }
    22. }

    Thanks for the above message too :)
     
  11. Offline

    Jatoc

    kreashenz

    could you try replacing shotWA within the shotWADelay function so that it reads:
    Code:java
    1.  
    2. public void shotWADelay(final Player p){
    3. plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new BukkitRunnable(){ // Line 202
    4. @Override
    5. public void run() {
    6. if(Guns.shotWA.contains(p.getName())){
    7. Guns.shotWA.remove(p.getName());
    8. }
    9. }
    10. }, a.getInt("Wooden_Axe.Delay"));
    11. }
    12.  
     
  12. Offline

    kreashenz

    Jatoc That's exactly what it is now, just not with the Guns.shotWA, because it is in the guns class.. Do you mean to make it static?
     
  13. Offline

    Jatoc


    Yes. Make it static, and specify the Guns class within the function. I don't know the specifics, but I think it has something to do with the function run() and the Bukkit scheduler running it not being in the same class or whatnot. I'm pretty new to this myself, but I'm sure someone else can provide more input. I've had issues like that fixed by specifying the class explicitly, and was wondering if that would fix your problem.
     
  14. Offline

    kreashenz

    Jatoc That made it worse.. Any other ideas you got?

    Can anyone please explain to me why this has an NPE..? I don't think it'd be plugin, because I have the constructor here
    Code:java
    1. private CoD plugin;
    2. public Guns(CoD plugin){
    3. this.plugin = plugin;
    4. }


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

    kreashenz

    Bump. I am still getting the NPE.
     
  16. Offline

    kreashenz

    Last bump..
     
Thread Status:
Not open for further replies.

Share This Page