m.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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.event.player.PlayerAnimationEvent;
  25. import org.bukkit.inventory.ItemStack;
  26. import com.gmail.nossr50.config.*;
  27. import com.gmail.nossr50.datatypes.PlayerProfile;
  28. import com.gmail.nossr50.datatypes.SkillType;
  29. import com.gmail.nossr50.events.FakeBlockBreakEvent;
  30. import com.gmail.nossr50.events.McMMOItemSpawnEvent;
  31. import com.gmail.nossr50.skills.Repair;
  32. public class m
  33. {
  34. public static final Logger log = Logger.getLogger("Minecraft");
  35. /*
  36. * I'm storing my misc functions/methods in here in an unorganized manner. Spheal with it.
  37. * This is probably the most embarrassing part of my code for mcMMO
  38. * I really should find an organized place for these things!
  39. */
  40. public static String getCapitalized(String target)
  41. {
  42. String firstLetter = target.substring(0,1);
  43. String remainder = target.substring(1);
  44. String capitalized = firstLetter.toUpperCase() + remainder.toLowerCase();
  45. return capitalized;
  46. }
  47. public static int getInt(String string)
  48. {
  49. if(isInt(string))
  50. return Integer.parseInt(string);
  51. else
  52. return 0;
  53. }
  54. public static boolean isDouble(String string)
  55. {
  56. try
  57. {
  58. Double.parseDouble(string);
  59. }
  60. catch(NumberFormatException nFE) {
  61. return false;
  62. }
  63. return true;
  64. }
  65. /**
  66. * Checks to see if a block type awards XP.
  67. *
  68. * @param material Block type to check
  69. * @return true if the block type awards XP, false otherwise
  70. */
  71. public static boolean shouldBeWatched(Material material)
  72. {
  73. switch(material){
  74. case BROWN_MUSHROOM:
  75. case CACTUS:
  76. case CLAY:
  77. case COAL_ORE:
  78. case DIAMOND_ORE:
  79. case DIRT:
  80. case ENDER_STONE:
  81. case GLOWING_REDSTONE_ORE:
  82. case GLOWSTONE:
  83. case GOLD_ORE:
  84. case GRASS:
  85. case GRAVEL:
  86. case IRON_ORE:
  87. case JACK_O_LANTERN:
  88. case LAPIS_ORE:
  89. case LOG:
  90. case MELON_BLOCK:
  91. case MOSSY_COBBLESTONE:
  92. case MYCEL:
  93. case NETHERRACK:
  94. case OBSIDIAN:
  95. case PUMPKIN:
  96. case RED_MUSHROOM:
  97. case RED_ROSE:
  98. case REDSTONE_ORE:
  99. case SAND:
  100. case SANDSTONE:
  101. case SOUL_SAND:
  102. case STONE:
  103. case SUGAR_CANE_BLOCK:
  104. case VINE:
  105. case WATER_LILY:
  106. case YELLOW_FLOWER:
  107. return true;
  108. }
  109. return false;
  110. }
  111. public static int getPowerLevel(Player player)
  112. {
  113. PlayerProfile PP = Users.getProfile(player);
  114. int x = 0;
  115. if(mcPermissions.getInstance().taming(player))
  116. x+=PP.getSkillLevel(SkillType.TAMING);
  117. if(mcPermissions.getInstance().mining(player))
  118. x+=PP.getSkillLevel(SkillType.MINING);
  119. if(mcPermissions.getInstance().woodcutting(player))
  120. x+=PP.getSkillLevel(SkillType.WOODCUTTING);
  121. if(mcPermissions.getInstance().unarmed(player))
  122. x+=PP.getSkillLevel(SkillType.UNARMED);
  123. if(mcPermissions.getInstance().herbalism(player))
  124. x+=PP.getSkillLevel(SkillType.HERBALISM);
  125. if(mcPermissions.getInstance().excavation(player))
  126. x+=PP.getSkillLevel(SkillType.EXCAVATION);
  127. if(mcPermissions.getInstance().archery(player))
  128. x+=PP.getSkillLevel(SkillType.ARCHERY);
  129. if(mcPermissions.getInstance().swords(player))
  130. x+=PP.getSkillLevel(SkillType.SWORDS);
  131. if(mcPermissions.getInstance().axes(player))
  132. x+=PP.getSkillLevel(SkillType.AXES);
  133. if(mcPermissions.getInstance().acrobatics(player))
  134. x+=PP.getSkillLevel(SkillType.ACROBATICS);
  135. if(mcPermissions.getInstance().repair(player))
  136. x+=PP.getSkillLevel(SkillType.REPAIR);
  137. if(mcPermissions.getInstance().fishing(player))
  138. x+=PP.getSkillLevel(SkillType.FISHING);
  139. return x;
  140. }
  141. public static boolean blockBreakSimulate(Block block, Player player, Boolean shouldArmSwing)
  142. {
  143. //Support for NoCheat
  144. if(shouldArmSwing)
  145. {
  146. PlayerAnimationEvent armswing = new PlayerAnimationEvent(player);
  147. Bukkit.getPluginManager().callEvent(armswing);
  148. }
  149. FakeBlockBreakEvent event = new FakeBlockBreakEvent(block, player);
  150. if(block != null && player != null){
  151. Bukkit.getServer().getPluginManager().callEvent(event);
  152. if(!event.isCancelled())
  153. {
  154. return true; //Return true if not cancelled
  155. } else {
  156. return false; //Return false if cancelled
  157. }
  158. } else {
  159. return false; //Return false if something went wrong
  160. }
  161. }
  162. public static Integer getTier(Player player)
  163. {
  164. ItemStack is = player.getItemInHand();
  165. if(Repair.isWoodTools(is))
  166. return 1;
  167. if(Repair.isStoneTools(is))
  168. return 2;
  169. if(Repair.isIronTools(is))
  170. return 3;
  171. if(Repair.isGoldTools(is))
  172. return 1;
  173. if(Repair.isDiamondTools(is))
  174. return 4;
  175. return 1;
  176. }
  177. public static boolean isNear(Location first, Location second, int maxDistance) {
  178. double relX = first.getX() - second.getX();
  179. double relY = first.getY() - second.getY();
  180. double relZ = first.getZ() - second.getZ();
  181. double dist = relX * relX + relY * relY + relZ * relZ;
  182. if (dist < maxDistance * maxDistance)
  183. return true;
  184. return false;
  185. }
  186. public static boolean abilityBlockCheck(Block block)
  187. {
  188. switch(block.getType()){
  189. case BED_BLOCK:
  190. case BREWING_STAND:
  191. case BURNING_FURNACE:
  192. case CAKE_BLOCK:
  193. case CHEST:
  194. case DISPENSER:
  195. case ENCHANTMENT_TABLE:
  196. case FENCE_GATE:
  197. case FURNACE:
  198. case IRON_DOOR_BLOCK:
  199. case JUKEBOX:
  200. case LEVER:
  201. case NOTE_BLOCK:
  202. case STONE_BUTTON:
  203. case TRAP_DOOR:
  204. case WALL_SIGN:
  205. case WOODEN_DOOR:
  206. case WORKBENCH:
  207. return false;
  208. }
  209. if(block.getTypeId() == LoadProperties.anvilID)
  210. return false;
  211. return true;
  212. }
  213. public static boolean isInt(String string)
  214. {
  215. try
  216. {
  217. Integer.parseInt(string);
  218. }
  219. catch(NumberFormatException nFE)
  220. {
  221. return false;
  222. }
  223. return true;
  224. }
  225. public static void mcDropItems(Location location, ItemStack is, int quantity)
  226. {
  227. for(int i = 0; i < quantity; i++)
  228. mcDropItem(location, is);
  229. }
  230. public static void mcRandomDropItem(Location location, ItemStack is, int chance)
  231. {
  232. if(Math.random() * 100 < chance)
  233. mcDropItem(location, is);
  234. }
  235. public static void mcRandomDropItems(Location location, ItemStack is, int chance, int quantity)
  236. {
  237. for(int i = 0; i < quantity; i++)
  238. mcRandomDropItem(location, is, chance);
  239. }
  240. public static void mcDropItem(Location location, ItemStack itemStack) {
  241. // We can't get the item until we spawn it and we want to make it cancellable, so we have a custom event.
  242. McMMOItemSpawnEvent event = new McMMOItemSpawnEvent(location, itemStack);
  243. Bukkit.getPluginManager().callEvent(event);
  244. if(event.isCancelled()) return;
  245. location.getWorld().dropItemNaturally(location, itemStack);
  246. }
  247. public static boolean isSwords(ItemStack is)
  248. {
  249. switch(is.getType()){
  250. case DIAMOND_SWORD:
  251. case GOLD_SWORD:
  252. case IRON_SWORD:
  253. case STONE_SWORD:
  254. case WOOD_SWORD:
  255. return true;
  256. }
  257. return false;
  258. }
  259. public static boolean isHoe(ItemStack is)
  260. {
  261. switch(is.getType()){
  262. case DIAMOND_HOE:
  263. case GOLD_HOE:
  264. case IRON_HOE:
  265. case STONE_HOE:
  266. case WOOD_HOE:
  267. return true;
  268. }
  269. return false;
  270. }
  271. public static boolean isShovel(ItemStack is)
  272. {
  273. switch(is.getType()){
  274. case DIAMOND_SPADE:
  275. case GOLD_SPADE:
  276. case IRON_SPADE:
  277. case STONE_SPADE:
  278. case WOOD_SPADE:
  279. return true;
  280. }
  281. return false;
  282. }
  283. public static boolean isAxes(ItemStack is)
  284. {
  285. switch(is.getType()){
  286. case DIAMOND_AXE:
  287. case GOLD_AXE:
  288. case IRON_AXE:
  289. case STONE_AXE:
  290. case WOOD_AXE:
  291. return true;
  292. }
  293. return false;
  294. }
  295. public static boolean isMiningPick(ItemStack is)
  296. {
  297. switch(is.getType()){
  298. case DIAMOND_PICKAXE:
  299. case GOLD_PICKAXE:
  300. case IRON_PICKAXE:
  301. case STONE_PICKAXE:
  302. case WOOD_PICKAXE:
  303. return true;
  304. }
  305. return false;
  306. }
  307. public static boolean isHelmet(ItemStack is)
  308. {
  309. switch(is.getType()){
  310. case DIAMOND_HELMET:
  311. case GOLD_HELMET:
  312. case IRON_HELMET:
  313. case LEATHER_HELMET:
  314. return true;
  315. }
  316. return false;
  317. }
  318. public static boolean isChestplate(ItemStack is)
  319. {
  320. switch(is.getType()){
  321. case DIAMOND_CHESTPLATE:
  322. case GOLD_CHESTPLATE:
  323. case IRON_CHESTPLATE:
  324. case LEATHER_CHESTPLATE:
  325. return true;
  326. }
  327. return false;
  328. }
  329. public static boolean isPants(ItemStack is)
  330. {
  331. switch(is.getType()){
  332. case DIAMOND_LEGGINGS:
  333. case GOLD_LEGGINGS:
  334. case IRON_LEGGINGS:
  335. case LEATHER_LEGGINGS:
  336. return true;
  337. }
  338. return false;
  339. }
  340. public static boolean isBoots(ItemStack is)
  341. {
  342. switch(is.getType()){
  343. case DIAMOND_BOOTS:
  344. case GOLD_BOOTS:
  345. case IRON_BOOTS:
  346. case LEATHER_BOOTS:
  347. return true;
  348. }
  349. return false;
  350. }
  351. public static boolean isOre(Block block)
  352. {
  353. switch (block.getType()) {
  354. case COAL_ORE:
  355. case DIAMOND_ORE:
  356. case GLOWING_REDSTONE_ORE:
  357. case GOLD_ORE:
  358. case IRON_ORE:
  359. case LAPIS_ORE:
  360. case REDSTONE_ORE:
  361. return true;
  362. }
  363. return false;
  364. }
  365. public static void convertToMySQL()
  366. {
  367. if(!LoadProperties.useMySQL)
  368. return;
  369. Bukkit.getScheduler().scheduleAsyncDelayedTask(Bukkit.getServer().getPluginManager().getPlugin("mcMMO"), new Runnable(){
  370. public void run() {
  371. String location = "plugins/mcMMO/FlatFileStuff/mcmmo.users";
  372. try {
  373. //Open the user file
  374. FileReader file = new FileReader(location);
  375. BufferedReader in = new BufferedReader(file);
  376. String line = "";
  377. 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;
  378. int id = 0, theCount = 0;
  379. while ((line = in.readLine()) != null) {
  380. //Find if the line contains the player we want.
  381. String[] character = line.split(":");
  382. playerName = character[0];
  383. //Check for things we don't want put in the DB
  384. if (playerName == null
  385. || playerName.equals("null")
  386. || playerName
  387. .equals("#Storage place for user information"))
  388. continue;
  389. //Get Mining
  390. if (character.length > 1)
  391. mining = character[1];
  392. //Party
  393. if (character.length > 3)
  394. party = character[3];
  395. //Mining XP
  396. if (character.length > 4)
  397. miningXP = character[4];
  398. if (character.length > 5)
  399. woodcutting = character[5];
  400. if (character.length > 6)
  401. woodCuttingXP = character[6];
  402. if (character.length > 7)
  403. repair = character[7];
  404. if (character.length > 8)
  405. unarmed = character[8];
  406. if (character.length > 9)
  407. herbalism = character[9];
  408. if (character.length > 10)
  409. excavation = character[10];
  410. if (character.length > 11)
  411. archery = character[11];
  412. if (character.length > 12)
  413. swords = character[12];
  414. if (character.length > 13)
  415. axes = character[13];
  416. if (character.length > 14)
  417. acrobatics = character[14];
  418. if (character.length > 15)
  419. repairXP = character[15];
  420. if (character.length > 16)
  421. unarmedXP = character[16];
  422. if (character.length > 17)
  423. herbalismXP = character[17];
  424. if (character.length > 18)
  425. excavationXP = character[18];
  426. if (character.length > 19)
  427. archeryXP = character[19];
  428. if (character.length > 20)
  429. swordsXP = character[20];
  430. if (character.length > 21)
  431. axesXP = character[21];
  432. if (character.length > 22)
  433. acrobaticsXP = character[22];
  434. if (character.length > 24)
  435. taming = character[24];
  436. if (character.length > 25)
  437. tamingXP = character[25];
  438. if (character.length > 34)
  439. fishing = character[34];
  440. if (character.length > 35)
  441. fishingXP = character[35];
  442. //Check to see if the user is in the DB
  443. id = mcMMO.database.GetInt("SELECT id FROM "
  444. + LoadProperties.MySQLtablePrefix
  445. + "users WHERE user = '" + playerName + "'");
  446. if (id > 0) {
  447. theCount++;
  448. //Update the skill values
  449. mcMMO.database.Write("UPDATE "
  450. + LoadProperties.MySQLtablePrefix
  451. + "users SET lastlogin = " + 0
  452. + " WHERE id = " + id);
  453. mcMMO.database.Write("UPDATE "
  454. + LoadProperties.MySQLtablePrefix
  455. + "skills SET " + " taming = taming+"
  456. + getInt(taming) + ", mining = mining+"
  457. + getInt(mining) + ", repair = repair+"
  458. + getInt(repair)
  459. + ", woodcutting = woodcutting+"
  460. + getInt(woodcutting)
  461. + ", unarmed = unarmed+" + getInt(unarmed)
  462. + ", herbalism = herbalism+"
  463. + getInt(herbalism)
  464. + ", excavation = excavation+"
  465. + getInt(excavation)
  466. + ", archery = archery+" + getInt(archery)
  467. + ", swords = swords+" + getInt(swords)
  468. + ", axes = axes+" + getInt(axes)
  469. + ", acrobatics = acrobatics+"
  470. + getInt(acrobatics)
  471. + ", fishing = fishing+" + getInt(fishing)
  472. + " WHERE user_id = " + id);
  473. mcMMO.database.Write("UPDATE "
  474. + LoadProperties.MySQLtablePrefix
  475. + "experience SET " + " taming = "
  476. + getInt(tamingXP) + ", mining = "
  477. + getInt(miningXP) + ", repair = "
  478. + getInt(repairXP) + ", woodcutting = "
  479. + getInt(woodCuttingXP) + ", unarmed = "
  480. + getInt(unarmedXP) + ", herbalism = "
  481. + getInt(herbalismXP) + ", excavation = "
  482. + getInt(excavationXP) + ", archery = "
  483. + getInt(archeryXP) + ", swords = "
  484. + getInt(swordsXP) + ", axes = "
  485. + getInt(axesXP) + ", acrobatics = "
  486. + getInt(acrobaticsXP) + ", fishing = "
  487. + getInt(fishingXP) + " WHERE user_id = "
  488. + id);
  489. } else {
  490. theCount++;
  491. //Create the user in the DB
  492. mcMMO.database.Write("INSERT INTO "
  493. + LoadProperties.MySQLtablePrefix
  494. + "users (user, lastlogin) VALUES ('"
  495. + playerName + "',"
  496. + System.currentTimeMillis() / 1000 + ")");
  497. id = mcMMO.database
  498. .GetInt("SELECT id FROM "
  499. + LoadProperties.MySQLtablePrefix
  500. + "users WHERE user = '"
  501. + playerName + "'");
  502. mcMMO.database.Write("INSERT INTO "
  503. + LoadProperties.MySQLtablePrefix
  504. + "skills (user_id) VALUES (" + id + ")");
  505. mcMMO.database.Write("INSERT INTO "
  506. + LoadProperties.MySQLtablePrefix
  507. + "experience (user_id) VALUES (" + id
  508. + ")");
  509. //Update the skill values
  510. mcMMO.database.Write("UPDATE "
  511. + LoadProperties.MySQLtablePrefix
  512. + "users SET lastlogin = " + 0
  513. + " WHERE id = " + id);
  514. mcMMO.database.Write("UPDATE "
  515. + LoadProperties.MySQLtablePrefix
  516. + "users SET party = '" + party
  517. + "' WHERE id = " + id);
  518. mcMMO.database.Write("UPDATE "
  519. + LoadProperties.MySQLtablePrefix
  520. + "skills SET " + " taming = "
  521. + getInt(taming) + ", mining = "
  522. + getInt(mining) + ", repair = "
  523. + getInt(repair) + ", woodcutting = "
  524. + getInt(woodcutting) + ", unarmed = "
  525. + getInt(unarmed) + ", herbalism = "
  526. + getInt(herbalism) + ", excavation = "
  527. + getInt(excavation) + ", archery = "
  528. + getInt(archery) + ", swords = "
  529. + getInt(swords) + ", axes = "
  530. + getInt(axes) + ", acrobatics = "
  531. + getInt(acrobatics) + ", fishing = "
  532. + getInt(fishing) + " WHERE user_id = "
  533. + id);
  534. mcMMO.database.Write("UPDATE "
  535. + LoadProperties.MySQLtablePrefix
  536. + "experience SET " + " taming = "
  537. + getInt(tamingXP) + ", mining = "
  538. + getInt(miningXP) + ", repair = "
  539. + getInt(repairXP) + ", woodcutting = "
  540. + getInt(woodCuttingXP) + ", unarmed = "
  541. + getInt(unarmedXP) + ", herbalism = "
  542. + getInt(herbalismXP) + ", excavation = "
  543. + getInt(excavationXP) + ", archery = "
  544. + getInt(archeryXP) + ", swords = "
  545. + getInt(swordsXP) + ", axes = "
  546. + getInt(axesXP) + ", acrobatics = "
  547. + getInt(acrobaticsXP) + ", fishing = "
  548. + getInt(fishingXP) + " WHERE user_id = "
  549. + id);
  550. }
  551. }
  552. System.out
  553. .println("[mcMMO] MySQL Updated from users file, "
  554. + theCount
  555. + " items added/updated to MySQL DB");
  556. in.close();
  557. } catch (Exception e) {
  558. log.log(Level.SEVERE, "Exception while reading " + location
  559. + " (Are you sure you formatted it correctly?)", e);
  560. }
  561. }
  562. }, 1);
  563. }
  564. }