/src/littlegruz/autoruncommands/CommandMain.java

https://github.com/LittleGruz/Autorun-Commands · Java · 639 lines · 509 code · 98 blank · 32 comment · 28 complexity · b1fb02cc85164a638882b99f80a62b8f MD5 · raw file

  1. package littlegruz.autoruncommands;
  2. import java.io.BufferedReader;
  3. import java.io.BufferedWriter;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileReader;
  7. import java.io.FileWriter;
  8. import java.io.IOException;
  9. import java.util.Date;
  10. import java.util.HashMap;
  11. import java.util.Iterator;
  12. import java.util.Map;
  13. import java.util.StringTokenizer;
  14. import java.util.Map.Entry;
  15. import java.util.logging.Logger;
  16. import littlegruz.autoruncommands.commands.Blocks;
  17. import littlegruz.autoruncommands.commands.Commands;
  18. import littlegruz.autoruncommands.commands.Config;
  19. import littlegruz.autoruncommands.commands.Death;
  20. import littlegruz.autoruncommands.commands.Join;
  21. import littlegruz.autoruncommands.commands.Repeat;
  22. import littlegruz.autoruncommands.commands.Respawn;
  23. import littlegruz.autoruncommands.commands.RightClick;
  24. import littlegruz.autoruncommands.commands.Startup;
  25. import littlegruz.autoruncommands.listeners.CommandBlockListener;
  26. import littlegruz.autoruncommands.listeners.CommandEntityListener;
  27. import littlegruz.autoruncommands.listeners.CommandPlayerListener;
  28. import littlegruz.autoruncommands.listeners.CommandServerListener;
  29. import org.bukkit.Location;
  30. import org.bukkit.command.CommandSender;
  31. import org.bukkit.plugin.java.JavaPlugin;
  32. public class CommandMain extends JavaPlugin{
  33. Logger log = Logger.getLogger("This is MINECRAFT!");
  34. private HashMap<String, String> playerCommandMap;
  35. private HashMap<Location, String> blockCommandMap;
  36. private HashMap<String, String> commandMap;
  37. private HashMap<String, Location> playerPosMap;
  38. private HashMap<String, String> deathCommandMap;
  39. private HashMap<String, String> respawnCommandMap;
  40. private HashMap<String, Integer> repeatCommandMap;
  41. private HashMap<String, String> runningRepeatCommandMap;
  42. private HashMap<String, String> joinCommandMap;
  43. private File playerFile;
  44. private File commandFile;
  45. private File blockFile;
  46. private File deathFile;
  47. private File respawnFile;
  48. private File startupFile;
  49. private File repeatFile;
  50. private File joinFile;
  51. private File remainderFile;
  52. private boolean placeBlock;
  53. private boolean startupDone;
  54. private boolean firstJoin;
  55. private String blockCommand;
  56. private String startupCommands;
  57. private int joinDelay;
  58. public void onDisable(){
  59. //Save player data
  60. try{
  61. BufferedWriter bw = new BufferedWriter(new FileWriter(playerFile));
  62. Iterator<Map.Entry<String, String>> it = playerCommandMap.entrySet().iterator();
  63. //Save the players and corresponding commands
  64. bw.write("<Player> <Command>\n");
  65. while(it.hasNext()){
  66. Entry<String, String> mp = it.next();
  67. bw.write(mp.getKey() + " " + mp.getValue() + "\n");
  68. }
  69. bw.close();
  70. }catch(IOException e){
  71. log.info("Error saving player command file");
  72. }
  73. //Save block data
  74. try{
  75. BufferedWriter bw = new BufferedWriter(new FileWriter(blockFile));
  76. Iterator<Map.Entry<Location, String>> it = blockCommandMap.entrySet().iterator();
  77. //Save the blocks and corresponding commands
  78. bw.write("<Block Location> <Command>\n");
  79. while(it.hasNext()){
  80. Entry<Location, String> mp = it.next();
  81. bw.write(mp.getKey().getWorld().getName() + " "
  82. + Double.toString(mp.getKey().getX()) + " "
  83. + Double.toString(mp.getKey().getY()) + " "
  84. + Double.toString(mp.getKey().getZ()) + " "
  85. + mp.getValue() + "\n");
  86. }
  87. bw.close();
  88. }catch(IOException e){
  89. log.info("Error saving block command file");
  90. }
  91. //Save player death data
  92. try{
  93. BufferedWriter bw = new BufferedWriter(new FileWriter(deathFile));
  94. Iterator<Map.Entry<String, String>> it = deathCommandMap.entrySet().iterator();
  95. //Save the players and corresponding commands
  96. bw.write("<Player> <Command>\n");
  97. while(it.hasNext()){
  98. Entry<String, String> mp = it.next();
  99. bw.write(mp.getKey() + " " + mp.getValue() + "\n");
  100. }
  101. bw.close();
  102. }catch(IOException e){
  103. log.info("Error saving player death command file");
  104. }
  105. //Save player respawn data
  106. try{
  107. BufferedWriter bw = new BufferedWriter(new FileWriter(respawnFile));
  108. Iterator<Map.Entry<String, String>> it = respawnCommandMap.entrySet().iterator();
  109. //Save the players and corresponding commands
  110. bw.write("<Player> <Command>\n");
  111. while(it.hasNext()){
  112. Entry<String, String> mp = it.next();
  113. bw.write(mp.getKey() + " " + mp.getValue() + "\n");
  114. }
  115. bw.close();
  116. }catch(IOException e){
  117. log.info("Error saving player respawn command file");
  118. }
  119. //Save player join data
  120. try{
  121. BufferedWriter bw = new BufferedWriter(new FileWriter(joinFile));
  122. Iterator<Map.Entry<String, String>> it = joinCommandMap.entrySet().iterator();
  123. //Save the join and first join commands
  124. bw.write("<Command> <Join>\n");
  125. while(it.hasNext()){
  126. Entry<String, String> mp = it.next();
  127. bw.write(mp.getKey() + " " + mp.getValue() + "\n");
  128. }
  129. bw.close();
  130. }catch(IOException e){
  131. log.info("Error saving player join command file");
  132. }
  133. //Save server start up data
  134. try{
  135. BufferedWriter bw = new BufferedWriter(new FileWriter(startupFile));
  136. StringTokenizer st = new StringTokenizer(startupCommands, ":");
  137. while(st.countTokens() > 0){
  138. bw.write(st.nextToken() + "\n");
  139. }
  140. bw.close();
  141. }catch(IOException e){
  142. log.info("Error saving server start up command file");
  143. }
  144. //Save repeating task remainder times
  145. try{
  146. BufferedWriter bw = new BufferedWriter(new FileWriter(remainderFile));
  147. Iterator<Map.Entry<String, String>> it = runningRepeatCommandMap.entrySet().iterator();
  148. StringTokenizer st;
  149. long time, prevTime;
  150. bw.write("<Command> <Remainder>\n");
  151. while(it.hasNext()){
  152. Entry<String, String> mp = it.next();
  153. st = new StringTokenizer(mp.getValue(), "|");
  154. time = new Date().getTime();
  155. /* Skip over the first token because it is the second one that is desired */
  156. st.nextToken();
  157. prevTime = Long.parseLong(st.nextToken());
  158. time /= 1000;
  159. // Get the time left until the command will run again
  160. time = repeatCommandMap.get(mp.getKey()) - ((time - prevTime) % repeatCommandMap.get(mp.getKey()));
  161. bw.write(mp.getKey() + " " + time + "\n");
  162. }
  163. bw.close();
  164. }catch(IOException e){
  165. log.info("Error saving repeating task remaining time");
  166. }
  167. //Save repeat command data
  168. try{
  169. BufferedWriter bw = new BufferedWriter(new FileWriter(repeatFile));
  170. Iterator<Map.Entry<String, Integer>> it = repeatCommandMap.entrySet().iterator();
  171. //Save the players and corresponding commands
  172. bw.write("<Command> <Interval>\n");
  173. while(it.hasNext()){
  174. Entry<String, Integer> mp = it.next();
  175. bw.write(mp.getKey() + " " + mp.getValue().toString() + "\n");
  176. }
  177. bw.close();
  178. }catch(IOException e){
  179. log.info("Error saving player repeat command file");
  180. }
  181. //Save command data
  182. try{
  183. BufferedWriter bw = new BufferedWriter(new FileWriter(commandFile));
  184. Iterator<Map.Entry<String, String>> it = commandMap.entrySet().iterator();
  185. //Save the blocks and corresponding commands
  186. bw.write("<Identifing name> <Command>\n");
  187. while(it.hasNext()){
  188. Entry<String, String> mp = it.next();
  189. bw.write(mp.getKey() + " " + mp.getValue() + "\n");
  190. }
  191. bw.close();
  192. }catch(IOException e){
  193. log.info("Error saving command file");
  194. }
  195. saveConfig();
  196. log.info(this.toString() + " disabled");
  197. }
  198. public void onEnable(){
  199. //Create the directory and files if needed
  200. new File(getDataFolder().toString()).mkdir();
  201. playerFile = new File(getDataFolder().toString() + "/playerList.txt");
  202. commandFile = new File(getDataFolder().toString() + "/commands.txt");
  203. blockFile = new File(getDataFolder().toString() + "/blockList.txt");
  204. deathFile = new File(getDataFolder().toString() + "/deathList.txt");
  205. respawnFile = new File(getDataFolder().toString() + "/respawnList.txt");
  206. startupFile = new File(getDataFolder().toString() + "/startupCommands.txt");
  207. repeatFile = new File(getDataFolder().toString() + "/repeatList.txt");
  208. joinFile = new File(getDataFolder().toString() + "/joinList.txt");
  209. remainderFile = new File(getDataFolder().toString() + "/taskRemainder.txt");
  210. //Load the player file data
  211. playerCommandMap = new HashMap<String, String>();
  212. try{
  213. BufferedReader br = new BufferedReader(new FileReader(playerFile));
  214. StringTokenizer st;
  215. String input;
  216. String name;
  217. String command;
  218. while((input = br.readLine()) != null){
  219. if(input.compareToIgnoreCase("<Player> <Command>") == 0){
  220. continue;
  221. }
  222. st = new StringTokenizer(input, " ");
  223. name = st.nextToken();
  224. command = st.nextToken();
  225. playerCommandMap.put(name, command);
  226. }
  227. br.close();
  228. }catch(FileNotFoundException e){
  229. log.info("No original player command file, creating new one.");
  230. }catch(IOException e){
  231. log.info("Error reading player command file");
  232. }catch(Exception e){
  233. log.info("Incorrectly formatted player command file");
  234. }
  235. //Load the block file data
  236. blockCommandMap = new HashMap<Location, String>();
  237. try{
  238. BufferedReader br = new BufferedReader(new FileReader(blockFile));
  239. StringTokenizer st;
  240. String input;
  241. String command;
  242. Location loc = null;
  243. while((input = br.readLine()) != null){
  244. if(input.compareToIgnoreCase("<Block Location> <Command>") == 0){
  245. continue;
  246. }
  247. st = new StringTokenizer(input, " ");
  248. loc = new Location(getServer().getWorld(st.nextToken()), Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken()));
  249. command = st.nextToken();
  250. blockCommandMap.put(loc, command);
  251. }
  252. br.close();
  253. }catch(FileNotFoundException e){
  254. log.info("No original block command file, creating new one.");
  255. }catch(IOException e){
  256. log.info("Error reading block command file");
  257. }catch(Exception e){
  258. log.info("Incorrectly formatted block command file");
  259. }
  260. //Load the player death file data
  261. deathCommandMap = new HashMap<String, String>();
  262. try{
  263. BufferedReader br = new BufferedReader(new FileReader(deathFile));
  264. StringTokenizer st;
  265. String input;
  266. String name;
  267. String command;
  268. while((input = br.readLine()) != null){
  269. if(input.compareToIgnoreCase("<Player> <Command>") == 0){
  270. continue;
  271. }
  272. st = new StringTokenizer(input, " ");
  273. name = st.nextToken();
  274. command = st.nextToken();
  275. deathCommandMap.put(name, command);
  276. }
  277. br.close();
  278. }catch(FileNotFoundException e){
  279. log.info("No original player death command file, creating new one.");
  280. }catch(IOException e){
  281. log.info("Error reading player death command file");
  282. }catch(Exception e){
  283. log.info("Incorrectly formatted player death command file");
  284. }
  285. //Load the player respawn file data
  286. respawnCommandMap = new HashMap<String, String>();
  287. try{
  288. BufferedReader br = new BufferedReader(new FileReader(respawnFile));
  289. StringTokenizer st;
  290. String input;
  291. String name;
  292. String command;
  293. while((input = br.readLine()) != null){
  294. if(input.compareToIgnoreCase("<Player> <Command>") == 0){
  295. continue;
  296. }
  297. st = new StringTokenizer(input, " ");
  298. name = st.nextToken();
  299. command = st.nextToken();
  300. respawnCommandMap.put(name, command);
  301. }
  302. br.close();
  303. }catch(FileNotFoundException e){
  304. log.info("No original player respawn command file, creating new one.");
  305. }catch(IOException e){
  306. log.info("Error reading player respawn command file");
  307. }catch(Exception e){
  308. log.info("Incorrectly formatted player respawn command file");
  309. }
  310. //Load the player join file data
  311. joinCommandMap = new HashMap<String, String>();
  312. try{
  313. BufferedReader br = new BufferedReader(new FileReader(joinFile));
  314. StringTokenizer st;
  315. String input;
  316. while((input = br.readLine()) != null){
  317. if(input.compareToIgnoreCase("<Command> <Join>") == 0){
  318. continue;
  319. }
  320. st = new StringTokenizer(input, " ");
  321. joinCommandMap.put(st.nextToken(), st.nextToken());
  322. }
  323. br.close();
  324. }catch(FileNotFoundException e){
  325. log.info("No original player join command file, creating new one.");
  326. }catch(IOException e){
  327. log.info("Error reading player join command file");
  328. }catch(Exception e){
  329. log.info("Incorrectly formatted player join command file");
  330. }
  331. //Load the start up data
  332. startupCommands = "";
  333. try{
  334. BufferedReader br = new BufferedReader(new FileReader(startupFile));
  335. String input;
  336. while((input = br.readLine()) != null){
  337. if(input.compareToIgnoreCase("<Command>") == 0){
  338. continue;
  339. }
  340. startupCommands += ":" + input;
  341. }
  342. br.close();
  343. }catch(FileNotFoundException e){
  344. log.info("No original start up command file, creating new one.");
  345. }catch(IOException e){
  346. log.info("Error reading start up command file");
  347. }catch(Exception e){
  348. log.info("Incorrectly formatted start up command file");
  349. }
  350. //Load the repeat command file data
  351. repeatCommandMap = new HashMap<String, Integer>();
  352. try{
  353. BufferedReader br = new BufferedReader(new FileReader(repeatFile));
  354. StringTokenizer st;
  355. String input;
  356. //Assumes that the name is only one token long
  357. while((input = br.readLine()) != null){
  358. if(input.compareToIgnoreCase("<Command> <Interval>") == 0){
  359. continue;
  360. }
  361. st = new StringTokenizer(input, " ");
  362. repeatCommandMap.put(st.nextToken(), Integer.parseInt(st.nextToken()));
  363. }
  364. br.close();
  365. }catch(FileNotFoundException e){
  366. log.info("No original repeat command file, creating new one.");
  367. }catch(IOException e){
  368. log.info("Error reading repeat command file");
  369. }catch(Exception e){
  370. log.info("Incorrectly formatted repeat command file");
  371. }
  372. //Load the command file data
  373. commandMap = new HashMap<String, String>();
  374. try{
  375. BufferedReader br = new BufferedReader(new FileReader(commandFile));
  376. StringTokenizer st;
  377. String input;
  378. String args;
  379. String name;
  380. //Assumes that the name is only one token long
  381. while((input = br.readLine()) != null){
  382. if(input.compareToIgnoreCase("<Identifing name> <Command>") == 0){
  383. continue;
  384. }
  385. st = new StringTokenizer(input, " ");
  386. name = st.nextToken();
  387. args = st.nextToken();
  388. while(st.hasMoreTokens()){
  389. args += " " + st.nextToken();
  390. }
  391. commandMap.put(name, args);
  392. }
  393. br.close();
  394. }catch(FileNotFoundException e){
  395. log.info("No original command file, creating new one.");
  396. }catch(IOException e){
  397. log.info("Error reading command file");
  398. }catch(Exception e){
  399. log.info("Incorrectly formatted command file");
  400. }
  401. placeBlock = false;
  402. startupDone = false;
  403. blockCommand = "";
  404. playerPosMap = new HashMap<String, Location>();
  405. runningRepeatCommandMap = new HashMap<String, String>();
  406. // Pulling data from config.yml
  407. if(getConfig().isBoolean("first_join"))
  408. firstJoin = getConfig().getBoolean("first_join");
  409. else
  410. firstJoin = true;
  411. if(getConfig().isInt("join_delay"))
  412. joinDelay = getConfig().getInt("join_delay");
  413. else
  414. joinDelay = 0;
  415. //Set up the listeners
  416. getServer().getPluginManager().registerEvents(new CommandPlayerListener(this), this);
  417. getServer().getPluginManager().registerEvents(new CommandBlockListener(this), this);
  418. getServer().getPluginManager().registerEvents(new CommandEntityListener(this), this);
  419. getServer().getPluginManager().registerEvents(new CommandServerListener(this), this);
  420. //Set up the commands
  421. getCommand("setclickcommand").setExecutor(new RightClick(this));
  422. getCommand("removeclickcommand").setExecutor(new RightClick(this));
  423. getCommand("displayclickcommand").setExecutor(new RightClick(this));
  424. getCommand("setdeathcommand").setExecutor(new Death(this));
  425. getCommand("removedeathcommand").setExecutor(new Death(this));
  426. getCommand("displaydeathcommand").setExecutor(new Death(this));
  427. getCommand("addrepeatcommand").setExecutor(new Repeat(this));
  428. getCommand("removerepeatcommand").setExecutor(new Repeat(this));
  429. getCommand("displayrepeatcommands").setExecutor(new Repeat(this));
  430. getCommand("addjoincommand").setExecutor(new Join(this));
  431. getCommand("addfirstjoincommand").setExecutor(new Join(this));
  432. getCommand("removejoincommand").setExecutor(new Join(this));
  433. getCommand("displayjoincommands").setExecutor(new Join(this));
  434. getCommand("setjoincommanddelay").setExecutor(new Join(this));
  435. getCommand("addstartupcommand").setExecutor(new Startup(this));
  436. getCommand("removestartupcommand").setExecutor(new Startup(this));
  437. getCommand("displaystartupcommands").setExecutor(new Startup(this));
  438. getCommand("setrespawncommand").setExecutor(new Respawn(this));
  439. getCommand("removerespawncommand").setExecutor(new Respawn(this));
  440. getCommand("displayrespawncommand").setExecutor(new Respawn(this));
  441. getCommand("setcommandblock").setExecutor(new Blocks(this));
  442. getCommand("displaycommandblocks").setExecutor(new Blocks(this));
  443. getCommand("addacommand").setExecutor(new Commands(this));
  444. getCommand("addopcommand").setExecutor(new Commands(this));
  445. getCommand("removeacommand").setExecutor(new Commands(this));
  446. getCommand("displaycommands").setExecutor(new Commands(this));
  447. getCommand("reloadautorunconfig").setExecutor(new Config(this));
  448. getCommand("toggleFirstJoinCommands").setExecutor(new Config(this));
  449. log.info(this.toString() + " enabled");
  450. }
  451. public void removeStartupCommand(CommandSender sender, String command){
  452. if(startupCommands.contains(":" + command)){
  453. startupCommands = startupCommands.replace(":" + command, "");
  454. sender.sendMessage("Command removal successful");
  455. }
  456. else{
  457. sender.sendMessage("No command set with that identifier");
  458. sender.sendMessage("Check with \'/displaystartupcommands' first");
  459. }
  460. }
  461. public boolean addStartupCommand(CommandSender sender, String command){
  462. if(startupCommands.contains(command)){
  463. sender.sendMessage("That command is already in use");
  464. return false;
  465. }
  466. else{
  467. startupCommands += ":" + command;
  468. sender.sendMessage("Command association successful");
  469. return true;
  470. }
  471. }
  472. public HashMap<String, String> getPlayerClickMap(){
  473. return playerCommandMap;
  474. }
  475. public HashMap<String, String> getPlayerDeathMap(){
  476. return deathCommandMap;
  477. }
  478. public HashMap<String, String> getPlayerRespawnMap(){
  479. return respawnCommandMap;
  480. }
  481. public HashMap<Location, String> getBlockCommandMap(){
  482. return blockCommandMap;
  483. }
  484. public HashMap<String, Integer> getRepeatMap(){
  485. return repeatCommandMap;
  486. }
  487. public HashMap<String, String> getRunningRepeatMap(){
  488. return runningRepeatCommandMap;
  489. }
  490. public HashMap<String, String> getCommandMap(){
  491. return commandMap;
  492. }
  493. public boolean isPlaceBlock(){
  494. return placeBlock;
  495. }
  496. public void setPlaceBlock(boolean placeBlock){
  497. this.placeBlock = placeBlock;
  498. }
  499. public String getBlockCommand(){
  500. return blockCommand;
  501. }
  502. public void setBlockCommand(String blockCommand){
  503. this.blockCommand = blockCommand;
  504. }
  505. public HashMap<String, Location> getPlayerPosMap() {
  506. return playerPosMap;
  507. }
  508. public String getStartupCommands() {
  509. return startupCommands;
  510. }
  511. public void setStartupCommands(String sc) {
  512. this.startupCommands = sc;
  513. }
  514. public boolean isStartupDone() {
  515. return startupDone;
  516. }
  517. public void setStartupDone(boolean startupDone) {
  518. this.startupDone = startupDone;
  519. }
  520. public HashMap<String, String> getPlayerJoinMap() {
  521. return joinCommandMap;
  522. }
  523. public File getRemainderFile(){
  524. return remainderFile;
  525. }
  526. public HashMap<String, Integer> getRepeatCommandMap(){
  527. return repeatCommandMap;
  528. }
  529. public void setFirstJoin(boolean firstJoin){
  530. this.firstJoin = firstJoin;
  531. }
  532. public boolean isFirstJoin(){
  533. return firstJoin;
  534. }
  535. public void setJoinDelay(int joinDelay){
  536. this.joinDelay = joinDelay;
  537. }
  538. public int getJoinDelay(){
  539. return joinDelay;
  540. }
  541. }