Stupid Delay Tasks

Discussion in 'Plugin Development' started by DatCookiez, Jan 21, 2014.

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

    DatCookiez

    Hey Guys, I have 2 problem with delays at the moment.

    Firstly I am trying to make it so if a player is hit by a snowball they are frozen for 5 seconds, if freezes the player but doesn't seem to last 5 seconds, it lasts forever until /reload

    Code:
    Code:java
    1. @EventHandler
    2. public void onFreeze (EntityDamageByEntityEvent e){
    3. if(e.getEntity() instanceof Player && e.getDamager() instanceof Snowball) {
    4. Player p = (Player) e.getEntity();
    5. Snowball snowball = (Snowball) e.getDamager();
    6. plugin.Snow.add(p.getName());
    7. p.sendMessage(ChatColor.RED + "You've been frozen");
    8. }
    9. }
    10.  
    11. @EventHandler
    12. public void onFreeze1 (PlayerMoveEvent e){
    13. Player p = e.getPlayer();
    14. if (plugin.Snow.contains(p.getName())) {
    15. e.setTo(e.getFrom());
    16. p.sendMessage(ChatColor.RED + "You cannot move, you are frozen!");
    17. delay(p);
    18.  
    19. }
    20. }
    21.  
    22. public void delay(final Player p){
    23. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this.plugin, new Runnable(){
    24. @Override
    25. public void run() {
    26. p.sendMessage(ChatColor.RED + "You can now move!");
    27. plugin.Snow.remove(p.getName());
    28.  
    29. }
    30. }, 200);
    31. }


    Second one is I am trying to hide the player who right clicks sugar goes invisible for 10 seconds then they are visible again. It doesn't seem to make the player invisible nor does it remove the player from 'invisibility'.

    Code:
    Code:java
    1. @EventHandler
    2. public void onSpy(PlayerInteractEvent e){
    3. Player p = e.getPlayer();
    4. if ((e.getAction() == Action.RIGHT_CLICK_AIR && p.getItemInHand().getType() == Material.SUGAR ||
    5. e.getAction() == Action.RIGHT_CLICK_BLOCK && p.getItemInHand().getType() == Material.SUGAR)){
    6.  
    7. int cooldownTime = 35; // Get number of seconds from wherever you want
    8. if(spy.containsKey(p.getName())) {
    9. long secondsLeft = ((spy.get(p.getName())/1000)+cooldownTime) - (System.currentTimeMillis()/1000);
    10. if(secondsLeft>0) {
    11. // Still cooling down
    12. p.sendMessage(ChatColor.RED + "Wait " + ChatColor.GOLD + secondsLeft + ChatColor.RED + " seconds to use this again!");
    13. return;
    14. }
    15. }
    16. // No cooldown found or cooldown has expired, save new cooldown
    17. spy.put(p.getName(), System.currentTimeMillis());
    18.  
    19. for(Player p1 : Bukkit.getOnlinePlayers()){
    20. p.sendMessage(ChatColor.GOLD + "You have gone invisible!");
    21. p.hidePlayer(p1);
    22.  
    23. delay(p);
    24.  
    25. }
    26. }
    27. }
    28.  
    29.  
    30.  
    31. public void delay(final Player p){
    32. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable(){
    33. public void run(){
    34. for(Player p1 : Bukkit.getOnlinePlayers()){
    35. p.sendMessage(ChatColor.RED + "You are now visible!");
    36. if(!(p1.canSee(p))){
    37. p.showPlayer(p1);
    38. }
    39. }
    40. }
    41. }, 200L); // 20 ticks = 1 second. So 200 ticks = 10 seconds.
    42. }
     
  2. Offline

    Niknea

    DatCookiez
    1. You set the first one to 10 seconds I believe, make it something like this
    Code:java
    1. }, 5 * 20);

    Also does the message you put even show up?

    2. Does anything perform? Like messages?
     
  3. Offline

    DatCookiez

    Niknea

    The "You are unfrozen" message does not show

    and the message "You are invisible" shows

    Niknea
    The error for the snowball one.
    http://pastebin.com/Jz4D5UWf

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

    Niknea

    DatCookiez Check this out and see if you can figure it out ;)
    Code:
    Caused by: java.lang.IllegalArgumentException: Plugin cannot be null
            at org.apache.commons.lang.Validate.notNull(Validate.java:203) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at org.bukkit.craftbukkit.v1_7_R1.scheduler.CraftScheduler.validate(CraftScheduler.java:391) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at org.bukkit.craftbukkit.v1_7_R1.scheduler.CraftScheduler.runTaskTimer(CraftScheduler.java:120) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at org.bukkit.craftbukkit.v1_7_R1.scheduler.CraftScheduler.scheduleSyncRepeatingTask(CraftScheduler.java:116) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at org.bukkit.craftbukkit.v1_7_R1.scheduler.CraftScheduler.scheduleSyncDelayedTask(CraftScheduler.java:100) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at me.mrgermanrain.main.FrozoneListener.delay(FrozoneListener.java:47) ~[?:?]
            at me.mrgermanrain.main.FrozoneListener.onFreeze1(FrozoneListener.java:41) ~[?:?]
     
  5. Offline

    DatCookiez

    Niknea
    I'm not sure what's wrong?

    Line 41:
    delay(p);

    Line 47:
    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable(){
     
  6. Offline

    Niknea

  7. Offline

    DatCookiez

    Niknea
    It's the first block of code in this thread
     
  8. Offline

    Niknea

    DatCookiez The WHOLE class please, including the imports and class name, therefore I can see what the real lines in the code are.
     
  9. Offline

    DatCookiez

    Niknea
    Code:java
    1. package me.mrgermanrain.main;
    2.  
    3. import java.util.HashMap;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    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.entity.EntityDamageByEntityEvent;
    12. import org.bukkit.event.player.PlayerMoveEvent;
    13.  
    14. public class FrozoneListener implements Listener{
    15. public HashMap<String, Long> snow = new HashMap<String, Long>();
    16. Main plugin;
    17.  
    18. public void ListenerClass(Main plugin) {
    19. this.plugin = plugin;
    20. }
    21.  
    22. @EventHandler
    23. public void onFreeze (EntityDamageByEntityEvent e){
    24. if(e.getEntity() instanceof Player && e.getDamager() instanceof Snowball) {
    25. Player p = (Player) e.getEntity();
    26. Snowball snowball = (Snowball) e.getDamager();
    27. plugin.Snow.add(p.getName());
    28. p.sendMessage(ChatColor.RED + "You've been frozen");
    29. }
    30. }
    31.  
    32. @EventHandler
    33. public void onFreeze1 (PlayerMoveEvent e){
    34. Player p = e.getPlayer();
    35. if (plugin.Snow.contains(p.getName())) {
    36. e.setTo(e.getFrom());
    37. p.sendMessage(ChatColor.RED + "You cannot move, you are frozen!");
    38. delay(p);
    39.  
    40. }
    41. }
    42.  
    43. public void delay(final Player p){
    44. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable(){
    45. public void run() {
    46. p.sendMessage(ChatColor.RED + "You can now move!");
    47. plugin.Snow.remove(p.getName());
    48.  
    49. }
    50. }, 5 * 20);
    51. }
    52.  
    53.  
    54. }


    *I removed 3 imports because they were not being moved*
     
  10. Offline

    Sagacious_Zed Bukkit Docs

    DatCookiez Stacktraces include line numbers that help you and everyone else narrow down the problem, if you change the code, the line numbers are no longer valid, and makes everyone's life that much harder.
     
    Niknea likes this.
  11. Offline

    Niknea

    DatCookiez Can you show us the new stack trace?
     
  12. Offline

    DatCookiez

    Niknea
    The error is still the same and it's the same 2 lines
     
  13. Offline

    Niknea

    DatCookiez I now don't know what line the error is on, as you removed a few things.
     
  14. Offline

    DatCookiez

    Niknea

    Error:
    Code:
    [16:17:08 ERROR]: Could not pass event PlayerMoveEvent to DatKitPvP v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:481) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:466) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java:227) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.PacketPlayInFlying.a(SourceFile:137) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.PacketPlayInLook.handle(SourceFile:98) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:655) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    Caused by: java.lang.IllegalArgumentException: Plugin cannot be null
        at org.apache.commons.lang.Validate.notNull(Validate.java:203) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.craftbukkit.v1_7_R1.scheduler.CraftScheduler.validate(CraftScheduler.java:391) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.craftbukkit.v1_7_R1.scheduler.CraftScheduler.runTaskTimer(CraftScheduler.java:120) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.craftbukkit.v1_7_R1.scheduler.CraftScheduler.scheduleSyncRepeatingTask(CraftScheduler.java:116) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.craftbukkit.v1_7_R1.scheduler.CraftScheduler.scheduleSyncDelayedTask(CraftScheduler.java:100) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at me.mrgermanrain.main.ClassListeners.FrozoneListener.delay(FrozoneListener.java:84) ~[?:?]
        at me.mrgermanrain.main.ClassListeners.FrozoneListener.onFreeze1(FrozoneListener.java:79) ~[?:?]
        at sun.reflect.GeneratedMethodAccessor56.invoke(Unknown Source) ~[?:?]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_13]
        at java.lang.reflect.Method.invoke(Method.java:601) ~[?:1.7.0_13]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        ... 13 more

    Code:
    Code:java
    1. package me.mrgermanrain.main.ClassListeners;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.HashMap;
    5. import java.util.List;
    6.  
    7. import me.mrgermanrain.main.Main;
    8.  
    9. import org.bukkit.Bukkit;
    10. import org.bukkit.ChatColor;
    11. import org.bukkit.Material;
    12. import org.bukkit.command.CommandSender;
    13. import org.bukkit.entity.Entity;
    14. import org.bukkit.entity.Player;
    15. import org.bukkit.entity.Snowball;
    16. import org.bukkit.event.EventHandler;
    17. import org.bukkit.event.Listener;
    18. import org.bukkit.event.block.Action;
    19. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    20. import org.bukkit.event.player.PlayerInteractEvent;
    21. import org.bukkit.event.player.PlayerMoveEvent;
    22. import org.bukkit.plugin.java.JavaPlugin;
    23. import org.bukkit.potion.PotionEffect;
    24. import org.bukkit.potion.PotionEffectType;
    25.  
    26. public class FrozoneListener implements Listener{
    27. public HashMap<String, Long> snow = new HashMap<String, Long>();
    28.  
    29. Main plugin;
    30.  
    31. public void ListenerClass(Main plugin) {
    32. this.plugin = plugin;
    33. }
    34.  
    35. @SuppressWarnings("unused")
    36. @EventHandler
    37. public void onPlayerFreeze(PlayerInteractEvent e){
    38. Player p = e.getPlayer();
    39. List<Entity> nearbyEntities = p.getNearbyEntities(10, 10, 10);
    40. ArrayList<Player> nearbyPlayers = new ArrayList<Player>();
    41. if ((e.getAction() == Action.LEFT_CLICK_BLOCK && e.getPlayer().getItemInHand().getType()
    42. == Material.SNOW_BLOCK
    43. || e.getAction() == Action.LEFT_CLICK_AIR && e.getPlayer().getItemInHand().getType()
    44. == Material.SNOW_BLOCK)){
    45.  
    46. int cooldownTime = 20; // Get number of seconds from wherever you want
    47. if(snow.containsKey(p.getName())) {
    48. long secondsLeft = ((snow.get(p.getName())/1000)+cooldownTime) - (System.currentTimeMillis()/1000);
    49. if(secondsLeft>0) {
    50. // Still cooling down
    51. p.sendMessage(ChatColor.RED + "Wait " + ChatColor.GOLD + secondsLeft + ChatColor.RED + " seconds to use this again!");
    52. return;
    53. }
    54. }
    55. // No cooldown found or cooldown has expired, save new cooldown
    56. snow.put(p.getName(), System.currentTimeMillis());
    57.  
    58. p.getLocation().getWorld();
    59. for (Entity e1 : nearbyEntities) {
    60. if (e1 instanceof Player) {
    61. nearbyPlayers.add((Player) e1);
    62. for (Player p1 : nearbyPlayers) {
    63. p1.getLocation().getWorld();
    64. Main.Snow.add(p1.getName());
    65. p1.sendMessage(ChatColor.GOLD + p.getName() + ChatColor.RED + " has frozen you!!");
    66.  
    67. }
    68. }
    69. }
    70. }
    71. }
    72.  
    73. @EventHandler
    74. public void onFreeze1 (PlayerMoveEvent e){
    75. Player p = e.getPlayer();
    76. if (Main.Snow.contains(p.getName())) {
    77. e.setTo(e.getFrom());
    78. p.sendMessage(ChatColor.RED + "You cannot move, you are frozen!");
    79. delay(p);
    80. }
    81. }
    82.  
    83. public void delay(final Player p){
    84. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable(){
    85. public void run() {
    86. p.sendMessage(ChatColor.RED + "You can now move!");
    87. Main.Snow.remove(p.getName());
    88.  
    89. }
    90. }, 5 * 20);
    91. }
    92.  
    93.  
    94. }
    95.  
     
  15. Offline

    Maurdekye

    You're scheduling it using the base Bukkit class, which is null because there's no server associated with the base class. You need to do plugin.getServer() and schedule it from that.
     
  16. Offline

    DatCookiez

    Maurdekye
    Still doesn't seem to be working
     
  17. Offline

    L33m4n123

    are you actually passing an instance of your main class into the Listener class or?
     
  18. Offline

    DatCookiez

  19. Offline

    DatCookiez

  20. Offline

    xTigerRebornx

  21. Offline

    DatCookiez

    xTigerRebornx
    Main class? What do you want to see in it? I have a lot of personal code in there
     
  22. Offline

    xTigerRebornx

    DatCookiez Well, its getting a null instance of your plugin from somewhere. You shouldn't worry about people taking your code, if thats what you mean when you say "personal code"

    EDIT: Nevermind, found the problem
    Code:
    public void ListenerClass(Main plugin) {
            this.plugin = plugin;
        }
    My guess is this is meant to be your constructor, fix this
     
  23. Offline

    DatCookiez

    xTigerRebornx ???
    Code:
        public void FrozoneClass(Main plugin) {
            this.plugin = plugin;
        }
     
  24. Offline

    xTigerRebornx

    DatCookiez Go learn what a constructor is (and how to make one)
     
    L33m4n123 likes this.
Thread Status:
Not open for further replies.

Share This Page