m.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /*
  2. This file is part of mcMMO.
  3. mcMMO is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. mcMMO is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with mcMMO. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. package com.gmail.nossr50;
  15. import java.io.BufferedReader;
  16. import java.io.FileReader;
  17. import java.util.logging.Level;
  18. import java.util.logging.Logger;
  19. import org.bukkit.Bukkit;
  20. import org.bukkit.Location;
  21. import org.bukkit.Material;
  22. import org.bukkit.block.Block;
  23. import org.bukkit.entity.*;
  24. import org.bukkit.inventory.ItemStack;
  25. import com.gmail.nossr50.config.*;
  26. import com.gmail.nossr50.datatypes.PlayerProfile;
  27. import com.gmail.nossr50.datatypes.SkillType;
  28. import com.gmail.nossr50.events.FakeBlockBreakEvent;
  29. import com.gmail.nossr50.events.McMMOItemSpawnEvent;
  30. public class m
  31. {
  32. public static final Logger log = Logger.getLogger("Minecraft");
  33. /*
  34. * I'm storing my misc functions/methods in here in an unorganized manner. Spheal with it.
  35. * This is probably the most embarrassing part of my code for mcMMO
  36. * I really should find an organized place for these things!
  37. */
  38. public static String getCapitalized(String target)
  39. {
  40. String firstLetter = target.substring(0,1);
  41. String remainder = target.substring(1);
  42. String capitalized = firstLetter.toUpperCase() + remainder.toLowerCase();
  43. return capitalized;
  44. }
  45. public static int getInt(String string)
  46. {
  47. if(isInt(string))
  48. {
  49. return Integer.parseInt(string);
  50. }
  51. else
  52. {
  53. return 0;
  54. }
  55. }
  56. public static Double getDouble(String string)
  57. {
  58. if(isDouble(string))
  59. {
  60. return Double.parseDouble(string);
  61. }
  62. else
  63. {
  64. return (double) 0;
  65. }
  66. }
  67. public static boolean isDouble(String string)
  68. {
  69. try
  70. {
  71. Double.parseDouble(string);
  72. }
  73. catch(NumberFormatException nFE) {
  74. return false;
  75. }
  76. return true;
  77. }
  78. public static boolean shouldBeWatched(Block block)
  79. {
  80. int id = block.getTypeId();
  81. return shouldBeWatched(id);
  82. }
  83. public static boolean shouldBeWatched(int id) {
  84. return id == 2 || id == 3 || id == 12 || id == 13 || id == 82 || //Excavation
  85. id == 1 || id == 14 || id == 15 || id == 16 || id == 21 || id == 24 || id == 49 || id == 56 || id == 73 || id == 74 || id == 87 || id == 89 || id == 112 || id == 121 || id == 48 || id == 98 || //Mining
  86. id == 17 || id == 37 || id == 38 || id == 39 || id == 40 || id == 81 || id == 83 || id == 86 || id == 91 || id == 103 || id == 106 || id == 111 || //Woodcutting & Herbalism
  87. id == LoadProperties.anvilID; //Anvil
  88. }
  89. public static int getPowerLevel(Player player)
  90. {
  91. PlayerProfile PP = Users.getProfile(player);
  92. int x = 0;
  93. if(mcPermissions.getInstance().taming(player))
  94. x+=PP.getSkillLevel(SkillType.TAMING);
  95. if(mcPermissions.getInstance().mining(player))
  96. x+=PP.getSkillLevel(SkillType.MINING);
  97. if(mcPermissions.getInstance().woodcutting(player))
  98. x+=PP.getSkillLevel(SkillType.WOODCUTTING);
  99. if(mcPermissions.getInstance().unarmed(player))
  100. x+=PP.getSkillLevel(SkillType.UNARMED);
  101. if(mcPermissions.getInstance().herbalism(player))
  102. x+=PP.getSkillLevel(SkillType.HERBALISM);
  103. if(mcPermissions.getInstance().excavation(player))
  104. x+=PP.getSkillLevel(SkillType.EXCAVATION);
  105. if(mcPermissions.getInstance().archery(player))
  106. x+=PP.getSkillLevel(SkillType.ARCHERY);
  107. if(mcPermissions.getInstance().swords(player))
  108. x+=PP.getSkillLevel(SkillType.SWORDS);
  109. if(mcPermissions.getInstance().axes(player))
  110. x+=PP.getSkillLevel(SkillType.AXES);
  111. if(mcPermissions.getInstance().acrobatics(player))
  112. x+=PP.getSkillLevel(SkillType.ACROBATICS);
  113. if(mcPermissions.getInstance().repair(player))
  114. x+=PP.getSkillLevel(SkillType.REPAIR);
  115. if(mcPermissions.getInstance().fishing(player))
  116. x+=PP.getSkillLevel(SkillType.FISHING);
  117. return x;
  118. }
  119. public static boolean blockBreakSimulate(Block block, Player player)
  120. {
  121. FakeBlockBreakEvent event = new FakeBlockBreakEvent(block, player);
  122. if(block != null && player != null){
  123. Bukkit.getServer().getPluginManager().callEvent(event);
  124. if(!event.isCancelled())
  125. {
  126. return true; //Return true if not cancelled
  127. } else {
  128. return false; //Return false if cancelled
  129. }
  130. } else {
  131. return false; //Return false if something went wrong
  132. }
  133. }
  134. public static Integer getTier(Player player)
  135. {
  136. int i = player.getItemInHand().getTypeId();
  137. if(i == 268 || i == 269 || i == 270 || i == 271 || i == 290){
  138. return 1; //WOOD
  139. } else if (i == 272 || i == 273 || i == 274 || i == 275 || i == 291){
  140. return 2; //STONE
  141. } else if (i == 256 || i == 257 || i == 258 || i == 267 || i == 292){
  142. return 3; //IRON
  143. } else if (i == 283 || i == 284 || i == 285 || i == 286 || i == 294){
  144. return 1; //GOLD
  145. } else if (i == 276 || i == 277 || i == 278 || i == 279 || i == 293){
  146. return 4; //DIAMOND
  147. } else {
  148. return 1; //UNRECOGNIZED
  149. }
  150. }
  151. public static double getDistance(Location loca, Location locb)
  152. {
  153. return Math.sqrt(Math.pow(loca.getX() - locb.getX(), 2) + Math.pow(loca.getY() - locb.getY(), 2)
  154. + Math.pow(loca.getZ() - locb.getZ(), 2));
  155. }
  156. public static boolean abilityBlockCheck(Block block)
  157. {
  158. int i = block.getTypeId();
  159. if(i == 107 ||i == 117 || i == 116 || i == 96 || i == 68 || i == 355 || i == 26 || i == 323 || i == 25 || i == 54 || i == 69 || i == 92 || i == 77 || i == 58 || i == 61 || i == 62 || i == LoadProperties.anvilID || i == 71 || i == 64 || i == 84 || i == 324 || i == 330){
  160. return false;
  161. } else {
  162. return true;
  163. }
  164. }
  165. public static boolean isBlockAround(Location loc, Integer radius, Integer typeid)
  166. {
  167. Block blockx = loc.getBlock();
  168. int ox = blockx.getX();
  169. int oy = blockx.getY();
  170. int oz = blockx.getZ();
  171. for (int cx = -radius; cx <= radius; cx++) {
  172. for (int cy = -radius; cy <= radius; cy++) {
  173. for (int cz = -radius; cz <= radius; cz++) {
  174. Block block = loc.getWorld().getBlockAt(ox + cx, oy + cy, oz + cz);
  175. if (block.getTypeId() == typeid) {
  176. return true;
  177. }
  178. }
  179. }
  180. }
  181. return false;
  182. }
  183. public static Integer calculateHealth(Integer health, Integer newvalue){
  184. if((health + newvalue) > 20){
  185. return 20;
  186. } else {
  187. return health+newvalue;
  188. }
  189. }
  190. public Integer calculateMinusHealth(Integer health, Integer newvalue){
  191. if((health - newvalue) < 1){
  192. return 0;
  193. } else {
  194. return health-newvalue;
  195. }
  196. }
  197. public static boolean isInt(String string)
  198. {
  199. try
  200. {
  201. Integer.parseInt(string);
  202. }
  203. catch(NumberFormatException nFE)
  204. {
  205. return false;
  206. }
  207. return true;
  208. }
  209. public static void mcDropItem(Location location, int id)
  210. {
  211. if(location == null) return;
  212. Material mat = Material.getMaterial(id);
  213. ItemStack item = new ItemStack(mat, 1, (byte) 0, (byte) 0);
  214. mcDropItem(location, item);
  215. }
  216. public static void mcDropItem(Location location, ItemStack itemStack) {
  217. // We can't get the item until we spawn it and we want to make it cancellable, so we have a custom event.
  218. McMMOItemSpawnEvent event = new McMMOItemSpawnEvent(location, itemStack);
  219. Bukkit.getPluginManager().callEvent(event);
  220. if(event.isCancelled()) return;
  221. location.getWorld().dropItemNaturally(location, itemStack);
  222. }
  223. public static boolean isSwords(ItemStack is)
  224. {
  225. int id = is.getTypeId();
  226. return id == 268 || id == 267 || id == 272 || id == 283 || id == 276;
  227. }
  228. public static boolean isHoe(ItemStack is)
  229. {
  230. int id = is.getTypeId();
  231. return id == 290 || id == 291 || id == 292 || id == 293 || id == 294;
  232. }
  233. public static boolean isShovel(ItemStack is)
  234. {
  235. int id = is.getTypeId();
  236. return id == 269 || id == 273 || id == 277 || id == 284 || id == 256;
  237. }
  238. public static boolean isAxes(ItemStack is)
  239. {
  240. int id = is.getTypeId();
  241. return id == 271 || id == 258 || id == 286 || id == 279 || id == 275;
  242. }
  243. public static boolean isMiningPick(ItemStack is)
  244. {
  245. int id = is.getTypeId();
  246. return id == 270 || id == 274 || id == 285 || id == 257 || id == 278;
  247. }
  248. public static void convertToMySQL()
  249. {
  250. if(!LoadProperties.useMySQL)
  251. return;
  252. Bukkit.getScheduler().scheduleAsyncDelayedTask(Bukkit.getServer().getPluginManager().getPlugin("mcMMO"), new Runnable(){
  253. public void run() {
  254. String location = "plugins/mcMMO/FlatFileStuff/mcmmo.users";
  255. try {
  256. //Open the user file
  257. FileReader file = new FileReader(location);
  258. BufferedReader in = new BufferedReader(file);
  259. String line = "";
  260. String playerName = null, mining = null, party = null, miningXP = null, woodcutting = null, woodCuttingXP = null, repair = null, unarmed = null, herbalism = null, excavation = null, archery = null, swords = null, axes = null, acrobatics = null, repairXP = null, unarmedXP = null, herbalismXP = null, excavationXP = null, archeryXP = null, swordsXP = null, axesXP = null, acrobaticsXP = null, taming = null, tamingXP = null, fishing = null, fishingXP = null;
  261. int id = 0, theCount = 0;
  262. while ((line = in.readLine()) != null) {
  263. //Find if the line contains the player we want.
  264. String[] character = line.split(":");
  265. playerName = character[0];
  266. //Check for things we don't want put in the DB
  267. if (playerName == null
  268. || playerName.equals("null")
  269. || playerName
  270. .equals("#Storage place for user information"))
  271. continue;
  272. //Get Mining
  273. if (character.length > 1)
  274. mining = character[1];
  275. //Party
  276. if (character.length > 3)
  277. party = character[3];
  278. //Mining XP
  279. if (character.length > 4)
  280. miningXP = character[4];
  281. if (character.length > 5)
  282. woodcutting = character[5];
  283. if (character.length > 6)
  284. woodCuttingXP = character[6];
  285. if (character.length > 7)
  286. repair = character[7];
  287. if (character.length > 8)
  288. unarmed = character[8];
  289. if (character.length > 9)
  290. herbalism = character[9];
  291. if (character.length > 10)
  292. excavation = character[10];
  293. if (character.length > 11)
  294. archery = character[11];
  295. if (character.length > 12)
  296. swords = character[12];
  297. if (character.length > 13)
  298. axes = character[13];
  299. if (character.length > 14)
  300. acrobatics = character[14];
  301. if (character.length > 15)
  302. repairXP = character[15];
  303. if (character.length > 16)
  304. unarmedXP = character[16];
  305. if (character.length > 17)
  306. herbalismXP = character[17];
  307. if (character.length > 18)
  308. excavationXP = character[18];
  309. if (character.length > 19)
  310. archeryXP = character[19];
  311. if (character.length > 20)
  312. swordsXP = character[20];
  313. if (character.length > 21)
  314. axesXP = character[21];
  315. if (character.length > 22)
  316. acrobaticsXP = character[22];
  317. if (character.length > 24)
  318. taming = character[24];
  319. if (character.length > 25)
  320. tamingXP = character[25];
  321. if (character.length > 34)
  322. fishing = character[34];
  323. if (character.length > 35)
  324. fishingXP = character[35];
  325. //Check to see if the user is in the DB
  326. id = mcMMO.database.GetInt("SELECT id FROM "
  327. + LoadProperties.MySQLtablePrefix
  328. + "users WHERE user = '" + playerName + "'");
  329. if (id > 0) {
  330. theCount++;
  331. //Update the skill values
  332. mcMMO.database.Write("UPDATE "
  333. + LoadProperties.MySQLtablePrefix
  334. + "users SET lastlogin = " + 0
  335. + " WHERE id = " + id);
  336. //if(getDouble(x) > 0 && getDouble(y) > 0 && getDouble(z) > 0)
  337. //mcMMO.database.Write("UPDATE "+LoadProperties.MySQLtablePrefix+"spawn SET world = '" + myspawnworld + "', x = " +getDouble(x)+", y = "+getDouble(y)+", z = "+getDouble(z)+" WHERE user_id = "+id);
  338. mcMMO.database.Write("UPDATE "
  339. + LoadProperties.MySQLtablePrefix
  340. + "skills SET " + " taming = taming+"
  341. + getInt(taming) + ", mining = mining+"
  342. + getInt(mining) + ", repair = repair+"
  343. + getInt(repair)
  344. + ", woodcutting = woodcutting+"
  345. + getInt(woodcutting)
  346. + ", unarmed = unarmed+" + getInt(unarmed)
  347. + ", herbalism = herbalism+"
  348. + getInt(herbalism)
  349. + ", excavation = excavation+"
  350. + getInt(excavation)
  351. + ", archery = archery+" + getInt(archery)
  352. + ", swords = swords+" + getInt(swords)
  353. + ", axes = axes+" + getInt(axes)
  354. + ", acrobatics = acrobatics+"
  355. + getInt(acrobatics)
  356. + ", fishing = fishing+" + getInt(fishing)
  357. + " WHERE user_id = " + id);
  358. mcMMO.database.Write("UPDATE "
  359. + LoadProperties.MySQLtablePrefix
  360. + "experience SET " + " taming = "
  361. + getInt(tamingXP) + ", mining = "
  362. + getInt(miningXP) + ", repair = "
  363. + getInt(repairXP) + ", woodcutting = "
  364. + getInt(woodCuttingXP) + ", unarmed = "
  365. + getInt(unarmedXP) + ", herbalism = "
  366. + getInt(herbalismXP) + ", excavation = "
  367. + getInt(excavationXP) + ", archery = "
  368. + getInt(archeryXP) + ", swords = "
  369. + getInt(swordsXP) + ", axes = "
  370. + getInt(axesXP) + ", acrobatics = "
  371. + getInt(acrobaticsXP) + ", fishing = "
  372. + getInt(fishingXP) + " WHERE user_id = "
  373. + id);
  374. } else {
  375. theCount++;
  376. //Create the user in the DB
  377. mcMMO.database.Write("INSERT INTO "
  378. + LoadProperties.MySQLtablePrefix
  379. + "users (user, lastlogin) VALUES ('"
  380. + playerName + "',"
  381. + System.currentTimeMillis() / 1000 + ")");
  382. id = mcMMO.database
  383. .GetInt("SELECT id FROM "
  384. + LoadProperties.MySQLtablePrefix
  385. + "users WHERE user = '"
  386. + playerName + "'");
  387. mcMMO.database.Write("INSERT INTO "
  388. + LoadProperties.MySQLtablePrefix
  389. + "spawn (user_id) VALUES (" + id + ")");
  390. mcMMO.database.Write("INSERT INTO "
  391. + LoadProperties.MySQLtablePrefix
  392. + "skills (user_id) VALUES (" + id + ")");
  393. mcMMO.database.Write("INSERT INTO "
  394. + LoadProperties.MySQLtablePrefix
  395. + "experience (user_id) VALUES (" + id
  396. + ")");
  397. //Update the skill values
  398. mcMMO.database.Write("UPDATE "
  399. + LoadProperties.MySQLtablePrefix
  400. + "users SET lastlogin = " + 0
  401. + " WHERE id = " + id);
  402. mcMMO.database.Write("UPDATE "
  403. + LoadProperties.MySQLtablePrefix
  404. + "users SET party = '" + party
  405. + "' WHERE id = " + id);
  406. /*
  407. if(getDouble(x) > 0 && getDouble(y) > 0 && getDouble(z) > 0)
  408. mcMMO.database.Write("UPDATE "+LoadProperties.MySQLtablePrefix+"spawn SET world = '" + myspawnworld + "', x = " +getDouble(x)+", y = "+getDouble(y)+", z = "+getDouble(z)+" WHERE user_id = "+id);
  409. */
  410. mcMMO.database.Write("UPDATE "
  411. + LoadProperties.MySQLtablePrefix
  412. + "skills SET " + " taming = "
  413. + getInt(taming) + ", mining = "
  414. + getInt(mining) + ", repair = "
  415. + getInt(repair) + ", woodcutting = "
  416. + getInt(woodcutting) + ", unarmed = "
  417. + getInt(unarmed) + ", herbalism = "
  418. + getInt(herbalism) + ", excavation = "
  419. + getInt(excavation) + ", archery = "
  420. + getInt(archery) + ", swords = "
  421. + getInt(swords) + ", axes = "
  422. + getInt(axes) + ", acrobatics = "
  423. + getInt(acrobatics) + ", fishing = "
  424. + getInt(fishing) + " WHERE user_id = "
  425. + id);
  426. mcMMO.database.Write("UPDATE "
  427. + LoadProperties.MySQLtablePrefix
  428. + "experience SET " + " taming = "
  429. + getInt(tamingXP) + ", mining = "
  430. + getInt(miningXP) + ", repair = "
  431. + getInt(repairXP) + ", woodcutting = "
  432. + getInt(woodCuttingXP) + ", unarmed = "
  433. + getInt(unarmedXP) + ", herbalism = "
  434. + getInt(herbalismXP) + ", excavation = "
  435. + getInt(excavationXP) + ", archery = "
  436. + getInt(archeryXP) + ", swords = "
  437. + getInt(swordsXP) + ", axes = "
  438. + getInt(axesXP) + ", acrobatics = "
  439. + getInt(acrobaticsXP) + ", fishing = "
  440. + getInt(fishingXP) + " WHERE user_id = "
  441. + id);
  442. }
  443. }
  444. System.out
  445. .println("[mcMMO] MySQL Updated from users file, "
  446. + theCount
  447. + " items added/updated to MySQL DB");
  448. in.close();
  449. } catch (Exception e) {
  450. log.log(Level.SEVERE, "Exception while reading " + location
  451. + " (Are you sure you formatted it correctly?)", e);
  452. }
  453. }
  454. }, 1);
  455. }
  456. }