PageRenderTime 37ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://github.com/tobias1222/Essentials
Java | 809 lines | 696 code | 112 blank | 1 comment | 45 complexity | 080c56ba8f5fbba718410ea0262ea83d 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. homes.put(name, loc);
  140. config.setProperty("homes." + name, loc);
  141. config.save();
  142. }
  143. public void delHome(String name) throws Exception
  144. {
  145. if (getHome(name) != null)
  146. {
  147. homes.remove(name);
  148. config.removeProperty("homes." + name);
  149. config.save();
  150. }
  151. else
  152. {
  153. //TODO: move this message to messages file
  154. throw new Exception("Home " + name + " doesn't exist");
  155. }
  156. }
  157. public boolean hasHome()
  158. {
  159. if (config.hasProperty("home"))
  160. {
  161. return true;
  162. }
  163. return false;
  164. }
  165. public String getNickname()
  166. {
  167. return config.getString("nickname");
  168. }
  169. public void setNickname(String nick)
  170. {
  171. config.setProperty("nickname", nick);
  172. config.save();
  173. }
  174. private List<Integer> unlimited;
  175. private List<Integer> _getUnlimited()
  176. {
  177. return config.getIntList("unlimited", new ArrayList<Integer>());
  178. }
  179. public List<Integer> getUnlimited()
  180. {
  181. return unlimited;
  182. }
  183. public boolean hasUnlimited(ItemStack stack)
  184. {
  185. return unlimited.contains(stack.getTypeId());
  186. }
  187. public void setUnlimited(ItemStack stack, boolean state)
  188. {
  189. if (unlimited.contains(stack.getTypeId()))
  190. {
  191. unlimited.remove(Integer.valueOf(stack.getTypeId()));
  192. }
  193. if (state)
  194. {
  195. unlimited.add(stack.getTypeId());
  196. }
  197. config.setProperty("unlimited", unlimited);
  198. config.save();
  199. }
  200. private Map<Integer, Object> powertools;
  201. @SuppressWarnings("unchecked")
  202. private Map<Integer, Object> _getPowertools()
  203. {
  204. Object o = config.getProperty("powertools");
  205. if (o instanceof Map)
  206. {
  207. return (Map<Integer, Object>)o;
  208. }
  209. else
  210. {
  211. return new HashMap<Integer, Object>();
  212. }
  213. }
  214. public void clearAllPowertools() {
  215. powertools.clear();
  216. config.setProperty("powertools", powertools);
  217. config.save();
  218. }
  219. public List<String> getPowertool(ItemStack stack)
  220. {
  221. return (List<String>)powertools.get(stack.getTypeId());
  222. }
  223. public void setPowertool(ItemStack stack, List<String> commandList)
  224. {
  225. if (commandList == null || commandList.isEmpty())
  226. {
  227. powertools.remove(stack.getTypeId());
  228. }
  229. else
  230. {
  231. powertools.put(stack.getTypeId(), commandList);
  232. }
  233. config.setProperty("powertools", powertools);
  234. config.save();
  235. }
  236. public boolean hasPowerTools()
  237. {
  238. return powertools.size() > 0;
  239. }
  240. private Location lastLocation;
  241. private Location _getLastLocation()
  242. {
  243. try
  244. {
  245. return config.getLocation("lastlocation", getServer());
  246. }
  247. catch (Exception e)
  248. {
  249. return null;
  250. }
  251. }
  252. public Location getLastLocation()
  253. {
  254. return lastLocation;
  255. }
  256. public void setLastLocation(Location loc)
  257. {
  258. lastLocation = loc;
  259. config.setProperty("lastlocation", loc);
  260. config.save();
  261. }
  262. private long lastTeleportTimestamp;
  263. private long _getLastTeleportTimestamp()
  264. {
  265. return config.getLong("timestamps.lastteleport", 0);
  266. }
  267. public long getLastTeleportTimestamp()
  268. {
  269. return lastTeleportTimestamp;
  270. }
  271. public void setLastTeleportTimestamp(long time)
  272. {
  273. lastTeleportTimestamp = time;
  274. config.setProperty("timestamps.lastteleport", time);
  275. config.save();
  276. }
  277. private long lastHealTimestamp;
  278. private long _getLastHealTimestamp()
  279. {
  280. return config.getLong("timestamps.lastheal", 0);
  281. }
  282. public long getLastHealTimestamp()
  283. {
  284. return lastHealTimestamp;
  285. }
  286. public void setLastHealTimestamp(long time)
  287. {
  288. lastHealTimestamp = time;
  289. config.setProperty("timestamps.lastheal", time);
  290. config.save();
  291. }
  292. private String jail;
  293. private String _getJail()
  294. {
  295. return config.getString("jail");
  296. }
  297. public String getJail()
  298. {
  299. return jail;
  300. }
  301. public void setJail(String jail)
  302. {
  303. if (jail == null || jail.isEmpty())
  304. {
  305. this.jail = null;
  306. config.removeProperty("jail");
  307. }
  308. else
  309. {
  310. this.jail = jail;
  311. config.setProperty("jail", jail);
  312. }
  313. config.save();
  314. }
  315. private List<String> mails;
  316. private List<String> _getMails()
  317. {
  318. return config.getStringList("mail", new ArrayList<String>());
  319. }
  320. public List<String> getMails()
  321. {
  322. return mails;
  323. }
  324. public void setMails(List<String> mails)
  325. {
  326. if (mails == null)
  327. {
  328. config.removeProperty("mail");
  329. mails = _getMails();
  330. }
  331. else
  332. {
  333. config.setProperty("mail", mails);
  334. }
  335. this.mails = mails;
  336. config.save();
  337. }
  338. public void addMail(String mail)
  339. {
  340. mails.add(mail);
  341. setMails(mails);
  342. }
  343. private ItemStack[] savedInventory;
  344. public ItemStack[] getSavedInventory()
  345. {
  346. return savedInventory;
  347. }
  348. private ItemStack[] _getSavedInventory()
  349. {
  350. int size = config.getInt("inventory.size", 0);
  351. if (size < 1 || (getInventory() != null && size > getInventory().getSize()))
  352. {
  353. return null;
  354. }
  355. ItemStack[] is = new ItemStack[size];
  356. for (int i = 0; i < size; i++)
  357. {
  358. is[i] = config.getItemStack("inventory." + i);
  359. }
  360. return is;
  361. }
  362. public void setSavedInventory(ItemStack[] is)
  363. {
  364. if (is == null || is.length == 0)
  365. {
  366. savedInventory = null;
  367. config.removeProperty("inventory");
  368. }
  369. else
  370. {
  371. savedInventory = is;
  372. config.setProperty("inventory.size", is.length);
  373. for (int i = 0; i < is.length; i++)
  374. {
  375. if (is[i] == null || is[i].getType() == Material.AIR)
  376. {
  377. continue;
  378. }
  379. config.setProperty("inventory." + i, is[i]);
  380. }
  381. }
  382. config.save();
  383. }
  384. private boolean teleportEnabled;
  385. private boolean getTeleportEnabled()
  386. {
  387. return config.getBoolean("teleportenabled", true);
  388. }
  389. public boolean isTeleportEnabled()
  390. {
  391. return teleportEnabled;
  392. }
  393. public void setTeleportEnabled(boolean set)
  394. {
  395. teleportEnabled = set;
  396. config.setProperty("teleportenabled", set);
  397. config.save();
  398. }
  399. public boolean toggleTeleportEnabled()
  400. {
  401. boolean ret = !isTeleportEnabled();
  402. setTeleportEnabled(ret);
  403. return ret;
  404. }
  405. public boolean toggleSocialSpy()
  406. {
  407. boolean ret = !isSocialSpyEnabled();
  408. setSocialSpyEnabled(ret);
  409. return ret;
  410. }
  411. private List<String> ignoredPlayers;
  412. public List<String> getIgnoredPlayers()
  413. {
  414. return config.getStringList("ignore", new ArrayList<String>());
  415. }
  416. public void setIgnoredPlayers(List<String> players)
  417. {
  418. if (players == null || players.isEmpty())
  419. {
  420. ignoredPlayers = new ArrayList<String>();
  421. config.removeProperty("ignore");
  422. }
  423. else
  424. {
  425. ignoredPlayers = players;
  426. config.setProperty("ignore", players);
  427. }
  428. config.save();
  429. }
  430. public boolean isIgnoredPlayer(String name)
  431. {
  432. return ignoredPlayers.contains(name.toLowerCase());
  433. }
  434. public void setIgnoredPlayer(String name, boolean set)
  435. {
  436. if (set)
  437. {
  438. ignoredPlayers.add(name.toLowerCase());
  439. }
  440. else
  441. {
  442. ignoredPlayers.remove(name.toLowerCase());
  443. }
  444. setIgnoredPlayers(ignoredPlayers);
  445. }
  446. private boolean godmode;
  447. private boolean getGodModeEnabled()
  448. {
  449. return config.getBoolean("godmode", false);
  450. }
  451. public boolean isGodModeEnabled()
  452. {
  453. return godmode;
  454. }
  455. public void setGodModeEnabled(boolean set)
  456. {
  457. godmode = set;
  458. config.setProperty("godmode", set);
  459. config.save();
  460. }
  461. public boolean toggleGodModeEnabled()
  462. {
  463. boolean ret = !isGodModeEnabled();
  464. setGodModeEnabled(ret);
  465. return ret;
  466. }
  467. private boolean muted;
  468. private boolean getMuted()
  469. {
  470. return config.getBoolean("muted", false);
  471. }
  472. public boolean isMuted()
  473. {
  474. return muted;
  475. }
  476. public void setMuted(boolean set)
  477. {
  478. muted = set;
  479. config.setProperty("muted", set);
  480. config.save();
  481. }
  482. public boolean toggleMuted()
  483. {
  484. boolean ret = !isMuted();
  485. setMuted(ret);
  486. return ret;
  487. }
  488. private long muteTimeout;
  489. private long _getMuteTimeout()
  490. {
  491. return config.getLong("timestamps.mute", 0);
  492. }
  493. public long getMuteTimeout()
  494. {
  495. return muteTimeout;
  496. }
  497. public void setMuteTimeout(long time)
  498. {
  499. muteTimeout = time;
  500. config.setProperty("timestamps.mute", time);
  501. config.save();
  502. }
  503. private boolean jailed;
  504. private boolean getJailed()
  505. {
  506. return config.getBoolean("jailed", false);
  507. }
  508. public boolean isJailed()
  509. {
  510. return jailed;
  511. }
  512. public void setJailed(boolean set)
  513. {
  514. jailed = set;
  515. config.setProperty("jailed", set);
  516. config.save();
  517. }
  518. public boolean toggleJailed()
  519. {
  520. boolean ret = !isJailed();
  521. setJailed(ret);
  522. return ret;
  523. }
  524. private long jailTimeout;
  525. private long _getJailTimeout()
  526. {
  527. return config.getLong("timestamps.jail", 0);
  528. }
  529. public long getJailTimeout()
  530. {
  531. return jailTimeout;
  532. }
  533. public void setJailTimeout(long time)
  534. {
  535. jailTimeout = time;
  536. config.setProperty("timestamps.jail", time);
  537. config.save();
  538. }
  539. public String getBanReason()
  540. {
  541. return config.getString("ban.reason");
  542. }
  543. public void setBanReason(String reason)
  544. {
  545. config.setProperty("ban.reason", reason);
  546. config.save();
  547. }
  548. public long getBanTimeout()
  549. {
  550. return config.getLong("ban.timeout", 0);
  551. }
  552. public void setBanTimeout(long time)
  553. {
  554. config.setProperty("ban.timeout", time);
  555. config.save();
  556. }
  557. private long lastLogin;
  558. private long _getLastLogin()
  559. {
  560. return config.getLong("timestamps.login", 0);
  561. }
  562. public long getLastLogin()
  563. {
  564. return lastLogin;
  565. }
  566. public void setLastLogin(long time)
  567. {
  568. lastLogin = time;
  569. config.setProperty("timestamps.login", time);
  570. config.save();
  571. }
  572. private long lastLogout;
  573. private long _getLastLogout()
  574. {
  575. return config.getLong("timestamps.logout", 0);
  576. }
  577. public long getLastLogout()
  578. {
  579. return lastLogout;
  580. }
  581. public void setLastLogout(long time)
  582. {
  583. lastLogout = time;
  584. config.setProperty("timestamps.logout", time);
  585. config.save();
  586. }
  587. private boolean afk;
  588. private boolean getAfk()
  589. {
  590. return config.getBoolean("afk", false);
  591. }
  592. public boolean isAfk()
  593. {
  594. return afk;
  595. }
  596. public void setAfk(boolean set)
  597. {
  598. afk = set;
  599. config.setProperty("afk", set);
  600. config.save();
  601. }
  602. public boolean toggleAfk()
  603. {
  604. boolean ret = !isAfk();
  605. setAfk(ret);
  606. return ret;
  607. }
  608. private boolean newplayer;
  609. private boolean getNew()
  610. {
  611. return config.getBoolean("newplayer", true);
  612. }
  613. public boolean isNew()
  614. {
  615. return newplayer;
  616. }
  617. public void setNew(boolean set)
  618. {
  619. newplayer = set;
  620. config.setProperty("newplayer", set);
  621. config.save();
  622. }
  623. private String geolocation;
  624. private String _getGeoLocation()
  625. {
  626. return config.getString("geolocation");
  627. }
  628. public String getGeoLocation()
  629. {
  630. return geolocation;
  631. }
  632. public void setGeoLocation(String geolocation)
  633. {
  634. if (geolocation == null || geolocation.isEmpty())
  635. {
  636. this.geolocation = null;
  637. config.removeProperty("geolocation");
  638. }
  639. else
  640. {
  641. this.geolocation = geolocation;
  642. config.setProperty("geolocation", geolocation);
  643. }
  644. config.save();
  645. }
  646. private boolean isSocialSpyEnabled;
  647. private boolean _isSocialSpyEnabled()
  648. {
  649. return config.getBoolean("socialspy", false);
  650. }
  651. public boolean isSocialSpyEnabled()
  652. {
  653. return isSocialSpyEnabled;
  654. }
  655. public void setSocialSpyEnabled(boolean status)
  656. {
  657. isSocialSpyEnabled = status;
  658. config.setProperty("socialspy", status);
  659. config.save();
  660. }
  661. private boolean isNPC;
  662. private boolean _isNPC()
  663. {
  664. return config.getBoolean("npc", false);
  665. }
  666. public boolean isNPC()
  667. {
  668. return isNPC;
  669. }
  670. public void setNPC(boolean set)
  671. {
  672. isNPC = set;
  673. config.setProperty("npc", set);
  674. config.save();
  675. }
  676. private boolean arePowerToolsEnabled;
  677. public boolean arePowerToolsEnabled()
  678. {
  679. return arePowerToolsEnabled;
  680. }
  681. public void setPowerToolsEnabled(boolean set)
  682. {
  683. arePowerToolsEnabled = set;
  684. config.setProperty("powertoolsenabled", set);
  685. config.save();
  686. }
  687. public boolean togglePowerToolsEnabled()
  688. {
  689. boolean ret = !arePowerToolsEnabled();
  690. setPowerToolsEnabled(ret);
  691. return ret;
  692. }
  693. private boolean _arePowerToolsEnabled()
  694. {
  695. return config.getBoolean("powertoolsenabled", true);
  696. }
  697. }