Advertisement
Guest User

ArenaManager

a guest
Jul 1st, 2014
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.84 KB | None | 0 0
  1. package me.junglesociety.tntwars2;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.Random;
  10.  
  11. import org.bukkit.Bukkit;
  12. import org.bukkit.ChatColor;
  13. import org.bukkit.GameMode;
  14. import org.bukkit.Location;
  15. import org.bukkit.World;
  16. import org.bukkit.entity.Player;
  17. import org.bukkit.inventory.ItemStack;
  18. import org.bukkit.plugin.Plugin;
  19. import org.bukkit.scheduler.BukkitScheduler;
  20.  
  21. import com.sk89q.worldedit.EmptyClipboardException;
  22. import com.sk89q.worldedit.FilenameException;
  23. import com.sk89q.worldedit.MaxChangedBlocksException;
  24. import com.sk89q.worldedit.bukkit.WorldEditPlugin;
  25. import com.sk89q.worldedit.data.DataException;
  26.  
  27.  
  28. public class ArenaManager {
  29.  
  30. public static ArenaManager am = new ArenaManager();
  31. TNTWars tnt = new TNTWars();
  32. SettingsManager settings = SettingsManager.getInstance();
  33.  
  34. private Map<String, ItemStack[]> inv = new HashMap<String, ItemStack[]>();
  35. private Map<String, ItemStack[]> armor = new HashMap<String, ItemStack[]>();
  36. private Map<String, Integer> xp = new HashMap<String, Integer>();
  37. private ArrayList<Arena> arenas = new ArrayList<Arena>();
  38. boolean iscounting = false;
  39.  
  40.  
  41. int lobbyx = tnt.getConfig().getInt("lobbyspawn." + "x");
  42. int lobbyy = tnt.getConfig().getInt("lobbyspawn." + "y");
  43. int lobbyz = tnt.getConfig().getInt("lobbyspawn." + "z");
  44. World lobbyworld = Bukkit.getServer().getWorld(tnt.getConfig().getString("lobbyspawn." + "world"));
  45. Location lobbyloc = new Location(lobbyworld,lobbyx,lobbyy,lobbyz);
  46.  
  47. int arenaSize = 0;
  48.  
  49. private ArenaManager(){
  50. }
  51.  
  52. public static ArenaManager getManager(){
  53. return am;
  54. }
  55.  
  56. public Arena getArena(int i){
  57. for(Arena a : arenas){
  58. if(a.getID() == i){
  59. return a;
  60. }
  61. }
  62. return null;
  63. }
  64.  
  65.  
  66. /*
  67. * ADD PLAYERS
  68. *
  69. */
  70.  
  71. public void addPlayer(Player player, int i){
  72. final Arena a = getArena(i);
  73.  
  74. if(a == null){
  75. return;
  76. }
  77.  
  78. if(a.getBluePlayers().size() == 0 && a.getRedPlayers().size() == 0){
  79. a.setInGame(false);
  80. }
  81.  
  82. if(a.isInGame() == true){
  83. player.sendMessage(ChatColor.RED + "Sorry that arena is full!");
  84. }else{
  85. if(a.getPlayers().contains(player.getName())){
  86. player.sendMessage(ChatColor.RED + "You are already in game!");
  87. }else{
  88.  
  89. if(iscounting = false){
  90.  
  91. //Add them
  92. a.getPlayers().add(player.getName());
  93.  
  94. //If there is enough to start the game up
  95. if(a.getPlayers().size() >= tnt.getConfig().getInt("minplayers") && a.getPlayers().size() <= tnt.getConfig().getInt("maxplayers")){
  96.  
  97. //Tell all the players of the time
  98. for(String allplayers : a.getPlayers()){
  99. @SuppressWarnings("deprecation")
  100. Player p = Bukkit.getPlayer(allplayers);
  101. p.sendMessage(ChatColor.YELLOW + "TNT Wars will start in " + tnt.getConfig().getInt("countdownbeforegame") + " seconds!");
  102. }
  103.  
  104. //What to do after that countdown is over
  105. BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
  106. scheduler.scheduleSyncDelayedTask((Plugin) this, new Runnable() {
  107. @SuppressWarnings("deprecation")
  108. @Override
  109. public void run() {
  110.  
  111. //put them on teams
  112. for(String list : a.getPlayers()){
  113. Player player = Bukkit.getPlayer(list);
  114. if (a.getRedPlayers().size() > a.getBluePlayers().size()){
  115. a.getBluePlayers().add(player.getName());
  116. }else if (a.getRedPlayers().size() < a.getBluePlayers().size()){
  117. a.getRedPlayers().add(player.getName());
  118. }else {
  119. Integer team = new Random().nextInt(2);
  120. if (team == 1){
  121. a.getRedPlayers().add(player.getName());
  122. }else {
  123. a.getBluePlayers().add(player.getName());
  124. }
  125. }
  126.  
  127.  
  128. //teleport to blue spawn
  129. for(String blueteam : a.getBluePlayers()){
  130. Player bluePlayers = Bukkit.getServer().getPlayer(blueteam);
  131. bluePlayers.teleport(a.getBlueSpawn());
  132. bluePlayers.sendMessage(ChatColor.BLUE + "You are on the Blue team!");
  133. }
  134.  
  135.  
  136. //teleport to red spawn
  137. for(String redteam : a.getRedPlayers()){
  138. Player redPlayers = Bukkit.getPlayer(redteam);
  139. redPlayers.teleport(a.getRedSpawn());
  140. redPlayers.sendMessage(ChatColor.RED + "You are on the Red team!");
  141. }
  142.  
  143.  
  144. for(String allplayers : a.getPlayers()){
  145. Player all = Bukkit.getPlayer(allplayers);
  146. saveInv(all);
  147. all.getInventory().clear();
  148. if(tnt.getConfig().getBoolean("items_gui_enabled") == true){
  149. ItemStack item = new ItemStack(tnt.getConfig().getInt("item_for_gui_opener"), 1);
  150. all.getInventory().addItem(item);
  151. }
  152.  
  153. all.setLevel(0);
  154.  
  155. all.setGameMode(GameMode.SURVIVAL);
  156. all.setHealth(20);
  157. all.setFoodLevel(20);
  158. }
  159. }
  160.  
  161.  
  162. //make it so no one can join
  163. a.setInGame(true);
  164. }
  165. }, tnt.getConfig().getInt("countdownbeforegame") * 20);
  166.  
  167. //If there isnt enough to start
  168. }else{
  169. int needed = tnt.getConfig().getInt("minplayers") - a.getPlayers().size();
  170.  
  171. for(String all: a.getPlayers()){
  172. @SuppressWarnings("deprecation")
  173. Player everyone = Bukkit.getPlayer(all);
  174. everyone.sendMessage(ChatColor.YELLOW + player.getName() + " has joined the game! " + needed + " more players until the game starts!");
  175. }
  176. }
  177. }else{
  178. a.getPlayers().add(player.getName());
  179. }
  180. }
  181. }
  182. }
  183.  
  184.  
  185. /*
  186. * REMOVE PLAYERS
  187. *
  188. */
  189.  
  190. @SuppressWarnings("deprecation")
  191. public void removePlayer(Player player, int i){
  192. Arena a = getArena(i);
  193.  
  194. if(a == null){
  195. player.sendMessage(ChatColor.RED + "Invalid arena!");
  196. }else{
  197.  
  198. World world = player.getWorld();
  199. //check if they are already in game
  200. if(a.getPlayers().contains(player.getName())){
  201. a.getPlayers().remove(player.getName());
  202. //check if they are on blue team
  203. if(a.getBluePlayers().contains(player.getName())){
  204. a.getBluePlayers().remove(player.getName());
  205. player.getInventory().clear();
  206. restoreInv(player);
  207. }
  208. //check if they are on red team
  209. if(a.getRedPlayers().contains(player.getName())){
  210. a.getRedPlayers().remove(player.getName());
  211. player.getInventory().clear();
  212. restoreInv(player);
  213. }
  214.  
  215.  
  216. //teleport to lobby spawn
  217. player.teleport(lobbyloc);
  218. player.sendMessage(ChatColor.YELLOW + "You left TnT Wars!");
  219. for(String id : a.getPlayers()){
  220. Player p = Bukkit.getServer().getPlayer(id);
  221. p.sendMessage(ChatColor.LIGHT_PURPLE + player.getName() + " has left the game!");
  222. }
  223.  
  224.  
  225. //Check for the last team remaining
  226. if((a.getRedPlayers().size() == 0 && a.getBluePlayers().size() >= 1) || (a.getBluePlayers().size() == 0 && a.getRedPlayers().size() >= 1)){
  227. loadRegion(world, i);
  228. for(String spectator : a.getSpectators()){
  229. Player p = Bukkit.getPlayer(spectator);
  230. unSpectate(player, i);
  231. }
  232.  
  233. for(String last : a.getPlayers()){
  234. Player lastplayer = Bukkit.getPlayer(last);
  235. lastplayer.sendMessage(ChatColor.YELLOW + "Congratulations! Your team won!");
  236. lastplayer.teleport(lobbyloc);
  237. lastplayer.getInventory().clear();
  238. restoreInv(lastplayer);
  239. //remove them
  240. if(a.getRedPlayers().contains(last)){
  241. a.getRedPlayers().remove(last);
  242. }
  243. if(a.getBluePlayers().contains(last)){
  244. a.getBluePlayers().remove(last);
  245. }
  246. a.getPlayers().remove(last);
  247. }
  248. }
  249. //if they aren't already in tntwars
  250. }else{
  251. player.sendMessage(ChatColor.RED + "You are not in TnT Wars!");
  252. }
  253. }
  254. }
  255.  
  256.  
  257. /*
  258. *
  259. * CREATE ARENA
  260. *
  261. */
  262.  
  263. public Arena createArena(Player player){
  264. if(settings.getData().getIntegerList("ArenaNumbers").isEmpty() || settings.getData().getIntegerList("ArenaNumbers") == null){
  265. int num = 1;
  266. settings.getData().getIntegerList("ArenaNumbers").add(num);
  267.  
  268. settings.getData().createSection("Arenas." + num + ".bluespawn.x");
  269. settings.getData().createSection("Arenas." + num + ".bluespawn.y");
  270. settings.getData().createSection("Arenas." + num + ".bluespawn.z");
  271. settings.getData().createSection("Arenas." + num + ".bluespawn.world");
  272.  
  273. settings.getData().createSection("Arenas." + num + ".redspawn.x");
  274. settings.getData().createSection("Arenas." + num + ".redspawn.y");
  275. settings.getData().createSection("Arenas." + num + ".redspawn.z");
  276. settings.getData().createSection("Arenas." + num + ".redspawn.world");
  277.  
  278. settings.getData().createSection("Arenas." + num + ".specspawn.x");
  279. settings.getData().createSection("Arenas." + num + ".specspawn.y");
  280. settings.getData().createSection("Arenas." + num + ".specspawn.z");
  281. settings.getData().createSection("Arenas." + num + ".specspawn.world");
  282.  
  283. settings.getData().createSection("Arenas." + num + ".minloc.x");
  284. settings.getData().createSection("Arenas." + num + ".minloc.y");
  285. settings.getData().createSection("Arenas." + num + ".minloc.z");
  286. settings.getData().createSection("Arenas." + num + ".minloc.world");
  287.  
  288. settings.getData().createSection("Arenas." + num + ".maxloc.x");
  289. settings.getData().createSection("Arenas." + num + ".maxloc.y");
  290. settings.getData().createSection("Arenas." + num + ".maxloc.z");
  291. settings.getData().createSection("Arenas." + num + ".maxloc.world");
  292.  
  293. settings.saveData();
  294.  
  295. Arena a = new Arena(num);
  296. arenas.add(a);
  297.  
  298. return a;
  299.  
  300. }else{
  301. List<Integer> ints = settings.getData().getIntegerList("ArenaNumbers");
  302.  
  303. int num = 0;
  304. int n = 0;
  305. for(int i : ints){
  306. n++;
  307. if(i != n){
  308. num = n;
  309. break;
  310. }
  311. }
  312.  
  313. settings.getData().getIntegerList("ArenaNumbers").add(num);
  314.  
  315. settings.getData().createSection("Arenas." + num + ".bluespawn.x");
  316. settings.getData().createSection("Arenas." + num + ".bluespawn.y");
  317. settings.getData().createSection("Arenas." + num + ".bluespawn.z");
  318. settings.getData().createSection("Arenas." + num + ".bluespawn.world");
  319.  
  320. settings.getData().createSection("Arenas." + num + ".redspawn.x");
  321. settings.getData().createSection("Arenas." + num + ".redspawn.y");
  322. settings.getData().createSection("Arenas." + num + ".redspawn.z");
  323. settings.getData().createSection("Arenas." + num + ".redspawn.world");
  324.  
  325. settings.getData().createSection("Arenas." + num + ".specspawn.x");
  326. settings.getData().createSection("Arenas." + num + ".specspawn.y");
  327. settings.getData().createSection("Arenas." + num + ".specspawn.z");
  328. settings.getData().createSection("Arenas." + num + ".specspawn.world");
  329.  
  330. settings.getData().createSection("Arenas." + num + ".minloc.x");
  331. settings.getData().createSection("Arenas." + num + ".minloc.y");
  332. settings.getData().createSection("Arenas." + num + ".minloc.z");
  333. settings.getData().createSection("Arenas." + num + ".minloc.world");
  334.  
  335. settings.getData().createSection("Arenas." + num + ".maxloc.x");
  336. settings.getData().createSection("Arenas." + num + ".maxloc.y");
  337. settings.getData().createSection("Arenas." + num + ".maxloc.z");
  338. settings.getData().createSection("Arenas." + num + ".maxloc.world");
  339.  
  340. settings.saveData();
  341.  
  342. Arena a = new Arena(num);
  343. arenas.add(a);
  344.  
  345. return a;
  346.  
  347. }
  348. }
  349.  
  350.  
  351. /*
  352. *
  353. * DELETE ARENA
  354. *
  355. */
  356.  
  357. public void deleteArena(int i, Player player){
  358.  
  359. if(getArena(i) == null){
  360. player.sendMessage(ChatColor.RED + "Invalid arena!");
  361. return;
  362. }
  363.  
  364. settings.getData().getIntegerList("ArenaNumbers").remove(i);
  365. settings.getData().getConfigurationSection("Arenas").set(String.valueOf(i), null);
  366. settings.saveData();
  367. }
  368.  
  369. /*
  370. *
  371. * SAVE LOCATIONS
  372. *
  373. */
  374.  
  375. public void setBlueSpawn(Player player, int i){
  376. Arena a = getArena(i);
  377.  
  378. if(a == null){
  379. player.sendMessage(ChatColor.RED + "Invalid Arena!");
  380. return;
  381. }else{
  382. double x = player.getLocation().getX();
  383. double y = player.getLocation().getY();
  384. double z = player.getLocation().getZ();
  385. String world = player.getWorld().toString();
  386.  
  387. settings.getData().set("Arenas." + i + "bluespawn.x", x);
  388. settings.getData().set("Arenas." + i + "bluespawn.y", y);
  389. settings.getData().set("Arenas." + i + "bluespawn.z", z);
  390. settings.getData().set("Arenas." + i + "bluespawn.world", world);
  391. }
  392. }
  393.  
  394.  
  395. public void setRedSpawn(Player player, int i){
  396. Arena a = getArena(i);
  397.  
  398. if(a == null){
  399. player.sendMessage(ChatColor.RED + "Invalid Arena!");
  400. return;
  401. }else{
  402. double x = player.getLocation().getX();
  403. double y = player.getLocation().getY();
  404. double z = player.getLocation().getZ();
  405. String world = player.getWorld().toString();
  406.  
  407. settings.getData().set("Arenas." + i + "redspawn.x", x);
  408. settings.getData().set("Arenas." + i + "redspawn.y", y);
  409. settings.getData().set("Arenas." + i + "redspawn.z", z);
  410. settings.getData().set("Arenas." + i + "redspawn.world", world);
  411. }
  412. }
  413.  
  414.  
  415. public void setSpectatorSpawn(Player player, int i){
  416. Arena a = getArena(i);
  417.  
  418. if(a == null){
  419. player.sendMessage(ChatColor.RED + "Invalid Arena!");
  420. return;
  421. }else{
  422. double x = player.getLocation().getX();
  423. double y = player.getLocation().getY();
  424. double z = player.getLocation().getZ();
  425. String world = player.getWorld().toString();
  426.  
  427. settings.getData().set("Arenas." + i + "specspawn.x", x);
  428. settings.getData().set("Arenas." + i + "specspawn.y", y);
  429. settings.getData().set("Arenas." + i + "specspawn.z", z);
  430. settings.getData().set("Arenas." + i + "specspawn.world", world);
  431. }
  432. }
  433.  
  434.  
  435. public void setMinLoc(Player player, int i){
  436. Arena a = getArena(i);
  437.  
  438. if(a == null){
  439. player.sendMessage(ChatColor.RED + "Invalid Arena!");
  440. return;
  441. }else{
  442. double x = player.getLocation().getX();
  443. double y = player.getLocation().getY();
  444. double z = player.getLocation().getZ();
  445. String world = player.getWorld().toString();
  446.  
  447. settings.getData().set("Arenas." + i + "minloc.x", x);
  448. settings.getData().set("Arenas." + i + "minloc.y", y);
  449. settings.getData().set("Arenas." + i + "minloc.z", z);
  450. settings.getData().set("Arenas." + i + "minloc.world", world);
  451. }
  452. }
  453.  
  454.  
  455. public void setMaxLoc(Player player, int i){
  456. Arena a = getArena(i);
  457.  
  458. if(a == null){
  459. player.sendMessage(ChatColor.RED + "Invalid Arena!");
  460. return;
  461. }else{
  462. double x = player.getLocation().getX();
  463. double y = player.getLocation().getY();
  464. double z = player.getLocation().getZ();
  465. String world = player.getWorld().toString();
  466.  
  467. settings.getData().set("Arenas." + i + "maxloc.x", x);
  468. settings.getData().set("Arenas." + i + "maxloc.y", y);
  469. settings.getData().set("Arenas." + i + "maxloc.z", z);
  470. settings.getData().set("Arenas." + i + "maxloc.world", world);
  471. }
  472. }
  473.  
  474.  
  475. public void setLobbySpawn(Player player){
  476. double x = player.getLocation().getX();
  477. double y = player.getLocation().getY();
  478. double z = player.getLocation().getZ();
  479. String world = player.getWorld().toString();
  480.  
  481. tnt.getConfig().set("lobbyspawn.x", x);
  482. tnt.getConfig().set("lobbyspawn.y", y);
  483. tnt.getConfig().set("lobbyspawn.z", z);
  484. tnt.getConfig().set("lobbyspawn.world", world);
  485.  
  486. }
  487.  
  488.  
  489. public void saveInv(Player p){
  490. inv.put(p.getName(), p.getInventory().getContents());
  491. armor.put(p.getName(), p.getInventory().getArmorContents());
  492. xp.put(p.getName(), p.getExpToLevel());
  493. }
  494.  
  495. //restore player inventory
  496. public void restoreInv(Player p){
  497. p.getInventory().setContents(inv.get(p.getName()));
  498. p.getInventory().setArmorContents(armor.get(p.getName()));
  499. p.setLevel(0);
  500. p.setLevel(xp.get(p.getName()));
  501. inv.remove(p.getName());
  502. armor.remove(p.getName());
  503. xp.remove(p.getName());
  504. }
  505.  
  506.  
  507. public void saveRegion(Location l1, Location l2, Player player, int i){
  508.  
  509. Arena a = getArena(i);
  510. if(a == null){
  511. player.sendMessage(ChatColor.RED + "Invalid Arena!");
  512. return;
  513. }
  514.  
  515. // ensure WorldEdit is available
  516. WorldEditPlugin wep = (WorldEditPlugin) Bukkit.getPluginManager().getPlugin("WorldEdit");
  517. if (wep == null) {
  518. // then don't try to use TerrainManager!
  519. }
  520.  
  521. Plugin plugin = (Plugin) this;
  522. // OR - without needing an associated Player
  523. TerrainManager tm = new TerrainManager(wep, player.getWorld());
  524.  
  525. // don't include an extension - TerrainManager will auto-add ".schematic"
  526. File saveFile = new File(plugin.getDataFolder(), "Arena" + i);
  527.  
  528. // save the terrain to a schematic file
  529. try {
  530. tm.saveTerrain(saveFile, l1, l2);
  531. } catch (FilenameException e) {
  532. // thrown by WorldEdit - it doesn't like the file name/location etc.
  533. } catch (DataException e) {
  534. // thrown by WorldEdit - problem with the data
  535. } catch (IOException e) {
  536. // problem with creating/writing to the file
  537. }
  538. }
  539.  
  540. public void loadRegion(World world, int i){
  541. Arena a = getArena(i);
  542. if(a == null){
  543. return;
  544. }
  545.  
  546. Plugin p = (Plugin) this;
  547. WorldEditPlugin wep = (WorldEditPlugin) Bukkit.getPluginManager().getPlugin("WorldEdit");
  548. if (wep == null) {
  549. // then don't try to use TerrainManager!
  550. }
  551.  
  552. TerrainManager tm = new TerrainManager(wep, world);
  553. File saveFile = new File(p.getDataFolder(), "Arena" + i);
  554.  
  555. try{
  556. tm.loadSchematic(saveFile);
  557. } catch (FilenameException e) {
  558. // thrown by WorldEdit - it doesn't like the file name/location etc.
  559. } catch (DataException e) {
  560. // thrown by WorldEdit - problem with the data
  561. } catch (IOException e) {
  562. // problem with opening/reading the file
  563. } catch (MaxChangedBlocksException e) {
  564. // thrown by WorldEdit - the schematic is larger than the configured block limit for the player
  565. } catch (EmptyClipboardException e) {
  566. // thrown by WorldEdit - should be self-explanatory
  567. }
  568. }
  569.  
  570. @SuppressWarnings("deprecation")
  571. public void spectate(Player p, int i){
  572. Arena a = getArena(i);
  573.  
  574. if(a == null){
  575. p.sendMessage(ChatColor.RED + "Invalid Arena!");
  576. return;
  577. }
  578.  
  579. p.teleport(a.getSpectateSpawn());
  580. a.getSpectators().add(p.getName());
  581. p.setAllowFlight(true);
  582. p.setFlying(true);
  583. p.setHealth(20);
  584. p.setFoodLevel(20);
  585. p.sendMessage(ChatColor.GREEN + "You are now spectating TNT Wars!");
  586.  
  587. for(String play : a.getPlayers()){
  588. Player player = Bukkit.getPlayer(play);
  589. player.hidePlayer(p);
  590. }
  591. }
  592.  
  593. @SuppressWarnings("deprecation")
  594. public void unSpectate(Player p, int i){
  595. Arena a = getArena(i);
  596. if(a == null){
  597. p.sendMessage(ChatColor.RED + "Could not leave spectate mode!");
  598. return;
  599. }
  600.  
  601. p.teleport(lobbyloc);
  602. p.setGameMode(GameMode.SURVIVAL);
  603. p.setAllowFlight(false);
  604. p.setFlying(false);
  605. p.setHealth(20);
  606. p.sendMessage(ChatColor.GREEN + "You are no longer spectating TNT Wars!");
  607. a.getSpectators().remove(p.getUniqueId());
  608.  
  609. for(Player player : Bukkit.getServer().getOnlinePlayers()){
  610. player.showPlayer(p);
  611. }
  612. }
  613.  
  614.  
  615. //Get arena list
  616. public ArrayList<Arena> getArenaList(){
  617. return arenas;
  618. }
  619.  
  620. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement