MiniGame Help Needed

Discussion in 'Plugin Development' started by superjesse07, Nov 7, 2013.

Thread Status:
Not open for further replies.
  1. i saw a minigame tutorial on bukkit and i got some code from it but i don't know why i get a error if i restart my server the error is that it say that i can't join the game because he can't find the arena
    this is all my code this is my main class:
    Code:java
    1. package me.jesse.run;
    2.  
    3. import org.bukkit.command.Command;
    4. import org.bukkit.command.CommandSender;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.plugin.java.JavaPlugin;
    7.  
    8. public class Run extends JavaPlugin {
    9.  
    10. @Override
    11. public void onEnable(){
    12. if(getConfig() == null)
    13. saveDefaultConfig();
    14.  
    15. new ArenaManager(this);
    16. ArenaManager.getManager().loadGames();
    17.  
    18. getServer().getPluginManager().registerEvents(new GameListener(this), this);
    19. }
    20.  
    21. @Override
    22. public void onDisable(){
    23. saveConfig();
    24. }
    25.  
    26. @Override
    27. public boolean onCommand(CommandSender sender, Command cmd, String label, String args[]){
    28. if(!(sender instanceof Player)){
    29. sender.sendMessage("Whoa there buddy, only players may execute this!");
    30. return true;
    31. }
    32.  
    33. Player p = (Player) sender;
    34.  
    35. if(cmd.getName().equalsIgnoreCase("runcreate")){
    36. ArenaManager.getManager().createArena(p.getLocation());
    37. p.sendMessage("Created arena at " + p.getLocation().toString());
    38.  
    39. return true;
    40. }
    41. if(cmd.getName().equalsIgnoreCase("runjoin")){
    42. if(args.length != 1){
    43. p.sendMessage("Insuffcient arguments!");
    44. return true;
    45. }
    46. int num = 0;
    47. try{
    48. num = Integer.parseInt(args[0]);
    49. p.sendMessage("Invalid arena ID");
    50. }
    51. ArenaManager.getManager().addPlayer(p, num);
    52.  
    53. return true;
    54. }
    55. if(cmd.getName().equalsIgnoreCase("runleave")){
    56. ArenaManager.getManager().removePlayer(p);
    57. p.sendMessage("You have left the arena!");
    58.  
    59. return true;
    60. }
    61. if(cmd.getName().equalsIgnoreCase("runremove")){
    62. if(args.length != 1){
    63. p.sendMessage("Insuffcient arguments!");
    64. return true;
    65. }
    66. int num = 0;
    67. try{
    68. num = Integer.parseInt(args[0]);
    69. p.sendMessage("Invalid arena ID");
    70. }
    71. ArenaManager.getManager().removeArena(num);
    72.  
    73. return true;
    74. }
    75.  
    76. return false;
    77. }
    78.  
    79. }
    80.  

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

    and this is my ArenaManager:
    Code:java
    1. package me.jesse.run;
    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.Bukkit;
    9. import org.bukkit.Location;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.inventory.ItemStack;
    12.  
    13. public class ArenaManager {
    14.  
    15. //save where the player teleported
    16. public Map<String, Location> locs = new HashMap<String, Location>();
    17. //make a new instance of the class
    18. public static ArenaManager a = new ArenaManager();
    19. //a few other fields
    20. Map<String, ItemStack[]> inv = new HashMap<String, ItemStack[]>();
    21. Map<String, ItemStack[]> armor = new HashMap<String, ItemStack[]>();
    22. //list of arenas
    23. List<Arena> arenas = new ArrayList<Arena>();
    24. int arenaSize = 0;
    25.  
    26. static Run plugin;
    27. public ArenaManager(Run arenaPVP) {
    28. plugin = arenaPVP;
    29. }
    30.  
    31. public ArenaManager(){
    32.  
    33. }
    34.  
    35. //we want to get an instance of the manager to work with it statically
    36. public static ArenaManager getManager(){
    37. return a;
    38. }
    39.  
    40. //get an Arena object from the list
    41. public Arena getArena(int i){
    42. for(Arena a : arenas){
    43. if(a.getId() == i){
    44. return a;
    45. }
    46. }
    47. return null;
    48. }
    49.  
    50. //add players to the arena, save their inventory
    51. public void addPlayer(Player p, int i){
    52. Arena a = getArena(i);//get the arena you want to join
    53. if(a == null){//make sure it is not null
    54. p.sendMessage("Invalid arena!");
    55. return;
    56. }
    57.  
    58. a.getPlayers().add(p.getName());//add them to the arena list of players
    59. inv.put(p.getName(), p.getInventory().getContents());//save inventory
    60. armor.put(p.getName(), p.getInventory().getArmorContents());
    61.  
    62. p.getInventory().setArmorContents(null);
    63. p.getInventory().clear();
    64.  
    65. p.teleport(a.spawn);//teleport to the arena spawn
    66. GameListener.add(p);
    67. }
    68.  
    69. //remove players
    70. public void removePlayer(Player p){
    71. Arena a = null;//make an arena
    72. for(Arena arena : arenas){
    73. if(arena.getPlayers().contains(p.getName())){
    74. a = arena;//if the arena has the player, the arena field would be the arena containing the player
    75. }
    76. //if none is found, the arena will be null
    77. }
    78. if(a == null || !a.getPlayers().contains(p.getName())){//make sure it is not null
    79. p.sendMessage("Invalid operation!");
    80. return;
    81. }
    82.  
    83. a.getPlayers().remove(p.getName());//remove from arena
    84.  
    85. p.getInventory().clear();
    86. p.getInventory().setArmorContents(null);
    87.  
    88. p.getInventory().setContents(inv.get(p.getName()));//restore inventory
    89. p.getInventory().setArmorContents(armor.get(p.getName()));
    90.  
    91. inv.remove(p.getName());//remove entries from hashmaps
    92. armor.remove(p.getName());
    93. p.teleport(locs.get(p.getName()));
    94. locs.remove(p.getName());
    95. }
    96.  
    97. //create arena
    98. public void createArena(Location l){
    99. int num = arenaSize + 1;
    100. arenaSize++;
    101.  
    102. Arena a = new Arena(l, num);
    103. arenas.add(a);
    104.  
    105. plugin.getConfig().set("Arenas" + num, serializeLoc(l));
    106. List<Integer> list = plugin.getConfig().getIntegerList("Arenas.Arenas");
    107. list.add(num);
    108. plugin.getConfig().set("Arenas.Arenas", list);
    109. plugin.saveConfig();
    110. }
    111.  
    112. public void removeArena(int i){
    113. for(String s : getArena(i).getPlayers()){
    114. Player p = Bukkit.getPlayerExact(s);
    115. if(p != null){
    116. p.teleport(locs.get(p.getName()));
    117. locs.remove(p.getName());
    118. }
    119. }
    120. arenas.remove(getArena(i));
    121. plugin.getConfig().set("Arenas" + num, null);
    122. List<Integer> list = plugin.getConfig().getIntegerList("Arenas.Arenas");
    123. list.remove(i);
    124. plugin.getConfig().set("Arenas.Arenas", list);
    125. plugin.saveConfig();
    126. }
    127.  
    128. public boolean isInGame(Player p){
    129. for(Arena a : arenas){
    130. if(a.getPlayers().contains(p.getName()))
    131. return true;
    132. }
    133. return false;
    134. }
    135.  
    136. public void loadGames(){
    137. if(plugin.getConfig().getIntegerList("Arenas.Arenas").isEmpty()){
    138. return;
    139. }
    140.  
    141. for(int i : plugin.getConfig().getIntegerList("Arenas.Arenas")){
    142. Arena a = new Arena(deserializeLoc(plugin.getConfig().getString("Arenas" + i)), i);
    143. arenas.add(a);
    144. }
    145. }
    146. public String serializeLoc(Location l){
    147. return l.getWorld().getName()+","+l.getBlockX()+","+l.getBlockY()+","+l.getBlockZ();
    148. }
    149. public Location deserializeLoc(String s){
    150. String[] st = s.split(",");
    151. return new Location(Bukkit.getWorld(st[1]), Integer.parseInt(st[1]), Integer.parseInt(st[2]), Integer.parseInt(st[3]));
    152. }
    153. }
    154.  

    and this is my GameListener:
    Code:java
    1. package me.jesse.run;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    11. import org.bukkit.event.entity.PlayerDeathEvent;
    12.  
    13. public class GameListener implements Listener{
    14.  
    15. static List<String> players = new ArrayList<String>();
    16. static Run plugin;
    17.  
    18. public GameListener(Run plugin){
    19. GameListener.plugin = plugin;
    20. }
    21.  
    22. @EventHandler
    23. public void onDamange(EntityDamageByEntityEvent e){
    24. if(e.getEntity() instanceof Player && players.contains(((Player) e.getEntity()).getName())){
    25. e.setCancelled(true);
    26. }
    27. }
    28.  
    29. @EventHandler
    30. public void onDeath(PlayerDeathEvent e){
    31. if(ArenaManager.getManager().isInGame(e.getEntity())){
    32. ArenaManager.getManager().removePlayer(e.getEntity());
    33. }
    34. }
    35.  
    36. public static void add(Player p){
    37. final String name = p.getName();
    38. players.add(name);
    39.  
    40. Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable(){
    41. @Override
    42. public void run(){
    43. players.remove(name);
    44. }
    45. }, 100L);
    46. }
    47. }
    Sorry for my bad english and please someone help me i get a error by the ArenaManager line 121
     
  2. Do you have a config.yml?
     
  3. Offline

    drtshock

    What's the error?
     
  4. Offline

    xTrollxDudex

  5. xTrollxDudex i'm not good at coding how do i make a load thing or how its called
     
  6. Offline

    xTrollxDudex

  7. xTrollxDudex ow thank you i i'm a little bit new in this but i'm very good at it already
     
Thread Status:
Not open for further replies.

Share This Page