m.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. package com.gmail.nossr50;
  2. import java.io.BufferedReader;
  3. import java.io.FileReader;
  4. import java.util.logging.Level;
  5. import java.util.logging.Logger;
  6. import org.bukkit.Bukkit;
  7. import org.bukkit.Location;
  8. import org.bukkit.block.Block;
  9. import org.bukkit.entity.*;
  10. import org.bukkit.event.entity.EntityDamageEvent;
  11. import org.bukkit.event.player.PlayerAnimationEvent;
  12. import org.bukkit.inventory.ItemStack;
  13. import com.gmail.nossr50.config.*;
  14. import com.gmail.nossr50.datatypes.PlayerProfile;
  15. import com.gmail.nossr50.datatypes.SkillType;
  16. import com.gmail.nossr50.events.FakeBlockBreakEvent;
  17. import com.gmail.nossr50.events.McMMOItemSpawnEvent;
  18. import com.gmail.nossr50.skills.Repair;
  19. public class m {
  20. public static final Logger log = Logger.getLogger("Minecraft");
  21. /**
  22. * Gets a capitalized version of the target string.
  23. *
  24. * @param target String to capitalize
  25. * @return the capitalized string
  26. */
  27. public static String getCapitalized(String target) {
  28. String firstLetter = target.substring(0,1);
  29. String remainder = target.substring(1);
  30. String capitalized = firstLetter.toUpperCase() + remainder.toLowerCase();
  31. return capitalized;
  32. }
  33. /**
  34. * Gets the int represented by this string.
  35. *
  36. * @param string The string to parse
  37. * @return the int represented by this string
  38. */
  39. public static int getInt(String string) {
  40. if (isInt(string)) {
  41. return Integer.parseInt(string);
  42. }
  43. else {
  44. return 0;
  45. }
  46. }
  47. /**
  48. * Checks to see if an entity is currently invincible.
  49. *
  50. * @param le The LivingEntity to check
  51. * @param event The event the entity is involved in
  52. * @return true if the entity is invincible, false otherwise
  53. */
  54. public static boolean isInvincible(LivingEntity le, EntityDamageEvent event) {
  55. /*
  56. * So apparently if you do more damage to a LivingEntity than its last damage int you bypass the invincibility.
  57. * So yeah, this is for that.
  58. */
  59. if (le.getNoDamageTicks() > le.getMaximumNoDamageTicks() / 2.0F && event.getDamage() <= le.getLastDamage()) {
  60. return true;
  61. }
  62. else {
  63. return false;
  64. }
  65. }
  66. /**
  67. * Gets the power level of a player.
  68. *
  69. * @param player The player to get the power level of
  70. * @param PP The profile of the player
  71. * @return the power level of the player
  72. */
  73. public static int getPowerLevel(Player player, PlayerProfile PP) {
  74. int powerLevel = 0;
  75. for (SkillType type : SkillType.values()) {
  76. if (type.getPermissions(player)) {
  77. powerLevel += PP.getSkillLevel(type);
  78. }
  79. }
  80. return powerLevel;
  81. }
  82. /**
  83. * Simulate a block break event.
  84. *
  85. * @param block The block to break
  86. * @param player The player breaking the block
  87. * @param shouldArmSwing true if an armswing event should be fired, false otherwise
  88. * @return true if the event wasn't cancelled, false otherwise
  89. */
  90. public static boolean blockBreakSimulate(Block block, Player player, Boolean shouldArmSwing) {
  91. //Support for NoCheat
  92. if (shouldArmSwing) {
  93. PlayerAnimationEvent armswing = new PlayerAnimationEvent(player);
  94. Bukkit.getPluginManager().callEvent(armswing);
  95. }
  96. FakeBlockBreakEvent event = new FakeBlockBreakEvent(block, player);
  97. Bukkit.getPluginManager().callEvent(event);
  98. if (!event.isCancelled()) {
  99. return true;
  100. }
  101. else {
  102. return false;
  103. }
  104. }
  105. /**
  106. * Get the upgrade tier of the item in hand.
  107. *
  108. * @param inHand The item to check the tier of
  109. * @return the tier of the item
  110. */
  111. public static Integer getTier(ItemStack inHand) {
  112. int tier = 0;
  113. if (Repair.isWoodTools(inHand)) {
  114. tier = 1;
  115. }
  116. else if (Repair.isStoneTools(inHand)) {
  117. tier = 2;
  118. }
  119. else if (Repair.isIronTools(inHand)) {
  120. tier = 3;
  121. }
  122. else if(Repair.isGoldTools(inHand)) {
  123. tier = 1;
  124. }
  125. else if(Repair.isDiamondTools(inHand))
  126. tier = 4;
  127. return tier;
  128. }
  129. /**
  130. * Determine if two locations are near each other.
  131. *
  132. * @param first The first location
  133. * @param second The second location
  134. * @param maxDistance The max distance apart
  135. * @return true if the distance between <code>first</code> and <code>second</code> is less than <code>maxDistance</code>, false otherwise
  136. */
  137. public static boolean isNear(Location first, Location second, int maxDistance) {
  138. double relX = first.getX() - second.getX();
  139. double relY = first.getY() - second.getY();
  140. double relZ = first.getZ() - second.getZ();
  141. double dist = (relX * relX) + (relY * relY) + (relZ * relZ);
  142. if (dist < maxDistance * maxDistance) {
  143. return true;
  144. }
  145. else {
  146. return false;
  147. }
  148. }
  149. /**
  150. * Determine if a string represents an Integer
  151. *
  152. * @param string String to check
  153. * @return true if the string is an Integer, false otherwise
  154. */
  155. public static boolean isInt(String string) {
  156. try {
  157. Integer.parseInt(string);
  158. return true;
  159. }
  160. catch (NumberFormatException nFE) {
  161. return false;
  162. }
  163. }
  164. /**
  165. * Drop items at a given location.
  166. *
  167. * @param location The location to drop the items at
  168. * @param is The items to drop
  169. * @param quantity The amount of items to drop
  170. */
  171. public static void mcDropItems(Location location, ItemStack is, int quantity) {
  172. for (int i = 0; i < quantity; i++) {
  173. mcDropItem(location, is);
  174. }
  175. }
  176. /**
  177. * Randomly drop an item at a given location.
  178. *
  179. * @param location The location to drop the items at
  180. * @param is The item to drop
  181. * @param chance The percentage chance for the item to drop
  182. */
  183. public static void mcRandomDropItem(Location location, ItemStack is, double chance) {
  184. if (Math.random() * 100 < chance) {
  185. mcDropItem(location, is);
  186. }
  187. }
  188. /**
  189. * Randomly drop items at a given location.
  190. *
  191. * @param location The location to drop the items at
  192. * @param is The item to drop
  193. * @param chance The percentage chance for the item to drop
  194. * @param quantity The amount of items to drop
  195. */
  196. public static void mcRandomDropItems(Location location, ItemStack is, int chance, int quantity) {
  197. for(int i = 0; i < quantity; i++) {
  198. mcRandomDropItem(location, is, chance);
  199. }
  200. }
  201. /**
  202. * Drop an item at a given location.
  203. *
  204. * @param location The location to drop the item at
  205. * @param itemStack The item to drop
  206. */
  207. public static void mcDropItem(Location location, ItemStack itemStack) {
  208. // We can't get the item until we spawn it and we want to make it cancellable, so we have a custom event.
  209. McMMOItemSpawnEvent event = new McMMOItemSpawnEvent(location, itemStack);
  210. Bukkit.getPluginManager().callEvent(event);
  211. if (event.isCancelled()) {
  212. return;
  213. }
  214. else {
  215. location.getWorld().dropItemNaturally(location, itemStack);
  216. }
  217. }
  218. /**
  219. * Convert FlatFile data to MySQL data.
  220. */
  221. public static void convertToMySQL() {
  222. if (!LoadProperties.useMySQL) {
  223. return;
  224. }
  225. Bukkit.getScheduler().scheduleAsyncDelayedTask(Bukkit.getPluginManager().getPlugin("mcMMO"), new Runnable() {
  226. public void run() {
  227. String location = "plugins/mcMMO/FlatFileStuff/mcmmo.users";
  228. try {
  229. //Open the user file
  230. FileReader file = new FileReader(location);
  231. BufferedReader in = new BufferedReader(file);
  232. String line = "";
  233. String playerName = null;
  234. String party = null;
  235. String mining = null;
  236. String woodcutting = null;
  237. String repair = null;
  238. String unarmed = null;
  239. String herbalism = null;
  240. String excavation = null;
  241. String archery = null;
  242. String swords = null;
  243. String axes = null;
  244. String acrobatics = null;
  245. String taming = null;
  246. String fishing = null;
  247. String miningXP = null;
  248. String woodCuttingXP = null;
  249. String repairXP = null;
  250. String unarmedXP = null;
  251. String herbalismXP = null;
  252. String excavationXP = null;
  253. String archeryXP = null;
  254. String swordsXP = null;
  255. String axesXP = null;
  256. String acrobaticsXP = null;
  257. String tamingXP = null;
  258. String fishingXP = null;
  259. int id = 0;
  260. int theCount = 0;
  261. while ((line = in.readLine()) != null) {
  262. //Find if the line contains the player we want.
  263. String[] character = line.split(":");
  264. playerName = character[0];
  265. //Check for things we don't want put in the DB
  266. if (playerName == null || playerName.equals("null") || playerName.equals("#Storage place for user information")) {
  267. continue;
  268. }
  269. if (character.length > 1) {
  270. mining = character[1];
  271. }
  272. if (character.length > 3) {
  273. party = character[3];
  274. }
  275. if (character.length > 4) {
  276. miningXP = character[4];
  277. }
  278. if (character.length > 5) {
  279. woodcutting = character[5];
  280. }
  281. if (character.length > 6) {
  282. woodCuttingXP = character[6];
  283. }
  284. if (character.length > 7) {
  285. repair = character[7];
  286. }
  287. if (character.length > 8) {
  288. unarmed = character[8];
  289. }
  290. if (character.length > 9) {
  291. herbalism = character[9];
  292. }
  293. if (character.length > 10) {
  294. excavation = character[10];
  295. }
  296. if (character.length > 11) {
  297. archery = character[11];
  298. }
  299. if (character.length > 12) {
  300. swords = character[12];
  301. }
  302. if (character.length > 13) {
  303. axes = character[13];
  304. }
  305. if (character.length > 14) {
  306. acrobatics = character[14];
  307. }
  308. if (character.length > 15) {
  309. repairXP = character[15];
  310. }
  311. if (character.length > 16) {
  312. unarmedXP = character[16];
  313. }
  314. if (character.length > 17) {
  315. herbalismXP = character[17];
  316. }
  317. if (character.length > 18) {
  318. excavationXP = character[18];
  319. }
  320. if (character.length > 19) {
  321. archeryXP = character[19];
  322. }
  323. if (character.length > 20) {
  324. swordsXP = character[20];
  325. }
  326. if (character.length > 21) {
  327. axesXP = character[21];
  328. }
  329. if (character.length > 22) {
  330. acrobaticsXP = character[22];
  331. }
  332. if (character.length > 24) {
  333. taming = character[24];
  334. }
  335. if (character.length > 25) {
  336. tamingXP = character[25];
  337. }
  338. if (character.length > 34) {
  339. fishing = character[34];
  340. }
  341. if (character.length > 35) {
  342. fishingXP = character[35];
  343. }
  344. //Check to see if the user is in the DB
  345. id = mcMMO.database.getInt("SELECT id FROM "
  346. + LoadProperties.MySQLtablePrefix
  347. + "users WHERE user = '" + playerName + "'");
  348. if (id > 0) {
  349. theCount++;
  350. //Update the skill values
  351. mcMMO.database.write("UPDATE "
  352. + LoadProperties.MySQLtablePrefix
  353. + "users SET lastlogin = " + 0
  354. + " WHERE id = " + id);
  355. mcMMO.database.write("UPDATE "
  356. + LoadProperties.MySQLtablePrefix
  357. + "skills SET " + " taming = taming+"
  358. + getInt(taming) + ", mining = mining+"
  359. + getInt(mining) + ", repair = repair+"
  360. + getInt(repair)
  361. + ", woodcutting = woodcutting+"
  362. + getInt(woodcutting)
  363. + ", unarmed = unarmed+" + getInt(unarmed)
  364. + ", herbalism = herbalism+"
  365. + getInt(herbalism)
  366. + ", excavation = excavation+"
  367. + getInt(excavation)
  368. + ", archery = archery+" + getInt(archery)
  369. + ", swords = swords+" + getInt(swords)
  370. + ", axes = axes+" + getInt(axes)
  371. + ", acrobatics = acrobatics+"
  372. + getInt(acrobatics)
  373. + ", fishing = fishing+" + getInt(fishing)
  374. + " WHERE user_id = " + id);
  375. mcMMO.database.write("UPDATE "
  376. + LoadProperties.MySQLtablePrefix
  377. + "experience SET " + " taming = "
  378. + getInt(tamingXP) + ", mining = "
  379. + getInt(miningXP) + ", repair = "
  380. + getInt(repairXP) + ", woodcutting = "
  381. + getInt(woodCuttingXP) + ", unarmed = "
  382. + getInt(unarmedXP) + ", herbalism = "
  383. + getInt(herbalismXP) + ", excavation = "
  384. + getInt(excavationXP) + ", archery = "
  385. + getInt(archeryXP) + ", swords = "
  386. + getInt(swordsXP) + ", axes = "
  387. + getInt(axesXP) + ", acrobatics = "
  388. + getInt(acrobaticsXP) + ", fishing = "
  389. + getInt(fishingXP) + " WHERE user_id = "
  390. + id);
  391. }
  392. else {
  393. theCount++;
  394. //Create the user in the DB
  395. mcMMO.database.write("INSERT INTO "
  396. + LoadProperties.MySQLtablePrefix
  397. + "users (user, lastlogin) VALUES ('"
  398. + playerName + "',"
  399. + System.currentTimeMillis() / 1000 + ")");
  400. id = mcMMO.database
  401. .getInt("SELECT id FROM "
  402. + LoadProperties.MySQLtablePrefix
  403. + "users WHERE user = '"
  404. + playerName + "'");
  405. mcMMO.database.write("INSERT INTO "
  406. + LoadProperties.MySQLtablePrefix
  407. + "skills (user_id) VALUES (" + id + ")");
  408. mcMMO.database.write("INSERT INTO "
  409. + LoadProperties.MySQLtablePrefix
  410. + "experience (user_id) VALUES (" + id
  411. + ")");
  412. //Update the skill values
  413. mcMMO.database.write("UPDATE "
  414. + LoadProperties.MySQLtablePrefix
  415. + "users SET lastlogin = " + 0
  416. + " WHERE id = " + id);
  417. mcMMO.database.write("UPDATE "
  418. + LoadProperties.MySQLtablePrefix
  419. + "users SET party = '" + party
  420. + "' WHERE id = " + id);
  421. mcMMO.database.write("UPDATE "
  422. + LoadProperties.MySQLtablePrefix
  423. + "skills SET " + " taming = "
  424. + getInt(taming) + ", mining = "
  425. + getInt(mining) + ", repair = "
  426. + getInt(repair) + ", woodcutting = "
  427. + getInt(woodcutting) + ", unarmed = "
  428. + getInt(unarmed) + ", herbalism = "
  429. + getInt(herbalism) + ", excavation = "
  430. + getInt(excavation) + ", archery = "
  431. + getInt(archery) + ", swords = "
  432. + getInt(swords) + ", axes = "
  433. + getInt(axes) + ", acrobatics = "
  434. + getInt(acrobatics) + ", fishing = "
  435. + getInt(fishing) + " WHERE user_id = "
  436. + id);
  437. mcMMO.database.write("UPDATE "
  438. + LoadProperties.MySQLtablePrefix
  439. + "experience SET " + " taming = "
  440. + getInt(tamingXP) + ", mining = "
  441. + getInt(miningXP) + ", repair = "
  442. + getInt(repairXP) + ", woodcutting = "
  443. + getInt(woodCuttingXP) + ", unarmed = "
  444. + getInt(unarmedXP) + ", herbalism = "
  445. + getInt(herbalismXP) + ", excavation = "
  446. + getInt(excavationXP) + ", archery = "
  447. + getInt(archeryXP) + ", swords = "
  448. + getInt(swordsXP) + ", axes = "
  449. + getInt(axesXP) + ", acrobatics = "
  450. + getInt(acrobaticsXP) + ", fishing = "
  451. + getInt(fishingXP) + " WHERE user_id = "
  452. + id);
  453. }
  454. }
  455. System.out.println("[mcMMO] MySQL Updated from users file, " + theCount + " items added/updated to MySQL DB");
  456. in.close();
  457. }
  458. catch (Exception e) {
  459. log.log(Level.SEVERE, "Exception while reading " + location + " (Are you sure you formatted it correctly?)", e);
  460. }
  461. }
  462. }, 1);
  463. }
  464. }