Tutorial Make a minigame plugin!

Discussion in 'Resources' started by xTrollxDudex, Aug 15, 2013.

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

    JeremyPark

    @xTrollxDudex
    @Onlineids

    Ok, I had another look at the arena class, and yeah, I didn't know what I was talking about. I thought the way I had set up the isInGame boolean to loop through the getPlayers() and then check if the player was in the correct arena. Now that I think about it, that doesn't make any sense, and I was thinking back to front... :|
     
  2. Offline

    frogman6102

    xTrollxDudex
    I was scrolling through the resources and came upon this. I like it very much! I wanted to create mini game plugins but had no clue where to start, now (Thanks to you) I understand how they work and function. Thank you so very much!
     
    xTrollxDudex likes this.
  3. Offline

    Charliechumbuck

    xTrollxDudex
    I'm trying to make multiple locations for the arenas, but I don't know how to serialize and deserialize multiple locations.
    Here's my Arena class:
    Code:java
    1. package minecraft.Charliechumbuck.me.BlockFighters;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.HashMap;
    5. import java.util.List;
    6.  
    7. import org.bukkit.Location;
    8. import org.bukkit.entity.Player;
    9.  
    10. import minecraft.Charliechumbuck.me.BlockFighters.ArenaManager.Team;
    11.  
    12. public class Arena {
    13.  
    14. public int id = 0;
    15. private int numPlayersAllowed=2;
    16. public Location redspawn = null;
    17. public Location bluespawn = null;
    18. public Location greenspawn = null;
    19. public Location yellowspawn = null;
    20. List<String> totalPlayers = new ArrayList<String>();
    21. private HashMap<String,Team> players = new HashMap<String,Team>();
    22.  
    23. public Arena(Location redl,Location bluel,Location greenl,Location yellowl,int id){
    24. this.id = id;
    25. this.redspawn = redl;
    26. this.bluespawn = bluel;
    27. this.greenspawn = greenl;
    28. this.yellowspawn = yellowl;
    29. }
    30.  
    31. public int getId(){
    32. return this.id;
    33. }
    34.  
    35. public List<String> getPlayers(){
    36. return this.totalPlayers;
    37. }
    38.  
    39. public Location getSpawn(Team team){
    40. switch(team){
    41. case RED:return redspawn;
    42. case BLUE:return bluespawn;
    43. case GREEN:return greenspawn;
    44. case YELLOW:return yellowspawn;
    45. default:return null;
    46. }
    47. }
    48.  
    49. public Team getTeam(Player p){
    50. return players.get(p.getName());
    51. }
    52.  
    53. public void addPlayer(Player p){
    54. players.put(p.getName(), getTeamWithLessPlayers());
    55. }
    56.  
    57. public void removePlayer(Player p){
    58. players.remove(p);
    59. }
    60.  
    61. private Team getTeamWithLessPlayers(){
    62. int red=0,blue=0,green=0,yellow=0,numPlayers=0;
    63. for(String p:players.keySet()){
    64. if(players.get(p)==Team.RED){red++;numPlayers++;}
    65. else if(players.get(p)==Team.BLUE){blue++;numPlayers++;}
    66. else if(players.get(p)==Team.GREEN){green++;numPlayers++;}
    67. else if(players.get(p)==Team.YELLOW){yellow++;numPlayers++;}
    68. }
    69. if(numPlayers>numPlayersAllowed)numPlayers = numPlayersAllowed;
    70. if(red>blue) return Team.BLUE;
    71. else if(red>green) return Team.GREEN;
    72. else if(red>yellow) return Team.YELLOW;
    73.  
    74. else if(blue>red) return Team.RED;
    75. else if(blue>green) return Team.GREEN;
    76. else if(blue>yellow) return Team.YELLOW;
    77.  
    78. else if(green>red) return Team.RED;
    79. else if(green>blue) return Team.BLUE;
    80. else if(green>yellow) return Team.YELLOW;
    81.  
    82. else if(yellow>red) return Team.RED;
    83. else if(yellow>blue) return Team.BLUE;
    84. else return Team.GREEN;
    85. }
    86.  
    87. public boolean containsPlayer(Player p){
    88. return players.keySet().contains(p.getName());
    89. }
    90. }

    The ArenaManager class:
    Code:java
    1. package minecraft.Charliechumbuck.me.BlockFighters;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.HashMap;
    5. import java.util.List;
    6. import java.util.Map;
    7.  
    8. import minecraft.Charliechumbuck.me.GryffinMain;
    9.  
    10. import org.bukkit.Bukkit;
    11. import org.bukkit.ChatColor;
    12. import org.bukkit.Location;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.inventory.ItemStack;
    15.  
    16.  
    17. public class ArenaManager {
    18. public enum Team {RED,BLUE,GREEN,YELLOW;}
    19. public Map<String,Location> locs = new HashMap<String,Location>();
    20. public static ArenaManager am = new ArenaManager();
    21. Map<String,ItemStack[]> inv = new HashMap<String,ItemStack[]>();
    22. Map<String,ItemStack[]> armor = new HashMap<String,ItemStack[]>();
    23. List<Arena> arenas = new ArrayList<Arena>();
    24. int arenaSize = 0;
    25. static GryffinMain plugin;
    26.  
    27. public ArenaManager(GryffinMain gm){
    28. plugin = gm;
    29. }
    30.  
    31. public ArenaManager(){}
    32.  
    33. public static ArenaManager getManager(){
    34. return am;
    35. }
    36.  
    37. private static ArenaManager instance = new ArenaManager();
    38.  
    39. public static ArenaManager getInstance(){
    40. return instance;
    41. }
    42.  
    43. public Arena getArena(int id){
    44. for(Arena a:arenas){
    45. if(a.getId()==id) return a;
    46. }
    47. return null;
    48. }
    49.  
    50. public Arena getArena(Player p){
    51. for(Arena a:arenas){
    52. if(a.containsPlayer(p)) return a;
    53. }
    54. return null;
    55. }
    56.  
    57. public void addPlayer(Player p,int i){
    58. Arena a = getArena(i);
    59. if(a==null){
    60. p.sendMessage(ChatColor.RED+"Invalid arena!");
    61. return;
    62. }
    63. a.getPlayers().add(p.getName());
    64. inv.put(p.getName(),p.getInventory().getContents());
    65. armor.put(p.getName(),p.getInventory().getArmorContents());
    66.  
    67. p.getInventory().setArmorContents(null);
    68. p.getInventory().clear();
    69.  
    70. locs.put(p.getName(),p.getLocation());
    71. p.teleport(a.getSpawn(a.getTeam(p)));
    72. }
    73.  
    74. public void removePlayer(Player p){
    75. Arena a = null;
    76. for(Arena arena:arenas){
    77. if(arena.getPlayers().contains(p.getName())){
    78. a = arena;
    79. }
    80. }
    81. if(a==null||!a.getPlayers().contains(p.getName())){
    82. p.sendMessage(ChatColor.RED+"Invalid operation!");
    83. return;
    84. }
    85.  
    86. a.getPlayers().remove(p.getName());
    87.  
    88. p.getInventory().clear();
    89. p.getInventory().setArmorContents(null);
    90.  
    91. p.getInventory().setContents(inv.get(p.getName()));
    92. p.getInventory().setArmorContents(armor.get(p.getName()));
    93.  
    94. inv.remove(p.getName());
    95. armor.remove(p.getName());
    96.  
    97. p.setFireTicks(0);
    98. }
    99.  
    100. public Arena createArena(Location red, Location blue, Location green, Location yellow){
    101. int num = arenaSize + 1;
    102. arenaSize++;
    103.  
    104. Arena a = new Arena(red,blue,green,yellow,num);
    105. arenas.add(a);
    106.  
    107. plugin.getConfig().set("Arenas."+num,serializeLocs(red,blue,green,yellow));
    108. List<Integer> list = plugin.getConfig().getIntegerList("Arenas.Arenas");
    109. list.add(num);
    110. plugin.getConfig().set("Arenas.Arenas", list);
    111. plugin.saveConfig();
    112.  
    113. return a;
    114. }
    115.  
    116. public Arena reloadArena(Location red, Location blue, Location green, Location yellow){
    117. int num = arenaSize+1;
    118. arenaSize++;
    119.  
    120. Arena a = new Arena(red,blue,green,yellow,num);
    121. arenas.add(a);
    122.  
    123. return a;
    124. }
    125.  
    126. public void removeArena(int i){
    127. Arena a = getArena(i);
    128. if(a==null){
    129. return;
    130. }
    131. arenas.remove(a);
    132.  
    133. plugin.getConfig().set("Arenas."+i, null);
    134. List<Integer> list = plugin.getConfig().getIntegerList("Arenas.Arenas");
    135. list.remove(i);
    136. plugin.getConfig().set("Arenas.Arenas", list);
    137. plugin.saveConfig();
    138. }
    139.  
    140. public boolean isInGame(Player p){
    141. for(Arena a:arenas){
    142. if(a.getPlayers().contains(p.getName())){
    143. return true;
    144. }
    145. }
    146. return false;
    147. }
    148.  
    149. public void loadGames(){
    150. arenaSize = 0;
    151.  
    152. if(plugin.getConfig().getIntegerList("Arenas.Arenas").isEmpty()){
    153. return;
    154. }
    155.  
    156. for(int i:plugin.getConfig().getIntegerList("Arenas.Arenas")){
    157. //Arena a = reloadArena(deserializeLocs(plugin.getConfig().getString("Arenas." + i));
    158. //a.id = i;
    159. }
    160. }
    161.  
    162.  
    163. public String serializeLocs(Location red,Location blue,Location green, Location yellow){
    164. return red.getWorld().getName()+","+red.getBlockX()+","+red.getBlockY()+","+red.getBlockZ()+
    165. blue.getWorld().getName()+","+blue.getBlockX()+","+blue.getBlockY()+","+blue.getBlockZ()+
    166. green.getWorld().getName()+","+green.getBlockX()+","+green.getBlockY()+","+green.getBlockZ()+
    167. yellow.getWorld().getName()+","+yellow.getBlockX()+","+yellow.getBlockY()+","+yellow.getBlockZ();
    168. }
    169.  
    170. //public Location deserializeLocs(String r,String b,String g,String y){
    171. //String[] rt = r.split(",");
    172. //String[] bt = b.split(",");
    173. //String[] gt = g.split(",");
    174. //String[] yt = y.split(",");
    175. //return new Location(Bukkit.getWorld(rt[0]), Integer.parseInt(rt[1]), Integer.parseInt(rt[2]), Integer.parseInt(rt[3]));
    176. //new Location;
    177. //}
    178. }


    Also, I commented out places where I didn't know how to fix the problem.
     
  4. Offline

    xTrollxDudex

    Charliechumbuck
    Access your locations and pass them into the location serialize method, and setstring to the config
     
  5. Offline

    MoeMix

    Wouldn't it be better to define the arena id in the constructor and not always have it equal to zero? hen when you instantiate the new arena class you can easily pass in an id.
     
  6. Offline

    xTrollxDudex

    What constructor?
     
  7. Offline

    MoeMix

    The constructor of the Arena class
     
  8. Offline

    xTrollxDudex

    Umm, it does take an id parameter though?
     
  9. Offline

    MoeMix

    Oh, then why do you originally set the id to 0 when you declare it? What's the point for that?
     
  10. Offline

    xTrollxDudex

    I'm initializing it when it loads, the value is set when constructed. Basically it's a placeholder.
     
  11. Offline

    MoeMix

    I see, it wouldn't make a difference if you just did private int id; instead of private int id = o;
    But np, whateva floats ya boat :)
     
  12. Offline

    Bailey Payne

    It took me a few good reads, but i do finally get whats going on, great tutorial, thanks man!
     
    xTrollxDudex likes this.
  13. Offline

    xTrollxDudex

    Glad to know it helped
     
  14. Offline

    Starfire1337

    This really helped me :)

    Edit: at
    Code:java
    1.  
    2. try{
    3. num = Integer.parseInt(args[0]);
    4. p.sendMessage(ChatColor.RED + "Invalid Arena ID");
    5. }

    Shouldn't you have a return after the p.sendMessage?
     
  15. Offline

    xTrollxDudex

    Maybe I should, I'll look into that, thanks :)
     
  16. Offline

    Starfire1337

    xTrollxDudex
    Also - You should add a few lines of code to check whether the player is already in the game before joining a new one, etc.

    Code:java
    1. public void addPlayer(Player p, int i){
    2. if(ArenaManager.getManager().isInGame(p)) {
    3. p.sendMessage("You are already in a game!");
    4. return;
    5. }
    6. }

    As of now, you can join another game while still in a game, which creates an error when you try to leave.
     
  17. Offline

    xTrollxDudex

    Starfire1337
    Many thanks, I updated the OP, hope the changes I as you liked.

    I had it done on an iPad, so syntactical accuracy is not guaranteed :)
     
  18. Very Good Tutorial! Thx Man, one question, how do u get the id of arena that the specific player has joined, im trying to make something like: If(!!!Player1!!! is in the same arena as !!!Player2!!! then:){
    ya, i hope u understood what i want of this, if p1 and p2 is in the same arena, the method gonna return true :)
    Thanks In Advance! //Carl
     
  19. Offline

    xTrollxDudex

    There's multiple ways, more specificly, you can look at the addPlayer method
     
  20. Offline

    xTrollxDudex

    Put the logic in the addPlayer method.
     
  21. Offline

    EvilWitchdoctor

    I just stumbled across this a minute ago...
    Oh why didn't I think to search for this yesterday, I could have used this and saved myself a lot of time building an arena class that I currently am using by taking and modifying this. :rolleyes:

    Ah well, we all learn. :)
     
    xTrollxDudex likes this.
  22. Offline

    Hawktasard

    Don't really do Minigames, But this looks pretty neat.
    I also love how you wrote all this on an iPad.
     
    TheMintyMate and xTrollxDudex like this.
  23. Offline

    DogeDebugger

    Awesome! Hey, can i make a minigame for my server off of this? Credits to you of course.
     
  24. Offline

    xTrollxDudex

    @DogeDebugger
    Of course. And you don't need to credit me because
    1. I don't care
    2. I care even less if its a private plugin
    3. I don't care
    Have fun :p
     
  25. Offline

    ProStriker123

    xTrollxDudex likes this.
  26. Offline

    woutwoot

    I'm currently working on a plugin like this. It's just a PvP plugin, but before I start doing too much programming, I want to make sure I've got the right design. Would you mind checking it out, giving me some tips? https://github.com/WorldofWoutCraft/WoWPvP
     
  27. Offline

    xTrollxDudex

    @woutwoot
    Jeez that's a lot of code :p

    It looks fine to me anyways. Good luck!
     
  28. Offline

    woutwoot

    Thanks! :)
     
  29. Offline

    DogeDebugger

    Wow, thanks! <3
     
  30. Offline

    _Cookie_

    Code:
        private final public Map<UUID, Location> locs = new HashMap<UUID, Location>();
    
    private final public? :confused: You drunk or haha. Just kidding mate, just a heads up for ya.
     
    xTrollxDudex likes this.
Thread Status:
Not open for further replies.

Share This Page