Guest User

Untitled

a guest
Jun 26th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 29.13 KB | None | 0 0
  1. package com.palmergames.bukkit.towny.db;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.sql.*;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import java.util.Scanner;
  9.  
  10. import org.bukkit.World;
  11.  
  12. import com.palmergames.bukkit.towny.*;
  13. import com.palmergames.bukkit.towny.object.*;
  14.  
  15. public class TownyDataSource
  16. {
  17. public static int ENEMY = 0;
  18. public static int ALLY = 1;
  19.  
  20. Connection conn = null;
  21.  
  22. protected TownyUniverse universe;
  23. protected Towny plugin;
  24. protected boolean firstRun = false;
  25.  
  26. public void initialize(Towny plugin, TownyUniverse universe)
  27. {
  28. this.universe = universe;
  29. this.plugin = plugin;
  30.  
  31. try
  32. {
  33. Towny.displayDebugMsg("Connection String: jdbc:mysql://" + TownySettings.getMySQLServer() + "/" + TownySettings.getMySQLDatabase() + "?user=" + TownySettings.getMySQLUsername() + "&password=" + TownySettings.getMySQLPassword());
  34. conn = DriverManager.getConnection("jdbc:mysql://" + TownySettings.getMySQLServer() + "/" + TownySettings.getMySQLDatabase() + "?user=" + TownySettings.getMySQLUsername() + "&password=" + TownySettings.getMySQLPassword());
  35.  
  36. query("SELECT COUNT(*) FROM worlds", true);
  37. //
  38. // query("ALTER TABLE `minecraft`.`nations` DROP FOREIGN KEY `nations_capital`");
  39. //
  40. // query("ALTER TABLE `minecraft`.`nations` CHANGE COLUMN `capital` `capital` VARCHAR(45) NULL DEFAULT NULL, ADD CONSTRAINT `nations_capital` FOREIGN KEY (`capital` ) REFERENCES `minecraft`.`towns` (`name` ) ON DELETE NO ACTION ON UPDATE NO ACTION");
  41.  
  42. // query("DROP TABLE IF EXISTS `plot_management_delete_ids`");
  43. // query("DROP TABLE IF EXISTS `plot_management_ignore_ids`");
  44. // query("DROP TABLE IF EXISTS `plot_management_mayor_delete`");
  45. // query("DROP TABLE IF EXISTS `unclaimed_zone_ignore_ids`");
  46. // query("UPDATE players SET town_name=null");
  47. // query("UPDATE towns SET nation_name=null AND home_block_world=null AND home_block_x=null AND home_block_z=null");
  48. // query("DROP TABLE IF EXISTS `political_relationships`");
  49. // query("ALTER TABLE `towns` DROP FOREIGN KEY `towns_nation` , DROP FOREIGN KEY `home_block` , DROP FOREIGN KEY `towns_mayor` , DROP FOREIGN KEY `towns_world`");
  50. // query("DROP TABLE IF EXISTS `friends`");
  51. // query("ALTER TABLE `blocks` DROP FOREIGN KEY `blocks_world` , DROP FOREIGN KEY `blocks_town` , DROP FOREIGN KEY `blocks_owner`");
  52. // query("DROP TABLE IF EXISTS `plot_data`");
  53. // query("DROP TABLE IF EXISTS `blocks`");
  54. // query("DROP TABLE IF EXISTS `players`");
  55. // query("DROP TABLE IF EXISTS `nations`");
  56. // query("DROP TABLE IF EXISTS `towns`");
  57. // query("DROP TABLE IF EXISTS `worlds`");
  58. }
  59. catch (SQLException ex)
  60. {
  61. // handle any errors
  62. Towny.displayError("SQLException: " + ex.getMessage());
  63. Towny.displayError("SQLState: " + ex.getSQLState());
  64. Towny.displayError("VendorError: " + ex.getErrorCode());
  65. }
  66. }
  67.  
  68. private void setup()
  69. {
  70. query("CREATE TABLE `worlds` ("
  71. + "`name` varchar(45) NOT NULL,"
  72. + "`claimable` tinyint(1) DEFAULT '0',"
  73. + "`pvp` tinyint(1) DEFAULT '0',"
  74. + "`force_pvp` tinyint(1) DEFAULT '0',"
  75. + "`force_town_mobs` tinyint(1) DEFAULT '0',"
  76. + "`world_mobs` tinyint(1) DEFAULT '0',"
  77. + "`fire_spread` tinyint(1) DEFAULT '0',"
  78. + "`force_fire_spread` tinyint(1) DEFAULT '0',"
  79. + "`explosions` tinyint(1) DEFAULT '0',"
  80. + "`force_explosions` tinyint(1) DEFAULT '0',"
  81. + "`enderman_protect` tinyint(1) DEFAULT '0',"
  82. + "`disable_player_trample` tinyint(1) DEFAULT '0',"
  83. + "`disable_creature_trample` tinyint(1) DEFAULT '0',"
  84. + "`unclaimed_zone_build` tinyint(1) DEFAULT '0',"
  85. + "`unclaimed_zone_destroy` tinyint(1) DEFAULT '0',"
  86. + "`unclaimed_zone_switch` tinyint(1) DEFAULT '0',"
  87. + "`unclaimed_zone_item_use` tinyint(1) DEFAULT '0',"
  88. + "`unclaimed_zone_name` varchar(45) DEFAULT NULL,"
  89. + "`unclaimed_zone_ignore_ids` tinyint(1) DEFAULT '0',"
  90. + "`using_plot_management_delete` tinyint(1) DEFAULT '0',"
  91. + "`plot_management_delete_ids` tinyint(1) DEFAULT '0',"
  92. + "`using_plot_management_mayor_delete` tinyint(1) DEFAULT '0',"
  93. + "`using_management_mayor_delete` tinyint(1) DEFAULT '0',"
  94. + "`using_plot_management_revert` tinyint(1) DEFAULT '0',"
  95. + "`using_plot_management_revert_speed` tinyint(1) DEFAULT '0',"
  96. + "`plot_management_ignore_ids` tinyint(1) DEFAULT '0',"
  97. + "`using_plot_management_wild_regen` tinyint(1) DEFAULT '0',"
  98. + "`using_plot_management_wild_regen_delay` tinyint(1) DEFAULT '0',"
  99. + "`global_chat_channel_format` text,"
  100. + "`town_chat_channel_format` text,"
  101. + "`nation_chat_channel_format` text,"
  102. + "`default_chat_channel_format` text,"
  103. + "`using_towny` tinyint(1) DEFAULT '0',"
  104. + "PRIMARY KEY (`name`)"
  105. + ") ENGINE=InnoDB DEFAULT CHARSET=utf8;");
  106.  
  107. query("CREATE TABLE `plot_management_delete_ids` ("
  108. + "`world_name` varchar(45) NOT NULL,"
  109. + "`id` int(11) NOT NULL,"
  110. + "PRIMARY KEY (`world_name`),"
  111. + "KEY `pmdi_world_name` (`world_name`) USING BTREE,"
  112. + "CONSTRAINT `pmdi_world_name` FOREIGN KEY (`world_name`) REFERENCES `worlds` (`name`) ON DELETE NO ACTION ON UPDATE NO ACTION"
  113. + ") ENGINE=InnoDB DEFAULT CHARSET=utf8;");
  114.  
  115. query("CREATE TABLE `plot_management_ignore_ids` ("
  116. + "`world_name` varchar(45) NOT NULL,"
  117. + "`id` int(11) NOT NULL,"
  118. + "PRIMARY KEY (`world_name`),"
  119. + "KEY `pmii_world_name` (`world_name`) USING BTREE,"
  120. + "CONSTRAINT `pmii_world_name` FOREIGN KEY (`world_name`) REFERENCES `worlds` (`name`) ON DELETE NO ACTION ON UPDATE NO ACTION"
  121. + ") ENGINE=InnoDB DEFAULT CHARSET=utf8;");
  122.  
  123. query("CREATE TABLE `plot_management_mayor_delete` ("
  124. + "`world_name` varchar(45) NOT NULL,"
  125. + "`material` varchar(45) NOT NULL,"
  126. + "PRIMARY KEY (`world_name`),"
  127. + "KEY `pmmd_world_name` (`world_name`) USING BTREE,"
  128. + "CONSTRAINT `pmmd_world_name` FOREIGN KEY (`world_name`) REFERENCES `worlds` (`name`) ON DELETE NO ACTION ON UPDATE NO ACTION"
  129. + ") ENGINE=InnoDB DEFAULT CHARSET=utf8;");
  130.  
  131. query("CREATE TABLE `unclaimed_zone_ignore_ids` ("
  132. + "`world_name` varchar(45) NOT NULL,"
  133. + "`id` int(11) NOT NULL,"
  134. + "PRIMARY KEY (`world_name`),"
  135. + "KEY `uzii_world_name` (`world_name`) USING BTREE,"
  136. + "CONSTRAINT `uzii_world_name` FOREIGN KEY (`world_name`) REFERENCES `worlds` (`name`) ON DELETE NO ACTION ON UPDATE NO ACTION"
  137. + ") ENGINE=InnoDB DEFAULT CHARSET=utf8;");
  138.  
  139. query("CREATE TABLE `towns` ("
  140. + "`name` varchar(45) NOT NULL,"
  141. + "`mayor_name` varchar(45) DEFAULT NULL,"
  142. + "`town_board` varchar(45) DEFAULT NULL,"
  143. + "`tag` varchar(100) DEFAULT NULL,"
  144. + "`plot_price` double DEFAULT '0',"
  145. + "`has_upkeep` tinyint(1) DEFAULT '0',"
  146. + "`tax_percent` tinyint(1) DEFAULT '0',"
  147. + "`taxes` double DEFAULT '0',"
  148. + "`plot_tax` double DEFAULT '0',"
  149. + "`commercial_plot_price` double DEFAULT '0',"
  150. + "`commercial_plot_tax` double DEFAULT '0',"
  151. + "`embassy_plot_price` double DEFAULT '0',"
  152. + "`embassy_plot_tax` double DEFAULT '0',"
  153. + "`public` tinyint(1) DEFAULT '1',"
  154. + "`home_block_world` varchar(45) DEFAULT NULL,"
  155. + "`home_block_x` int(11) DEFAULT NULL,"
  156. + "`home_block_z` int(11) DEFAULT NULL,"
  157. + "`spawn_world` varchar(45) DEFAULT NULL,"
  158. + "`spawn_x` double DEFAULT NULL,"
  159. + "`spawn_y` double DEFAULT NULL,"
  160. + "`spawn_z` double DEFAULT NULL,"
  161. + "`spawn_pitch` float DEFAULT NULL,"
  162. + "`spawn_yawn` float DEFAULT NULL,"
  163. + "`nation_name` varchar(45) DEFAULT NULL,"
  164. + "`world_name` varchar(45) NOT NULL,"
  165. + "`bonus_blocks` int(11) DEFAULT '0',"
  166. + "`purchased_blocks` int(11) DEFAULT '0',"
  167. + "`denyAll` tinyint(1) DEFAULT '0',"
  168. + "`residentBuild` tinyint(1) DEFAULT '0',"
  169. + "`residentDestroy` tinyint(1) DEFAULT '0',"
  170. + "`residentSwitch` tinyint(1) DEFAULT '0',"
  171. + "`residentItemUse` tinyint(1) DEFAULT '0',"
  172. + "`outsiderBuild` tinyint(1) DEFAULT '0',"
  173. + "`outsiderDestroy` tinyint(1) DEFAULT '0',"
  174. + "`outsiderSwitch` tinyint(1) DEFAULT '0',"
  175. + "`outsiderItemUse` tinyint(1) DEFAULT '0',"
  176. + "`allyBuild` tinyint(1) DEFAULT '0',"
  177. + "`allyDestroy` tinyint(1) DEFAULT '0',"
  178. + "`allySwitch` tinyint(1) DEFAULT '0',"
  179. + "`allyItemUse` tinyint(1) DEFAULT '0',"
  180. + "`pvp` tinyint(1) DEFAULT '0',"
  181. + "`fire` tinyint(1) DEFAULT '0',"
  182. + "`explosion` tinyint(1) DEFAULT '0',"
  183. + "`mobs` tinyint(1) DEFAULT '0',"
  184. + "PRIMARY KEY (`name`),"
  185. + "KEY `towns_nation` (`nation_name`) USING HASH,"
  186. + "KEY `towns_world` (`world_name`) USING HASH,"
  187. + "KEY `mayors` (`mayor_name`) USING HASH,"
  188. + "KEY `home_block` (`home_block_world`,`home_block_x`,`home_block_z`) USING HASH"
  189. + ") ENGINE=InnoDB DEFAULT CHARSET=utf8;");
  190.  
  191. query("CREATE TABLE `nations` ("
  192. + "`name` varchar(45) NOT NULL,"
  193. + "`capital` varchar(45) DEFAULT NULL,"
  194. + "`tag` varchar(45) DEFAULT NULL,"
  195. + "`taxes` double DEFAULT '0',"
  196. + "`neutral` tinyint(1) DEFAULT '0',"
  197. + "PRIMARY KEY (`name`),"
  198. + "KEY `nations_capital` (`capital`) USING HASH"
  199. + ") ENGINE=InnoDB DEFAULT CHARSET=utf8;");
  200.  
  201. query("CREATE TABLE `players` ("
  202. + "`name` varchar(45) NOT NULL,"
  203. + "`last_online` bigint(20) DEFAULT '0',"
  204. + "`town_name` varchar(45) DEFAULT NULL,"
  205. + "`registered` bigint(20) DEFAULT '0',"
  206. + "`is_npc` tinyint(1) DEFAULT '0',"
  207. + "`title` varchar(45) DEFAULT NULL,"
  208. + "`surname` varchar(45) DEFAULT NULL,"
  209. + "`town_assistant` tinyint(1) DEFAULT '0',"
  210. + "`nation_assistant` tinyint(1) DEFAULT '0',"
  211. + "`denyAll` tinyint(1) DEFAULT NULL,"
  212. + "`residentBuild` tinyint(1) DEFAULT NULL,"
  213. + "`residentDestroy` tinyint(1) DEFAULT NULL,"
  214. + "`residentSwitch` tinyint(1) DEFAULT NULL,"
  215. + "`residentItemUse` tinyint(1) DEFAULT NULL,"
  216. + "`outsiderBuild` tinyint(1) DEFAULT NULL,"
  217. + "`outsiderDestroy` tinyint(1) DEFAULT NULL,"
  218. + "`outsiderSwitch` tinyint(1) DEFAULT NULL,"
  219. + "`outsiderItemUse` tinyint(1) DEFAULT NULL,"
  220. + "`allyBuild` tinyint(1) DEFAULT NULL,"
  221. + "`allyDestroy` tinyint(1) DEFAULT NULL,"
  222. + "`allySwitch` tinyint(1) DEFAULT NULL,"
  223. + "`allyItemUse` tinyint(1) DEFAULT NULL,"
  224. + "`pvp` tinyint(1) DEFAULT NULL,"
  225. + "`fire` tinyint(1) DEFAULT NULL,"
  226. + "`explosion` tinyint(1) DEFAULT NULL,"
  227. + "`mobs` tinyint(1) DEFAULT NULL,"
  228. + "`global_chat` tinyint(1) DEFAULT '1',"
  229. + "`town_chat` tinyint(1) DEFAULT '1',"
  230. + "`nation_chat` tinyint(1) DEFAULT '1',"
  231. + "PRIMARY KEY (`name`),"
  232. + "KEY `players_town_name` (`town_name`) USING BTREE,"
  233. + "CONSTRAINT `players_town_name` FOREIGN KEY (`town_name`) REFERENCES `towns` (`name`) ON DELETE NO ACTION ON UPDATE NO ACTION"
  234. + ") ENGINE=InnoDB DEFAULT CHARSET=utf8;");
  235.  
  236. query("CREATE TABLE `blocks` ("
  237. + "`world_name` varchar(45) NOT NULL,"
  238. + "`x` int(11) NOT NULL,"
  239. + "`z` int(11) NOT NULL,"
  240. + "`town_name` varchar(45) DEFAULT NULL,"
  241. + "`type` tinyint(4) DEFAULT NULL,"
  242. + "`price` double DEFAULT NULL,"
  243. + "`changed` tinyint(1) DEFAULT '0',"
  244. + "`regen` tinyint(1) DEFAULT '0',"
  245. + "`player_owner` varchar(45) DEFAULT NULL,"
  246. + "`denyAll` varchar(45) DEFAULT NULL,"
  247. + "`residentBuild` varchar(45) DEFAULT NULL,"
  248. + "`residentDestroy` varchar(45) DEFAULT NULL,"
  249. + "`residentSwitch` varchar(45) DEFAULT NULL,"
  250. + "`residentItemUse` varchar(45) DEFAULT NULL,"
  251. + "`outsiderBuild` varchar(45) DEFAULT NULL,"
  252. + "`outsiderDestroy` varchar(45) DEFAULT NULL,"
  253. + "`outsiderSwitch` varchar(45) DEFAULT NULL,"
  254. + "`outsiderItemUse` varchar(45) DEFAULT NULL,"
  255. + "`allyBuild` varchar(45) DEFAULT NULL,"
  256. + "`allyDestroy` varchar(45) DEFAULT NULL,"
  257. + "`allySwitch` varchar(45) DEFAULT NULL,"
  258. + "`allyItemUse` varchar(45) DEFAULT NULL,"
  259. + "`pvp` varchar(45) DEFAULT NULL,"
  260. + "`fire` varchar(45) DEFAULT NULL,"
  261. + "`explosion` varchar(45) DEFAULT NULL,"
  262. + "`mobs` varchar(45) DEFAULT NULL,"
  263. + "PRIMARY KEY (`world_name`,`x`,`z`),"
  264. + "KEY `blocks_world` (`world_name`) USING BTREE,"
  265. + "KEY `blocks_town` (`town_name`) USING BTREE,"
  266. + "KEY `blocks_owner` (`player_owner`) USING HASH,"
  267. + "CONSTRAINT `blocks_owner` FOREIGN KEY (`player_owner`) REFERENCES `players` (`name`) ON DELETE NO ACTION ON UPDATE NO ACTION,"
  268. + "CONSTRAINT `blocks_town` FOREIGN KEY (`town_name`) REFERENCES `towns` (`name`) ON DELETE NO ACTION ON UPDATE NO ACTION,"
  269. + "CONSTRAINT `blocks_world` FOREIGN KEY (`world_name`) REFERENCES `worlds` (`name`) ON DELETE NO ACTION ON UPDATE NO ACTION"
  270. + ") ENGINE=InnoDB DEFAULT CHARSET=utf8;");
  271.  
  272. query("CREATE TABLE `friends` ("
  273. + "`player_1` varchar(45) NOT NULL,"
  274. + "`player_2` varchar(45) NOT NULL,"
  275. + "PRIMARY KEY (`player_1`,`player_2`),"
  276. + "KEY `friends_player_2` (`player_2`),"
  277. + "KEY `friends_player_1` (`player_1`) USING BTREE,"
  278. + "CONSTRAINT `friends_player_1` FOREIGN KEY (`player_1`) REFERENCES `players` (`name`) ON DELETE NO ACTION ON UPDATE NO ACTION,"
  279. + "CONSTRAINT `friends_player_2` FOREIGN KEY (`player_2`) REFERENCES `players` (`name`) ON DELETE NO ACTION ON UPDATE NO ACTION"
  280. + ") ENGINE=InnoDB DEFAULT CHARSET=utf8;");
  281.  
  282. query("CREATE TABLE `political_relationships` ("
  283. + "`nation_name_1` varchar(45) NOT NULL,"
  284. + "`nation_name_2` varchar(45) NOT NULL,"
  285. + "`relation_type` int(11) NOT NULL,"
  286. + "PRIMARY KEY (`nation_name_1`,`nation_name_2`),"
  287. + "KEY `pr_nations_2` (`nation_name_2`),"
  288. + "KEY `pr_nations_1` (`nation_name_1`) USING BTREE,"
  289. + "CONSTRAINT `pr_nations_1` FOREIGN KEY (`nation_name_1`) REFERENCES `nations` (`name`) ON DELETE NO ACTION ON UPDATE NO ACTION,"
  290. + "CONSTRAINT `pr_nations_2` FOREIGN KEY (`nation_name_2`) REFERENCES `nations` (`name`) ON DELETE NO ACTION ON UPDATE NO ACTION"
  291. + ") ENGINE=InnoDB DEFAULT CHARSET=utf8;");
  292.  
  293. query("CREATE TABLE `plot_data` ("
  294. + "`world_name` varchar(45) NOT NULL,"
  295. + "`x` int(11) NOT NULL,"
  296. + "`z` int(11) NOT NULL,"
  297. + "`block_x` int(11) NOT NULL,"
  298. + "`block_y` int(11) NOT NULL,"
  299. + "`block_z` int(11) NOT NULL,"
  300. + "`block_type` int(11) NOT NULL,"
  301. + "PRIMARY KEY (`world_name`,`x`,`z`,`block_x`,`block_y`,`block_z`),"
  302. + "KEY `pd_block` (`world_name`,`x`,`z`) USING BTREE,"
  303. + "CONSTRAINT `pd_block` FOREIGN KEY (`world_name`, `x`, `z`) REFERENCES `blocks` (`world_name`, `x`, `z`) ON DELETE NO ACTION ON UPDATE NO ACTION"
  304. + ") ENGINE=InnoDB DEFAULT CHARSET=utf8;");
  305.  
  306. query("ALTER TABLE `towns` "
  307. + "ADD CONSTRAINT `towns_world` FOREIGN KEY (`world_name` ) REFERENCES `worlds` (`name`) ON DELETE NO ACTION ON UPDATE NO ACTION, "
  308. + "ADD CONSTRAINT `towns_mayor` FOREIGN KEY (`mayor_name` ) REFERENCES `players` (`name` ) ON DELETE NO ACTION ON UPDATE NO ACTION, "
  309. + "ADD CONSTRAINT `home_block` FOREIGN KEY (`home_block_world` , `home_block_x` , `home_block_z` ) REFERENCES `blocks` (`world_name` , `x` , `z` ) ON DELETE NO ACTION ON UPDATE NO ACTION, "
  310. + "ADD CONSTRAINT `towns_nation` FOREIGN KEY (`nation_name` ) REFERENCES `nations` (`name` ) ON DELETE NO ACTION ON UPDATE NO ACTION");
  311. }
  312.  
  313. public void deleteFile(String fileName)
  314. {
  315. File file = new File(fileName);
  316.  
  317. if (file.exists())
  318. {
  319. file.delete();
  320. }
  321. }
  322.  
  323. public ResultSet query(String queryStr)
  324. {
  325. return query(queryStr, false, false);
  326. }
  327.  
  328. public ResultSet query(String queryStr, boolean first)
  329. {
  330. return query(queryStr, false, false);
  331. }
  332.  
  333. public ResultSet query(String queryStr, boolean first, boolean suppressErrorMsg)
  334. {
  335. Statement stmt = null;
  336. ResultSet rs = null;
  337.  
  338. Towny.displayDebugMsg("Query: " + queryStr);
  339.  
  340. try
  341. {
  342. stmt = conn.createStatement();
  343.  
  344. if (queryStr.startsWith("SELECT"))
  345. {
  346. stmt.executeQuery(queryStr);
  347. rs = stmt.getResultSet();
  348. }
  349. else
  350. {
  351. rs = null;
  352. stmt.execute(queryStr);
  353. }
  354. }
  355. catch (SQLException ex)
  356. {
  357. if (first)
  358. {
  359. setup();
  360. }
  361. else
  362. {
  363. if (!suppressErrorMsg)
  364. {
  365. Towny.displayError("Query: " + queryStr);
  366. Towny.displayError("SQLException: " + ex.getMessage());
  367. Towny.displayError("SQLState: " + ex.getSQLState());
  368. Towny.displayError("VendorError: " + ex.getErrorCode());
  369. ex.printStackTrace();
  370. }
  371. }
  372. }
  373. catch (Exception e)
  374. {
  375. e.printStackTrace();
  376. }
  377.  
  378. return rs;
  379. }
  380.  
  381. public void backup() throws IOException
  382. {
  383. }
  384.  
  385. public void cleanupBackups()
  386. {
  387. }
  388.  
  389. public boolean confirmContinuation(String msg)
  390. {
  391. Boolean choice = null;
  392. String input = null;
  393. while (choice == null)
  394. {
  395. System.out.println(msg);
  396. System.out.print(" Continue (y/n): ");
  397.  
  398. Scanner in = new Scanner(System.in);
  399.  
  400. input = in.next();
  401. input = input.toLowerCase();
  402.  
  403. if (input.equals("y") || input.equals("yes"))
  404. {
  405. in.close();
  406.  
  407. return true;
  408. }
  409. else if (input.equals("n") || input.equals("no"))
  410. {
  411. in.close();
  412.  
  413. return false;
  414. }
  415. }
  416.  
  417. System.out.println("[Towny] Error recieving input, exiting.");
  418.  
  419. return false;
  420. }
  421.  
  422. public void sendDebugMsg(String msg)
  423. {
  424. if (plugin != null)
  425. {
  426. TownyMessaging.sendDebugMsg(msg);
  427. }
  428. else
  429. {
  430. System.out.println("[Towny] Debug: " + msg);
  431. }
  432. }
  433.  
  434. public boolean loadAll()
  435. {
  436. TownyUniverse.locked = true;
  437.  
  438. boolean result = loadWorldList() && loadResidentList() && loadNationList() && loadTownList() && loadWorlds() && loadResidents() && loadNations() && loadRegenList();
  439.  
  440. TownyUniverse.locked = false;
  441.  
  442. return result;
  443. }
  444.  
  445. public boolean saveAll()
  446. {
  447. return loadRegenList();
  448. }
  449.  
  450. public List<TownyWorld> getWorlds()
  451. {
  452. return universe.getWorlds();
  453. }
  454.  
  455. public Resident getResident(String name)
  456. {
  457. try
  458. {
  459. return universe.getResident(name);
  460. }
  461. catch (NotOnlineException e)
  462. {
  463. return null;
  464. }
  465. catch (NotRegisteredException e)
  466. {
  467. return null;
  468. }
  469. }
  470.  
  471. public Town getTown(String name)
  472. {
  473. try
  474. {
  475. return universe.getTown(name);
  476. }
  477. catch (NotRegisteredException e)
  478. {
  479. return null;
  480. }
  481. }
  482.  
  483. public Nation getNation(String name)
  484. {
  485. try
  486. {
  487. return universe.getNation(name);
  488. }
  489. catch (NotRegisteredException e)
  490. {
  491. return null;
  492. }
  493. }
  494.  
  495. public boolean loadResidents()
  496. {
  497. sendDebugMsg("Loading Residents");
  498.  
  499. for (Resident resident : universe.getResidents())
  500. {
  501. if (!resident.load())
  502. {
  503. System.out.println("[Towny] Loading Error: Could not read resident data '" + resident.getName() + "'.");
  504.  
  505. return false;
  506. }
  507. else
  508. {
  509. // Push all Towns belonging to this world
  510. }
  511. }
  512.  
  513. return true;
  514. }
  515.  
  516. public boolean loadNations()
  517. {
  518. sendDebugMsg("Loading Nations");
  519.  
  520. for (Nation nation : universe.getNations())
  521. {
  522. if (!nation.load())
  523. {
  524. System.out.println("[Towny] Loading Error: Could not read resident data '" + nation.getName() + "'.");
  525.  
  526. return false;
  527. }
  528. else
  529. {
  530. // Push all Towns belonging to this world
  531. }
  532. }
  533.  
  534. return true;
  535. }
  536.  
  537. public boolean loadWorlds()
  538. {
  539. sendDebugMsg("Loading Worlds");
  540.  
  541. for (TownyWorld world : universe.getWorlds())
  542. {
  543. if (!world.load())
  544. {
  545. System.out.println("[Towny] Loading Error: Could not read world data '" + world.getName() + "'.");
  546.  
  547. return false;
  548. }
  549. else
  550. {
  551. // Push all Towns belonging to this world
  552. }
  553. }
  554.  
  555. return true;
  556. }
  557.  
  558. public boolean loadResidentList()
  559. {
  560. Towny.displayDebugMsg("Loading Resident List");
  561.  
  562. ResultSet rs = query("SELECT name FROM players");
  563.  
  564. try
  565. {
  566. while (rs != null && rs.next())
  567. {
  568. universe.newResident(rs.getString("name"));
  569. }
  570. }
  571. catch (AlreadyRegisteredException e)
  572. {
  573. e.printStackTrace();
  574. }
  575. catch (Exception e)
  576. {
  577. e.printStackTrace();
  578. return false;
  579. }
  580.  
  581. return true;
  582. }
  583.  
  584. public boolean loadTownList()
  585. {
  586. Towny.displayDebugMsg("Loading Town List");
  587.  
  588. ResultSet rs = query("SELECT name, world_name FROM towns");
  589.  
  590. try
  591. {
  592. while (rs != null && rs.next())
  593. {
  594. // Towny.displayMsg("rs.getString(\"world_name\"): " + rs.getString("world_name"));
  595. // Towny.displayMsg("TownyUniverse.getTownWorld(" + rs.getString("world_name") + "): " + TownyUniverse.getWorld(rs.getString("world_name")));
  596. universe.newTown(rs.getString("name"), TownyUniverse.getWorld(rs.getString("world_name")));
  597. }
  598. }
  599. catch (AlreadyRegisteredException e)
  600. {
  601. e.printStackTrace();
  602. }
  603. catch (Exception e)
  604. {
  605. e.printStackTrace();
  606. return false;
  607. }
  608.  
  609. return true;
  610. }
  611.  
  612. public boolean loadNationList()
  613. {
  614. Towny.displayDebugMsg("Loading Nation List");
  615.  
  616. ResultSet rs = query("SELECT name, capital FROM nations");
  617.  
  618. try
  619. {
  620. while (rs != null && rs.next())
  621. {
  622. universe.newNation(rs.getString("name"));
  623. }
  624. }
  625. catch (AlreadyRegisteredException e)
  626. {
  627. e.printStackTrace();
  628. }
  629. catch (Exception e)
  630. {
  631. e.printStackTrace();
  632. return false;
  633. }
  634.  
  635. return true;
  636. }
  637.  
  638. public boolean loadWorldList()
  639. {
  640. Towny.displayDebugMsg("Loading World List");
  641.  
  642. if (plugin != null)
  643. {
  644. for (World world : plugin.getServer().getWorlds())
  645. {
  646. try
  647. {
  648. universe.newWorld(world.getName());
  649. }
  650. catch (AlreadyRegisteredException e)
  651. {
  652. e.printStackTrace();
  653. }
  654. catch (NotRegisteredException e)
  655. {
  656. e.printStackTrace();
  657. }
  658. }
  659. }
  660. else
  661. {
  662. Towny.displayDebugMsg("Loading World List Failure ... plugin == null?");
  663. return false;
  664. }
  665.  
  666. Towny.displayDebugMsg("Loading World List Sucessful");
  667. return true;
  668. }
  669.  
  670. public boolean loadRegenList()
  671. {
  672. Towny.displayDebugMsg("Loading Regen List");
  673.  
  674. ResultSet rs = query("SELECT world_name, x, z FROM blocks WHERE regen='1'");
  675.  
  676. PlotBlockData plotData;
  677.  
  678. try
  679. {
  680. while (rs != null && rs.next())
  681. {
  682. plotData = loadPlotData(rs.getString("world_name"), rs.getInt("x"), rs.getInt("z"));
  683. if (plotData != null)
  684. {
  685. TownyRegenAPI.addPlotChunk(plotData, false);
  686. }
  687. }
  688. }
  689. catch (Exception e)
  690. {
  691. e.printStackTrace();
  692. return false;
  693. }
  694.  
  695. return true;
  696. }
  697.  
  698. // TODO Hook up to add friend cmd
  699. public boolean saveFriend(Resident resident, Resident friend)
  700. {
  701. try
  702. {
  703. ResultSet rs = query("SELECT player_2 FROM friends WHERE player_1='" + resident.getName() + "' AND player_2='" + friend.getName() + "'");
  704. if (rs == null || !rs.next())
  705. {
  706. query("INSERT INTO friends VALUES ('" + resident.getName() + "', '" + friend.getName() + "')");
  707. }
  708. }
  709. catch (SQLException e)
  710. {
  711. e.printStackTrace();
  712. return false;
  713. }
  714. catch (Exception e)
  715. {
  716. e.printStackTrace();
  717. return false;
  718. }
  719.  
  720. return true;
  721. }
  722.  
  723. public boolean savePlotData(PlotBlockData plotChunk)
  724. {
  725. return true;
  726. }
  727.  
  728. public PlotBlockData loadPlotData(String worldName, int x, int z)
  729. {
  730. return null;
  731. }
  732.  
  733. public PlotBlockData loadPlotData(TownBlock townBlock)
  734. {
  735. return null;
  736. }
  737.  
  738. public void deletePlotData(PlotBlockData plotChunk)
  739. {
  740. }
  741.  
  742. public void deleteResident(Resident resident)
  743. {
  744. query("DELETE FROM players WHERE name='" + resident.getName() + "'");
  745. }
  746.  
  747. public void deleteTownHomeBlock(Town town)
  748. {
  749. query("UPDATE towns SET home_block_world=null, home_block_x=null, home_block_z=null WHERE name='" + town.getName() + "'");
  750. }
  751.  
  752. public void deleteTown(Town town)
  753. {
  754. deleteTownHomeBlock(town);
  755. query("DELETE FROM blocks WHERE town_name='" + town.getName() + "'");
  756. query("DELETE FROM towns WHERE name='" + town.getName() + "'");
  757. }
  758.  
  759. public void deleteNation(Nation nation)
  760. {
  761. query("UPDATE towns SET nation_name=null WHERE nation_name='" + nation.getName() + "'");
  762. query("DELETE FROM political_relationships WHERE nation_name_1='" + nation.getName() + "' OR nation_name_2='" + nation.getName() + "'");
  763. query("DELETE FROM nations WHERE name='" + nation.getName() + "'");
  764. }
  765.  
  766. public void deleteWorld(TownyWorld world)
  767. {
  768. query("DELETE FROM worlds WHERE name='" + world.getName() + "'");
  769. }
  770.  
  771. public void deleteTownBlock(TownBlock townBlock)
  772. {
  773. query("DELETE FROM blocks WHERE world_name='" + townBlock.getWorld().getName() + "' AND x='" + townBlock.getX() + "' AND z='" + townBlock.getZ() + "'");
  774. }
  775.  
  776. public void setResidentTown(Town town, String player)
  777. {
  778. query("UPDATE blocks SET player_owner=null WHERE player_owner='" + player + "'");
  779.  
  780. if (town == null)
  781. {
  782. query("UPDATE players SET town_name=null WHERE name='" + player + "'");
  783. }
  784. else
  785. {
  786. query("UPDATE players SET town_name='" + town.getName() + "' WHERE name='" + player + "'");
  787. }
  788. }
  789.  
  790. public void removeTownFromResidents(Town town)
  791. {
  792. query("UPDATE players SET town_name=null WHERE town_name='" + town.getName() + "'");
  793. }
  794.  
  795. public void isResidentInATown(String playerName) throws AlreadyRegisteredException
  796. {
  797. ResultSet rs = query("SELECT town_name FROM players WHERE name='" + playerName + "'");
  798.  
  799. try
  800. {
  801. if (rs != null && rs.next())
  802. {
  803. if (rs.getString("town_name") != null)
  804. {
  805. throw new AlreadyRegisteredException();
  806. }
  807. }
  808. }
  809. catch (SQLException e)
  810. {
  811. e.printStackTrace(Towny.pw);
  812. e.printStackTrace();
  813. }
  814. }
  815.  
  816. public void setBlockPlayerOwner(TownBlock townBlock, String name)
  817. {
  818. query("UPDATE blocks SET player_owner='" + name + "' WHERE world_name='" + townBlock.getWorld().getName() + "' AND x='" + townBlock.getX() + "' AND z='" + townBlock.getZ() + "'");
  819. }
  820.  
  821. public void deleteBlockPlayerOwner(TownBlock townBlock)
  822. {
  823. query("UPDATE blocks SET player_owner=null WHERE world_name='" + townBlock.getWorld().getName() + "' AND x='" + townBlock.getX() + "' AND z='" + townBlock.getZ() + "'");
  824. }
  825.  
  826. public boolean hasFriend(String player1, String player2)
  827. {
  828. ResultSet rs = query("SELECT player_2 FROM friends WHERE player_1='" + player1 + "' AND player_2='" + player2 + "'");
  829.  
  830. try
  831. {
  832. if (rs != null && rs.next())
  833. {
  834. if (rs.getString("player_2") != null)
  835. {
  836. return true;
  837. }
  838. }
  839. }
  840. catch (SQLException e)
  841. {
  842. e.printStackTrace(Towny.pw);
  843. e.printStackTrace();
  844. }
  845.  
  846. return false;
  847. }
  848.  
  849. public Town getResidentTown(String name)
  850. {
  851. ResultSet rs = query("SELECT town_name FROM players WHERE name='" + name + "'");
  852.  
  853. try
  854. {
  855. if (rs != null && rs.next())
  856. {
  857. String townName = rs.getString("town_name");
  858. if (townName != null)
  859. {
  860. return getTown(townName);
  861. }
  862. }
  863. }
  864. catch (SQLException e)
  865. {
  866. e.printStackTrace(Towny.pw);
  867. e.printStackTrace();
  868. }
  869.  
  870. return null;
  871. }
  872.  
  873. public void updateTownName(Town town, String oldName)
  874. {
  875. query("CREATE TEMPORARY TABLE towns2 ENGINE=MEMORY SELECT * FROM towns WHERE name='" + oldName + "'");
  876. query("UPDATE towns2 SET name='" + town.getName() + "' WHERE name='" + oldName + "'");
  877. query("INSERT INTO towns SELECT * FROM towns2 WHERE name='" + town.getName() + "'");
  878. query("DROP TABLE towns2");
  879. query("UPDATE blocks SET town_name='" + town.getName() + "' WHERE town_name='" + oldName + "'");
  880. query("UPDATE players SET town_name='" + town.getName() + "' WHERE town_name='" + oldName + "'");
  881. query("UPDATE nations SET capital='" + town.getName() + "' WHERE capital='" + oldName + "'");
  882. query("DELETE FROM towns WHERE name='" + oldName + "'");
  883. }
  884.  
  885. public void updateNationName(Nation nation, String oldName)
  886. {
  887. query("CREATE TEMPORARY TABLE nations2 ENGINE=MEMORY SELECT * FROM nations WHERE name='" + oldName + "'");
  888. query("UPDATE nations2 SET name='" + nation.getName() + "' WHERE name='" + oldName + "'");
  889. query("INSERT INTO nations SELECT * FROM nations2 WHERE name='" + nation.getName() + "'");
  890. query("DROP TABLE nations2");
  891. query("UPDATE towns SET nation_name='" + nation.getName() + "' WHERE nation_name='" + oldName + "'");
  892. query("UPDATE players SET nation_name_1='" + nation.getName() + "' WHERE nation_name_1='" + oldName + "'");
  893. query("UPDATE players SET nation_name_2='" + nation.getName() + "' WHERE nation_name_2='" + oldName + "'");
  894. query("DELETE FROM towns WHERE name='" + oldName + "'");
  895. }
  896.  
  897. public List<String> getNPCs()
  898. {
  899. List<String> names = new ArrayList<String>();
  900.  
  901. ResultSet rs = query("SELECT name FROM players WHERE is_npc='1'");
  902.  
  903. try
  904. {
  905. if (rs != null)
  906. {
  907. while(rs.next())
  908. {
  909. String name = rs.getString("name");
  910. if (name != null)
  911. {
  912. names.add(name);
  913. }
  914. }
  915. }
  916. }
  917. catch (SQLException e)
  918. {
  919. e.printStackTrace();
  920. }
  921.  
  922. return names;
  923. }
  924. }
Add Comment
Please, Sign In to add comment