Advertisement
Guest User

Untitled

a guest
Feb 16th, 2014
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.71 KB | None | 0 0
  1. public class BT2ArenaManager {
  2.  
  3. //save where the player teleported
  4. public Map<String, Location> locs = new HashMap<String, Location>();
  5. //make a new instance of the class
  6. public static BT2ArenaManager am = new BT2ArenaManager();
  7. //a few other fields
  8. Map<String, ItemStack[]> inv = new HashMap<String, ItemStack[]>();
  9. Map<String, ItemStack[]> armor = new HashMap<String, ItemStack[]>();
  10. //list of BT2Arenas
  11. List<BT2Arena> BT2Arenas = new ArrayList<BT2Arena>();
  12. int BT2ArenaSize = 0; // <--- that is how the arenas get their id's
  13.  
  14. GameMode g = null;
  15.  
  16. //Stuff to add to inventory for minigame
  17. ItemStack bow = new ItemStack(Material.BOW, 1);
  18. ItemStack arrow = new ItemStack(Material.ARROW, 1);
  19.  
  20. static BowThrow2 plugin;
  21. public BT2ArenaManager(BowThrow2 bowThrow2) {
  22. plugin = bowThrow2;
  23. }
  24.  
  25. public BT2ArenaManager(){
  26.  
  27. }
  28.  
  29. //we want to get an instance of the manager to work with it statically
  30. public static BT2ArenaManager getManager(){
  31. return am;
  32. }
  33.  
  34. //get an BT2Arena object from the list
  35. public BT2Arena getBT2Arena(int i){
  36. for(BT2Arena a : BT2Arenas){
  37. if(a.getId() == i){
  38. return a;
  39. }
  40. }
  41. return null;
  42. }
  43.  
  44. //add players to the BT2Arena, save their inventory
  45. public void addPlayer(Player p, int i){
  46. BT2Arena a = getBT2Arena(i);//get the BT2Arena you want to join
  47. if(a == null){//make sure it is not null
  48. p.sendMessage(ChatColor.RED + "Arena is null (probably doesn't exist)!");
  49. return;
  50. }
  51.  
  52. a.getPlayers().add(p.getName());//add them to the BT2Arena list of players
  53. inv.put(p.getName(), p.getInventory().getContents());//save inventory
  54. armor.put(p.getName(), p.getInventory().getArmorContents());
  55.  
  56. p.getInventory().setArmorContents(null);
  57. p.getInventory().clear();
  58. p.getInventory().addItem(bow);
  59. p.getInventory().addItem(arrow);
  60. g = p.getGameMode();
  61. p.setGameMode(GameMode.SURVIVAL);
  62.  
  63. BT2GameListener.addPlayer(p);
  64.  
  65. locs.put(p.getName(), p.getLocation());
  66.  
  67. p.teleport(a.spawn);
  68.  
  69. p.sendMessage(ChatColor.GREEN + "You joined Arena " + a.id);
  70. }
  71.  
  72. //remove players
  73. public void removePlayer(Player p){
  74. BT2Arena a = null;//make an BT2Arena
  75. for(BT2Arena BT2Arena : BT2Arenas){
  76. if(BT2Arena.getPlayers().contains(p.getName())){
  77. a = BT2Arena;//if the BT2Arena has the player, the BT2Arena field would be the BT2Arena containing the player
  78. }
  79. //if none is found, the BT2Arena will be null
  80. }
  81. if(a == null || !a.getPlayers().contains(p.getName())){//make sure it is not null
  82. p.sendMessage("Invalid operation!");
  83. return;
  84. }
  85.  
  86. a.getPlayers().remove(p.getName());//remove from BT2Arena
  87.  
  88. p.getInventory().clear();
  89. p.getInventory().setArmorContents(null);
  90.  
  91. p.getInventory().setContents(inv.get(p.getName()));//restore inventory
  92. p.getInventory().setArmorContents(armor.get(p.getName()));
  93.  
  94. inv.remove(p.getName());//remove entries from hashmaps
  95. armor.remove(p.getName());
  96. p.teleport(locs.get(p.getName()));
  97. locs.remove(p.getName());
  98. p.setGameMode(g);
  99.  
  100. p.setFireTicks(0);
  101. }
  102.  
  103. //create BT2Arena
  104. public BT2Arena createBT2Arena(Location l){
  105. int num = BT2ArenaSize + 1;
  106. BT2ArenaSize++;
  107.  
  108. BT2Arena a = new BT2Arena(l, num);
  109. BT2Arenas.add(a);
  110.  
  111. plugin.getConfig().set("BT2Arenas." + num, serializeLoc(l));
  112. List<Integer> list = plugin.getConfig().getIntegerList("BT2Arenas.BT2Arenas");
  113. list.add(num);
  114. plugin.getConfig().set("BT2Arenas.BT2Arenas", list);
  115. plugin.saveConfig();
  116.  
  117. return a;
  118. }
  119.  
  120. public BT2Arena reloadBT2Arena(Location l) {
  121. int num = BT2ArenaSize + 1;
  122. BT2ArenaSize++;
  123.  
  124. BT2Arena a = new BT2Arena(l, num);
  125. BT2Arenas.add(a);
  126.  
  127. return a;
  128. }
  129.  
  130. public void removeBT2Arena(int i) {
  131. BT2Arena a = getBT2Arena(i);
  132.  
  133. if(a == null) {
  134. return;
  135. }
  136.  
  137. BT2Arenas.remove(a);
  138. BT2ArenaSize--;
  139.  
  140. if (BT2ArenaSize == -1) {
  141. BT2ArenaSize = 0;
  142. }
  143.  
  144.  
  145. plugin.getConfig().set("BT2Arenas." + i, null);
  146. List<Integer> list = plugin.getConfig().getIntegerList("BT2Arenas.BT2Arenas");
  147. list.remove(a);
  148. plugin.getConfig().set("BT2Arenas.BT2Arenas", list);
  149. plugin.saveConfig();
  150. }
  151.  
  152. public boolean isInGame(Player p){
  153. for(BT2Arena a : BT2Arenas){
  154. if(a.getPlayers().contains(p.getName()))
  155. return true;
  156. }
  157. return false;
  158. }
  159.  
  160. public void loadGames(){
  161. BT2ArenaSize = 0;
  162.  
  163. if(plugin.getConfig().getIntegerList("BT2Arenas.BT2Arenas").isEmpty()){
  164. return;
  165. }
  166.  
  167. for(int i : plugin.getConfig().getIntegerList("BT2Arenas.BT2Arenas")){
  168. BT2Arena a = reloadBT2Arena(deserializeLoc(plugin.getConfig().getString("BT2Arenas." + i)));
  169. a.id = i;
  170. }
  171. }
  172.  
  173. public String serializeLoc(Location l){
  174. return l.getWorld().getName()+","+l.getBlockX()+","+l.getBlockY()+","+l.getBlockZ();
  175. }
  176. public Location deserializeLoc(String s){
  177. String[] st = s.split(",");
  178. return new Location(Bukkit.getWorld(st[0]), Integer.parseInt(st[1]), Integer.parseInt(st[2]), Integer.parseInt(st[3]));
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement