BukkitRunnable() Help!!

Discussion in 'Plugin Development' started by johnny boy, Jan 23, 2017.

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

    johnny boy

    For the argument .runTaskLater() I need to get my main class. How would I do this? (I know it's something simple.. I just forgot)

    Code:Java
    1. new BukkitRunnable() {
    2.  
    3. public void run() {
    4. cooldown.put(player, cooldown.get(player) - 1);
    5. if (cooldown.get(player) <= 0) {
    6. cooldown.remove(player);
    7. cancel();
    8. }
    9. }
    10. }.runTaskLater(I NEED TO CHANGE THIS, 20);
     
  2. Offline

    timtower Administrator Administrator Moderator

    @MemeplexOwner If this is a different class: constructor with main instance.
    If it is the main. "this"
     
  3. Offline

    johnny boy

    So I do this
    Code:Java
    1.  
    2. public PlayerRightClick(MainClass main) {
    3.  
    4. }
    5.  

    Then in my main class:
    Code:Java
    1. Bukkit.getPluginManager().registerEvents(new PlayerRightClick(this), this);


    EDIT: hmm.. Made the constructor but mainclass is still telling me to make the constructor?!
     
  4. Offline

    timtower Administrator Administrator Moderator

    @MemeplexOwner Save all fikes, check all constructors.
    Or just hit the quick fix button.
     
  5. Offline

    johnny boy

    But I have already made the constructor.... (In the other class.)

    Please tim! Help me! @timtower :p
     
    Last edited by a moderator: Jan 23, 2017
  6. Offline

    timtower Administrator Administrator Moderator

    @MemeplexOwner Post both classes, mark the line with the error with a comment.
     
  7. Offline

    johnny boy

    IT WORKS. I just needed to do the magic thing... Control + s!! But not the bukkitrunnable will not run every second...
    BukkitRunnable:
    Code:Java
    1.  
    2. package me.p250.playerabilities.events;
    3.  
    4. import java.util.HashMap;
    5.  
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Material;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.block.Action;
    12. import org.bukkit.event.player.PlayerInteractEvent;
    13. import org.bukkit.potion.PotionEffect;
    14. import org.bukkit.potion.PotionEffectType;
    15. import org.bukkit.scheduler.BukkitRunnable;
    16.  
    17. import me.p250.playerabilities.MainClass;
    18.  
    19. public class PlayerRightClick implements Listener {
    20.  
    21. MainClass plugin;
    22.  
    23. public PlayerRightClick(MainClass plugin) {
    24. this.plugin = plugin;
    25. }
    26.  
    27. @EventHandler
    28. public void playerInteractEvent(PlayerInteractEvent e) {
    29.  
    30. HashMap<Player, Integer> cooldown = new HashMap<Player, Integer>();
    31.  
    32. final Player player = (Player) e.getPlayer();
    33.  
    34. if (cooldown.containsKey(player)) {
    35. player.sendMessage("You have to wait " + cooldown.get(player) + " seconds!");
    36. return;
    37. }
    38.  
    39. if (player.getItemInHand() == null || e.getAction() == null) {
    40. return;
    41. }
    42.  
    43. if (player.getInventory().getItemInHand().getType().equals(Material.DIAMOND_SWORD) && e.getAction() == Action.RIGHT_CLICK_AIR) {
    44. player.sendMessage(ChatColor.RED + "Strength mode activated!");
    45. player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 30 * 20, 1));
    46. cooldown.put(player, 30);
    47. System.out.println(cooldown.toString());
    48. }
    49.  
    50. if (player.getInventory().getItemInHand().getType().equals(Material.DIAMOND_SWORD) && e.getAction() == Action.RIGHT_CLICK_BLOCK) {
    51. player.sendMessage(ChatColor.RED + "Strength mode activated!");
    52. player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 30 * 20, 1));
    53. cooldown.put(player, 30);
    54. }
    55.  
    56. new BukkitRunnable() {
    57.  
    58. public void run() {
    59. cooldown.put(player, cooldown.get(player) - 1);
    60. if (cooldown.get(player) <= 0) {
    61. cooldown.remove(player);
    62. cancel();
    63. }
    64. }
    65. }.runTaskLater(plugin, 20);
    66. }
    67. }
    68.  

    MainClass:
    Code:Java
    1.  
    2. package me.p250.playerabilities;
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.configuration.file.FileConfiguration;
    6. import org.bukkit.plugin.java.JavaPlugin;
    7.  
    8. import me.p250.playerabilities.events.PlayerRightClick;
    9.  
    10. public class MainClass extends JavaPlugin {
    11.  
    12. public FileConfiguration config;
    13.  
    14. public void onEnable() {
    15. Bukkit.getPluginManager().registerEvents(new PlayerRightClick(this), this);
    16. }
    17.  
    18. }
    19.  


    HALP!!

    EDIT: I did some debugging and it stays at 30. I think I know why, would I make a repeating task instead?
     
  8. Offline

    timtower Administrator Administrator Moderator

    @MemeplexOwner Because it only runs once every time the event gets called.
    Put the cooldown in the class itself.
    Make it use UUID, not player.
    Start the runnable in the constructor.
    Use runTaskTimer
     
  9. Offline

    johnny boy

    Why in the constructor??
     
  10. Offline

    timtower Administrator Administrator Moderator

    @MemeplexOwner You only need to start 1.
    Then you do need to make sure that you loop through all players in the map though.
    Starting it in the event will increase the speed of the cooldown if you spam the event.
     
  11. Offline

    johnny boy

    But it will always load the constructor, making increasing the speed..... I can make a check to see if there is already a task running?
     
  12. Offline

    timtower Administrator Administrator Moderator

    @MemeplexOwner Constructor will only be called once.
    Unless you also register the event 20 times.
     
  13. Offline

    johnny boy

    BUT That sparks a problem.. because after the cooldown I will never be able to start another one?
     
  14. Offline

    timtower Administrator Administrator Moderator

    @MemeplexOwner That is why it is a runTaskTimer.
    Never stops unless you tell it to stop.
     
  15. Offline

    johnny boy

    But I need to stop it because I need my players to get out of the cooldown. WAIT A MINUTE! I don't need to stop it! I just need to remove them from the cooldown. Easy!

    EDIT: so your saying if I put the runnable in the constructor it will run forever, because I need it to!
     
  16. Offline

    timtower Administrator Administrator Moderator

    @MemeplexOwner And if you make a runTaskTimer out of it, make it loop through all the players in the map, and remove the cancel?
    Yes
     
  17. Offline

    johnny boy

    I forgot about multiple people... Let me make that now (Before I do this, I need to make my hashmap public but I can't.... HELP!!)
     
  18. Offline

    timtower Administrator Administrator Moderator

    @MemeplexOwner And why can't you make the hashmap public?
    Did you forgot to pull it outside the event?
     
  19. Offline

    johnny boy

    it just says illegal modifier..
     
  20. Offline

    timtower Administrator Administrator Moderator

  21. Offline

    johnny boy

    When you put it inside the eventHandler. So now I need to run the runnable inside the constructor? Question, why does it not say "Waiting moderation" when I post now?

    EDIT: Noticed another problem....... I can't make player public either and it needs to stay inside the event handler..
     
  22. Offline

    timtower Administrator Administrator Moderator

    @MemeplexOwner You don't put it in the eventHandler.
    That is what I told you.
    You put it in the class itself.
    And warnings expired
     
  23. Offline

    johnny boy

    Warnings? The ones before the Temp Ban? (Going off topic..) So now that I put the runnable inside the constructor I need to make player public.. somehow.
     
  24. Offline

    timtower Administrator Administrator Moderator

    @MemeplexOwner Every warning that you got will expire.
    No, you don't use the player there anymore.
    You loop through the values in the hashmap.
     
  25. Offline

    johnny boy

    How would I get the values?
     
  26. Offline

    timtower Administrator Administrator Moderator

    Map.keySet()
     
  27. Offline

    johnny boy

    Another problem... All the keys will probably be the same.. Since the key is the timer ;-;
     
  28. Offline

    timtower Administrator Administrator Moderator

  29. Offline

    johnny boy

    So I make it reverse of what it is. HOWEVER, I still need to access player OUTSIDE the class if I want to do this....... Damn this is hard.
     
  30. Offline

    timtower Administrator Administrator Moderator

    @MemeplexOwner Don't reverse anything.
    Map<uuid,time>
    No need for players outside the event.
     
Thread Status:
Not open for further replies.

Share This Page