Solved Getting an argument from a command and using it as the name of a class.

Discussion in 'Plugin Development' started by EscapeMC, Sep 11, 2017.

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

    EscapeMC

    Hello Bukkit Development Forum.

    I have been pondering this idea for awhile now and I know it is possible but I do not know how to do it.

    Basically, I am trying to use one of the arguments from a command (args[2]) and use it for an instance of a class I have made. Let me show you:
    Code:
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    
            if(cmd.getName() == "arena") {
               
                if(args[0] == "create") {
                   
                    if(args[1] == "arena") {
                       
                        MainBase.addArena(args[2]);
                       
                    }
                   
                }
               
            }
    
            return false;
           
        }
    
    So, with that stated let me show you the next part.

    This is my Arena class, this is the class I am trying to create another instance of with this command.

    Code:
    import java.util.ArrayList;
    
    public class Arena {
    
        public static String name;
        public static ArrayList<String> teams = new ArrayList<String>();
       
        public static void addTeam(Team teamName) {
           
            teamName = new Team();
           
           
        }   
       
    }
    
    Now lastly, the MainBase class.

    Code:
    public class MainBase {
               
        public static void addArena(String name) {
           
           
           
        }
       
    }
    
    It is really nothing. And why? As I do not know how to use name, which I have obtained from the command, to make a new Arena();

    Thanks for the help! :p
     
  2. Offline

    timtower Administrator Administrator Moderator

    @EscapeMC Strings are compared with equals and equalsIgnoreCase, not with ==
     
  3. Offline

    Caderape2

    @EscapeMC
    You cannot create multiple instance of your arena class since the field name is static. Static field are unique.

    Remove the keyworld static then create a constructor with a string as parameter.
    Then you will be able to do a 'new Arena(args[2])'
     
  4. Offline

    timtower Administrator Administrator Moderator

    @EscapeMC You don't need any static in that regards btw.
     
  5. Offline

    EscapeMC

    @Caderape2 Where should I remove static? I did do the constructor and it seems to be fine.
     
    Last edited: Sep 11, 2017
  6. Offline

    Caderape2

    @EscapeMC Remove all static from your arena class. As i said, static field are unique. So you cannot create two differents instance of this class.
     
  7. Offline

    EscapeMC

    @Caderape2 Thank you very much! Is this all I have to do to have of the Arena class? And they are all different/seperate from one another?
     
  8. Offline

    Caderape2

    @EscapeMC Each instance is unique, even if the name is the same. It's why you have to store your instance if you wanna do stuff with it later.
    Get a look at this small example.
    Code:
    public class ArenaManager {
    
     
     
        public List<Arena> arenas = new ArrayList<>();
     
     
     
        public ArenaManager()
        {     
            arenas.add(new Arena("arena1"));
            arenas.add(new Arena("arena2"));
            arenas.add(new Arena("arena3"));
         
            sendArena1Broadcast();
        }
     
     
     
        private void sendArena1Broadcast()
        {
            Arena first = getArenaByName("arena1");
            if (first != null)  first.sendBrodcast();
        }
    
    
    
        public Arena getArenaByName(String name)
        {
            for (Arena a : arenas)
            {
                if (a.getName().equals(name)) return a;
            }
         
            return null;
        }
     
     
     
     
     
        public class Arena
        {
            private final String name;
         
            public Arena(String name)
            {
                this.name = name;
            }
         
    
            public String getName() {
                return name;
            }
         
            public void sendBrodcast() {
                Bukkit.broadcastMessage("name : " + name);
            }
        }
    }
     
  9. Offline

    EscapeMC

    @Caderape2 Ok I get it now.

    Now, I have teams within the Arenas. Would I do the same process, with inside of Arena class, make an ArrayList<Team> ? If I wanted to assign a player to that team, how would I? Would I make an ArrayList<String> within the Team class and add a player name to that? Would that specifically make them assigned to the team within the arena, or just the team class itself?

    EDIT:
    I have come to an issue. Am I able to make the methods I have to add a team static? But that would require me to make the teams and arenas static as well... Aghhh!!! Can I do that?
     
    Last edited: Sep 12, 2017
  10. Offline

    timtower Administrator Administrator Moderator

    @EscapeMC why would you need them static then?
     
  11. Offline

    EscapeMC

    I am trying to access them from my main class. I have two classes right now (Using Eclipse), Main and MainBase. Main is the main class, where I have my commands. I will make a github for this one minute

    EDIT

    Here
    https://github.com/EscapeMC/ArenaPluginRefined
     
  12. Offline

    timtower Administrator Administrator Moderator

    @EscapeMC First decide where you want to store your arena's.
    The teams will probably be stored inside the arena
     
  13. Offline

    EscapeMC

    In a config would work right?

    Or would a yml be better?
     
  14. Offline

    timtower Administrator Administrator Moderator

    @EscapeMC I am talking about when the server is running
     
  15. Offline

    EscapeMC

    Not sure what you mean then about "where to store it."
     
  16. Offline

    timtower Administrator Administrator Moderator

    @EscapeMC You need to store the arena's in a variable somewhere.
    Where do you want to put that variable without using public static?
     
  17. Offline

    EscapeMC

  18. Offline

    timtower Administrator Administrator Moderator

    @EscapeMC But that is using public (bad to do anyways with variables)
    Not to mention that you are saving stuff in 2 different locations: Main and MainBase.

    Rename MainBase to ArenaManager
    Make the fields private
    Remove the name lists from the Main class
    Store the teams in the arena where they are playing.
     
  19. Offline

    EscapeMC

    The Arenas and Teams are supposed to be able to be created by the sender with a command. How am I supposed to link the two together? I cannot use the addArena method in Main as it is not static so what are you telling me to do to fix it?
     
  20. Offline

    timtower Administrator Administrator Moderator

    @EscapeMC Then you need to make an instance of the ArenaManager in the main class.
    And you can create them with a command if you have ArenaManager#addArena and Arena#addTeam
     
  21. Offline

    EscapeMC

    Sorry, new-ish with Java. How would I create this instance? Its not reflections right..?
     
  22. Offline

    timtower Administrator Administrator Moderator

    @EscapeMC Please learn Java before you start with Bukkit, it will make a lot of things way easier.
    An instance is a variable with a value.
     
  23. Offline

    EscapeMC

    I know Java but I have not had to do these kinds of things before. I have never worked with Bukkit before (well, at least very little experience).

    Would you please just point me in the direction I need to go with instances?
     
  24. Offline

    timtower Administrator Administrator Moderator

  25. Offline

    EscapeMC

    I know they are not, I just haven't dealt with them before as I have not needed to :p

    But thank you for helping, I will see where this will get me!

    Expect me to be back soon... Should I use the same later, I assume so?

    Alright so I am back.

    I am trying to figure out how to add players to a team. Now, I know I can just make a String list (Like i have done in the past) but I am trying to figure out how to make a list per team.

    But I am not sure how to do this. The command i have formats like /arena create team [team name] [the arena for the team]
    But I cannot figure out how to test if that arena is an arena, then add a team to that arena.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Sep 13, 2017
  26. Offline

    timtower Administrator Administrator Moderator

    @EscapeMC HashMap<String,List<UUID>> inside the Team class
    I assume that you have a getArena(String) method, if that returns null: not an arena
     
  27. Offline

    EscapeMC

  28. Offline

    timtower Administrator Administrator Moderator

    @EscapeMC First change your code so the teams are INSIDE the arena's
     
  29. Offline

    EscapeMC

    Alright. Doing this was simple. But now I would need to make a new instance of the Arena class, yes?
    Well, for the arena class it would need name (as a String). What do I make it? How about ex.
    I get an error with this. Hm.

    On top of that, with the arena instance having this name, how would it be separate arenas and teams for those arenas?

    Sorry just not seeing how the finished product will look.
    GitHub updated too
     
  30. Offline

    timtower Administrator Administrator Moderator

    @EscapeMC I am out.
    This is basic Java in my opinion. Won't help with that.
     
Thread Status:
Not open for further replies.

Share This Page