Solved Clan methods not working?

Discussion in 'Plugin Development' started by Gonmarte, Mar 15, 2016.

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

    Gonmarte

    Hello :)
    Im working on a clans plugin. Before a player creates a clan i would like to check if that clan already exists, so i made a code for that, The problem is that i cant check if the clan exists before it is created, cuz i dont know what is the clan. If i create first and lets say that then i check if that clan exists, it wont work, cuz it will say that exists cuz it was created.
    Problem u arent understanding so well, so im going to leave the code here, thank you!

    Code:
      
        public static void createClan(String clanname, Player player) {
                      //i dont know what is the clan so i cant check it....
            if(clanExists(//its suppose to be the clan that he created,  but i cant check it cuz i dont know it)){
                mm.severe(player, "Ja existe");
            }else{
            Clans clan = new Clans(clanname);
            playersClan.put(player.getName(), clan);
            mm.severe(player, "Criste um clan!");
            }
        }
    
    public static boolean clanExists(Clans clan) {
          
            if (clans.contains(clan)) {
                return true;
            }
    
            return false;
        }
    
     
    Last edited: Mar 16, 2016
  2. Offline

    darthteddy1

    What's not working about it?
     
  3. Offline

    Gonmarte

    I create a clan called "hi" if i do /clan delete "hi" it will say send me a msg saying that the clan doesnt exist
     
  4. Offline

    darthteddy1

    I recommend a HashMap rather than an ArrayList
     
  5. Offline

    Gonmarte

    For what?

    @darthteddy1 forget that problem, my problem now is that i need to check the clan and i dont know what clan is it... take a look at the first post that i edited with the new problem, ty.
    PS: Sorry admins for double post, just realized now.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Mar 16, 2016
  6. Offline

    darthteddy1

    After you check if the clan exists, under the closing bracket that returns true, put an "else" and make that contain return false.
     
  7. Offline

    Gonmarte

    Like this? But that wont solve my problem...
    Code:
        public static boolean clanExists(Clans clan) {
           
            if (clans.contains(clan)) {
                return true;
            }else{
    
            return false;
            }
        }
    
     
  8. Offline

    adgdeveloping

    Code:
    public static boolean clanExists(Clans clan) {
            for(Clans c : clans.values()) {
                if(c.getName() == clan.getName()) return true;
            }
    
            return false;
    }
    
     
  9. Online

    timtower Administrator Administrator Moderator

    CodePlaysMinecraft and Gonmarte like this.
  10. Offline

    adgdeveloping

    Yeah, it was to show the idea of how to do it, you could use c.getName().equals(clan.getName()) or c.getName().equalsIgnoreCase(clan.getName()) instead.
     
  11. Offline

    Gonmarte

    My problem isnt in that method, my problem is in createClan method, i dont know the clan so i cant see if it exists do u understand?
     
  12. @Gonmarte Do you really think that an ArrayList is the best way to store a clan in?
    I'll let you think about this.
     
  13. Offline

    Gonmarte

    Firsly, im only using the arraylist to store my class.
    Secondly, i store the players and the clans in a hashmap.
    Maybe its not the best way, but its my way, so i will keep it. If you have a better sugestion u can let me know.
     
    Last edited: Mar 16, 2016
  14. @Gonmarte Oops, I meant HashMap, my bad. Just saying, there is a better way. I'm thinking more along the lines of a configuration file. This way, even after the server stops, the clan's info still exists, and you can also check if a clan exists.
     
  15. Offline

    Gonmarte

    So after the server reload the arraylist will keep the data and the hashmap not? So the clans will be there, but the players associate to them will be gone? After i fix this bugs, i will save and load the hashmap in the config, so it wont be such a problem right?
    Code:
        private static ArrayList<Clans> clans = new ArrayList<>();
    
        private static HashMap<String, Clans> playersClan = new HashMap<>();
    
     
    Last edited: Mar 16, 2016
  16. Offline

    mcdorli

    Don't abuse static, use getters and setters.
    No, nothing will be kept after reload or after completely stopping the server, use the bukkit built-in configuration API, to save data to a config (Or just use a .txt file, I think configs are overkill for this, because you don't actually configure anything in it)
     
    Gonmarte and CodePlaysMinecraft like this.
  17. Offline

    Gonmarte

    Thank you, so i will defenitly save and load the hashmap in the config.
    Can u help me at my main question pls?
    For those who didnt understand im create a method to create a clan. Then i created a method to check if the clan exists, and in this method (clanExists(Clans clan)) i have set 1 parameter that is the clan. So when im creating the clan like this (Clans clan = new Clans("nameofclan")) i need to check if that clan had already been created, but once that variable is after the check i cant get it
    @CodePlaysMinecraft
    Are you saying that i should have a list for clans and a list for players? So i would associate a player to a clan without having an hashmap?
     
    Last edited: Mar 16, 2016
  18. @Gonmarte No, not at all. I said config file, didn't I? :p
    Like @mcdorli said, it doesn't need to be a YAML file, it could also be a text file you just write to to set and get information. If you're not sure how to do this, there are always tutorials out there.
     
  19. Offline

    Gonmarte

    I solved this alone guys! Anyway, thank you!
    @CodePlaysMinecraft I will be fixing more bugs and adding more features and then i will google about saving hashmaps and lists. Thank you!
     
    DoggyCode™ likes this.
  20. Offline

    DoggyCode™

    This code is aids sorry. But I'm on a phone rn so I can't help. I'll be back hopefully soon!
     
  21. Offline

    Gonmarte

    Anything wrong?
     
  22. Offline

    DoggyCode™

    The static. I would rather create the clan as an object. An example could be: create a new class called "Clan"

    In this class you could write for example (taking use of private fields):

    Code:
    private Player owner;
    private String name;
    private List members;
    
    public Clan(Player owner, String clanName){
      this.owner = owner;
      this.name = clanName;
      this.members = new ArrayList<String>();
    }
    
    public void addMember(String uuid){
      this.members.add(uuid);
    }
    
    public boolean isOwner(Player p){
      return this.owner.equals(p);
    }
    
    public ArrayList getMembers(){
      return this.members;
    }
    
    //etc...
    To create a new clan you would do:
    Clan clan = new Clan (...);

    I just wrote this on my phone quick and it might be wrong. But don't use static, this is OBJECT orientated programming.
     
  23. Offline

    Gonmarte

    I created the clan as an object...
     
  24. Offline

    DoggyCode™

    Shit. I just saw the static lmao sorry. My bad
     
  25. Offline

    Gonmarte

    Its ok xd I just didnt put all the code. Although im using only String clanname with paraments to create a clan and not a owner.
     
    DoggyCode™ likes this.
Thread Status:
Not open for further replies.

Share This Page