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

/me/insanj/OreMagnet/OreMagnet.java

https://bitbucket.org/insanj/oremagnet
Java | 525 lines | 336 code | 118 blank | 71 comment | 52 complexity | f3e218c0ef296af0ee01e9a0836e6ebe MD5 | raw file
  1. /*
  2. Created by Julian Weiss (insanj), updates frequent on Google+ (and sometimes Twitter)!
  3. Please do not modify or decompile at any date, but feel free to distribute with credit.
  4. Designed and created entirely on Friday, June 8th, 2011.
  5. Last edited on: 8/2/11
  6. OreMagnet Version 1.2_02!
  7. Special thanks to:
  8. Aaron Zehm, for some alpha testing and brainstorming.
  9. Matthew Weiss, for practicality-checks, resource lister, and for being an idea-bouncing wall.
  10. nossr50, for helping solve a big problem with implementing mcMMO in the Beta version.
  11. Works with the current CraftBukkit Build (#1000).
  12. All other information should be available at bukkit.org under OreMagnet.
  13. Currently supports:
  14. Permissions plugin, version 3.1.6!
  15. McMMO
  16. iConomy!
  17. THIS VERSION CURRENT HAS THREE CLASSES:
  18. OreMagnet.java
  19. OreListener.java
  20. OreServer.java
  21. */
  22. package me.insanj.OreMagnet;
  23. import java.io.BufferedWriter;
  24. import java.io.File;
  25. import java.io.FileNotFoundException;
  26. import java.io.FileWriter;
  27. import java.io.IOException;
  28. import java.io.Writer;
  29. import java.util.ArrayList;
  30. import java.util.Scanner;
  31. import java.util.Timer;
  32. import java.util.TimerTask;
  33. import java.util.logging.Logger;
  34. import org.bukkit.ChatColor;
  35. import org.bukkit.command.Command;
  36. import org.bukkit.command.CommandSender;
  37. import org.bukkit.entity.Player;
  38. import org.bukkit.event.Event;
  39. import org.bukkit.plugin.Plugin;
  40. import org.bukkit.plugin.PluginManager;
  41. import org.bukkit.plugin.java.JavaPlugin;
  42. import com.gmail.nossr50.mcMMO;
  43. import com.nijiko.permissions.PermissionHandler;
  44. import com.nijikokun.bukkit.Permissions.Permissions;
  45. import com.iConomy.*;
  46. public class OreMagnet extends JavaPlugin
  47. {
  48. private static final Logger log = Logger.getLogger("Minecraft");
  49. private final OreListener blockListener = new OreListener(this);
  50. private final OreServer serverListener = new OreServer(this);
  51. public final ArrayList<Player> MagnetUsers = new ArrayList<Player>();
  52. public static PermissionHandler permissionHandler;
  53. public static mcMMO mmoPlugin;
  54. public boolean mmoChecker;
  55. public iConomy iConomy;
  56. public static ArrayList<String> text = new ArrayList<String>();
  57. public static ArrayList<String> text2 = new ArrayList<String>();
  58. public static String directory = "plugins/OreMagnet";
  59. String version = "1.2_02";
  60. static double cooldown;
  61. static boolean runtime = true;
  62. public Integer mmoExp = 5;
  63. //When the plugin is enabled...
  64. @Override
  65. public void onEnable()
  66. {
  67. log.info("{OreMagnet} version " + version + " (by insanj) has been enabled!");
  68. PluginManager pm = getServer().getPluginManager();
  69. pm.registerEvent(Event.Type.BLOCK_DAMAGE, blockListener, Event.Priority.Normal, this);
  70. pm.registerEvent(Event.Type.PLUGIN_ENABLE, serverListener, Event.Priority.Normal, this);
  71. pm.registerEvent(Event.Type.PLUGIN_DISABLE, serverListener, Event.Priority.Normal, this);
  72. addLines();
  73. //If the config file doesn't already exist, generate the two text files.
  74. if( !(new File("plugins/OreMagnet/config.txt").exists()) ){
  75. try{
  76. //README
  77. new File(directory).mkdir();
  78. File file = new File("plugins/OreMagnet/readme.txt");
  79. Writer output = new BufferedWriter(new FileWriter(file));
  80. for(int i = 0; i < text.size(); i++)
  81. output.write(text.get(i));
  82. output.close();
  83. //MAIN CONFIG
  84. File config = new File("plugins/OreMagnet/config.txt");
  85. BufferedWriter configWriter = new BufferedWriter(new FileWriter(config));
  86. for(int i = 0; i < text2.size(); i++)
  87. configWriter.write(text2.get(i));
  88. configWriter.flush();
  89. log.info("{OreMagnet} successfully created the OreMagnet folder and containing files!");
  90. }//end try
  91. //If there's a problem generating!
  92. catch (Exception e){
  93. log.severe("{OreMagnet} had a problem creating/storing in the directory! Error: " + e.getMessage());
  94. }
  95. }//end if
  96. //If the directory DOES exist...
  97. else{
  98. boolean regenerate = false;
  99. //Reads from the config file, sees if the property "regenerate: true" exists.
  100. try{
  101. Scanner outdoors = new Scanner(new File("plugins/OreMagnet/config.txt"));
  102. while( outdoors.hasNextLine() ){
  103. String next = outdoors.nextLine();
  104. if(next.contains("regenerate: ") )
  105. regenerate = Boolean.parseBoolean(next.substring(12));
  106. }//end while
  107. }//end try
  108. catch(Exception e){
  109. log.severe("{OreMagnet} had some trouble accessing its \"config.txt\" file! >:( ");
  110. }
  111. //Regenerate as if the directory never existed, if the property tests true.
  112. if(regenerate){
  113. try{
  114. //README
  115. new File(directory).mkdir();
  116. File file = new File("plugins/OreMagnet/readme.txt");
  117. Writer output = new BufferedWriter(new FileWriter(file));
  118. for(int i = 0; i < text.size(); i++)
  119. output.write(text.get(i));
  120. output.close();
  121. //MAIN CONFIG
  122. File config = new File("plugins/OreMagnet/config.txt");
  123. BufferedWriter configWriter = new BufferedWriter(new FileWriter(config));
  124. for(int i = 0; i < text2.size(); i++)
  125. configWriter.write(text2.get(i));
  126. configWriter.close();
  127. log.info("{OreMagnet} successfully regenerated the OreMagnet folder and containing files!");
  128. }
  129. catch (Exception e){
  130. log.info("{OreMagnet} had a problem creating/storing in the directory! Error: " + e.getMessage());
  131. }
  132. }//end if
  133. }//end else
  134. setupPermissions();
  135. setupmcMMO();
  136. }
  137. @Override
  138. public void onDisable()
  139. {
  140. log.info("{OreMagnet} version " + version + " (by insanj) has been disabled!");
  141. }
  142. //When the user calls any command with "/magnet" as the beginning.
  143. @Override
  144. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
  145. {
  146. if(!(sender instanceof Player)){
  147. log.info("{OreMagnet} doesn't think it's very funny that something that isn't a Player thinks it can mine ores. :P");
  148. return true;
  149. }
  150. Player player = (Player) sender;
  151. //For just the normal OreMagnet.
  152. if( commandLabel.equalsIgnoreCase("magnet") && permissionsTester(player, new String[] {"use", "always"}) ){
  153. if(args.length == 0){
  154. toggleMagnet(player);
  155. return true;
  156. }//end args if
  157. //For setting a personal radius.
  158. else if(args.length > 1){
  159. if( (args[0].equals("radius") && args.length == 2) && permissionsTester(player, new String[] {"radius"}) ){
  160. try{
  161. if( !configContains(player.getDisplayName() + " radius: ") ){
  162. FileWriter writer = new FileWriter("plugins/OreMagnet/config.txt", true);
  163. double userRadius = Double.parseDouble(args[1]);
  164. writer.write(player.getDisplayName() + " radius: " + userRadius + "\n");
  165. player.sendMessage(ChatColor.GREEN + "Your personal radius of " + userRadius + " has been set!");
  166. writer.close();
  167. }//end if
  168. else{
  169. amendProperty( (player.getDisplayName() + " radius: "), (player.getDisplayName() + " radius: " + Double.parseDouble(args[1])) );
  170. player.sendMessage(ChatColor.GREEN + "Your personal radius of " + Double.parseDouble(args[1]) + " has been set!");
  171. }//end else
  172. }//end try
  173. catch(Exception e){
  174. System.out.println(e.getMessage());
  175. player.sendMessage(ChatColor.RED + "Uh-oh, there was something wrong with your syntax or the config file!\n" + ChatColor.GRAY + " Try: /magnet radius [#] .");
  176. }
  177. return true;
  178. }//end if
  179. //For setting a personal cooldown.
  180. else if( (args[0].equals("cooldown") && args.length == 2) && permissionsTester(player, new String[] {"cooldown"}) ){
  181. try{
  182. if( !configContains(player.getDisplayName() + " cooldown: ") ){
  183. FileWriter writer = new FileWriter("plugins/OreMagnet/config.txt", true);
  184. double userCooldown = Double.parseDouble(args[1]);
  185. writer.write(player.getDisplayName() + " cooldown: " + userCooldown + "\n");
  186. player.sendMessage(ChatColor.YELLOW + "Your personal cooldown of " + userCooldown + " has been set!");
  187. writer.close();
  188. }//end if
  189. else{
  190. amendProperty( (player.getDisplayName() + " cooldown: "), (player.getDisplayName() + " cooldown: " + Double.parseDouble(args[1])) );
  191. player.sendMessage(ChatColor.YELLOW + "Your personal cooldown of " + Double.parseDouble(args[1]) + " has been set!");
  192. }//end else
  193. }//end try
  194. catch(Exception e){
  195. player.sendMessage(ChatColor.RED + "Uh-oh, there was something wrong with your syntax or the config file!\n" + ChatColor.GRAY + " Try: /magnet cooldown [#] .");
  196. }
  197. return true;
  198. }//end else if
  199. }//end else if
  200. }//end commandlabel if
  201. return false;
  202. }//end onCommand()
  203. //Toggles whether or not the plugin is enabled for a user.
  204. private void toggleMagnet(Player sender){
  205. if( !enabled(sender) ){
  206. MagnetUsers.add(sender);
  207. sender.sendMessage(ChatColor.BLUE + "OreMagnet has been enabled! " + ChatColor.GRAY + "(/magnet)");
  208. }
  209. else {
  210. MagnetUsers.remove(sender);
  211. sender.sendMessage(ChatColor.RED + "OreMagnet has been disabled!");
  212. }
  213. }//end toggleMagnet()
  214. //Checks if the plugin is enabled for a user.
  215. public boolean enabled(Player player){
  216. return MagnetUsers.contains(player);
  217. }//end enabled()
  218. //Checks if a cooldown is still active for a given player (returns amount left).
  219. public double checkCooldown(Player player){
  220. return cooldown;
  221. }//end checkCooldown()
  222. //Resets the cooldown for a player.
  223. public TimerTask resetCooldown(Player player) throws InterruptedException{
  224. String strCooldown = "";
  225. boolean found = false;
  226. //Reads from the config, sees what the cooldown for the player is (if set).
  227. try{
  228. Scanner outdoors = new Scanner(new File("plugins/OreMagnet/config.txt"));
  229. while( outdoors.hasNextLine() ){
  230. String next = outdoors.nextLine();
  231. if(next.contains(player.getDisplayName() + " cooldown: ") ){
  232. strCooldown = next.substring(player.getDisplayName().length() + 11);
  233. found = true;
  234. }
  235. }//end while
  236. if(!found){
  237. while( outdoors.hasNextLine() ){
  238. String next = outdoors.nextLine();
  239. if(next.contains(player.getDisplayName() + "mine_cooldown: ") ){
  240. strCooldown = next.substring(15);
  241. found = true;
  242. }
  243. }//end while
  244. }//end if
  245. } catch (Exception e){
  246. log.severe("{OreMagnet} had trouble with reading a cooldown, when " + player.getDisplayName() + " used OreMagnet! Check the config!" );
  247. }
  248. try{
  249. cooldown = Double.parseDouble(strCooldown);
  250. }catch(Exception e){
  251. log.severe("{OreMagnet} uhh... It looks like the global cooldown, and/or " + player.getDisplayName() + "'s cooldown is messed up... Defaulting to 10 seconds.");
  252. cooldown = 10;
  253. }
  254. cooldownManager();
  255. return null;
  256. }//end resetCooldown()
  257. //Manages the cooldown: waits a second, then updates the cooldown, until cooldown == 0.
  258. private void cooldownManager() throws InterruptedException{
  259. Timer countdown = new Timer();
  260. while(cooldown > 0 && runtime == true){
  261. runtime = false;
  262. countdown.schedule(new minusTask(), 500);
  263. }
  264. }//end cooldownManager()
  265. //Creates a type of task that minuses the cooldown by one. Neat!
  266. class minusTask extends TimerTask {
  267. @Override
  268. public void run() {
  269. cooldown = cooldown - .5;
  270. runtime = true;
  271. try {
  272. cooldownManager();
  273. } catch (InterruptedException e) {
  274. System.out.println(e.getMessage());
  275. }
  276. }//end run
  277. }//end class TimerTask
  278. //Sets up the Permissions plugin.
  279. private void setupPermissions() {
  280. if (permissionHandler != null)
  281. return;
  282. Plugin permissionsPlugin = this.getServer().getPluginManager().getPlugin("Permissions");
  283. if (permissionsPlugin == null)
  284. {
  285. log.warning("{OreMagnet} didn't find a permissions setup, and is defaulting to OP-only.");
  286. return;
  287. }
  288. permissionHandler = ((Permissions) permissionsPlugin).getHandler();
  289. log.info("{OreMagnet} found a permissions system, and will use " + ((Permissions)permissionsPlugin).getDescription().getFullName() + "!");
  290. }//end setupPermissions()
  291. //Sets up the mcMMO plugin.
  292. private void setupmcMMO(){
  293. if (mmoPlugin != null){
  294. mmoChecker = true;
  295. return;
  296. }
  297. Plugin mcMMOChecker = this.getServer().getPluginManager().getPlugin("mcMMO");
  298. if (mcMMOChecker == null){
  299. log.info("{OreMagnet} didn't find the mcMMO plugin, but, that's cool, no problem.");
  300. mmoChecker = false;
  301. return;
  302. }
  303. try{
  304. String exp = "5";
  305. Scanner outdoors = new Scanner(new File("plugins/OreMagnet/config.txt"));
  306. while(outdoors.hasNextLine() ){
  307. String next = outdoors.nextLine();
  308. if(next.contains("mcMMO_exp: " ) )
  309. exp = next.substring(11);
  310. }//end while
  311. mmoExp = Integer.parseInt(exp);
  312. log.info("{OreMagnet} notices you're using the mcMMO plugin, that's cool, we'll give everyone " + mmoExp + " exp for every gathered mineral. (Tip: You can change this in the config file!)");
  313. }//end try
  314. catch(Exception e){
  315. log.severe("{OreMagnet} can't find the \"mcMMO exp: #\" line in the config file! I'll live but I'm not happy about it!");
  316. log.info("{OreMagnet} notices you're using the mcMMO plugin, that's cool, we'll give everyone " + mmoExp + " exp for every gathered mineral.");
  317. }
  318. mmoChecker = true;
  319. }//end setupMMO()
  320. public boolean permissionsTester(Player player, String[] nodes){
  321. if( permissionHandler == null )
  322. if( player.isOp() )
  323. return true;
  324. else
  325. for(int i = 0; nodes.length > i; i++ )
  326. if( OreMagnet.permissionHandler.has(player, ("OreMagnet." + nodes[i])) )
  327. return true;
  328. return false;
  329. }//end permissionsTester()
  330. public void addLines(){
  331. text.add("OREMAGNET was created entirely by Julian (insanj) Weiss. Please do not modify, but feel free to distribute with credit.");
  332. text.add("\nAll needed information for this plugin can be found in the Bukkit forums under OreMagnet.");
  333. text.add("\nFrequent updates and news can be found on Google+ (and sometimes Twitter) from Julian (insanj) Weiss!");
  334. text.add("\n\nThis is an automatically generated configuration folder, created by OreMagnet.");
  335. text.add("\nIf you are not familiar with the formatting and abilities that the files in here contain, please do not modify them.");
  336. text.add("\nAlso, if, for some reason, you feel these files are fautly, you can change the regenerate property in the config file, or just delete the entire folder.");
  337. text.add("\nAfter reading this file, feel free to delete it, or even add more information, but every time there is a file regeneration, it will be automatically recreated.");
  338. text2.add("This is an automatically generated configuration file, that holds all the OreMagnet persistant information.");
  339. text2.add("\nTampering with this file directly is highly unadvised, and can cause major errors with OreMagnet!");
  340. text2.add("\nChanging the \"regenerate: false\" property to \"true\" will make this file regenerate from default.");
  341. text2.add("\nIF YOU WANT TO ADD ANNOTATIONS, OR ANY COMMENTS, JUST ADD THEM FREELY ON A SEPERATE LINE FROM THE PROPERTIES, NO # OR ANYTHING NEEDED!");
  342. text2.add("\n\nmine_minerals: 21, 16, 14, 56, 73, 74, 15");
  343. text2.add("\nmine_tools: 257, 270, 274, 278, 285");
  344. text2.add("\ndurability_loss: 15");
  345. text2.add("\nmine_radius: 6");
  346. text2.add("\nmine_cooldown: 10");
  347. text2.add("\nregenerate: false");
  348. text2.add("\n\nThe amount chosen below for the iConomy price will be subtracted from the account that is under the user's name.");
  349. text2.add("\nmcMMO_exp: 10");
  350. text2.add("\niConomy_price: 0\n\n");
  351. }//end addLines()
  352. public boolean configContains(String check) throws FileNotFoundException{
  353. Scanner outdoors = new Scanner(new File("plugins/OreMagnet/config.txt"));
  354. while( outdoors.hasNextLine() )
  355. if( outdoors.nextLine().contains(check))
  356. return true;
  357. return false;
  358. }//end configContains()
  359. //Replaces a line that contains the "before" with the "after" line, by rewriting the file through an array.
  360. public void amendProperty(String before, String after) throws IOException{
  361. boolean multiple = false;
  362. File config = new File("plugins/OreMagnet/config.txt");
  363. Scanner outdoors = new Scanner(config);
  364. ArrayList<String> contents = new ArrayList<String>();
  365. while(outdoors.hasNextLine())
  366. contents.add(outdoors.nextLine());
  367. for(int i = 0; i < contents.size(); i++){
  368. if(contents.get(i).contains(before)){
  369. if(multiple == false){
  370. contents.set(i, after);
  371. multiple = true;
  372. }else
  373. contents.remove(i);
  374. }//end if
  375. }//end for
  376. FileWriter configWriter = new FileWriter(config, false);
  377. for(int i = 0; i < contents.size(); i++)
  378. configWriter.write(contents.get(i) + "\n");
  379. configWriter.close();
  380. }//end amendProperty()
  381. }//end class
  382. /****************************Current Contents of the PLUGIN.YML**************************
  383. name: OreMagnet
  384. main: me.insanj.OreMagnet.OreMagnet
  385. author: insanj
  386. version: 1.1
  387. commands:
  388. magnet:
  389. description: Smoothly removes all the adjacent materials/blocks from a hit block, with a ton of malleable properties.
  390. permissions: |
  391. -"OreMagnet.use"
  392. -"OreMagnet.radius"
  393. -"OreMagnet.cooldown"
  394. -"OreMagnet.always"
  395. usage: |
  396. /<command>
  397. /<command> radius [#]
  398. /<command> cooldown [#]
  399. *****************************************************************************************/