Advertisement
Guest User

Untitled

a guest
Nov 11th, 2013
1,119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.54 KB | None | 0 0
  1. package me.JPG.Tester;
  2.  
  3. import org.bukkit.Bukkit;
  4. import org.bukkit.ChatColor;
  5. import org.bukkit.Location;
  6. import org.bukkit.World;
  7. import org.bukkit.configuration.file.FileConfiguration;
  8. import org.bukkit.entity.Player;
  9.  
  10. /**
  11. *
  12. * @Author Jake
  13. */
  14. public class ArenaManager {
  15.  
  16. private static ArenaManager am = new ArenaManager();
  17.  
  18. //Usefull for getting the ArenaManager, like so: ArenaManager.getManager()
  19. public static ArenaManager getManager() {
  20. return am;
  21. }
  22.  
  23.  
  24. //A method for getting one of the Arenas out of the list by name:
  25. public Arena getArena(String name) {
  26. for (Arena a: Arena.arenaObjects) { //For all of the arenas in the list of objects
  27. if (a.getName().equals(name)) { //If the name of an arena object in the list is equal to the one in the parameter...
  28. return a; //Return that object
  29. }
  30. }
  31. return null; //No objects were found, return null
  32. }
  33.  
  34.  
  35. //A method for adding players
  36. public void addPlayers(Player player, String arenaName) {
  37.  
  38. if (getArena(arenaName) != null) { //If the arena exsists
  39.  
  40. Arena arena = getArena(arenaName); //Create an arena for using in this method
  41.  
  42. if (!arena.isFull()) { //If the arena is not full
  43.  
  44. if (!arena.isInGame()) {
  45.  
  46. //Every check is complete, arena is joinable
  47. player.getInventory().clear(); //Clear the players inventory
  48. player.setHealth(player.getMaxHealth()); //Heal the player
  49. player.setFireTicks(0); //Heal the player even more ^ ^ ^
  50.  
  51. //Teleport to the arena's join location
  52. player.teleport(arena.getJoinLocation());
  53.  
  54. //Add the player to the arena list
  55. arena.getPlayers().add(player.getName()); //Add the players name to the arena
  56.  
  57. int playersLeft = arena.getMaxPlayers() - arena.getPlayers().size(); //How many players needed to start
  58. //Send the arena's players a message
  59. arena.sendMessage(ChatColor.BLUE + player.getName() + " has joined the arena! We only need " + playersLeft + " to start the game!");
  60.  
  61.  
  62. if (playersLeft == 0) { //IF there are 0 players needed to start the game
  63. startArena(arenaName); //Start the arena, see the method way below :)
  64. }
  65.  
  66.  
  67. } else { //Specifiend arena is in game, send the player an error message
  68. player.sendMessage(ChatColor.RED + "The arena you are looking for is currently full!");
  69.  
  70. }
  71. } else { //Specified arena is full, send the player an error message
  72. player.sendMessage(ChatColor.RED + "The arena you are looking for is currently full!");
  73. }
  74.  
  75. } else { //The arena doesn't exsist, send the player an error message
  76. player.sendMessage(ChatColor.RED + "The arena you are looking for could not be found!");
  77. }
  78.  
  79. }
  80.  
  81.  
  82. //A method for removing players
  83. public void removePlayer(Player player, String arenaName) {
  84.  
  85. if (getArena(arenaName) != null) { //If the arena exsists
  86.  
  87. Arena arena = getArena(arenaName); //Create an arena for using in this method
  88.  
  89. if (arena.getPlayers().contains(player.getName())) { //If the arena has the player already
  90.  
  91. //Every check is complete, arena is leavable
  92. player.getInventory().clear(); //Clear the players inventory
  93. player.setHealth(player.getMaxHealth()); //Heal the player
  94. player.setFireTicks(0); //Heal the player even more ^ ^ ^
  95.  
  96. //Teleport to the arena's join location
  97. player.teleport(arena.getEndLocation());
  98.  
  99. //remove the player to the arena list
  100. arena.getPlayers().remove(player.getName()); //Removes the players name to the arena
  101.  
  102. //Send the arena's players a message
  103. arena.sendMessage(ChatColor.BLUE + player.getName() + " has left the Arena! There are " + arena.getPlayers().size() + "players currently left!");
  104.  
  105.  
  106.  
  107.  
  108. } else { //Specified arena doesn't have the player, send the player an error message
  109. player.sendMessage(ChatColor.RED + "Your not in the arena your looking for!");
  110.  
  111. }
  112.  
  113.  
  114. } else { //The arena doesn't exsist, send the player an error message
  115. player.sendMessage(ChatColor.RED + "The arena you are looking for could not be found!");
  116. }
  117. }
  118.  
  119.  
  120. //A method for starting an Arena:
  121. public void startArena(String arenaName) {
  122.  
  123. if (getArena(arenaName) != null) { //If the arena exsists
  124.  
  125. Arena arena = getArena(arenaName); //Create an arena for using in this method
  126.  
  127. arena.sendMessage(ChatColor.GOLD + "The arena has BEGUN!");
  128.  
  129. //Set ingame
  130. arena.setInGame(true);
  131.  
  132. for (String s: arena.getPlayers()) {//Loop through every player in the arena
  133.  
  134. Bukkit.getPlayer(s).teleport(arena.getStartLocation()); //Teleports the player to the arena start location
  135.  
  136. //Do custom stuff here, like give weapons etc, but for the purpose of this tutorial, i'll do nothing
  137.  
  138. //Set inGa
  139.  
  140.  
  141. }
  142.  
  143.  
  144. }
  145.  
  146. }
  147.  
  148.  
  149. //A method for ending an Arena:
  150. public void endArena(String arenaName) {
  151.  
  152. if (getArena(arenaName) != null) { //If the arena exsists
  153.  
  154. Arena arena = getArena(arenaName); //Create an arena for using in this method
  155.  
  156. //Send them a message
  157. arena.sendMessage(ChatColor.GOLD + "The arena has ended :(");
  158.  
  159. //Set ingame
  160. arena.setInGame(false);
  161.  
  162. for (String s: arena.getPlayers()) {//Loop through every player in the arena
  163.  
  164. //Teleport them:
  165.  
  166. Player player = Bukkit.getPlayer(s); //Create a player by the name
  167. player.teleport(arena.getEndLocation());
  168.  
  169. player.getInventory().clear(); //Clear the players inventory
  170. player.setHealth(player.getMaxHealth()); //Heal the player
  171. player.setFireTicks(0); //Heal the player even more ^ ^ ^
  172.  
  173. //Remove them all from the list
  174. arena.getPlayers().remove(player.getName());
  175.  
  176. }
  177.  
  178.  
  179. }
  180. }
  181.  
  182.  
  183. //And our final method, loading each arena
  184. //This will be resonsible for creating each arena from the config, and creating an object to represent it
  185. //Call this method in your main class, onEnable
  186.  
  187.  
  188. public void loadArenas() {
  189.  
  190. //I just create a quick Config Variable, obviously don't do this.
  191. //Use your own config file
  192. FileConfiguration fc = null; //If you just use this code, it will erorr, its null. Read the notes above, USE YOUR OWN CONFIGURATION FILE
  193.  
  194. //Youll get an error here, FOR THE LOVE OF GAWD READ THE NOTES ABOVE!!!
  195. for (String keys: fc.getConfigurationSection("arenas").getKeys(false)) { //For each arena name in the arena file
  196.  
  197. //Now lets get all of the values, and make an Arena object for each:
  198. //Just to help me remember: Arena myArena = new Arena("My Arena", joinLocation, startLocation, endLocation, 17)
  199.  
  200. World world = Bukkit.getWorld("arenas." + keys + ".world");
  201.  
  202. //Arena's name is keys
  203.  
  204. double joinX = fc.getDouble("arenas." + "keys." + "joinX");
  205. double joinY = fc.getDouble("arenas." + "keys." + "joinY");
  206. double joinZ = fc.getDouble("arenas." + "keys." + "joinZ");
  207. Location joinLocation = new Location(world, joinX, joinY, joinZ);
  208.  
  209. double startX = fc.getDouble("arenas." + "keys." + "startX");
  210. double startY = fc.getDouble("arenas." + "keys." + "startY");
  211. double startZ = fc.getDouble("arenas." + "keys." + "startZ");
  212.  
  213. Location startLocation = new Location(world, startX, startY, startZ);
  214.  
  215. double endX = fc.getDouble("arenas." + "keys." + "endX");
  216. double endY = fc.getDouble("arenas." + "keys." + "endX");
  217. double endZ = fc.getDouble("arenas." + "keys." + "endX");
  218.  
  219. Location endLocation = new Location(world, endX, endY, endZ);
  220.  
  221. int maxPlayers = fc.getInt("arenas." + keys + ".maxPlayers");
  222.  
  223. //Now lets create an object to represent it:
  224. Arena arena = new Arena(keys, joinLocation, startLocation, endLocation, 17);
  225.  
  226. }
  227.  
  228.  
  229. }
  230.  
  231. //Our final method, create arena!
  232. public void createArena(String arenaName, Location joinLocation, Location startLocation, Location endLocation, int maxPlayers) {
  233.  
  234. //Now, lets create an arena object to represent it:
  235. Arena arena = new Arena(arenaName, joinLocation, startLocation, endLocation, maxPlayers);
  236.  
  237. //Now here is where you would save it all to a file, again, im going to create a null FileConfiguration, USE YOUR OWN!!!
  238. FileConfiguration fc = null; //USE YOUR OWN PUNK
  239.  
  240. fc.set("arenas." + arenaName, null); //Set its name
  241. //Now sets the other values
  242.  
  243. String path = "arenas." + arenaName + "."; //Shortcut
  244. //Sets the paths
  245. fc.set(path + "joinX", joinLocation.getX());
  246. fc.set(path + "joinY", joinLocation.getY());
  247. fc.set(path + "joinZ", joinLocation.getZ());
  248.  
  249. fc.set(path + "startX", startLocation.getX());
  250. fc.set(path + "startY", startLocation.getY());
  251. fc.set(path + "startZ", startLocation.getZ());
  252.  
  253. fc.set(path + "endX", endLocation.getX());
  254. fc.set(path + "endY", endLocation.getY());
  255. fc.set(path + "endZ", endLocation.getZ());
  256.  
  257. fc.set(path + "maxPlayers", maxPlayers);
  258.  
  259. //Now save it up down here
  260.  
  261. }
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement