IllegalArgumentException: Plugin already initialized!

Discussion in 'Plugin Development' started by Robin Bi, Mar 23, 2014.

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

    Robin Bi

    Hey there, it's me again.



    I'm recently working on a plugin that hides players to provide something like a SilentHub.

    I have three classes, a Main.java were i defined the commands, an EventListener.java that handles the PlayerJoinEvent and PlayerQuitEvent and a Hiders.java for the hiding- or showing-processes.


    This is my Main.java: http://pastebin.com/NRWnPQVc
    The commands /online and /private are just for debugging, you'll see the methods in Hiders.java.
    This is my EventListener.java: http://pastebin.com/M043MusS
    This is my Hiders.java: http://pastebin.com/1VAR8Zuj
    This is my plugin.yml: http://pastebin.com/PEK5rstn


    I'm not getting any errors, but when typing /hider in chat, there's always the output "Silent Hub: Enabled" so i guess i'm not even being added to the ArrayList<Player> online.
    Also, nobody's hidden, but that seems legit if there's noone in the ArrayList.
    /online and /private also have no output.




    Hopefully someone knows the solution.



    Yours sincerely,
    Robin Bi


    EDIT: Oh damn, that's an old title. Could someone please change it to "Add to ArrayList<Player> doesn't work"? *Being embarrassed*
     
  2. Offline

    rfsantos1996

    You are actually creating a new Hider instance for every time you do something, so the list is always recreated.

    "new Hiders();" will create a new Hider instance, this will be a new variable, a new hider, so to maintain the same list, just keep using the same Hiders.

    To do that you:
    On Main.java, do these changes (check diference)
    Code:java
    1. public class Main extends JavaPlugin implements CommandExecutor {
    2.  
    3. public Hiders hiders;
    4.  
    5. @Override
    6. public void onEnable() {
    7. getServer().getPluginManager().registerEvents(new EventListener(this), this);
    8. getServer().getPluginCommand(<command here>).setExecutor(this); // don't know if this is necessary since you said commands were working
    9. hiders = new Hiders();
    10. }


    On event listener
    Code:java
    1. private final Main plugin;
    2.  
    3. public EventListener(Main plugin) {
    4. this.plugin = plugin;
    5. // REPLACE ALL 'hiders variable' to plugin.hiders.<method here>
    6. }


    By doing this, you keep the same Hider instance, so you will: keep only one hiders on the memory, with only one list, methods, etc
     
    Sharkfang17 likes this.
  3. Offline

    Robin Bi

    Thanks a bunch for your answer :)
    Adding and removing users who write /hider works now, but players are not hidden. And I have no fricking idea why...

    I'd appreciate you watching my code again.



    Yours sincerely,
    Robin Bi

    rfsantos1996 forgot to tag you and I'm not sure if you get the alert if i just edit my post.

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

    rfsantos1996

    Thanks for the tag, I didnt saw your comment.

    Instead of a list of online players. I recommend you using "plugin.getServer().getOnlinePlayers()", I think you can replace every "online" (except the .add() and .remove() of course, that you need to delete) without causing any error.

    With this change you can actually delete your EventListener.

    I recommend you using if { } else { } on every command, this can help you finding bugs and any bug like forgoting a "return"

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    2.  
    3. if(cmd.getName().equalsIgnoreCase("hider")) {
    4. if(sender instanceof Player) {
    5. if(sender.hasPermission("hiders.switch")) {
    6. hiders.switchView((Player) sender);
    7. sender.sendMessage("Switched!");
    8. return true;
    9. } else {
    10. sender.sendMessage("No permission!");
    11. return true;
    12. }
    13. } else {
    14. sender.sendMessage("You're not a player! Sorry");
    15. return true;
    16. }
    17.  
    18. } else if(cmd.getName().equalsIgnoreCase("online")) {
    19. for(Player online : getServer().getOnlinePlayers()) {
    20. sender.sendMessage(online.getName());
    21. }
    22. sender.sendMessage(Integer.toString(getServer().getOnlinePlayers().size()));
    23. return true;
    24.  
    25. } else if(cmd.getName().equalsIgnoreCase("switched")) {
    26. for(Player silented : hiders.getWhoHaveSwitched()) { // ignore the 'silented', it was supposed to be switched but i'm lazy to rewrite
    27. sender.sendMessage(silented.getName());
    28. }
    29. sender.sendMessage(Integer.toString(hiders.getWhoHaveSwitched().size()));
    30. return true;
    31.  
    32. } else {
    33. return false;
    34. }
    35. }
    36. }


    On hiders:
    Code:java
    1. ArrayList<Player> switched = new ArrayList();
    2.  
    3. public void switchView(Player player) {
    4. if(switched.contains(player)) {
    5. showPlayers(player);
    6. switched.remove(player);
    7. } else {
    8. hidePlayers(player);
    9. switched.add(player);
    10. }
    11. }
    12.  
    13. public void hidePlayers(Player toHideFrom) {
    14. for(Player online : plugin.getServer().getOnlinePlayers()) {
    15. toHideFrom.hidePlayer(online);
    16. }
    17. }
    18.  
    19. public void showPlayers(Player toShow) {
    20. for(Player online : plugin.getServer().getOnlinePlayers()) {
    21. if(!toShow.canSee(online)) {
    22. toShow.showPlayer(online);
    23. }
    24. }
    25. }
    26.  
    27. public void removePlayer(Player player) {
    28. if(switched.contains(player)) {
    29. switched.remove(player);
    30. }
    31. }
    32.  
    33. public ArrayList<Player> getWhoHaveSwitched() {
    34. return switched;
    35. }


    Actually you need the EventListener:
    Code:java
    1. // on join
    2. for(Player hideTo : plugin.hiders.getWhoHaveSwitched()) {
    3. hideTo.hidePlayer(event.getPlayer()); // hide players who was supposed to be hidden
    4. }
    5.  
    6. // on quit
    7. hiders.removePlayer(event.getPlayer()); // if you don't do this, player will be saved on the list and won't be able to be removed from all RAM memory
     
    Robin Bi likes this.
  5. Offline

    Robin Bi

    rfsantos1996 To be honest, i really don't understand why i need
    Code:java
    1. hiders.removePlayer(event.getPlayer());

    because the player should be deleted from the onlineList anyways, shouldn't he?

    Also, Eclipse doesn't accept removePlayer(). Or did you mean
    Code:java
    1. plugin.hiders.privates.remove(p);

    ?


    Whatever, huge thanks again :) There are so many nice people on this forums helping newbies like me <3



    Yours sincerely,
    Robin Bi
     
  6. Offline

    rfsantos1996

    I mean
    Code:java
    1. plugin.hiders.privates.remove(p);
    if you keep a player on a list, it won't be removed from the memory, the player will be offline but if you don't remove it from the list, he can't be deleted, search for "memory leak".
     
  7. Offline

    Botifier

    have you tried checking if another plugin's main class is called main?
    that could be causing the problem.
     
Thread Status:
Not open for further replies.

Share This Page