Tutorial Make a minigame plugin!

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

Thread Status:
Not open for further replies.
  1. Connal
    Red lines do matter, they're errors. Show me your code.
     
    Connal likes this.
  2. Offline

    Connal

    Oh and btw, when i do /gmbJoin or /gmbCreate it just says an Internal Command occured, if u want the log then i can give u it

    Assist Forgot to tag u

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 6, 2015
  3. Connal
    Please put the code inside ["code"] (without quotation marks). What and where are the errors?
     
  4. Offline

    Connal

    oh sorry, ok

    what the hell? i cant :/

    Code:java
    1. package me.ConnalDonohoe.GuessMyBuild;
    2.  
    3. import org.bukkit.Location;
    4. import org.bukkit.entity.Player;
    5.  
    6. public class ArenaManager {
    7. //we want to get an instance of the manager to work with it statically
    8. public static ArenaManager getManager(){
    9. return a;
    10. }
    11.  
    12. //get an Arena object from the list
    13. public Arena getArena(int i){
    14. for(Arena a : arenas){
    15. if(a.getId() == i){
    16. return a;
    17. }
    18. }
    19. return null;
    20. }
    21.  
    22. //add players to the arena, save their inventory
    23. public void addPlayer(Player p, int i){
    24. Arena a = getArena(i);//get the arena you want to join
    25. if(a == null){//make sure it is not null
    26. p.sendMessage("Invalid arena!");
    27. return;
    28. }
    29.  
    30. a.getPlayers().add(p.getName());//add them to the arena list of players
    31. inv.put(p.getName(), p.getInventory().getContents());//save inventory
    32. armor.put(p.getName(), p.getInventory().getArmorContents());
    33. p.teleport(a.spawn);//teleport to the arena spawn
    34. }
    35.  
    36. //remove players
    37. public void removePlayer(Player p){
    38. Arena a = null;//make an arena
    39. for(Arena arena : arenas){
    40. if(arena.getPlayers().contains(p.getName())){
    41. a = arena;//if the arena has the player, the arena field would be the arena containing the player
    42. }
    43. //if none is found, the arena will be null
    44. }
    45. if(a == null || !a.getPlayers().contains(p.getName())){//make sure it is not null
    46. p.sendMessage("Invalid operation!");
    47. return;
    48. }
    49.  
    50. a.getPlayers().remove(p.getName());//remove from arena
    51. p.getInventory().setContents(inv.get(p.getName()));//restore inventory
    52. p.getInventory().setArmorContents(armor.get(p.getName()));
    53.  
    54. inv.remove(p.getName());//remove entries from hashmaps
    55. armor.remove(p.getName());
    56. p.teleport(locs.get(p.getName()));
    57. locs.remove(p.getName());
    58. }
    59.  
    60. //create arena
    61. public void createArena(Location l){
    62. int num = arenas.size() + 1;
    63.  
    64. Arena a = new Arena(l, num);
    65. arenas.add(a);
    66. }
    67. }
    68.  
    69.  


    Assist
    Errors:

    Line 9 : red line under the letter "a"

    Line 14 : red line under the word "arenas"

    Line 31 : red line under the word "inv."

    Line 32 : red line under the word "armor."

    Line 39 : red line under the word "arenas"

    Lin3 51 : red line under the word "inv."

    Line 52 : red line under the word "armour."

    Line 54 : red line under the word "inv."

    Line 55 : red line under the word "armor."

    Line 56 : red line under the word "locs."

    Line 57 : red line under the word "locs."

    Line 62 : red line under the word "arenas."

    Line 65 : red line under the word "arenas."

    These are where all the arrors are Assist

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 6, 2015
  5. Connal
    You forgot the variables. Add these on top of "public static ArenaManager getManager(){"
    Code:
    public Map<String, Location> locs = new HashMap<String, Location>();
    public static ArenaManager a = new ArenaManager();
    Map<String, ItemStack[]> inv = new HashMap<String, ItemStack[]>();
    Map<String, ItemStack[]> armor = new HashMap<String, ItemStack[]>();
    List<Arena> arenas = new ArrayList<Arena>();
     
  6. Offline

    Connal

    Thanks! No errors now, thank you so much for a few things

    1. for the help and time u put into helping me!
    2. Not calling me a noob at java (which i am)

    You are so helpful!

    I love you (no homo)

    Assist Ok so no errors, in the CLASS files and code, but only problem is when i do /gmbCreate it says nothing and when i try to join it says internal error occured, why is this happening? Do i need a yml file to store the arenas or...?
    Here are all my class files below

    GuessMyBuild (Main Class)

    Code:java
    1. package me.ConnalDonohoe.GuessMyBuild;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5. import java.util.Random;
    6. import java.util.logging.Logger;
    7.  
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.plugin.PluginDescriptionFile;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14.  
    15. public class GuessMyBuild extends JavaPlugin {
    16. public final Logger logger = Logger.getLogger("Minecraft");
    17. public static GuessMyBuild plugin;
    18. ArrayList<String> lobby = new ArrayList<String>();
    19. public int number = 10;
    20.  
    21. public void onDisable() {
    22. PluginDescriptionFile pdfFile = this.getDescription();
    23. this.logger.info(pdfFile.getName() + " Has Been Disabled!");
    24.  
    25. }
    26.  
    27. public void onEnable() {
    28. PluginDescriptionFile pdfFile = this.getDescription();
    29. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion()
    30. + " Has Been Enabled!");
    31. getConfig().options().copyDefaults(true);
    32. saveConfig();
    33. }
    34.  
    35. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    36. Player player = (Player) sender;
    37. if (commandLabel.equalsIgnoreCase("gmb")) {
    38. player.sendMessage(ChatColor.GOLD
    39. + "Guess My Build created by ConnalDonohoe");
    40. }else if (commandLabel.equalsIgnoreCase("gmbcreate")) {
    41. Player p = (Player) sender;
    42. ArenaManager.getManager().createArena(p.getLocation());//Sets the spawn where i'm standing
    43. } else if (commandLabel.equalsIgnoreCase("gmblobby")) {
    44. player.sendMessage(ChatColor.DARK_RED + "While we add this feature, please do /Warp GMB");
    45. } else if (commandLabel.equalsIgnoreCase("gmbjoin")) {
    46. int num = 0;
    47. try{
    48. num = Integer.parseInt(args[0]);
    49. player.sendMessage("Invalid arena ID");
    50. }
    51. ArenaManager.getManager().addPlayer(player, num);
    52. lobby.add(player.getName());
    53. } else if (commandLabel.equalsIgnoreCase("gmbleave")) {
    54. player.sendMessage(ChatColor.RED + "You have left the lobby!");
    55. lobby.remove(player.getName());
    56. } else if (commandLabel.equalsIgnoreCase("gmbword")) {
    57. player.sendMessage(ChatColor.AQUA + "Choosing a random word to build...");
    58. // setup
    59. Random rand = new Random();
    60. List<String> words = new ArrayList<String>();
    61. words.add("Bannana");
    62. words.add("Steve");
    63. words.add("Lamp");
    64. words.add("Key");
    65. words.add("Poop");
    66. words.add("Giraffe");
    67. words.add("Book");
    68. words.add("Phone");
    69. words.add("Tree");
    70. words.add("Knife");
    71. words.add("Spoon");
    72. words.add("Helicopter");
    73. words.add("Pencil");
    74. words.add("Bee");
    75. words.add("Glasses");
    76. words.add("Person");
    77. words.add("Ring");
    78. words.add("Tripod");
    79. words.add("Guitar");
    80. words.add("Coin");
    81. words.add("Lips");
    82. words.add("Cloud");
    83. words.add("Present");
    84. words.add("Cake");
    85. words.add("Hand");
    86. words.add("Pole");
    87. words.add("Door");
    88. words.add("Trap");
    89. words.add("Rubix Cube");
    90. words.add("Sword");
    91. words.add("Gun");
    92. words.add("Grenade");
    93. words.add("Sun");
    94. words.add("Mountain");
    95. words.add("Box");
    96. words.add("Laptop");
    97. words.add("Mouse");
    98. words.add("Boot (Shoe)");
    99. words.add("Carrot");
    100. words.add("Lego");
    101. words.add("Drivers License");
    102. words.add("Car");
    103. player.sendMessage(ChatColor.GOLD + "Too hard? Do /gmbWord to change your word!");
    104.  
    105.  
    106. // choose a random word
    107. String word = words.get(rand.nextInt(words.size()));
    108. player.sendMessage("Chosen word: " + words.get(rand.nextInt(words.size())));
    109. } else if (commandLabel.equalsIgnoreCase("gmbhelp")) {
    110. player.sendMessage(ChatColor.GOLD + "--------------------");
    111. player.sendMessage(ChatColor.DARK_AQUA
    112. + "/gmbHelp | Open Help Menu");
    113. player.sendMessage(ChatColor.DARK_AQUA
    114. + "/gmbLobby | Go to the lobby");
    115. player.sendMessage(ChatColor.DARK_AQUA
    116. + "/gmbJoin | Join game");
    117. player.sendMessage(ChatColor.DARK_AQUA
    118. + "/gmbLeave | Leave game");
    119. player.sendMessage(ChatColor.DARK_AQUA
    120. + "/gmbInfo | Display info about the plugin");
    121. player.sendMessage(ChatColor.DARK_AQUA
    122. + "/gmbAdmin | Show admin commands");
    123. } else if (commandLabel.equalsIgnoreCase("gmbadmin")) {
    124. player.sendMessage(ChatColor.LIGHT_PURPLE + "/gmbStart | Start the game" );
    125. player.sendMessage(ChatColor.LIGHT_PURPLE + "/gmbCreate | Creates an arena");
    126. player.sendMessage(ChatColor.GOLD + "More setup commands coming soon!");
    127. } else if (commandLabel.equalsIgnoreCase("gmbinfo")) {
    128. player.sendMessage(ChatColor.GOLD + "Created By : ConnalDonohoe");
    129. player.sendMessage(ChatColor.GOLD + "Version : 1.2.0 DEV");
    130. player.sendMessage(ChatColor.GOLD + "Email : [email][email protected][/email]");
    131. player.sendMessage(ChatColor.GOLD + "Website : PartyCraft.tk");
    132. } else if (commandLabel.equalsIgnoreCase("gmbstart")) {
    133. player.sendMessage(ChatColor.GREEN + "You started the game!");
    134.  
    135.  
    136. }
    137. return false;
    138. }
    139. }


    ArenaManager

    Code:java
    1. package me.ConnalDonohoe.GuessMyBuild;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.HashMap;
    5. import java.util.List;
    6. import java.util.Map;
    7.  
    8. import org.bukkit.Location;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.inventory.ItemStack;
    11.  
    12. public class ArenaManager {
    13. public Map<String, Location> locs = new HashMap<String, Location>();
    14. public static ArenaManager a = new ArenaManager();
    15. Map<String, ItemStack[]> inv = new HashMap<String, ItemStack[]>();
    16. Map<String, ItemStack[]> armor = new HashMap<String, ItemStack[]>();
    17. List<Arena> arenas = new ArrayList<Arena>();
    18. //we want to get an instance of the manager to work with it statically
    19. public static ArenaManager getManager(){
    20. return a;
    21. }
    22.  
    23. //get an Arena object from the list
    24. public Arena getArena(int i){
    25. for(Arena a : arenas){
    26. if(a.getId() == i){
    27. return a;
    28. }
    29. }
    30. return null;
    31. }
    32.  
    33. //add players to the arena, save their inventory
    34. public void addPlayer(Player p, int i){
    35. Arena a = getArena(i);//get the arena you want to join
    36. if(a == null){//make sure it is not null
    37. p.sendMessage("Invalid arena!");
    38. return;
    39. }
    40.  
    41. a.getPlayers().add(p.getName());//add them to the arena list of players
    42. inv.put(p.getName(), p.getInventory().getContents());//save inventory
    43. armor.put(p.getName(), p.getInventory().getArmorContents());
    44. p.teleport(a.spawn);//teleport to the arena spawn
    45. }
    46.  
    47. //remove players
    48. public void removePlayer(Player p){
    49. Arena a = null;//make an arena
    50. for(Arena arena : arenas){
    51. if(arena.getPlayers().contains(p.getName())){
    52. a = arena;//if the arena has the player, the arena field would be the arena containing the player
    53. }
    54. //if none is found, the arena will be null
    55. }
    56. if(a == null || !a.getPlayers().contains(p.getName())){//make sure it is not null
    57. p.sendMessage("Invalid operation!");
    58. return;
    59. }
    60.  
    61. a.getPlayers().remove(p.getName());//remove from arena
    62. p.getInventory().setContents(inv.get(p.getName()));//restore inventory
    63. p.getInventory().setArmorContents(armor.get(p.getName()));
    64.  
    65. inv.remove(p.getName());//remove entries from hashmaps
    66. armor.remove(p.getName());
    67. p.teleport(locs.get(p.getName()));
    68. locs.remove(p.getName());
    69. }
    70.  
    71. //create arena
    72. public void createArena(Location l){
    73. int num = arenas.size() + 1;
    74.  
    75. Arena a = new Arena(l, num);
    76. arenas.add(a);
    77. }
    78. }
    79.  
    80.  


    Arena

    Code:java
    1. package me.ConnalDonohoe.GuessMyBuild;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5.  
    6. import org.bukkit.Location;
    7.  
    8. public class Arena {
    9. //you want some info about the arena stored here
    10. int number = 0;//the arena id
    11. Location spawn = null;//spawn location for the arena
    12. List<String> players = new ArrayList<String>();//list of players
    13.  
    14. //now let's make a few getters/setters, and a constructor
    15. public Arena(Location loc, int id){
    16. this.spawn = loc;
    17. this.number = id;
    18. }
    19.  
    20. public int getId(){
    21. return this.number;
    22. }
    23.  
    24. public List<String> getPlayers(){
    25. return this.players;
    26. }
    27. }
    28.  
    29.  
    30.  


    Assist heres the log when i try to join + when i try to create an arena it says nothing ty :)

    Code:java
    1. 15:30:56 [INFO] TheTroll7 issued server command: /gmbjoin
    2. 15:30:56 [SEVERE] null
    3. org.bukkit.command.CommandException: Unhandled exception executing command 'gmbj
    4. oin' in plugin GuessMyBuild v1.2.0 DEV
    5. at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
    6. at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:19
    7. 2)
    8. at org.bukkit.craftbukkit.v1_6_R2.CraftServer.dispatchCommand(CraftServe
    9. r.java:527)
    10. at net.minecraft.server.v1_6_R2.PlayerConnection.handleCommand(PlayerCon
    11. nection.java:979)
    12. at net.minecraft.server.v1_6_R2.PlayerConnection.chat(PlayerConnection.j
    13. ava:890)
    14. at net.minecraft.server.v1_6_R2.PlayerConnection.a(PlayerConnection.java
    15. :837)
    16. at net.minecraft.server.v1_6_R2.Packet3Chat.handle(SourceFile:49)
    17. at org.spigotmc.netty.NettyNetworkManager.b(NettyNetworkManager.java:230
    18. )
    19. at net.minecraft.server.v1_6_R2.PlayerConnection.e(PlayerConnection.java
    20. :116)
    21. at net.minecraft.server.v1_6_R2.ServerConnection.b(SourceFile:37)
    22. at org.spigotmc.netty.NettyServerConnection.b(NettyServerConnection.java
    23. :125)
    24. at net.minecraft.server.v1_6_R2.MinecraftServer.t(MinecraftServer.java:5
    25. 92)
    26. at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:2
    27. 39)
    28. at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:4
    29. 81)
    30. at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java
    31. :413)
    32. at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:5
    33. 82)
    34. Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
    35. at me.ConnalDonohoe.GuessMyBuild.GuessMyBuild.onCommand(GuessMyBuild.jav
    36. a:48)
    37. at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    38. ... 15 more


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 6, 2015
  7. Connal
    Can you please make your own thread instead of spamming this one? This is a tutorial thread, not a thread to ask help at. Make your own one, post the code and error, then tag me. :)
     
  8. Offline

    Connal

    Sure thing!
     
  9. Offline

    xTrollxDudex

    Assist
    Sure, go ahead. I'll post in OP
    ilethz
    Don't really have time for this anymore, BattleDome's got some things not working.
     
  10. Offline

    ilethz

    xTrollxDudex
    Umm, but thank you! I know..
     
  11. Offline

    xTrollxDudex

    ilethz
    Try tagging Assist
     
  12. Offline

    AstramG

    Can you help us integrate a lobby system into this? So that when a player joins the arena they get teleported into a lobby first then into the actual arena.
     
  13. Offline

    xTrollxDudex

    AstramG
    You would modify the addPlayer method to teleport to the set lobby then run a scheduler to spawn them in the arena
     
  14. I totally forgot about this thread.. Please either tahg me (@Assist), start a conversation with me, or post the feature request on my wall.

    I'll be adding the features to this post, and then making a new thread about it, explaining what it does, and how to do it.
     
  15. Offline

    JPG2000

    xTrollxDudex All your code looks amazing, but just one question. The ArrayLists, spawn points and everything go away after a reload, correct?
     
  16. Offline

    xTrollxDudex

    JPG2000
    As said in OP, you don't need to tahg me :p. anyways, of course they go away. Also mentioned in OP, there should be a loadGames method to fill up the lists on enable with new instances of Arena.
     
  17. Offline

    JPG2000

    Okay! Thanks!

    Could you please add an example of the load() method? This would be VERY usefull, as its the main part of a minigame plugin.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 6, 2015
  18. Offline

    xTrollxDudex

    JPG2000
    On page one I linked an example repo. It throws a NullPointerException however I don't really have time to fix it.
     
  19. Offline

    StaticE

    Cool tutorial, sucks that I found it after spending hours figuring everything out by myself (lol). Anyway, what would be the best way to store/save the Arena objects for when the server restarts? Is it even possible to save the objects with their set locations, etc?
     
  20. Offline

    xTrollxDudex

    StaticE
    You can't save an Arena object I don't think, the only way would be probably to persist it (somehow :oops:) or load new Arena objects into the list onEnable
     
  21. Offline

    StaticE

    xTrollxDudex So I could just grab all the data of each Arena Object (spawn locations, etc) on disable, store them to a yml file or something, then recreate the objects on enable?
     
  22. Offline

    xTrollxDudex

    StaticE
    Yes. Then again there are solutions like db4o(DataBaseForObjects)that can store stuff like that locally which is really, really cool.
     
  23. Offline

    Awesomeman2

    I think this should be a sticky therad because this could help alot of people
     
  24. Offline

    StaticE

    xTrollxDudex That looks sweet! Although, since I have to add it to my classpath doesn't that mean anyone using my plugin would have to have db40?
     
  25. Offline

    xTrollxDudex

    StaticE likes this.
  26. Offline

    Awesomeman2

  27. Offline

    CdoingBaddie

    What should the main class be?
     
  28. Offline

    xTrollxDudex

    CdoingBaddie
    You make your own main class. Since each of these classes I've shown are utility/objects, they don't need to be registered.
     
  29. Offline

    CdoingBaddie

    Oh yeah! Thanks haha
     
  30. Offline

    Awesomeman2

    You should add setting up random spawns for people or multiple spawns
     
Thread Status:
Not open for further replies.

Share This Page