PageRenderTime 222ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/war/src/main/java/bukkit/tommytony/war/War.java

https://github.com/mongizzle/war
Java | 1140 lines | 990 code | 99 blank | 51 comment | 408 complexity | ef333882fbd20f470f16f05915bef5dd MD5 | raw file
  1. package bukkit.tommytony.war;
  2. import org.bukkit.*;
  3. import org.bukkit.block.Block;
  4. import org.bukkit.block.BlockFace;
  5. import org.bukkit.command.Command;
  6. import org.bukkit.entity.Player;
  7. import org.bukkit.event.Event;
  8. import org.bukkit.event.Event.Priority;
  9. import org.bukkit.inventory.ItemStack;
  10. import org.bukkit.plugin.PluginDescriptionFile;
  11. import org.bukkit.plugin.PluginLoader;
  12. import org.bukkit.plugin.PluginManager;
  13. import org.bukkit.plugin.java.JavaPlugin;
  14. import com.tommytony.war.Monument;
  15. import com.tommytony.war.Team;
  16. import com.tommytony.war.TeamMaterials;
  17. import com.tommytony.war.WarHub;
  18. import com.tommytony.war.Warzone;
  19. import com.tommytony.war.ZoneLobby;
  20. import com.tommytony.war.mappers.VolumeMapper;
  21. import com.tommytony.war.mappers.WarMapper;
  22. import com.tommytony.war.mappers.WarzoneMapper;
  23. import java.io.File;
  24. import java.util.ArrayList;
  25. import java.util.HashMap;
  26. import java.util.List;
  27. import java.util.Map;
  28. import java.util.logging.Level;
  29. import java.util.logging.Logger;
  30. /**
  31. *
  32. * @author tommytony
  33. *
  34. */
  35. public class War extends JavaPlugin {
  36. public War(PluginLoader pluginLoader, Server instance,
  37. PluginDescriptionFile desc, File folder, File plugin, ClassLoader cLoader) {
  38. super(pluginLoader, instance, desc, folder, plugin, cLoader);
  39. // TODO: switch to bukkit config file
  40. }
  41. private WarPlayerListener playerListener = new WarPlayerListener(this);
  42. private WarEntityListener entityListener = new WarEntityListener(this);
  43. private WarBlockListener blockListener = new WarBlockListener(this);
  44. private Logger log;
  45. String name = "War";
  46. String version = "0.3.001";
  47. String versionCodeName = "Patton";
  48. private final List<Warzone> warzones = new ArrayList<Warzone>();
  49. private final List<String> zoneMakerNames = new ArrayList<String>();
  50. private final List<String> zoneMakersImpersonatingPlayers = new ArrayList<String>();
  51. private final HashMap<Integer, ItemStack> defaultLoadout = new HashMap<Integer, ItemStack>();
  52. private int defaultLifepool = 42;
  53. private boolean defaultFriendlyFire = false;
  54. private boolean defaultDrawZoneOutline = true;
  55. private boolean defaultAutoAssignOnly = false;
  56. private int defaultTeamCap = 7;
  57. private int defaultScoreCap = 10;
  58. private boolean pvpInZonesOnly = false;
  59. private WarHub warHub;
  60. public void onDisable() {
  61. for(Warzone warzone : warzones) {
  62. if(warzone.getLobby() != null) {
  63. warzone.getLobby().getVolume().resetBlocks();
  64. }
  65. warzone.getVolume().resetBlocks();
  66. }
  67. if(warHub != null) {
  68. warHub.getVolume().resetBlocks();
  69. }
  70. this.info("All warzone blocks reset. War v" + version + " (" + versionCodeName + ") is off.");
  71. }
  72. public void onEnable() {
  73. this.log = Logger.getLogger("Minecraft");
  74. // Register hooks
  75. PluginManager pm = getServer().getPluginManager();
  76. pm.registerEvent(Event.Type.PLAYER_LOGIN, playerListener, Priority.Normal, this);
  77. pm.registerEvent(Event.Type.PLAYER_QUIT, playerListener, Priority.Normal, this);
  78. pm.registerEvent(Event.Type.PLAYER_MOVE, playerListener, Priority.High, this);
  79. pm.registerEvent(Event.Type.ENTITY_DAMAGEDBY_ENTITY, entityListener, Priority.Normal, this);
  80. pm.registerEvent(Event.Type.BLOCK_PLACED, blockListener, Priority.Normal, this);
  81. pm.registerEvent(Event.Type.BLOCK_DAMAGED, blockListener, Priority.Normal, this);
  82. // Load files from disk or create them (using these defaults)
  83. this.defaultLoadout.put(0, new ItemStack(Material.STONE_SWORD, 1, (byte) 8));
  84. this.defaultLoadout.put(1, new ItemStack(Material.BOW, 1, (byte) 8));
  85. this.defaultLoadout.put(2, new ItemStack(Material.ARROW, 7));
  86. this.defaultLoadout.put(3, new ItemStack(Material.IRON_PICKAXE, 1, (byte) 8));
  87. this.defaultLoadout.put(4, new ItemStack(Material.STONE_SPADE, 1, (byte) 8));
  88. this.defaultLifepool = 7;
  89. this.defaultFriendlyFire = false;
  90. this.defaultAutoAssignOnly = false;
  91. WarMapper.load(this, this.getServer().getWorlds()[0]);
  92. this.info("War v"+ version + " (" + versionCodeName + ") is on.");
  93. }
  94. public boolean onCommand(Player player, Command cmd, String commandLabel, String[] args) {
  95. String command = cmd.getName();
  96. String[] arguments = null;
  97. // Handle both /war <command> and /<war command>. I.e. "/war zone temple" == "/zone temple"
  98. if((command.equals("war") || command.equals("War")) && args.length > 1) {
  99. command = args[1];
  100. arguments = new String[args.length - 2];
  101. for(int i = 2; i <= arguments.length; i++) {
  102. arguments[i-2] = args[i];
  103. }
  104. } else if (command.equals("war") || command.equals("War")) {
  105. player.sendMessage(this.str("War is on. Please pick your battle. " +
  106. "Use /warhub, /zones and /zone."));
  107. } else {
  108. arguments = new String[args.length];
  109. for(int i = 1; i <= arguments.length; i++) {
  110. arguments[i-1] = args[i-1];
  111. }
  112. }
  113. // Player commands: /warzones, /warzone, /teams, /join, /leave
  114. // warzones
  115. if(command.equals("zones") || command.equals("warzones")){
  116. String warzonesMessage = "Warzones: ";
  117. if(this.getWarzones().isEmpty()){
  118. warzonesMessage += "none.";
  119. }
  120. for(Warzone warzone : this.getWarzones()) {
  121. warzonesMessage += warzone.getName() + " ("
  122. + warzone.getTeams().size() + " teams, ";
  123. int playerTotal = 0;
  124. for(Team team : warzone.getTeams()) {
  125. playerTotal += team.getPlayers().size();
  126. }
  127. warzonesMessage += playerTotal + " players) ";
  128. }
  129. player.sendMessage(this.str(warzonesMessage + " Use /zone <zone-name> to " +
  130. "teleport to a warzone. "));
  131. }
  132. // warzone
  133. else if(command.equals("zone") || command.equals("warzone")) {
  134. if(arguments.length < 1) {
  135. player.sendMessage(this.str("Usage: /zone <warzone-name>."));
  136. } else {
  137. boolean warped = false;
  138. for(Warzone warzone : this.getWarzones()) {
  139. if(warzone.getName().equals(arguments[0]) && warzone.getTeleport() != null){
  140. player.teleportTo(warzone.getTeleport());
  141. warped = true;
  142. }
  143. }
  144. if(!warped) {
  145. player.sendMessage("No such warzone.");
  146. }
  147. }
  148. }
  149. // /teams
  150. else if(command.equals("teams")){
  151. if(!this.inAnyWarzone(player.getLocation()) && !this.inAnyWarzoneLobby(player.getLocation())) {
  152. player.sendMessage(this.str("Usage: /teams. " +
  153. "Must be in a warzone or zone lobby (try /war, /zones and /zone)."));
  154. } else {
  155. player.sendMessage(this.str("" + playerListener.getAllTeamsMsg(player)));
  156. }
  157. }
  158. // /join <teamname>
  159. else if(command.equals("join")) {
  160. if(arguments.length < 1 || (!this.inAnyWarzone(player.getLocation()) && !this.inAnyWarzoneLobby(player.getLocation()))
  161. || (arguments.length > 0 && TeamMaterials.teamMaterialFromString(arguments[0]) == null)) {
  162. player.sendMessage(this.str("Usage: /join <diamond/iron/gold/d/i/g>." +
  163. " Teams are warzone specific." +
  164. " You must be inside a warzone or zone lobby to join a team." +
  165. " Use as an alternative to walking through the team gate."));
  166. } else {
  167. // drop from old team if any
  168. Team previousTeam = this.getPlayerTeam(player.getName());
  169. if(previousTeam != null) {
  170. if(!previousTeam.removePlayer(player.getName())){
  171. warn("Could not remove player " + player.getName() + " from team " + previousTeam.getName());
  172. }
  173. previousTeam.resetSign();
  174. }
  175. // join new team
  176. String name = TeamMaterials.teamMaterialToString(TeamMaterials.teamMaterialFromString(arguments[0]));
  177. Warzone warzone = this.warzone(player.getLocation());
  178. ZoneLobby lobby = this.lobby(player.getLocation());
  179. if(warzone == null && lobby != null) {
  180. warzone = lobby.getZone();
  181. } else {
  182. lobby = warzone.getLobby();
  183. }
  184. List<Team> teams = warzone.getTeams();
  185. boolean foundTeam = false;
  186. for(Team team : teams) {
  187. if(team.getName().equals(name)) {
  188. if(!warzone.hasPlayerInventory(player.getName())) {
  189. warzone.keepPlayerInventory(player);
  190. player.sendMessage(this.str("Your inventory is is storage until you /leave."));
  191. }
  192. if(team.getPlayers().size() < warzone.getTeamCap()) {
  193. team.addPlayer(player);
  194. team.resetSign();
  195. warzone.respawnPlayer(team, player);
  196. foundTeam = true;
  197. } else {
  198. player.sendMessage(this.str("Team " + name + " is full."));
  199. foundTeam = true;
  200. }
  201. }
  202. }
  203. if(foundTeam) {
  204. for(Team team : teams){
  205. team.teamcast(this.str("" + player.getName() + " joined " + name));
  206. }
  207. } else {
  208. player.sendMessage(this.str("No such team. Try /teams."));
  209. }
  210. }
  211. }
  212. // /leave
  213. else if(command.equals("leave")) {
  214. if(!this.inAnyWarzone(player.getLocation()) || this.getPlayerTeam(player.getName()) == null) {
  215. player.sendMessage(this.str("Usage: /leave. " +
  216. "Must be in a team already."));
  217. } else {
  218. Team playerTeam = this.getPlayerTeam(player.getName());
  219. playerTeam.removePlayer(player.getName());
  220. playerTeam.resetSign();
  221. Warzone zone = this.warzone(player.getLocation());
  222. player.teleportTo(zone.getTeleport());
  223. player.sendMessage(this.str("Left the zone."));
  224. zone.restorePlayerInventory(player);
  225. player.sendMessage(this.str("Your inventory has (hopefully) been restored."));
  226. }
  227. }
  228. // /team <msg>
  229. else if(command.equals("team")) {
  230. if(!this.inAnyWarzone(player.getLocation())) {
  231. player.sendMessage(this.str("Usage: /team <message>. " +
  232. "Sends a message only to your teammates."));
  233. } else {
  234. Team playerTeam = this.getPlayerTeam(player.getName());
  235. String teamMessage = player.getName();
  236. for(int j = 0 ; j<arguments.length; j++) {
  237. String part = arguments[j];
  238. teamMessage += part + " ";
  239. }
  240. playerTeam.teamcast(this.str(teamMessage));
  241. }
  242. }
  243. // /warhub
  244. else if(command.equals("warhub")) {
  245. if(this.getWarHub() == null) {
  246. player.sendMessage("No warhub on this War server. Try /zones and /zone.");
  247. } else {
  248. Team playerTeam = this.getPlayerTeam(player.getName());
  249. Warzone playerWarzone = getPlayerWarzone(player.getName());
  250. if(playerTeam != null) { // was in zone
  251. playerTeam.removePlayer(player.getName());
  252. }
  253. if(playerWarzone != null) {
  254. playerWarzone.getLobby().resetTeamGateSign(playerTeam);
  255. this.getWarHub().resetZoneSign(playerWarzone); // gotta see i just left
  256. }
  257. player.teleportTo(this.getWarHub().getLocation());
  258. }
  259. } else if(this.isZoneMaker(player.getName())) {
  260. // Mod commands : /nextbattle
  261. // /nextbattle
  262. if(command.equals("nextbattle")) {
  263. if(!this.inAnyWarzone(player.getLocation())) {
  264. player.sendMessage(this.str("Usage: /nextbattle. Resets the zone blocks and all teams' life pools. Must be in warzone."));
  265. } else {
  266. Warzone warzone = this.warzone(player.getLocation());
  267. for(Team team: warzone.getTeams()) {
  268. team.teamcast(this.str("The battle was interrupted. " + playerListener.getAllTeamsMsg(player) + " Resetting warzone " + warzone.getName() + " and life pools..."));
  269. }
  270. int resetBlocks = warzone.getVolume().resetBlocks();
  271. warzone.initializeZone();
  272. player.sendMessage(this.str("Warzone reset. " + resetBlocks + " blocks reset."));
  273. info(resetBlocks + " blocks reset in warzone " + warzone.getName() + ".");
  274. }
  275. }
  276. // Warzone maker commands: /setzone, /savezone, /setteam, /setmonument, /resetzone
  277. // /setzone
  278. else if(command.equals("setzone")) {
  279. if(arguments.length < 2 || arguments.length > 2
  280. || (arguments.length == 2 && (!arguments[1].equals("southeast") && !arguments[1].equals("northwest")
  281. && !arguments[1].equals("se") && !arguments[1].equals("nw")))) {
  282. player.sendMessage(this.str("Usage: /setzone <warzone-name> <'southeast'/'northwest'/'se'/'nw'>. " +
  283. "Set one corner, then the next. Defines the outline of the warzone, which will be reset at the start of every battle. " +
  284. "Saves the zone blocks if the zone if the outline is correct."));
  285. } else {
  286. Warzone warzone = this.findWarzone(arguments[0]);
  287. String message = "";
  288. if(warzone == null) {
  289. // create the warzone
  290. warzone = new Warzone(this, player.getLocation().getWorld(), arguments[0]);
  291. this.addWarzone(warzone);
  292. WarMapper.save(this);
  293. if(arguments[1].equals("northwest") || arguments[1].equals("nw")) {
  294. warzone.setNorthwest(player.getLocation());
  295. player.sendMessage(this.str("Warzone " + warzone.getName() + " added. Northwesternmost point set at x="
  296. + (int)warzone.getNorthwest().getBlockX() + " z=" + (int)warzone.getNorthwest().getBlockZ() + "."));
  297. } else {
  298. warzone.setSoutheast(player.getLocation());
  299. player.sendMessage(this.str("Warzone " + warzone.getName() + " added. Southeasternmost point set at x="
  300. + (int)warzone.getSoutheast().getBlockX() + " z=" + (int)warzone.getSoutheast().getBlockZ() + "."));
  301. }
  302. WarzoneMapper.save(this, warzone, false);
  303. } else {
  304. // change existing warzone
  305. if(arguments[1].equals("northwest") || arguments[1].equals("nw")) {
  306. if(warzone.getSoutheast() != null
  307. && (player.getLocation().getBlockX() >= warzone.getSoutheast().getBlockX()
  308. || player.getLocation().getBlockZ() <= warzone.getSoutheast().getBlockZ())) {
  309. player.sendMessage(this.str("You must place that corner northwest relative to the existing southeast corner!"));
  310. } else {
  311. int reset = warzone.getVolume().resetBlocks();
  312. warzone.setNorthwest(player.getLocation());
  313. player.sendMessage(this.str("Saving warzone " + warzone.getName() + "."));
  314. warzone.saveState();
  315. warzone.initializeZone();
  316. message += "Northwesternmost point set at x=" + (int)warzone.getNorthwest().getBlockX()
  317. + " z=" + (int)warzone.getNorthwest().getBlockZ() + " on warzone " + warzone.getName() + ". " +
  318. reset + " blocks reset. Zone saved. ";
  319. }
  320. } else {
  321. if(warzone.getNorthwest() != null
  322. && (player.getLocation().getBlockX() <= warzone.getNorthwest().getBlockX()
  323. || player.getLocation().getBlockZ() >= warzone.getNorthwest().getBlockZ())) {
  324. player.sendMessage(this.str("You must place that corner southeast relative to the existing northwest corner! "));
  325. } else {
  326. int reset = warzone.getVolume().resetBlocks();
  327. warzone.setSoutheast(player.getLocation());
  328. player.sendMessage(this.str("Saving warzone " + warzone.getName() + "."));
  329. warzone.saveState();
  330. warzone.initializeZone();
  331. message += "Southeasternmost point set at x=" + (int)warzone.getSoutheast().getBlockX()
  332. + " z=" + (int)warzone.getSoutheast().getBlockZ() + " on warzone " + warzone.getName() + ". " +
  333. reset + " blocks reset. Zone saved. ";
  334. }
  335. }
  336. WarzoneMapper.save(this, warzone, true);
  337. }
  338. if(warzone.getNorthwest() == null) {
  339. message += "Still missing northwesternmost point. ";
  340. }
  341. if(warzone.getSoutheast() == null) {
  342. message += "Still missing southeasternmost point. ";
  343. }
  344. if(warzone.getNorthwest() != null && warzone.getSoutheast() != null) {
  345. if(warzone.ready()) {
  346. message += "Warzone " + warzone.getName() + " outline done. Use /setteam, /setmonument and /savezone to complete the zone.";
  347. } else if (warzone.tooSmall()) {
  348. message += "Warzone " + warzone.getName() + " is too small. Min north-south size: 20. Min east-west size: 20.";
  349. } else if (warzone.tooBig()) {
  350. message += "Warzone " + warzone.getName() + " is too Big. Max north-south size: 500. Max east-west size: 500.";
  351. }
  352. }
  353. player.sendMessage(this.str(message));
  354. }
  355. }
  356. else if(command.equals("setzonelobby")) {
  357. if((!this.inAnyWarzone(player.getLocation())
  358. && !this.inAnyWarzoneLobby(player.getLocation()))
  359. || arguments.length < 1 || arguments.length > 1
  360. || (arguments.length == 1 && !arguments[0].equals("north") && !arguments[0].equals("n")
  361. && !arguments[0].equals("east") && !arguments[0].equals("e")
  362. && !arguments[0].equals("south") && !arguments[0].equals("s")
  363. && !arguments[0].equals("west") && !arguments[0].equals("w"))) {
  364. player.sendMessage(this.str("Usage: /setzonelobby <north/n/east/e/south/s/west/w>. Must be in warzone." +
  365. "Defines on which side the zone lobby lies. " +
  366. "Removes any previously set lobby."));
  367. } else {
  368. Warzone warzone = this.warzone(player.getLocation());
  369. ZoneLobby lobby = this.lobby(player.getLocation());
  370. if(warzone == null && lobby != null) {
  371. warzone = lobby.getZone();
  372. } else {
  373. lobby = warzone.getLobby();
  374. }
  375. BlockFace wall = null;
  376. String wallStr = "";
  377. if(arguments[0].equals("north") || arguments[0].equals("n")) {
  378. wall = BlockFace.NORTH;
  379. wallStr = "north";
  380. } else if(arguments[0].equals("east") || arguments[0].equals("e")) {
  381. wall = BlockFace.EAST;
  382. wallStr = "east";
  383. } else if(arguments[0].equals("south") || arguments[0].equals("s")) {
  384. wall = BlockFace.SOUTH;
  385. wallStr = "south";
  386. } else if(arguments[0].equals("west") || arguments[0].equals("w")) {
  387. wall = BlockFace.WEST;
  388. wallStr = "west";
  389. }
  390. if(lobby != null) {
  391. // reset existing lobby
  392. lobby.getVolume().resetBlocks();
  393. lobby.changeWall(wall);
  394. lobby.initialize();
  395. player.sendMessage(this.str("Warzone lobby moved to " + wallStr + " side of zone."));
  396. } else {
  397. // new lobby
  398. lobby = new ZoneLobby(this, warzone, wall);
  399. warzone.setLobby(lobby);
  400. lobby.initialize();
  401. if(warHub != null) { // warhub has to change
  402. warHub.getVolume().resetBlocks();
  403. warHub.initialize();
  404. }
  405. player.sendMessage(this.str("Warzone lobby created on " + wallStr + "side of zone."));
  406. }
  407. WarzoneMapper.save(this, warzone, false);
  408. }
  409. }
  410. // /savezone
  411. else if(command.equals("savezone")) {
  412. if(!this.inAnyWarzone(player.getLocation()) && !this.inAnyWarzoneLobby(player.getLocation())) {
  413. player.sendMessage(this.str("Usage: /savezone lifepool:8 teamsize:5 maxscore:7 autoassign:on outline:off ff:on " +
  414. "All named params optional. Saves the blocks of the warzone (i.e. the current zone state will be reloaded at each battle start). Must be in warzone."));
  415. } else {
  416. Warzone warzone = this.warzone(player.getLocation());
  417. ZoneLobby lobby = this.lobby(player.getLocation());
  418. if(warzone == null && lobby != null) {
  419. warzone = lobby.getZone();
  420. } else {
  421. lobby = warzone.getLobby();
  422. }
  423. player.sendMessage(this.str("Saving warzone " + warzone.getName() + "."));
  424. int savedBlocks = warzone.saveState();
  425. if(warzone.getLobby() == null) {
  426. // Set default lobby on south side
  427. lobby = new ZoneLobby(this, warzone, BlockFace.SOUTH);
  428. warzone.setLobby(lobby);
  429. lobby.initialize();
  430. if(warHub != null) { // warhub has to change
  431. warHub.getVolume().resetBlocks();
  432. warHub.initialize();
  433. }
  434. player.sendMessage(this.str("Default lobby created on south side of zone."));
  435. }
  436. updateZoneFromNamedParams(warzone, arguments);
  437. WarzoneMapper.save(this, warzone, true);
  438. warzone.getVolume().resetBlocks();
  439. if(lobby != null) {
  440. lobby.getVolume().resetBlocks();
  441. }
  442. warzone.initializeZone(); // bring back team spawns etc
  443. player.sendMessage(this.str("Warzone " + warzone.getName() + " initial state changed. Saved " + savedBlocks + " blocks."));
  444. }
  445. }
  446. // /setzoneconfig
  447. else if(command.equals("setzoneconfig")) {
  448. if((!this.inAnyWarzone(player.getLocation()) && !this.inAnyWarzoneLobby(player.getLocation()))
  449. || arguments.length == 0) {
  450. player.sendMessage(this.str("Usage: /setzoneconfig lifepool:8 teamsize:5 maxscore:7 autoassign:on outline:off ff:on " +
  451. "Please give at leaset one named parameter. Does not save the blocks of the warzone. Resets the zone with the new config. Must be in warzone."));
  452. } else {
  453. Warzone warzone = this.warzone(player.getLocation());
  454. ZoneLobby lobby = this.lobby(player.getLocation());
  455. if(warzone == null && lobby != null) {
  456. warzone = lobby.getZone();
  457. } else {
  458. lobby = warzone.getLobby();
  459. }
  460. if(updateZoneFromNamedParams(warzone, arguments)) {
  461. player.sendMessage(this.str("Saving config and resetting warzone " + warzone.getName() + "."));
  462. WarzoneMapper.save(this, warzone, false);
  463. warzone.getVolume().resetBlocks();
  464. if(lobby != null) {
  465. lobby.getVolume().resetBlocks();
  466. }
  467. warzone.initializeZone(); // bring back team spawns etc
  468. player.sendMessage(this.str("Warzone config saved. Zone reset."));
  469. } else {
  470. player.sendMessage(this.str("Failed to read named parameters."));
  471. }
  472. }
  473. }
  474. // /resetwarzone
  475. else if(command.equals("resetzone")) {
  476. if(!this.inAnyWarzone(player.getLocation()) && !this.inAnyWarzoneLobby(player.getLocation())) {
  477. player.sendMessage(this.str("Usage: /resetzone <hard/h>. Reloads the zone (from disk if the hard option is specified). Must be in warzone or lobby."));
  478. } else {
  479. Warzone warzone = this.warzone(player.getLocation());
  480. ZoneLobby lobby = this.lobby(player.getLocation());
  481. if(warzone == null && lobby != null) {
  482. warzone = lobby.getZone();
  483. } else {
  484. lobby = warzone.getLobby();
  485. }
  486. int resetBlocks = 0;
  487. for(Team team: warzone.getTeams()) {
  488. team.teamcast(this.str("The war has ended. " + playerListener.getAllTeamsMsg(player) + " Resetting warzone " + warzone.getName() + " and teams..."));
  489. for(Player p : team.getPlayers()) {
  490. p.teleportTo(warzone.getTeleport());
  491. warzone.restorePlayerInventory(p);
  492. player.sendMessage(this.str("You have left the warzone. Your inventory has (hopefully) been restored."));
  493. }
  494. team.getPlayers().clear();
  495. }
  496. Warzone resetWarzone = null;
  497. player.sendMessage(this.str("Reloading warzone " + warzone.getName() + "."));
  498. if(arguments.length == 1 && (arguments[0].equals("hard") || arguments[0].equals("h"))) {
  499. // reset from disk
  500. this.getWarzones().remove(warzone);
  501. resetWarzone = WarzoneMapper.load(this, warzone.getName(), true);
  502. this.getWarzones().add(resetWarzone);
  503. warzone.getVolume().resetBlocks();
  504. if(lobby!=null) {
  505. lobby.getVolume().resetBlocks();
  506. }
  507. resetWarzone.initializeZone();
  508. } else {
  509. resetBlocks = warzone.getVolume().resetBlocks();
  510. if(lobby!=null) {
  511. lobby.getVolume().resetBlocks();
  512. }
  513. warzone.initializeZone();
  514. }
  515. player.sendMessage(this.str("Warzone and teams reset. " + resetBlocks + " blocks reset."));
  516. info(resetBlocks + " blocks reset in warzone " + warzone.getName() + ".");
  517. }
  518. }
  519. // /deletezone
  520. else if(command.equals("deletezone")) {
  521. if(!this.inAnyWarzone(player.getLocation()) && !this.inAnyWarzoneLobby(player.getLocation())) {
  522. player.sendMessage(this.str("Usage: /deletezone. " +
  523. "Deletes the warzone. " +
  524. "Must be in the warzone (try /zones and /zone). "));
  525. } else {
  526. Warzone warzone = this.warzone(player.getLocation());
  527. ZoneLobby lobby = this.lobby(player.getLocation());
  528. if(warzone == null && lobby != null) {
  529. warzone = lobby.getZone();
  530. } else {
  531. lobby = warzone.getLobby();
  532. }
  533. for(Team t : warzone.getTeams()) {
  534. t.getVolume().resetBlocks();
  535. }
  536. for(Monument m : warzone.getMonuments()) {
  537. m.getVolume().resetBlocks();
  538. }
  539. if(warzone.getLobby() != null) {
  540. warzone.getLobby().getVolume().resetBlocks();
  541. }
  542. warzone.getVolume().resetBlocks();
  543. this.getWarzones().remove(warzone);
  544. WarMapper.save(this);
  545. WarzoneMapper.delete(this, warzone.getName());
  546. if(warHub != null) { // warhub has to change
  547. warHub.getVolume().resetBlocks();
  548. warHub.initialize();
  549. }
  550. player.sendMessage(this.str("Warzone " + warzone.getName() + " removed."));
  551. }
  552. }
  553. // /setteam <diamond/iron/gold/d/i/g>
  554. else if(command.equals("setteam")) {
  555. if(arguments.length < 1 || !this.inAnyWarzone(player.getLocation())
  556. || (arguments.length > 0 && TeamMaterials.teamMaterialFromString(arguments[0]) == null)) {
  557. player.sendMessage(this.str("Usage: /setteam <diamond/iron/gold/d/i/g>. " +
  558. "Sets the team spawn to the current location. " +
  559. "Must be in a warzone (try /zones and /zone). "));
  560. } else {
  561. Material teamMaterial = TeamMaterials.teamMaterialFromString(arguments[0]);
  562. String name = TeamMaterials.teamMaterialToString(teamMaterial);
  563. Warzone warzone = this.warzone(player.getLocation());
  564. Team existingTeam = warzone.getTeamByMaterial(teamMaterial);
  565. if(existingTeam != null) {
  566. // relocate
  567. existingTeam.setTeamSpawn(player.getLocation());
  568. player.sendMessage(this.str("Team " + existingTeam.getName() + " spawn relocated."));
  569. } else {
  570. // new team
  571. Team newTeam = new Team(name, teamMaterial, player.getLocation(), this, warzone);
  572. newTeam.setRemainingTickets(warzone.getLifePool());
  573. warzone.getTeams().add(newTeam);
  574. if(warzone.getLobby() != null) {
  575. warzone.getLobby().getVolume().resetBlocks();
  576. warzone.getVolume().resetWallBlocks(warzone.getLobby().getWall());
  577. warzone.addZoneOutline(warzone.getLobby().getWall());
  578. warzone.getLobby().initialize();
  579. }
  580. newTeam.setTeamSpawn(player.getLocation());
  581. player.sendMessage(this.str("Team " + name + " created with spawn here."));
  582. }
  583. WarzoneMapper.save(this, warzone, false);
  584. }
  585. }
  586. // /deleteteam <teamname>
  587. else if(command.equals("deleteteam")) {
  588. if(arguments.length < 1 || (!this.inAnyWarzone(player.getLocation())
  589. && !this.inAnyWarzoneLobby(player.getLocation()))) {
  590. player.sendMessage(this.str("Usage: /deleteteam <team-name>." +
  591. " Deletes the team and its spawn. " +
  592. "Must be in a warzone or lobby (try /zones and /zone). "));
  593. } else {
  594. String name = TeamMaterials.teamMaterialToString(TeamMaterials.teamMaterialFromString(arguments[0]));
  595. Warzone warzone = this.warzone(player.getLocation());
  596. ZoneLobby lobby = this.lobby(player.getLocation());
  597. if(warzone == null && lobby != null) {
  598. warzone = lobby.getZone();
  599. } else {
  600. lobby = warzone.getLobby();
  601. }
  602. List<Team> teams = warzone.getTeams();
  603. Team team = null;
  604. for(Team t : teams) {
  605. if(name.equals(t.getName())) {
  606. team = t;
  607. }
  608. }
  609. if(team != null) {
  610. team.getVolume().resetBlocks();
  611. warzone.getTeams().remove(team);
  612. if(warzone.getLobby() != null) {
  613. warzone.getLobby().getVolume().resetBlocks();
  614. warzone.getVolume().resetWallBlocks(warzone.getLobby().getWall());
  615. warzone.addZoneOutline(warzone.getLobby().getWall());
  616. warzone.getLobby().initialize();
  617. }
  618. WarzoneMapper.save(this, warzone, false);
  619. player.sendMessage(this.str("Team " + name + " removed."));
  620. } else {
  621. player.sendMessage(this.str("No such team."));
  622. }
  623. }
  624. }
  625. // /setmonument
  626. else if(command.equals("setmonument")) {
  627. if(!this.inAnyWarzone(player.getLocation()) || arguments.length < 1 || arguments.length > 1
  628. || (arguments.length == 1 && this.warzone(player.getLocation()) != null
  629. && arguments[0].equals(this.warzone(player.getLocation()).getName()))) {
  630. player.sendMessage(this.str("Usage: /setmonument <name>. Creates or moves a monument. Monument can't have same name as zone. Must be in warzone."));
  631. } else {
  632. Warzone warzone = this.warzone(player.getLocation());
  633. String monumentName = arguments[0];
  634. if(warzone.hasMonument(monumentName)) {
  635. // move the existing monument
  636. Monument monument = warzone.getMonument(monumentName);
  637. monument.getVolume().resetBlocks();
  638. monument.setLocation(player.getLocation());
  639. player.sendMessage(this.str("Monument " + monument.getName() + " was moved."));
  640. } else {
  641. // create a new monument
  642. Monument monument = new Monument(arguments[0], this, warzone, player.getLocation());
  643. warzone.getMonuments().add(monument);
  644. player.sendMessage(this.str("Monument " + monument.getName() + " created."));
  645. }
  646. WarzoneMapper.save(this, warzone, false);
  647. warzone.initializeZone(); // bring back team spawns etc
  648. }
  649. }
  650. // /deletemonument <name>
  651. else if(command.equals("deletemonument")) {
  652. if(arguments.length < 1 || (!this.inAnyWarzone(player.getLocation())
  653. && !this.inAnyWarzoneLobby(player.getLocation()))) {
  654. player.sendMessage(this.str("Usage: /deletemonument <name>." +
  655. " Deletes the monument. " +
  656. "Must be in a warzone or lobby (try /warzones and /warzone). "));
  657. } else {
  658. String name = arguments[0];
  659. Warzone warzone = this.warzone(player.getLocation());
  660. ZoneLobby lobby = this.lobby(player.getLocation());
  661. if(warzone == null && lobby != null) {
  662. warzone = lobby.getZone();
  663. } else {
  664. lobby = warzone.getLobby();
  665. }
  666. Monument monument = warzone.getMonument(name);
  667. if(monument != null) {
  668. monument.getVolume().resetBlocks();
  669. warzone.getMonuments().remove(monument);
  670. WarzoneMapper.save(this, warzone, false);
  671. warzone.initializeZone(); // bring back team spawns etc
  672. player.sendMessage(this.str("Monument " + name + " removed."));
  673. } else {
  674. player.sendMessage(this.str("No such monument."));
  675. }
  676. }
  677. }
  678. // /setwarhub
  679. else if(command.equals("setwarhub")) {
  680. if(warzones.size() > 0) {
  681. if(warHub != null) {
  682. // reset existing hub
  683. warHub.getVolume().resetBlocks();
  684. warHub.setLocation(player.getLocation());
  685. warHub.initialize();
  686. player.sendMessage(str("War hub moved."));
  687. } else {
  688. warHub = new WarHub(this, player.getLocation());
  689. warHub.initialize();
  690. for(Warzone zone : warzones) {
  691. zone.getLobby().getVolume().resetBlocks();
  692. zone.getLobby().initialize();
  693. }
  694. player.sendMessage(str("War hub created."));
  695. }
  696. WarMapper.save(this);
  697. } else {
  698. player.sendMessage(str("No warzones yet."));
  699. }
  700. }
  701. // /deletewarhub
  702. else if(command.equals("deletewarhub")) {
  703. if(warHub != null) {
  704. // reset existing hub
  705. warHub.getVolume().resetBlocks();
  706. VolumeMapper.delete(warHub.getVolume(), this);
  707. this.warHub = null;
  708. for(Warzone zone : warzones) {
  709. zone.getLobby().getVolume().resetBlocks();
  710. zone.getLobby().initialize();
  711. }
  712. player.sendMessage(this.str("War hub removed."));
  713. } else {
  714. player.sendMessage(this.str("No War hub to delete."));
  715. }
  716. WarMapper.save(this);
  717. }
  718. // /setwarconfig
  719. else if(command.equals("setwarconfig")) {
  720. if(arguments.length == 0) {
  721. player.sendMessage(this.str("Usage: /setwarconfig pvpinzonesonly:on lifepool:8 teamsize:5 maxscore:7 autoassign:on outline:off ff:on " +
  722. "Changes the server defaults for new warzones. Please give at leaset one named parameter. Must be in warzone."));
  723. } else {
  724. if(updateFromNamedParams(arguments)) {
  725. WarMapper.save(this);
  726. player.sendMessage(this.str("War config saved."));
  727. } else {
  728. player.sendMessage(this.str("Failed to read named parameters."));
  729. }
  730. }
  731. }
  732. // /zonemaker
  733. else if(command.equals("zonemaker")) {
  734. if(arguments.length > 2) {
  735. player.sendMessage(this.str("Usage: /zonemaker <player-name>, /zonemaker" +
  736. "Elevates the player to zone maker or removes his rights. " +
  737. "If you are already a zonemaker, you can toggle between player and zone maker modes by using the command without arguments."));
  738. } else {
  739. if(arguments.length == 1) {
  740. // make someone zonemaker or remove the right
  741. if(zoneMakerNames.contains(arguments[0])) {
  742. // kick
  743. zoneMakerNames.remove(arguments[0]);
  744. player.sendMessage(str(arguments[0] + " is not a zone maker anymore."));
  745. Player kickedMaker = getServer().getPlayer(arguments[0]);
  746. if(kickedMaker != null) {
  747. kickedMaker.sendMessage(str(player.getName() + " took away your warzone maker priviledges."));
  748. }
  749. } else {
  750. // add
  751. zoneMakerNames.add(arguments[0]);
  752. player.sendMessage(str(arguments[0] + " is now a zone maker."));
  753. Player newMaker = getServer().getPlayer(arguments[0]);
  754. if(newMaker != null) {
  755. newMaker.sendMessage(str(player.getName() + " made you warzone maker."));
  756. }
  757. }
  758. } else {
  759. // toggle to player mode
  760. for(String name : zoneMakerNames) {
  761. if(name.equals(player.getName())) {
  762. getZoneMakersImpersonatingPlayers().add(player.getName());
  763. }
  764. }
  765. zoneMakerNames.remove(player.getName());
  766. player.sendMessage(str("You are now impersonating a regular player. Type /zonemaker again to toggle back to war maker mode."));
  767. }
  768. WarMapper.save(this);
  769. }
  770. }
  771. } else if (command.equals("setzone") // Not a zone maker but War command.
  772. || command.equals("nextbattle")
  773. || command.equals("setzonelobby")
  774. || command.equals("savezone")
  775. || command.equals("setzoneconfig")
  776. || command.equals("resetzone")
  777. || command.equals("deletezone")
  778. || command.equals("setteam")
  779. || command.equals("deleteteam")
  780. || command.equals("setmonument")
  781. || command.equals("deletemonument")
  782. || command.equals("setwarhub")
  783. || command.equals("deletewarhub")
  784. || command.equals("setwarconfig")) {
  785. player.sendMessage(this.str("You can't do this if you are not a warzone maker."));
  786. } else if (command.equals("zonemaker")) {
  787. boolean wasImpersonating = false;
  788. for(String name : getZoneMakersImpersonatingPlayers()) {
  789. if(player.getName().equals(name)) {
  790. zoneMakerNames.add(player.getName());
  791. wasImpersonating = true;
  792. }
  793. }
  794. if(wasImpersonating) {
  795. getZoneMakersImpersonatingPlayers().remove(player.getName());
  796. player.sendMessage(str("You are back as a zone maker."));
  797. }
  798. WarMapper.save(this);
  799. }
  800. return true;
  801. }
  802. private boolean updateZoneFromNamedParams(Warzone warzone, String[] arguments) {
  803. try {
  804. Map<String,String> namedParams = new HashMap();
  805. for(String namedPair : arguments) {
  806. String[] pairSplit = namedPair.split(":");
  807. if(pairSplit.length == 2) {
  808. namedParams.put(pairSplit[0], pairSplit[1]);
  809. }
  810. }
  811. if(namedParams.containsKey("lifepool")){
  812. warzone.setLifePool(Integer.parseInt(namedParams.get("lifepool")));
  813. }
  814. if(namedParams.containsKey("teamsize")){
  815. warzone.setTeamCap(Integer.parseInt(namedParams.get("teamsize")));
  816. }
  817. if(namedParams.containsKey("maxscore")){
  818. warzone.setScoreCap(Integer.parseInt(namedParams.get("maxscore")));
  819. }
  820. if(namedParams.containsKey("ff")){
  821. String onOff = namedParams.get("ff");
  822. warzone.setFriendlyFire(onOff.equals("on") || onOff.equals("true"));
  823. }
  824. if(namedParams.containsKey("autoassign")){
  825. String onOff = namedParams.get("autoassign");
  826. warzone.setAutoAssignOnly(onOff.equals("on") || onOff.equals("true"));
  827. }
  828. if(namedParams.containsKey("outline")){
  829. String onOff = namedParams.get("outline");
  830. warzone.setDrawZoneOutline(onOff.equals("on") || onOff.equals("true"));
  831. }
  832. return true;
  833. } catch (Exception e) {
  834. return false;
  835. }
  836. }
  837. private boolean updateFromNamedParams(String[] arguments) {
  838. try {
  839. Map<String,String> namedParams = new HashMap();
  840. for(String namedPair : arguments) {
  841. String[] pairSplit = namedPair.split(":");
  842. if(pairSplit.length == 2) {
  843. namedParams.put(pairSplit[0], pairSplit[1]);
  844. }
  845. }
  846. if(namedParams.containsKey("lifepool")){
  847. setDefaultLifepool(Integer.parseInt(namedParams.get("lifepool")));
  848. }
  849. if(namedParams.containsKey("teamsize")){
  850. setDefaultTeamCap(Integer.parseInt(namedParams.get("teamsize")));
  851. }
  852. if(namedParams.containsKey("maxscore")){
  853. setDefaultScoreCap(Integer.parseInt(namedParams.get("maxscore")));
  854. }
  855. if(namedParams.containsKey("ff")){
  856. String onOff = namedParams.get("ff");
  857. setDefaultFriendlyFire(onOff.equals("on"));
  858. }
  859. if(namedParams.containsKey("autoassign")){
  860. String onOff = namedParams.get("autoassign");
  861. setDefaultAutoAssignOnly(onOff.equals("on") || onOff.equals("true"));
  862. }
  863. if(namedParams.containsKey("outline")){
  864. String onOff = namedParams.get("outline");
  865. setDefaultDrawZoneOutline(onOff.equals("on") || onOff.equals("true"));
  866. }
  867. if(namedParams.containsKey("pvpinzonesonly")){
  868. String onOff = namedParams.get("pvpinzonesonly");
  869. setPvpInZonesOnly(onOff.equals("on") || onOff.equals("true"));
  870. }
  871. return true;
  872. } catch (Exception e) {
  873. return false;
  874. }
  875. }
  876. public Team getPlayerTeam(String playerName) {
  877. for(Warzone warzone : warzones) {
  878. Team team = warzone.getPlayerTeam(playerName);
  879. if(team != null) return team;
  880. }
  881. return null;
  882. }
  883. public Warzone getPlayerWarzone(String playerName) {
  884. for(Warzone warzone : warzones) {
  885. Team team = warzone.getPlayerTeam(playerName);
  886. if(team != null) return warzone;
  887. }
  888. return null;
  889. }
  890. public Logger getLogger() {
  891. return log;
  892. }
  893. public Warzone warzone(Location location) {
  894. for(Warzone warzone : warzones) {
  895. if(warzone.getVolume() != null && warzone.getVolume().contains(location)) return warzone;
  896. }
  897. return null;
  898. }
  899. public boolean inAnyWarzone(Location location) {
  900. Block locBlock = location.getWorld().getBlockAt(location.getBlockX(), location.getBlockY(), location.getBlockZ());
  901. Warzone currentZone = warzone(location);
  902. if(currentZone == null) {
  903. return false;
  904. } else if (currentZone.getVolume().isWallBlock(locBlock)) {
  905. return false; // wall block doesnt count. this lets people in at the lobby side wall because wall gates overlap with the zone.
  906. }
  907. return true;
  908. }
  909. public boolean inWarzone(String warzoneName, Location location) {
  910. Warzone currentZone = warzone(location);
  911. if(currentZone == null) {
  912. return false;
  913. } else if (warzoneName.equals(currentZone.getName())){
  914. return true;
  915. }
  916. return false;
  917. }
  918. public void addWarzone(Warzone zone) {
  919. warzones.add(zone);
  920. }
  921. public List<Warzone> getWarzones() {
  922. return warzones;
  923. }
  924. public String str(String str) {
  925. String out = ChatColor.GRAY + "[War] " + ChatColor.WHITE + str;
  926. return out;
  927. }
  928. public void info(String str) {
  929. this.getLogger().log(Level.INFO, "[War] " + str);
  930. }
  931. public void warn(String str) {
  932. this.getLogger().log(Level.INFO, "[War] " + str);
  933. }
  934. public Warzone findWarzone(String warzoneName) {
  935. for(Warzone warzone : warzones) {
  936. if(warzone.getName().equals(warzoneName)) {
  937. return warzone;
  938. }
  939. }
  940. return null;
  941. }
  942. public HashMap<Integer, ItemStack> getDefaultLoadout() {
  943. return defaultLoadout;
  944. }
  945. public void setDefaultLifepool(int defaultLifepool) {
  946. this.defaultLifepool = defaultLifepool;
  947. }
  948. public int getDefaultLifepool() {
  949. return defaultLifepool;
  950. }
  951. public void setDefaultFriendlyFire(boolean defaultFriendlyFire) {
  952. this.defaultFriendlyFire = defaultFriendlyFire;
  953. }
  954. public boolean getDefaultFriendlyFire() {
  955. return defaultFriendlyFire;
  956. }
  957. public String getName() {
  958. return name;
  959. }
  960. public Warzone zoneOfZoneWallAtProximity(Location location) {
  961. for(Warzone zone : warzones) {
  962. if(zone.isNearWall(location)) return zone;
  963. }
  964. return null;
  965. }
  966. public List<String> getZoneMakerNames() {
  967. return zoneMakerNames;
  968. }
  969. public boolean isZoneMaker(String playerName) {
  970. for(String zoneMaker : zoneMakerNames) {
  971. if(zoneMaker.equals(playerName)) return true;
  972. }
  973. return false;
  974. }
  975. public boolean getDefaultDrawZoneOutline() {
  976. return isDefaultDrawZoneOutline() ;
  977. }
  978. public boolean getDefaultAutoAssignOnly() {
  979. return defaultAutoAssignOnly;
  980. }
  981. public void setDefaultAutoAssignOnly(boolean autoAssign) {
  982. this.defaultAutoAssignOnly = autoAssign;
  983. }
  984. public WarHub getWarHub() {
  985. return warHub;
  986. }
  987. public void setWarHub(WarHub warHub) {
  988. this.warHub = warHub;
  989. }
  990. public ZoneLobby lobby(Location location) {
  991. for(Warzone warzone : warzones) {
  992. if(warzone.getLobby() != null
  993. && warzone.getLobby().getVolume() != null
  994. && warzone.getLobby().getVolume().contains(location))
  995. return warzone.getLobby();
  996. }
  997. return null;
  998. }
  999. public boolean inAnyWarzoneLobby(Location location) {
  1000. if(lobby(location) == null) {
  1001. return false;
  1002. }
  1003. return true;
  1004. }
  1005. public boolean inWarzoneLobby(String warzoneName, Location location) {
  1006. ZoneLobby currentLobby = lobby(location);
  1007. if(currentLobby == null) {
  1008. return false;
  1009. } else if (warzoneName.equals(currentLobby.getZone().getName())){
  1010. return true;
  1011. }
  1012. return false;
  1013. }
  1014. public void setDefaultTeamCap(int defaultTeamCap) {
  1015. this.defaultTeamCap = defaultTeamCap;
  1016. }
  1017. public int getDefaultTeamCap() {
  1018. return defaultTeamCap;
  1019. }
  1020. public void setPvpInZonesOnly(boolean pvpInZonesOnly) {
  1021. this.pvpInZonesOnly = pvpInZonesOnly;
  1022. }
  1023. public boolean isPvpInZonesOnly() {
  1024. return pvpInZonesOnly;
  1025. }
  1026. public void setDefaultScoreCap(int defaultScoreCap) {
  1027. this.defaultScoreCap = defaultScoreCap;
  1028. }
  1029. public int getDefaultScoreCap() {
  1030. return defaultScoreCap;
  1031. }
  1032. public void setDefaultDrawZoneOutline(boolean defaultDrawZoneOutline) {
  1033. this.defaultDrawZoneOutline = defaultDrawZoneOutline;
  1034. }
  1035. public boolean isDefaultDrawZoneOutline() {
  1036. return defaultDrawZoneOutline;
  1037. }
  1038. public List<String> getZoneMakersImpersonatingPlayers() {
  1039. return zoneMakersImpersonatingPlayers;
  1040. }
  1041. }