m.java 18 KB

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