m.java 18 KB

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