Resource How to create an Arena

Discussion in 'Spigot Plugin Development' started by Alex0589, Jul 9, 2018.

  1. HI spigot Users this is one of my first resources and I hope you are going to like it. Let's start:
    1. First of all let's create a new class called Arena where we will define the constructor:
      Code (Text):
      import org.bukkit.Location;
      import org.bukkit.entity.Player;

      import java.util.HashSet;
      import java.util.Set;

      public class Arena {

          private String name;
          private int minPlayers;
          private int maxPlayers;
          private Set<Player> players;
          private Location spawn;

          public Arena(String name, int minPlayers, int maxPlayers, Location spawn) {
              this.name = name;
              this.minPlayers = minPlayers;
              this.maxPlayers = maxPlayers;
              this.players = new HashSet<>();
              this.spawn = spawn;
          }

          public String getName() {
              return name;
          }

          public void setName(String name) {
              this.name = name;
          }

          public int getMinPlayers() {
              return minPlayers;
          }

          public void setMinPlayers(int minPlayers) {
              this.minPlayers = minPlayers;
          }

          public int getMaxPlayers() {
              return maxPlayers;
          }

          public void setMaxPlayers(int maxPlayers) {
              this.maxPlayers = maxPlayers;
          }

          public Set<Player> getPlayers() {
              return players;
          }

          public void addPlayer(Player player) {
              players.add(player);
          }

          public Location getSpawn() {
              return spawn;
          }

          public void setSpawn(Location spawn) {
              this.spawn = spawn;
          }
      }
    2. Now we'll need an ArenaManager:
      Code (Text):
      simport java.util.ArrayList;
      import java.util.List;

      public class ArenaManager {
          public List<Arena> arenas = new ArrayList<>();
          public Arena getArena(String name){
              return arenas.stream().filter(arena -> arena.getName().equals(name)).findFirst().orElse(null);
          }
      }
       
    3. Now in your enable define a new ArenaManage Object:
      Code (Text):
          private ArenaManager arenaManager;
          @Override
          public void onEnable() {
              arenaManager = new ArenaManager();
          }

          @Override
          public void onDisable() {
             arenaManager = null;
          }

          public ArenaManager getArenaManager() {
              return arenaManager;
          }
      }
       
    4. When you want to access the ArenaManager just use a Dependency Injection to get your main class like this:
      Code (Text):
      public class Event implements Listener {
          private MainTester plugin;
          public Event(MainTester plugin){
              this.plugin = plugin;
          }
          public void onJoin(PlayerJoinEvent event){
              ArenaManager manager = plugin.getArenaManager();
              Arena arena = manager.getArena(event.getPlayer().getWorld().getName());
              if(arena == null){
                  return;
              }
              //DO STUFF
          }
      }
       
    Hope you liked my tutorial. If you have any suggestion just tell me. If I see some interest in this resource I'll add stuff to it
     
    #1 Alex0589, Jul 9, 2018
    Last edited: Jul 9, 2018
    • Like Like x 1
  2. DavidDevelops

    Supporter

    4. "[/code] When you want to access the ArenaManager just use a Dependency Injection to get your main class like this:"
    Extra code bracket Haha? Anyways you can also use the [Resource] tag for this, also thanks ill be reading this ;)
     
  3. I forgot to :ROFLMAO:
     
  4. FrostedSnowman

    Resource Staff

    im sorry but this is useless. in theory all you're doing is showing how to create and object and store it, (you should be using a set over list in this case), which most of us developers, or those who have a basic java understanding, are already aware of.
     
  5. No? Not neccessarily.
     
    • Optimistic Optimistic x 1
  6. FrostedSnowman

    Resource Staff

    if this isn't just creating a POJO and storing it in a collection with getters & setters, then how so?
     
    • Agree Agree x 1
  7. Benz56

    Moderator Supporter

    I'm also quite confused as to where the actual Arena part factors in?

    There's not really anything in this code that actually defines an arena. All you are doing is essentially adding players to an Arena object and nothing else. Maybe you should title your resource differently or showcase this functionality in a different context.

    Nonetheless, your code is very clean and follows proper OOP; kudos!
     
    • Agree Agree x 1
  8. Thanks! I'll add stuff to it
     
  9. This looks like a cookie-cutter example for something that really should be more arbitrary, since not all arenas will have only 1 spawn (e.g hunger games has 24). Also with regards to saving players in a set rather than their UUID, I would suggest mentioning that they have to take into consideration players leaving, since on rejoin the Player object is different so hence that object will be stuck in the map until arena is cleared or otherwise.
    For the arena manager, I'd also set the Arena set to private to limit unwanted access and add methods that add/modify/get an arena.

    Overall, code is clean enough (like the stream usage), I'd recommend going into a bit of implementation. As mentioned above, you just created objects that are called Arena and ArenaManager, not really showing what qualities make them an arena (adding players to arena, taking them away, other characteristics)
     
  10. Ya, I'm going to post this features tomorrow, here it's kind of late