Solved Why won't this work?

Discussion in 'Plugin Development' started by GreatMinerZ, Feb 9, 2014.

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

    GreatMinerZ

    Trying to toggle clouds if player is flying. If i use /toggleclouds on it will only display "Clouds enabled"

    Code:java
    1. ArrayList <Player> flying = new ArrayList<Player>();
    2.  
    3. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    4.  
    5. if(sender instanceof Player)
    6. {
    7. Player player = (Player) sender;
    8. if(commandLabel.equalsIgnoreCase("toggleclouds"))
    9. {
    10. if(args.length == 0)
    11. {
    12. if(flying.contains(player))
    13. {
    14. flying.remove(player);
    15. }
    16. else
    17. {
    18. flying.add(player);
    19. }
    20. }
    21. else
    22. {
    23. boolean toggle = false;
    24. if(args[0].equalsIgnoreCase("on"))
    25. {
    26. toggle = true;
    27. }
    28. if(toggle && !flying.contains(player))
    29. {
    30. flying.add(player);
    31. }
    32. else if(toggle)
    33. {
    34. player.sendMessage("Clouds enabled");
    35. }
    36. else if(!toggle && flying.contains(player))
    37. {
    38. flying.remove(player);
    39. }
    40. else if(!toggle)
    41. {
    42. player.sendMessage("Clouds disabled");
    43. }
    44. }
    45. }
    46. }
    47.  
    48. return false;
    49. }
     
  2. Offline

    stirante

  3. Offline

    GreatMinerZ

    stirante
    Code:java
    1. @EventHandler
    2. public void onPlayerFly(PlayerMoveEvent e) {
    3. Player p = e.getPlayer();
    4. Location location = p.getLocation();
    5.  
    6. if(p.isFlying()){
    7.  
    8. ParticleEffects.sendToLocation(ParticleEffects.CLOUD, location, 0.5F, 0.5F, 1.0F, 0, 20);
    9.  
    10. }
    11. }
     
  4. Offline

    stirante

  5. Offline

    GreatMinerZ

    stirante did that the clouds work fine but the command doesnt
     
  6. Offline

    hamzaxx

    GreatMinerZ Your going to cause a memory leak if you save the player object instead save the players name.
     
    DeGambler likes this.
  7. Offline

    stirante

    GreatMinerZ Change if(p.isFlying()){ to if(p.isFlying() && flying.contains(p)){

    But as hamzaxx said don't save Player object. Save player's name instead.
     
  8. Offline

    GreatMinerZ

    hamzaxx stirante Changed if(p.isFlying()){ to if(p.isFlying() && flying.contains(p)){ But the only problem now is that flying.add(player.getName()); is giving the following error: "The method add(Player) in the type ArrayList is not applicable for the arguments (String)
     
  9. Offline

    stirante

    GreatMinerZ Change generic parameter of list from Player to String
     
  10. Offline

    GreatMinerZ

    stirante Clouds don't show anymore. I think it's because of if(p.isFlying() && flying.contains(p)) {

    Code:

    Code:java
    1. ArrayList <String> flying = new ArrayList<String>();
    2.  
    3. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    4.  
    5. if(sender instanceof Player)
    6. {
    7. Player player = (Player) sender;
    8. if(commandLabel.equalsIgnoreCase("toggleclouds"))
    9. {
    10. if(args.length == 0)
    11. {
    12. if(flying.contains(player.getName()))
    13. {
    14. flying.remove(player.getName());
    15. }
    16. else
    17. {
    18. flying.add(player.getName());
    19. }
    20. }
    21. else
    22. {
    23. boolean toggle = false;
    24. if(args[0].equalsIgnoreCase("on"))
    25. {
    26. toggle = true;
    27. }
    28. if(toggle && !flying.contains(player.getName()))
    29. {
    30. flying.add(player.getName());
    31. }
    32. else if(toggle)
    33. {
    34. player.sendMessage("Clouds enabled");
    35. }
    36. else if(!toggle && flying.contains(player.getName()))
    37. {
    38. flying.remove(player.getName());
    39. }
    40. else if(!toggle)
    41. {
    42. player.sendMessage("Clouds disabled");
    43. }
    44. }
    45. }
    46. }
    47.  
    48. return false;
    49. }
    50.  
    51. @EventHandler
    52. public void onPlayerFly(PlayerMoveEvent e) {
    53. Player p = e.getPlayer();
    54. Location location = p.getLocation();
    55.  
    56. if(p.isFlying() && flying.contains(p)) {
    57.  
    58. ParticleEffects.sendToLocation(ParticleEffects.CLOUD, location, 0.5F, 0.5F, 1.0F, 0, 20);
    59.  
    60. }
    61. }
    62.  
    63. }
     
  11. Offline

    stirante

    GreatMinerZ likes this.
  12. Offline

    GreatMinerZ

Thread Status:
Not open for further replies.

Share This Page