Player Interact Stops Working?

Discussion in 'Plugin Development' started by WesJD, Oct 22, 2014.

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

    WesJD

    Well, yesterday I was just playing around with spawnFallintBlock(), though today I've realized I can only use the sword once, then the plugin needs to be reloaded. Put some debug messages in, but it seems that it isn't creating the runnable again?

    Code:
    Code:java
    1. package com.excomm.test;
    2.  
    3. import java.util.Arrays;
    4. import java.util.List;
    5. import java.util.Random;
    6.  
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.DyeColor;
    9. import org.bukkit.Material;
    10. import org.bukkit.block.Block;
    11. import org.bukkit.entity.FallingBlock;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.event.EventHandler;
    14. import org.bukkit.event.Listener;
    15. import org.bukkit.event.block.Action;
    16. import org.bukkit.event.entity.EntityChangeBlockEvent;
    17. import org.bukkit.event.player.PlayerInteractEvent;
    18. import org.bukkit.scheduler.BukkitRunnable;
    19. import org.bukkit.scheduler.BukkitTask;
    20. import org.bukkit.util.Vector;
    21.  
    22. public class Handler implements Listener {
    23.  
    24. Random r = new Random();
    25.  
    26. List<DyeColor> colors = Arrays.asList(DyeColor.RED, DyeColor.ORANGE, DyeColor.YELLOW, DyeColor.GREEN, DyeColor.BLUE, DyeColor.LIGHT_BLUE, DyeColor.PINK, DyeColor.MAGENTA, DyeColor.LIME, DyeColor.CYAN, DyeColor.PURPLE, DyeColor.BLACK, DyeColor.WHITE);
    27. DyeColor color = colors.get(r.nextInt(colors.size()));
    28.  
    29. static int time = 6;
    30. static BukkitTask task;
    31.  
    32. @EventHandler
    33. public void onClick(final PlayerInteractEvent e) {
    34. final Player p = e.getPlayer();
    35. final Block block = e.getClickedBlock();
    36. if(!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
    37. if(!(e.getItem().getType() == Material.GOLD_SWORD)) return;
    38. p.sendMessage(ChatColor.GREEN + "You started a wool rain! (5 seconds)");
    39. task = new BukkitRunnable() {
    40. public void run() {
    41. time--;
    42. if(time == 5 || time == 4 || time == 3 || time == 2 || time == 1) {
    43. p.sendMessage(ChatColor.YELLOW + "Wool rain ends in " + time + "!");
    44. FallingBlock block1 = block.getWorld().spawnFallingBlock(block.getLocation(), Material.WOOL, color.getWoolData());
    45. float x = (float) 0 + (float) (((0 - 0) + 0));
    46. double y = 2.4;
    47. float z = (float) 0 + (float) (((0 - 0) + 0));
    48. block1.setVelocity(new Vector(x, y, z));
    49. } else if(time == 0) {
    50. p.sendMessage(ChatColor.RED + "Wool rain has eneded.");
    51. task.cancel();
    52. }
    53. }
    54. }.runTaskTimer(Main.getInstance(),20L,20L);
    55. }
    56. @EventHandler
    57. public void onBlockFall(EntityChangeBlockEvent e) {
    58. if(e.getEntity() instanceof FallingBlock) {
    59. e.getBlock().setType(Material.AIR);
    60. e.getBlock().breakNaturally();
    61. e.setCancelled(true);
    62. }
    63. }
    64. }


    Thanks,
    Wes
     
  2. WesJD
    Probably because the time variable is outside of the method, and thus on first run it will increment it all the way down to 0 and never work again because of your if checks.
     
  3. Offline

    WesJD

    Assist Explain a bit more please? I'm not understanding.
     
  4. WesJD
    Sorry I don't have time to explain very well right now. You have the time variable there set to 6 (seconds). When the runnable runs the first time, it will increment the integer by 1 every time until it's 0, which is when you cancel the task. Now this will stay at 0 forever until you set it equal to something again. You should place the time variable inside the method so a new one will be created each time the event runs rather than using the same old one. You should also do this with the random dye thing.

    Also. What in the world is this madness?
    Code:
    float x = (float) 0 + (float) (((0 - 0) + 0));
    
     
  5. Offline

    WesJD

    Assist
    Had it there from testing, never removed it.

    EDIT:

    Got it working, thanks!
     
Thread Status:
Not open for further replies.

Share This Page