PageRenderTime 192ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/gmail/nossr50/m.java

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