Solved [HELP] ArrayList problem

Discussion in 'Plugin Development' started by Cheesepro, Dec 6, 2014.

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

    Skionz

    Cheesepro cmdAFK. Name your classes with the first letter uppercase. onPlayerMove seems like a method.
     
  2. Offline

    Cheesepro

    Avygeil Like this?
    Code:java
    1. public boolean contains(String id){
    2. return isAFK.contains(id);
    3. }


    @Skionz
    Like this?
    Code:java
    1. private CEMain plugin;
    2. private final Messenger msg;
    3. public ArrayList<String> isAFK;
    4.  
    5. public cmdAFK(CEMain plugin){
    6. this.isAFK = new ArrayList<String>();
    7. this.plugin = plugin;
    8. this.msg = new Messenger(plugin);
    9. }


    <Edit by mrCookieSlime: Merged posts. Please don't double post. There is an Edit Button right next to the Date.>
     
  3. Offline

    Avygeil

    Cheesepro A Set, look for HashSet.
    Now I suggest you close your IDE, read others' suggestions, learn Java, open IDE, laugh at your old code, fix yourself using the massive spoonfed code, ???, profit.
     
    teej107 likes this.
  4. Offline

    Skionz

    *facepalm*
    You literally just created a pointless wrapper method when you were supposed to use a Set. Where did you even get that idea from?
     
    JordyPwner and Avygeil like this.
  5. Offline

    Cheesepro

    WesJD teej107 JordyPwner Skionz Avygeil
    I DID!!! After checking out the chapter in my Java textbook...

    The correct code is posted below, for anyone who needs it :D

    cmdAFK.java:
    Code:java
    1. package me.cheesepro.ce.mainpack;
    2.  
    3. import me.cheesepro.ce.extra.ConsoleSender;
    4. import me.cheesepro.ce.extra.Messenger;
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandExecutor;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.EventPriority;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.player.PlayerMoveEvent;
    14. import org.bukkit.potion.PotionEffect;
    15. import org.bukkit.potion.PotionEffectType;
    16.  
    17. import java.util.*;
    18.  
    19. /**
    20. * Created by Mark on 06/12/2014.
    21. */
    22. public class cmdAFK implements CommandExecutor, Listener{
    23.  
    24. public static Set<String> isAFK = new HashSet<String>();
    25.  
    26. private CEMain plugin;
    27. private final Messenger msg;
    28.  
    29. public cmdAFK(CEMain plugin){
    30. this.plugin = plugin;
    31. this.msg = new Messenger(plugin);
    32. }
    33.  
    34. @Override
    35. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    36. if(sender instanceof Player) {
    37. Player p = (Player) sender;
    38. if (label.equalsIgnoreCase("afk")) {
    39. if (!(isAFK.contains(p.getUniqueId().toString()))/*if isAFK is false*/) {
    40. isAFK.add(p.getUniqueId().toString());
    41. PotionEffectType effect=PotionEffectType.BLINDNESS;
    42. p.addPotionEffect(effect.createEffect(1000000,10000));
    43. msg.broadcast(ChatColor.YELLOW.toString() + ChatColor.BOLD + p.getName() + ChatColor.GRAY.toString() + " is AFK");
    44.  
    45. } else/*if isAFK is false*/ {
    46. isAFK.remove(p.getUniqueId().toString());
    47. p.removePotionEffect(PotionEffectType.BLINDNESS);
    48. msg.broadcast(ChatColor.YELLOW.toString() + ChatColor.BOLD + p.getName() + ChatColor.GRAY.toString() + " is no longer AFK"); }
    49. }
    50. }else{
    51. ConsoleSender.noConsole();
    52. }
    53. return false;
    54. }
    55.  
    56. public Set<String> getIsAFK() {
    57. return isAFK;
    58. }
    59.  
    60. }
    61.  


    onPlayerMove.java:
    Code:java
    1. package me.cheesepro.ce.listener;
    2.  
    3. import me.cheesepro.ce.mainpack.cmdAFK;
    4. import me.cheesepro.ce.extra.Messenger;
    5. import me.cheesepro.ce.mainpack.CEMain;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.EventPriority;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.player.PlayerMoveEvent;
    12. import org.bukkit.potion.PotionEffectType;
    13.  
    14. import java.util.ArrayList;
    15. import java.util.HashSet;
    16. import java.util.Set;
    17.  
    18. /**
    19. * Created by Mark on 06/12/2014.
    20. */
    21. public class onPlayerMove implements Listener{
    22.  
    23. CEMain plugin;
    24. Messenger msg;
    25. Set<String> afkPlayer;
    26.  
    27. public onPlayerMove(CEMain plugin)
    28. {
    29. plugin.getServer().getPluginManager().registerEvents(this, plugin);
    30. this.plugin = plugin;
    31. this.msg = new Messenger(plugin);
    32. cmdAFK afkclass = new cmdAFK(plugin);
    33. this.afkPlayer = afkclass.getIsAFK();
    34. }
    35.  
    36. @EventHandler(priority=EventPriority.MONITOR)
    37. public void onAFKMove(PlayerMoveEvent e){
    38. Player p = e.getPlayer();
    39. String id = p.getUniqueId().toString();
    40. if(this.afkPlayer.contains(id)) {
    41. this.afkPlayer.remove(id);
    42. p.removePotionEffect(PotionEffectType.BLINDNESS);
    43. msg.broadcast(ChatColor.YELLOW.toString() + ChatColor.BOLD + p.getName() + ChatColor.GRAY.toString() + " is no longer AFK");
    44. }
    45. }
    46. }
    47.  


    At the end I just want to Thank you guys ALLL :DDD
     
    Avygeil likes this.
  6. Offline

    JordyPwner

    you are still doing it wrong...
     
  7. Offline

    Cheesepro

  8. Offline

    JordyPwner

    leak of memory cough **
     
  9. Offline

    Cheesepro

  10. Offline

    JordyPwner

    cause of useless piece of code >.<
     
  11. Offline

    Cheesepro

  12. Offline

    JordyPwner

    you really dont understand java do you?
     
  13. Offline

    Cheesepro

    JordyPwner
    Sure... but can you tell me where?
     
  14. Offline

    JordyPwner

    ill make it for you ;) just hang on

    EDIT: Cheesepro here you go :) untested but it will work i hope :p

    Show Spoiler
    Code:java
    1. public class Main extends JavaPlugin implements CommandExecutor, Listener{
    2.  
    3. ArrayList<Player> isafk = new ArrayList<Player>();
    4.  
    5. public void onEnable(){
    6.  
    7. }
    8.  
    9. public void onDisable(){
    10.  
    11. }
    12.  
    13.  
    14. public boolean onCommand(CommandSender sender, Command cmd, String cl, String[] args) {
    15.  
    16. if(!(sender instanceof Player)){
    17. return true;
    18. }
    19. if(cmd.getName().equalsIgnoreCase("afk")){
    20. Player p = (Player) sender;
    21. if(isafk.contains(p)){
    22. Bukkit.broadcastMessage(p.getName() + "is no longer afk!");
    23. isafk.remove(p);
    24. }else{
    25. isafk.add(p);
    26. Bukkit.broadcastMessage(p.getName() + "is now afk!");
    27. }
    28.  
    29.  
    30.  
    31. }
    32. return false;
    33. }
    34.  
    35. @EventHandler
    36. public void onPlayerMoveEvent(PlayerMoveEvent e){
    37. Player p = e.getPlayer();
    38. if(!(isafk.contains(p))){
    39. return;
    40. }
    41. if(isafk.contains(p)){
    42. Bukkit.broadcastMessage(p.getName() + "is no longer afk!");
    43. isafk.remove(p);
    44. }
    45.  
    46. }
    47. }
    48.  
     
  15. Offline

    Cheesepro

  16. Offline

    JordyPwner

    i forgot to register but i bet you can do that at your own?
     
  17. Offline

    Cheesepro

  18. Offline

    Avygeil

    JordyPwner Stop with the unnecessary negative posts. He fixed it by himself which is a great step forward. Well done Cheesepro. Now we can help him fix the remaining flaws.

    1. Add scope modifiers to your class fields (private, public...).
    2. Don't make the Set static, the field should belong to your object instance.
    3. Use cmd.getName() and not label.getName() for alias support.
    4. You can use a Set<UUID> which will save the need to write .toString() every time.
    5. Don't use the Monitor priority in your event. Highest is well suited, even if you don't cancel it (well, that's really not important for your event).
    6. Don't use the wildcard .* import.
    7. Why does cmdAfk implement Listener?
    8. You probably don't need such a high Blindness, stay safe for the client.
    9. You have to remove the UUID from the list when a player disconnects.
    10. Code could also be better OOP-wise. Most importantly, I'd move the Set to the main class, and encapsulate a .setAfk(UUID uuid, boolean afk) and a isAfk(UUID uuid). The way you assign "afkplayer" in the event class is plain wrong. But you will learn that with time. ;)
    11. Rest would be just coding style and look.

    EDIT: JordyPwner Didn't you mention memory leak earlier? On top of using the wrong Collection, you just added one by referencing a Player object in it.
     
    bennie3211 and Cheesepro like this.
  19. Offline

    Cheesepro

    Avygeil Thank you very much :D Learned alot from this :D
     
    JordyPwner and Avygeil like this.
Thread Status:
Not open for further replies.

Share This Page