m.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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.FakeBlockBreakEvent;
  28. import com.gmail.nossr50.datatypes.SkillType;
  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 == 103 || id == 82 || id == 16 || id == 73 || id == 49 || id == 81 || id == 83 || id == 86 || id == 91 || id == 1 || id == 17 || id == 42 || id == 87 || id == 89 || id == 2 || id == 3 || id == 12 || id == 13 || id == 21 || id == 15 || id == 14 || id == 56 || id == 38 || id == 37 || id == 39 || id == 40 || id == 24;
  85. }
  86. public static int getPowerLevel(Player player)
  87. {
  88. PlayerProfile PP = Users.getProfile(player);
  89. int x = 0;
  90. if(mcPermissions.getInstance().taming(player))
  91. x+=PP.getSkillLevel(SkillType.TAMING);
  92. if(mcPermissions.getInstance().mining(player))
  93. x+=PP.getSkillLevel(SkillType.MINING);
  94. if(mcPermissions.getInstance().woodcutting(player))
  95. x+=PP.getSkillLevel(SkillType.WOODCUTTING);
  96. if(mcPermissions.getInstance().unarmed(player))
  97. x+=PP.getSkillLevel(SkillType.UNARMED);
  98. if(mcPermissions.getInstance().herbalism(player))
  99. x+=PP.getSkillLevel(SkillType.HERBALISM);
  100. if(mcPermissions.getInstance().excavation(player))
  101. x+=PP.getSkillLevel(SkillType.EXCAVATION);
  102. if(mcPermissions.getInstance().archery(player))
  103. x+=PP.getSkillLevel(SkillType.ARCHERY);
  104. if(mcPermissions.getInstance().swords(player))
  105. x+=PP.getSkillLevel(SkillType.SWORDS);
  106. if(mcPermissions.getInstance().axes(player))
  107. x+=PP.getSkillLevel(SkillType.AXES);
  108. if(mcPermissions.getInstance().acrobatics(player))
  109. x+=PP.getSkillLevel(SkillType.ACROBATICS);
  110. if(mcPermissions.getInstance().repair(player))
  111. x+=PP.getSkillLevel(SkillType.REPAIR);
  112. if(mcPermissions.getInstance().fishing(player))
  113. x+=PP.getSkillLevel(SkillType.FISHING);
  114. return x;
  115. }
  116. public static boolean blockBreakSimulate(Block block, Player player)
  117. {
  118. FakeBlockBreakEvent event = new FakeBlockBreakEvent(block, player);
  119. if(block != null && player != null){
  120. Bukkit.getServer().getPluginManager().callEvent(event);
  121. if(!event.isCancelled())
  122. {
  123. return true; //Return true if not cancelled
  124. } else {
  125. return false; //Return false if cancelled
  126. }
  127. } else {
  128. return false; //Return false if something went wrong
  129. }
  130. }
  131. public static void damageTool(Player player, short damage)
  132. {
  133. if(player.getItemInHand().getTypeId() == 0)
  134. return;
  135. player.getItemInHand().setDurability((short) (player.getItemInHand().getDurability() + damage));
  136. if(player.getItemInHand().getDurability() >= getMaxDurability(getTier(player), player.getItemInHand()))
  137. {
  138. ItemStack[] inventory = player.getInventory().getContents();
  139. for(ItemStack x : inventory)
  140. {
  141. if(x != null && x.getTypeId() == player.getItemInHand().getTypeId() && x.getDurability() == player.getItemInHand().getDurability()){
  142. x.setTypeId(0);
  143. x.setAmount(0);
  144. player.getInventory().setContents(inventory);
  145. return;
  146. }
  147. }
  148. }
  149. }
  150. public static Integer getTier(Player player)
  151. {
  152. int i = player.getItemInHand().getTypeId();
  153. if(i == 268 || i == 269 || i == 270 || i == 271 || i == 290){
  154. return 1; //WOOD
  155. } else if (i == 272 || i == 273 || i == 274 || i == 275 || i == 291){
  156. return 2; //STONE
  157. } else if (i == 256 || i == 257 || i == 258 || i == 267 || i == 292){
  158. return 3; //IRON
  159. } else if (i == 283 || i == 284 || i == 285 || i == 286 || i == 294){
  160. return 1; //GOLD
  161. } else if (i == 276 || i == 277 || i == 278 || i == 279 || i == 293){
  162. return 4; //DIAMOND
  163. } else {
  164. return 1; //UNRECOGNIZED
  165. }
  166. }
  167. public static Integer getMaxDurability(Integer tier, ItemStack item)
  168. {
  169. int id = item.getTypeId();
  170. if(tier == 1){
  171. if((id == 283 || id == 284 || id == 285 || id == 286 || id == 294)){
  172. return 33; //GOLD
  173. } else {
  174. return 60; //WOOD
  175. }
  176. } else if (tier == 2){
  177. return 132;
  178. } else if (tier == 3){
  179. return 251;
  180. } else if (tier == 4){
  181. return 1562;
  182. } else {
  183. return 0;
  184. }
  185. }
  186. public static double getDistance(Location loca, Location locb)
  187. {
  188. return Math.sqrt(Math.pow(loca.getX() - locb.getX(), 2) + Math.pow(loca.getY() - locb.getY(), 2)
  189. + Math.pow(loca.getZ() - locb.getZ(), 2));
  190. }
  191. public static boolean abilityBlockCheck(Block block)
  192. {
  193. int i = block.getTypeId();
  194. 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 == 42 || i == 71 || i == 64 || i == 84 || i == 324 || i == 330){
  195. return false;
  196. } else {
  197. return true;
  198. }
  199. }
  200. public static boolean isBlockAround(Location loc, Integer radius, Integer typeid)
  201. {
  202. Block blockx = loc.getBlock();
  203. int ox = blockx.getX();
  204. int oy = blockx.getY();
  205. int oz = blockx.getZ();
  206. for (int cx = -radius; cx <= radius; cx++) {
  207. for (int cy = -radius; cy <= radius; cy++) {
  208. for (int cz = -radius; cz <= radius; cz++) {
  209. Block block = loc.getWorld().getBlockAt(ox + cx, oy + cy, oz + cz);
  210. if (block.getTypeId() == typeid) {
  211. return true;
  212. }
  213. }
  214. }
  215. }
  216. return false;
  217. }
  218. public static Integer calculateHealth(Integer health, Integer newvalue){
  219. if((health + newvalue) > 20){
  220. return 20;
  221. } else {
  222. return health+newvalue;
  223. }
  224. }
  225. public Integer calculateMinusHealth(Integer health, Integer newvalue){
  226. if((health - newvalue) < 1){
  227. return 0;
  228. } else {
  229. return health-newvalue;
  230. }
  231. }
  232. public static boolean isInt(String string)
  233. {
  234. try
  235. {
  236. Integer.parseInt(string);
  237. }
  238. catch(NumberFormatException nFE)
  239. {
  240. return false;
  241. }
  242. return true;
  243. }
  244. public static void mcDropItem(Location location, int id)
  245. {
  246. if(location == null) return;
  247. Material mat = Material.getMaterial(id);
  248. ItemStack item = new ItemStack(mat, 1, (byte) 0, (byte) 0);
  249. mcDropItem(location, item);
  250. }
  251. public static void mcDropItem(Location location, ItemStack itemStack) {
  252. // We can't get the item until we spawn it and we want to make it cancellable, so we have a custom event.
  253. McMMOItemSpawnEvent event = new McMMOItemSpawnEvent(location, itemStack);
  254. Bukkit.getPluginManager().callEvent(event);
  255. if(event.isCancelled()) return;
  256. location.getWorld().dropItemNaturally(location, itemStack);
  257. }
  258. public static boolean isSwords(ItemStack is)
  259. {
  260. int id = is.getTypeId();
  261. return id == 268 || id == 267 || id == 272 || id == 283 || id == 276;
  262. }
  263. public static boolean isHoe(ItemStack is)
  264. {
  265. int id = is.getTypeId();
  266. return id == 290 || id == 291 || id == 292 || id == 293 || id == 294;
  267. }
  268. public static boolean isShovel(ItemStack is)
  269. {
  270. int id = is.getTypeId();
  271. return id == 269 || id == 273 || id == 277 || id == 284 || id == 256;
  272. }
  273. public static boolean isAxes(ItemStack is)
  274. {
  275. int id = is.getTypeId();
  276. return id == 271 || id == 258 || id == 286 || id == 279 || id == 275;
  277. }
  278. public static boolean isMiningPick(ItemStack is)
  279. {
  280. int id = is.getTypeId();
  281. return id == 270 || id == 274 || id == 285 || id == 257 || id == 278;
  282. }
  283. // public boolean isGold(ItemStack is)
  284. // {
  285. // int id = is.getTypeId();
  286. // return id == 283 || id == 284 || id == 285 || id == 286 || id == 294 || id == 314 || id == 315 || id == 316 || id == 317;
  287. // }
  288. public static void convertToMySQL()
  289. {
  290. if(!LoadProperties.useMySQL)
  291. return;
  292. Bukkit.getScheduler().scheduleAsyncDelayedTask(Bukkit.getServer().getPluginManager().getPlugin("mcMMO"), new Runnable(){
  293. public void run() {
  294. String location = "plugins/mcMMO/FlatFileStuff/mcmmo.users";
  295. try {
  296. //Open the user file
  297. FileReader file = new FileReader(location);
  298. BufferedReader in = new BufferedReader(file);
  299. String line = "";
  300. 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;
  301. int id = 0, theCount = 0;
  302. while ((line = in.readLine()) != null) {
  303. //Find if the line contains the player we want.
  304. String[] character = line.split(":");
  305. playerName = character[0];
  306. //Check for things we don't want put in the DB
  307. if (playerName == null
  308. || playerName.equals("null")
  309. || playerName
  310. .equals("#Storage place for user information"))
  311. continue;
  312. //Get Mining
  313. if (character.length > 1)
  314. mining = character[1];
  315. //Party
  316. if (character.length > 3)
  317. party = character[3];
  318. //Mining XP
  319. if (character.length > 4)
  320. miningXP = character[4];
  321. if (character.length > 5)
  322. woodcutting = character[5];
  323. if (character.length > 6)
  324. woodCuttingXP = character[6];
  325. if (character.length > 7)
  326. repair = character[7];
  327. if (character.length > 8)
  328. unarmed = character[8];
  329. if (character.length > 9)
  330. herbalism = character[9];
  331. if (character.length > 10)
  332. excavation = character[10];
  333. if (character.length > 11)
  334. archery = character[11];
  335. if (character.length > 12)
  336. swords = character[12];
  337. if (character.length > 13)
  338. axes = character[13];
  339. if (character.length > 14)
  340. acrobatics = character[14];
  341. if (character.length > 15)
  342. repairXP = character[15];
  343. if (character.length > 16)
  344. unarmedXP = character[16];
  345. if (character.length > 17)
  346. herbalismXP = character[17];
  347. if (character.length > 18)
  348. excavationXP = character[18];
  349. if (character.length > 19)
  350. archeryXP = character[19];
  351. if (character.length > 20)
  352. swordsXP = character[20];
  353. if (character.length > 21)
  354. axesXP = character[21];
  355. if (character.length > 22)
  356. acrobaticsXP = character[22];
  357. if (character.length > 24)
  358. taming = character[24];
  359. if (character.length > 25)
  360. tamingXP = character[25];
  361. if (character.length > 34)
  362. fishing = character[34];
  363. if (character.length > 35)
  364. fishingXP = character[35];
  365. //Check to see if the user is in the DB
  366. id = mcMMO.database.GetInt("SELECT id FROM "
  367. + LoadProperties.MySQLtablePrefix
  368. + "users WHERE user = '" + playerName + "'");
  369. if (id > 0) {
  370. theCount++;
  371. //Update the skill values
  372. mcMMO.database.Write("UPDATE "
  373. + LoadProperties.MySQLtablePrefix
  374. + "users SET lastlogin = " + 0
  375. + " WHERE id = " + id);
  376. //if(getDouble(x) > 0 && getDouble(y) > 0 && getDouble(z) > 0)
  377. //mcMMO.database.Write("UPDATE "+LoadProperties.MySQLtablePrefix+"spawn SET world = '" + myspawnworld + "', x = " +getDouble(x)+", y = "+getDouble(y)+", z = "+getDouble(z)+" WHERE user_id = "+id);
  378. mcMMO.database.Write("UPDATE "
  379. + LoadProperties.MySQLtablePrefix
  380. + "skills SET " + " taming = taming+"
  381. + getInt(taming) + ", mining = mining+"
  382. + getInt(mining) + ", repair = repair+"
  383. + getInt(repair)
  384. + ", woodcutting = woodcutting+"
  385. + getInt(woodcutting)
  386. + ", unarmed = unarmed+" + getInt(unarmed)
  387. + ", herbalism = herbalism+"
  388. + getInt(herbalism)
  389. + ", excavation = excavation+"
  390. + getInt(excavation)
  391. + ", archery = archery+" + getInt(archery)
  392. + ", swords = swords+" + getInt(swords)
  393. + ", axes = axes+" + getInt(axes)
  394. + ", acrobatics = acrobatics+"
  395. + getInt(acrobatics)
  396. + ", fishing = fishing+" + getInt(fishing)
  397. + " WHERE user_id = " + id);
  398. mcMMO.database.Write("UPDATE "
  399. + LoadProperties.MySQLtablePrefix
  400. + "experience SET " + " taming = "
  401. + getInt(tamingXP) + ", mining = "
  402. + getInt(miningXP) + ", repair = "
  403. + getInt(repairXP) + ", woodcutting = "
  404. + getInt(woodCuttingXP) + ", unarmed = "
  405. + getInt(unarmedXP) + ", herbalism = "
  406. + getInt(herbalismXP) + ", excavation = "
  407. + getInt(excavationXP) + ", archery = "
  408. + getInt(archeryXP) + ", swords = "
  409. + getInt(swordsXP) + ", axes = "
  410. + getInt(axesXP) + ", acrobatics = "
  411. + getInt(acrobaticsXP) + ", fishing = "
  412. + getInt(fishingXP) + " WHERE user_id = "
  413. + id);
  414. } else {
  415. theCount++;
  416. //Create the user in the DB
  417. mcMMO.database.Write("INSERT INTO "
  418. + LoadProperties.MySQLtablePrefix
  419. + "users (user, lastlogin) VALUES ('"
  420. + playerName + "',"
  421. + System.currentTimeMillis() / 1000 + ")");
  422. id = mcMMO.database
  423. .GetInt("SELECT id FROM "
  424. + LoadProperties.MySQLtablePrefix
  425. + "users WHERE user = '"
  426. + playerName + "'");
  427. mcMMO.database.Write("INSERT INTO "
  428. + LoadProperties.MySQLtablePrefix
  429. + "spawn (user_id) VALUES (" + id + ")");
  430. mcMMO.database.Write("INSERT INTO "
  431. + LoadProperties.MySQLtablePrefix
  432. + "skills (user_id) VALUES (" + id + ")");
  433. mcMMO.database.Write("INSERT INTO "
  434. + LoadProperties.MySQLtablePrefix
  435. + "experience (user_id) VALUES (" + id
  436. + ")");
  437. //Update the skill values
  438. mcMMO.database.Write("UPDATE "
  439. + LoadProperties.MySQLtablePrefix
  440. + "users SET lastlogin = " + 0
  441. + " WHERE id = " + id);
  442. mcMMO.database.Write("UPDATE "
  443. + LoadProperties.MySQLtablePrefix
  444. + "users SET party = '" + party
  445. + "' WHERE id = " + id);
  446. /*
  447. if(getDouble(x) > 0 && getDouble(y) > 0 && getDouble(z) > 0)
  448. mcMMO.database.Write("UPDATE "+LoadProperties.MySQLtablePrefix+"spawn SET world = '" + myspawnworld + "', x = " +getDouble(x)+", y = "+getDouble(y)+", z = "+getDouble(z)+" WHERE user_id = "+id);
  449. */
  450. mcMMO.database.Write("UPDATE "
  451. + LoadProperties.MySQLtablePrefix
  452. + "skills SET " + " taming = "
  453. + getInt(taming) + ", mining = "
  454. + getInt(mining) + ", repair = "
  455. + getInt(repair) + ", woodcutting = "
  456. + getInt(woodcutting) + ", unarmed = "
  457. + getInt(unarmed) + ", herbalism = "
  458. + getInt(herbalism) + ", excavation = "
  459. + getInt(excavation) + ", archery = "
  460. + getInt(archery) + ", swords = "
  461. + getInt(swords) + ", axes = "
  462. + getInt(axes) + ", acrobatics = "
  463. + getInt(acrobatics) + ", fishing = "
  464. + getInt(fishing) + " WHERE user_id = "
  465. + id);
  466. mcMMO.database.Write("UPDATE "
  467. + LoadProperties.MySQLtablePrefix
  468. + "experience SET " + " taming = "
  469. + getInt(tamingXP) + ", mining = "
  470. + getInt(miningXP) + ", repair = "
  471. + getInt(repairXP) + ", woodcutting = "
  472. + getInt(woodCuttingXP) + ", unarmed = "
  473. + getInt(unarmedXP) + ", herbalism = "
  474. + getInt(herbalismXP) + ", excavation = "
  475. + getInt(excavationXP) + ", archery = "
  476. + getInt(archeryXP) + ", swords = "
  477. + getInt(swordsXP) + ", axes = "
  478. + getInt(axesXP) + ", acrobatics = "
  479. + getInt(acrobaticsXP) + ", fishing = "
  480. + getInt(fishingXP) + " WHERE user_id = "
  481. + id);
  482. }
  483. }
  484. System.out
  485. .println("[mcMMO] MySQL Updated from users file, "
  486. + theCount
  487. + " items added/updated to MySQL DB");
  488. in.close();
  489. } catch (Exception e) {
  490. log.log(Level.SEVERE, "Exception while reading " + location
  491. + " (Are you sure you formatted it correctly?)", e);
  492. }
  493. }
  494. }, 1);
  495. }
  496. }