Solved Joining arena help

Discussion in 'Plugin Development' started by drpk, Sep 19, 2014.

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

    drpk

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

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

    Commands class:
    Code:java
    1. @Override
    2. public boolean onCommand(CommandSender sender, Command cmd, String label, String args[]) {
    3. if (!(sender instanceof Player)) {
    4. sender.sendMessage("Whoa there buddy, only players may execute this!");
    5. return true;
    6. }
    7. if (cmd.getName().equalsIgnoreCase("koth")) {
    8. String sub = args[0];
    9.  
    10. Player p = (Player) sender;
    11.  
    12. if (sub.equalsIgnoreCase("create")) {
    13. if (p.hasPermission("KoTH.create")) {
    14. ArenaManager.getManager().createArena(p.getLocation());
    15. p.sendMessage("Created arena at " + p.getLocation().toString());
    16.  
    17. return true;
    18. }
    19. }
    20. if (sub.equalsIgnoreCase("join")) {
    21. if (args.length != 2) {
    22. p.sendMessage("Insufficient arguments!");
    23. return true;
    24. }
    25. int num = 0;
    26. try {
    27. num = Integer.parseInt(args[0]);
    28. } catch (NumberFormatException e) {
    29. p.sendMessage("Invalid arena ID");
    30. }
    31. ArenaManager.getManager().addPlayer(p, num);
    32.  
    33. return true;
    34. }
    35. if (sub.equalsIgnoreCase("leave")) {
    36. ArenaManager.getManager().removePlayer(p);
    37. p.sendMessage("You have left the arena!");
    38.  
    39. return true;
    40. }
    41. if (sub.equalsIgnoreCase("remove")) {
    42. if (p.hasPermission("KoTH.remove") || p.hasPermission("KoTH.*"))
    43. if (args.length != 1) {
    44. p.sendMessage("Insuffcient arguments!");
    45. return true;
    46. }
    47. int num = 0;
    48. try {
    49. num = Integer.parseInt(args[0]);
    50. } catch (NumberFormatException e) {
    51. p.sendMessage("Invalid arena ID");
    52. }
    53. ArenaManager.getManager().removeArena(num);
    54.  
    55. return true;
    56. }
    57.  
    58. return false;
    59. }
    60. return false;
    61. }
     
  2. Offline

    rbrick

    tl;dr
    EDIT: Does the arena actually exist? The code looks like it will work.
     
  3. Offline

    drpk

    rbrick yes it works, I have a file where all the info is saved, but cannot teleport to any arenas.
    EDIT: fixed, I forgot to change the args[0] to args[1] in the join command
     
  4. Offline

    fireblast709

    drpk for God's sake remove that static instance and the empty constructor. Currently you are just asking for a NullPointerException.
     
    rbrick likes this.
  5. Offline

    xTrollxDudex

    Oh yeah... The GitHub code is outdated, look on the forum post. Gotta remind myself to update it.
     
    rbrick likes this.
Thread Status:
Not open for further replies.

Share This Page