[Tutorial] Creating a Lobby for your plugin

Discussion in 'Resources' started by MrAwellstein, Feb 19, 2014.

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

    MrAwellstein

    Alright, this is my first tutorial, so please try to ignore any mistakes I make, or anything like that. Also at this time, I am still writing this, and adding to it, because I decided to write this with 14 minutes before I had to leave (I'm at class).


    Okay, so first thing we want to do, is create a class called Lobby.
    Code:java
    1. public class Lobby{
    2.  
    3. }


    Good, now the next thing we want to do, is allow our Lobby class to hold our world, so add add this next line. We want it to be private and non static, so we can make an instance of it an object or something (I suck at terminology)
    Code:java
    1. private World lobby = null;


    After that, we want to create a loadLobby method that loads our world.
    Make it public, so it can be accessed later, and make it take in a string for the world name. This code tells bukkit to create a new world (yes I said create a new world). Why is this our loading class you ask? Its because if the world already exist, the WorldCreator wont create a new world. This is also an easy way to load the world, because doing Bukkit.getWorld(<String>) wont work if the world isn't loaded.
    Code:java
    1. public void loadLobby(String name){
    2. try{
    3. Bukkit.createWorld(new WorldCreator(name).environment(World.Environment.NORMAL));
    4. lobby = Bukkit.getWorld(name);
    5. }catch(Exception e){}
    6. }
    7.  


    This line here is so you can call the world when ever you want, instead of having to create a World object when you want to use the world.
    Code:java
    1. public World getLobby(){
    2. return lobby;
    3. }


    Next lets make it simple to move players to our lobby. Again, make sure that they are public. Now as you can see, both methods are named sendtoLobby. One of the methods takes in the prams Player[], which is an arraylist of players incase you want to move a bunch of players, and the other just takes in Player, which is a single player. You can use other things to send the player to the lobby, its just that this is something simple that you can use.
    Code:java
    1. public void sendToLobby(Player[] players){
    2. for(Player player: players){
    3. sendToLobby(player);
    4. }
    5. }
    6.  
    7. public void sendToLobby(Player p){
    8. try{
    9. if(p != null && lobby != null){
    10. try{
    11. p.teleport(lobby.getSpawnLocation());
    12. }catch(Exception e){
    13. try{
    14. p.sendMessage(ChatColor.RED+"Failed to send you to the lobby!");
    15. }catch(Exception e1){}
    16. }
    17. }
    18. }catch(Exception e){}
    19. }


    Finally we arrive at the way to set Lobby, when you create a new instance of it. All we have to do, is take in the prams as a String, and call our loadLobby() method. Once we do this, we can call the world anytime we want, and are not limited to our Object (we can do Bukkit.getWorld(<String>)). Btw, note here, that you can have multiple lobbies, as long as they have different names.
    Code:java
    1. public Lobby(String name){
    2. loadLobby(name);
    3. }



    If you are asking how you use this newly created class, it should be pretty simple. You simple create a new Object, and yeah. Btw note that you can use this object to get the world anytime you want, as long as you have it available for other classes.
    Code:java
    1. Lobby lobby = new Lobby("Lobby");



    The final thing should look something like this:
    Code:java
    1. public class Lobby {
    2. private World lobby;
    3.  
    4. public Lobby(String name){
    5. loadLobby(name);
    6. }
    7. public World getWorld(){
    8. return lobby;
    9. }
    10. public void loadLobby(String name){
    11. try{
    12. Bukkit.createWorld(new WorldCreator(name).environment(World.Environment.NORMAL));
    13. lobby = Bukkit.getWorld(name);
    14. }catch(Exception e){}
    15. }
    16.  
    17. public void sendToLobby(Player[] players){
    18. for(Player player: players){
    19. sendToLobby(player);
    20. }
    21. }
    22. public void sendToLobby(Player p){
    23. try{
    24. if(p != null && lobby != null){
    25. try{
    26. p.teleport(lobby.getSpawnLocation());
    27. }catch(Exception e){
    28. try{
    29. p.sendMessage(ChatColor.RED+"Failed to send you to the lobby!");
    30. }catch(Exception e1){}
    31. }
    32. }
    33. }catch(Exception e){}
    34. }
    35. }



    Any questions or comments? Feel free to post below :D
     
    Jacc734 likes this.
  2. Offline

    97WaterPolo

    MrAwellstein
    Not sure if on purpose, but like 22 and line 17, shouldn't it be Lobby? not Loby. Also, does this create a new world for the lobby :confused:
     
    MrAwellstein likes this.
  3. Offline

    MrAwellstein


    Yes it does, I forgot to mention that, also ignore my spelling, I'm pretty bad at English haha....
    What happened was I directly copy n' pasted the final example from my IDE and the spelling errors where there.
     
  4. Offline

    97WaterPolo

    unforgiven5232 and MrAwellstein like this.
  5. Offline

    MrAwellstein

    Thanks for the catch in the code btw. I would have never noticed it >.<
     
    97waterpolo likes this.
Thread Status:
Not open for further replies.

Share This Page