PageRenderTime 169ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/Essentials/src/com/earth2me/essentials/UserData.java

https://github.com/YellowFellow/Essentials
Java | 811 lines | 697 code | 112 blank | 2 comment | 45 complexity | 9f4901868dfb55d6c7b8b912128b633d MD5 | raw file
  1. package com.earth2me.essentials;
  2. import com.earth2me.essentials.commands.NotEnoughArgumentsException;
  3. import java.io.File;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.logging.Logger;
  9. import org.bukkit.Location;
  10. import org.bukkit.Material;
  11. import org.bukkit.entity.Player;
  12. import org.bukkit.inventory.ItemStack;
  13. public abstract class UserData extends PlayerExtension implements IConf
  14. {
  15. private final EssentialsConf config;
  16. private static final Logger logger = Logger.getLogger("Minecraft");
  17. protected UserData(Player base, IEssentials ess)
  18. {
  19. super(base, ess);
  20. File folder = new File(ess.getDataFolder(), "userdata");
  21. if (!folder.exists())
  22. {
  23. folder.mkdirs();
  24. }
  25. config = new EssentialsConf(new File(folder, Util.sanitizeFileName(base.getName()) + ".yml"));
  26. reloadConfig();
  27. }
  28. public final void reloadConfig()
  29. {
  30. config.load();
  31. money = _getMoney();
  32. unlimited = _getUnlimited();
  33. powertools = _getPowertools();
  34. homes = _getHomes();
  35. lastLocation = _getLastLocation();
  36. lastTeleportTimestamp = _getLastTeleportTimestamp();
  37. lastHealTimestamp = _getLastHealTimestamp();
  38. jail = _getJail();
  39. mails = _getMails();
  40. savedInventory = _getSavedInventory();
  41. teleportEnabled = getTeleportEnabled();
  42. ignoredPlayers = getIgnoredPlayers();
  43. godmode = getGodModeEnabled();
  44. muted = getMuted();
  45. muteTimeout = _getMuteTimeout();
  46. jailed = getJailed();
  47. jailTimeout = _getJailTimeout();
  48. lastLogin = _getLastLogin();
  49. lastLogout = _getLastLogout();
  50. afk = getAfk();
  51. newplayer = getNew();
  52. geolocation = _getGeoLocation();
  53. isSocialSpyEnabled = _isSocialSpyEnabled();
  54. isNPC = _isNPC();
  55. arePowerToolsEnabled = _arePowerToolsEnabled();
  56. }
  57. private double money;
  58. private double _getMoney()
  59. {
  60. double money = ess.getSettings().getStartingBalance();
  61. if (config.hasProperty("money"))
  62. {
  63. money = config.getDouble("money", money);
  64. }
  65. if (Math.abs(money) > ess.getSettings().getMaxMoney())
  66. {
  67. money = money < 0 ? -ess.getSettings().getMaxMoney() : ess.getSettings().getMaxMoney();
  68. }
  69. return money;
  70. }
  71. public double getMoney()
  72. {
  73. return money;
  74. }
  75. public void setMoney(double value)
  76. {
  77. money = value;
  78. if (Math.abs(money) > ess.getSettings().getMaxMoney())
  79. {
  80. money = money < 0 ? -ess.getSettings().getMaxMoney() : ess.getSettings().getMaxMoney();
  81. }
  82. config.setProperty("money", value);
  83. config.save();
  84. }
  85. private Map<String, Object> homes;
  86. private Map<String, Object> _getHomes()
  87. {
  88. Object o = config.getProperty("homes");
  89. if (o instanceof Map)
  90. {
  91. return (Map<String, Object>)o;
  92. }
  93. else
  94. {
  95. return new HashMap<String, Object>();
  96. }
  97. }
  98. public Location getHome(String name) throws Exception
  99. {
  100. Location loc = config.getLocation("homes." + name, getServer());
  101. if (loc == null)
  102. {
  103. try
  104. {
  105. loc = config.getLocation("homes." + getHomes().get(Integer.parseInt(name) - 1), getServer());
  106. }
  107. catch (IndexOutOfBoundsException e)
  108. {
  109. return null;
  110. }
  111. catch (NumberFormatException e)
  112. {
  113. return null;
  114. }
  115. }
  116. return loc;
  117. }
  118. public Location getHome(Location world) throws Exception
  119. {
  120. Location loc;
  121. for (String home : getHomes())
  122. {
  123. loc = config.getLocation("homes." + home, getServer());
  124. if (world.getWorld() == loc.getWorld())
  125. {
  126. return loc;
  127. }
  128. }
  129. loc = config.getLocation("homes." + getHomes().get(0), getServer());
  130. return loc;
  131. }
  132. public List<String> getHomes()
  133. {
  134. List<String> list = new ArrayList(homes.keySet());
  135. return list;
  136. }
  137. public void setHome(String name, Location loc)
  138. {
  139. //Invalid names will corrupt the yaml
  140. name = Util.sanitizeFileName(name);
  141. homes.put(name, loc);
  142. config.setProperty("homes." + name, loc);
  143. config.save();
  144. }
  145. public void delHome(String name) throws Exception
  146. {
  147. if (getHome(name) != null)
  148. {
  149. homes.remove(name);
  150. config.removeProperty("homes." + name);
  151. config.save();
  152. }
  153. else
  154. {
  155. //TODO: move this message to messages file
  156. throw new Exception("Home " + name + " doesn't exist");
  157. }
  158. }
  159. public boolean hasHome()
  160. {
  161. if (config.hasProperty("home"))
  162. {
  163. return true;
  164. }
  165. return false;
  166. }
  167. public String getNickname()
  168. {
  169. return config.getString("nickname");
  170. }
  171. public void setNickname(String nick)
  172. {
  173. config.setProperty("nickname", nick);
  174. config.save();
  175. }
  176. private List<Integer> unlimited;
  177. private List<Integer> _getUnlimited()
  178. {
  179. return config.getIntList("unlimited", new ArrayList<Integer>());
  180. }
  181. public List<Integer> getUnlimited()
  182. {
  183. return unlimited;
  184. }
  185. public boolean hasUnlimited(ItemStack stack)
  186. {
  187. return unlimited.contains(stack.getTypeId());
  188. }
  189. public void setUnlimited(ItemStack stack, boolean state)
  190. {
  191. if (unlimited.contains(stack.getTypeId()))
  192. {
  193. unlimited.remove(Integer.valueOf(stack.getTypeId()));
  194. }
  195. if (state)
  196. {
  197. unlimited.add(stack.getTypeId());
  198. }
  199. config.setProperty("unlimited", unlimited);
  200. config.save();
  201. }
  202. private Map<Integer, Object> powertools;
  203. @SuppressWarnings("unchecked")
  204. private Map<Integer, Object> _getPowertools()
  205. {
  206. Object o = config.getProperty("powertools");
  207. if (o instanceof Map)
  208. {
  209. return (Map<Integer, Object>)o;
  210. }
  211. else
  212. {
  213. return new HashMap<Integer, Object>();
  214. }
  215. }
  216. public void clearAllPowertools() {
  217. powertools.clear();
  218. config.setProperty("powertools", powertools);
  219. config.save();
  220. }
  221. public List<String> getPowertool(ItemStack stack)
  222. {
  223. return (List<String>)powertools.get(stack.getTypeId());
  224. }
  225. public void setPowertool(ItemStack stack, List<String> commandList)
  226. {
  227. if (commandList == null || commandList.isEmpty())
  228. {
  229. powertools.remove(stack.getTypeId());
  230. }
  231. else
  232. {
  233. powertools.put(stack.getTypeId(), commandList);
  234. }
  235. config.setProperty("powertools", powertools);
  236. config.save();
  237. }
  238. public boolean hasPowerTools()
  239. {
  240. return powertools.size() > 0;
  241. }
  242. private Location lastLocation;
  243. private Location _getLastLocation()
  244. {
  245. try
  246. {
  247. return config.getLocation("lastlocation", getServer());
  248. }
  249. catch (Exception e)
  250. {
  251. return null;
  252. }
  253. }
  254. public Location getLastLocation()
  255. {
  256. return lastLocation;
  257. }
  258. public void setLastLocation(Location loc)
  259. {
  260. lastLocation = loc;
  261. config.setProperty("lastlocation", loc);
  262. config.save();
  263. }
  264. private long lastTeleportTimestamp;
  265. private long _getLastTeleportTimestamp()
  266. {
  267. return config.getLong("timestamps.lastteleport", 0);
  268. }
  269. public long getLastTeleportTimestamp()
  270. {
  271. return lastTeleportTimestamp;
  272. }
  273. public void setLastTeleportTimestamp(long time)
  274. {
  275. lastTeleportTimestamp = time;
  276. config.setProperty("timestamps.lastteleport", time);
  277. config.save();
  278. }
  279. private long lastHealTimestamp;
  280. private long _getLastHealTimestamp()
  281. {
  282. return config.getLong("timestamps.lastheal", 0);
  283. }
  284. public long getLastHealTimestamp()
  285. {
  286. return lastHealTimestamp;
  287. }
  288. public void setLastHealTimestamp(long time)
  289. {
  290. lastHealTimestamp = time;
  291. config.setProperty("timestamps.lastheal", time);
  292. config.save();
  293. }
  294. private String jail;
  295. private String _getJail()
  296. {
  297. return config.getString("jail");
  298. }
  299. public String getJail()
  300. {
  301. return jail;
  302. }
  303. public void setJail(String jail)
  304. {
  305. if (jail == null || jail.isEmpty())
  306. {
  307. this.jail = null;
  308. config.removeProperty("jail");
  309. }
  310. else
  311. {
  312. this.jail = jail;
  313. config.setProperty("jail", jail);
  314. }
  315. config.save();
  316. }
  317. private List<String> mails;
  318. private List<String> _getMails()
  319. {
  320. return config.getStringList("mail", new ArrayList<String>());
  321. }
  322. public List<String> getMails()
  323. {
  324. return mails;
  325. }
  326. public void setMails(List<String> mails)
  327. {
  328. if (mails == null)
  329. {
  330. config.removeProperty("mail");
  331. mails = _getMails();
  332. }
  333. else
  334. {
  335. config.setProperty("mail", mails);
  336. }
  337. this.mails = mails;
  338. config.save();
  339. }
  340. public void addMail(String mail)
  341. {
  342. mails.add(mail);
  343. setMails(mails);
  344. }
  345. private ItemStack[] savedInventory;
  346. public ItemStack[] getSavedInventory()
  347. {
  348. return savedInventory;
  349. }
  350. private ItemStack[] _getSavedInventory()
  351. {
  352. int size = config.getInt("inventory.size", 0);
  353. if (size < 1 || (getInventory() != null && size > getInventory().getSize()))
  354. {
  355. return null;
  356. }
  357. ItemStack[] is = new ItemStack[size];
  358. for (int i = 0; i < size; i++)
  359. {
  360. is[i] = config.getItemStack("inventory." + i);
  361. }
  362. return is;
  363. }
  364. public void setSavedInventory(ItemStack[] is)
  365. {
  366. if (is == null || is.length == 0)
  367. {
  368. savedInventory = null;
  369. config.removeProperty("inventory");
  370. }
  371. else
  372. {
  373. savedInventory = is;
  374. config.setProperty("inventory.size", is.length);
  375. for (int i = 0; i < is.length; i++)
  376. {
  377. if (is[i] == null || is[i].getType() == Material.AIR)
  378. {
  379. continue;
  380. }
  381. config.setProperty("inventory." + i, is[i]);
  382. }
  383. }
  384. config.save();
  385. }
  386. private boolean teleportEnabled;
  387. private boolean getTeleportEnabled()
  388. {
  389. return config.getBoolean("teleportenabled", true);
  390. }
  391. public boolean isTeleportEnabled()
  392. {
  393. return teleportEnabled;
  394. }
  395. public void setTeleportEnabled(boolean set)
  396. {
  397. teleportEnabled = set;
  398. config.setProperty("teleportenabled", set);
  399. config.save();
  400. }
  401. public boolean toggleTeleportEnabled()
  402. {
  403. boolean ret = !isTeleportEnabled();
  404. setTeleportEnabled(ret);
  405. return ret;
  406. }
  407. public boolean toggleSocialSpy()
  408. {
  409. boolean ret = !isSocialSpyEnabled();
  410. setSocialSpyEnabled(ret);
  411. return ret;
  412. }
  413. private List<String> ignoredPlayers;
  414. public List<String> getIgnoredPlayers()
  415. {
  416. return config.getStringList("ignore", new ArrayList<String>());
  417. }
  418. public void setIgnoredPlayers(List<String> players)
  419. {
  420. if (players == null || players.isEmpty())
  421. {
  422. ignoredPlayers = new ArrayList<String>();
  423. config.removeProperty("ignore");
  424. }
  425. else
  426. {
  427. ignoredPlayers = players;
  428. config.setProperty("ignore", players);
  429. }
  430. config.save();
  431. }
  432. public boolean isIgnoredPlayer(String name)
  433. {
  434. return ignoredPlayers.contains(name.toLowerCase());
  435. }
  436. public void setIgnoredPlayer(String name, boolean set)
  437. {
  438. if (set)
  439. {
  440. ignoredPlayers.add(name.toLowerCase());
  441. }
  442. else
  443. {
  444. ignoredPlayers.remove(name.toLowerCase());
  445. }
  446. setIgnoredPlayers(ignoredPlayers);
  447. }
  448. private boolean godmode;
  449. private boolean getGodModeEnabled()
  450. {
  451. return config.getBoolean("godmode", false);
  452. }
  453. public boolean isGodModeEnabled()
  454. {
  455. return godmode;
  456. }
  457. public void setGodModeEnabled(boolean set)
  458. {
  459. godmode = set;
  460. config.setProperty("godmode", set);
  461. config.save();
  462. }
  463. public boolean toggleGodModeEnabled()
  464. {
  465. boolean ret = !isGodModeEnabled();
  466. setGodModeEnabled(ret);
  467. return ret;
  468. }
  469. private boolean muted;
  470. private boolean getMuted()
  471. {
  472. return config.getBoolean("muted", false);
  473. }
  474. public boolean isMuted()
  475. {
  476. return muted;
  477. }
  478. public void setMuted(boolean set)
  479. {
  480. muted = set;
  481. config.setProperty("muted", set);
  482. config.save();
  483. }
  484. public boolean toggleMuted()
  485. {
  486. boolean ret = !isMuted();
  487. setMuted(ret);
  488. return ret;
  489. }
  490. private long muteTimeout;
  491. private long _getMuteTimeout()
  492. {
  493. return config.getLong("timestamps.mute", 0);
  494. }
  495. public long getMuteTimeout()
  496. {
  497. return muteTimeout;
  498. }
  499. public void setMuteTimeout(long time)
  500. {
  501. muteTimeout = time;
  502. config.setProperty("timestamps.mute", time);
  503. config.save();
  504. }
  505. private boolean jailed;
  506. private boolean getJailed()
  507. {
  508. return config.getBoolean("jailed", false);
  509. }
  510. public boolean isJailed()
  511. {
  512. return jailed;
  513. }
  514. public void setJailed(boolean set)
  515. {
  516. jailed = set;
  517. config.setProperty("jailed", set);
  518. config.save();
  519. }
  520. public boolean toggleJailed()
  521. {
  522. boolean ret = !isJailed();
  523. setJailed(ret);
  524. return ret;
  525. }
  526. private long jailTimeout;
  527. private long _getJailTimeout()
  528. {
  529. return config.getLong("timestamps.jail", 0);
  530. }
  531. public long getJailTimeout()
  532. {
  533. return jailTimeout;
  534. }
  535. public void setJailTimeout(long time)
  536. {
  537. jailTimeout = time;
  538. config.setProperty("timestamps.jail", time);
  539. config.save();
  540. }
  541. public String getBanReason()
  542. {
  543. return config.getString("ban.reason");
  544. }
  545. public void setBanReason(String reason)
  546. {
  547. config.setProperty("ban.reason", reason);
  548. config.save();
  549. }
  550. public long getBanTimeout()
  551. {
  552. return config.getLong("ban.timeout", 0);
  553. }
  554. public void setBanTimeout(long time)
  555. {
  556. config.setProperty("ban.timeout", time);
  557. config.save();
  558. }
  559. private long lastLogin;
  560. private long _getLastLogin()
  561. {
  562. return config.getLong("timestamps.login", 0);
  563. }
  564. public long getLastLogin()
  565. {
  566. return lastLogin;
  567. }
  568. public void setLastLogin(long time)
  569. {
  570. lastLogin = time;
  571. config.setProperty("timestamps.login", time);
  572. config.save();
  573. }
  574. private long lastLogout;
  575. private long _getLastLogout()
  576. {
  577. return config.getLong("timestamps.logout", 0);
  578. }
  579. public long getLastLogout()
  580. {
  581. return lastLogout;
  582. }
  583. public void setLastLogout(long time)
  584. {
  585. lastLogout = time;
  586. config.setProperty("timestamps.logout", time);
  587. config.save();
  588. }
  589. private boolean afk;
  590. private boolean getAfk()
  591. {
  592. return config.getBoolean("afk", false);
  593. }
  594. public boolean isAfk()
  595. {
  596. return afk;
  597. }
  598. public void setAfk(boolean set)
  599. {
  600. afk = set;
  601. config.setProperty("afk", set);
  602. config.save();
  603. }
  604. public boolean toggleAfk()
  605. {
  606. boolean ret = !isAfk();
  607. setAfk(ret);
  608. return ret;
  609. }
  610. private boolean newplayer;
  611. private boolean getNew()
  612. {
  613. return config.getBoolean("newplayer", true);
  614. }
  615. public boolean isNew()
  616. {
  617. return newplayer;
  618. }
  619. public void setNew(boolean set)
  620. {
  621. newplayer = set;
  622. config.setProperty("newplayer", set);
  623. config.save();
  624. }
  625. private String geolocation;
  626. private String _getGeoLocation()
  627. {
  628. return config.getString("geolocation");
  629. }
  630. public String getGeoLocation()
  631. {
  632. return geolocation;
  633. }
  634. public void setGeoLocation(String geolocation)
  635. {
  636. if (geolocation == null || geolocation.isEmpty())
  637. {
  638. this.geolocation = null;
  639. config.removeProperty("geolocation");
  640. }
  641. else
  642. {
  643. this.geolocation = geolocation;
  644. config.setProperty("geolocation", geolocation);
  645. }
  646. config.save();
  647. }
  648. private boolean isSocialSpyEnabled;
  649. private boolean _isSocialSpyEnabled()
  650. {
  651. return config.getBoolean("socialspy", false);
  652. }
  653. public boolean isSocialSpyEnabled()
  654. {
  655. return isSocialSpyEnabled;
  656. }
  657. public void setSocialSpyEnabled(boolean status)
  658. {
  659. isSocialSpyEnabled = status;
  660. config.setProperty("socialspy", status);
  661. config.save();
  662. }
  663. private boolean isNPC;
  664. private boolean _isNPC()
  665. {
  666. return config.getBoolean("npc", false);
  667. }
  668. public boolean isNPC()
  669. {
  670. return isNPC;
  671. }
  672. public void setNPC(boolean set)
  673. {
  674. isNPC = set;
  675. config.setProperty("npc", set);
  676. config.save();
  677. }
  678. private boolean arePowerToolsEnabled;
  679. public boolean arePowerToolsEnabled()
  680. {
  681. return arePowerToolsEnabled;
  682. }
  683. public void setPowerToolsEnabled(boolean set)
  684. {
  685. arePowerToolsEnabled = set;
  686. config.setProperty("powertoolsenabled", set);
  687. config.save();
  688. }
  689. public boolean togglePowerToolsEnabled()
  690. {
  691. boolean ret = !arePowerToolsEnabled();
  692. setPowerToolsEnabled(ret);
  693. return ret;
  694. }
  695. private boolean _arePowerToolsEnabled()
  696. {
  697. return config.getBoolean("powertoolsenabled", true);
  698. }
  699. }