PageRenderTime 180ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/BodyshotzVG/Essentials
Java | 490 lines | 447 code | 41 blank | 2 comment | 99 complexity | 8692a4ab7a166972774f0e6f566156f3 MD5 | raw file
  1. package com.earth2me.essentials;
  2. import java.util.Arrays;
  3. import java.util.Iterator;
  4. import java.util.List;
  5. import java.util.logging.Level;
  6. import java.util.logging.Logger;
  7. import org.bukkit.Location;
  8. import org.bukkit.Material;
  9. import org.bukkit.Server;
  10. import org.bukkit.World;
  11. import org.bukkit.block.Block;
  12. import org.bukkit.entity.Player;
  13. import org.bukkit.event.block.Action;
  14. import org.bukkit.event.player.PlayerAnimationEvent;
  15. import org.bukkit.event.player.PlayerAnimationType;
  16. import org.bukkit.event.player.PlayerBucketEmptyEvent;
  17. import org.bukkit.event.player.PlayerChatEvent;
  18. import org.bukkit.event.player.PlayerCommandPreprocessEvent;
  19. import org.bukkit.event.player.PlayerEggThrowEvent;
  20. import org.bukkit.event.player.PlayerInteractEvent;
  21. import org.bukkit.event.player.PlayerJoinEvent;
  22. import org.bukkit.event.player.PlayerListener;
  23. import org.bukkit.event.player.PlayerLoginEvent;
  24. import org.bukkit.event.player.PlayerLoginEvent.Result;
  25. import org.bukkit.event.player.PlayerMoveEvent;
  26. import org.bukkit.event.player.PlayerQuitEvent;
  27. import org.bukkit.event.player.PlayerRespawnEvent;
  28. import org.bukkit.event.player.PlayerTeleportEvent;
  29. import org.bukkit.inventory.ItemStack;
  30. public class EssentialsPlayerListener extends PlayerListener
  31. {
  32. private static final Logger LOGGER = Logger.getLogger("Minecraft");
  33. private final transient Server server;
  34. private final transient IEssentials ess;
  35. public EssentialsPlayerListener(final IEssentials parent)
  36. {
  37. this.ess = parent;
  38. this.server = parent.getServer();
  39. }
  40. @Override
  41. public void onPlayerRespawn(final PlayerRespawnEvent event)
  42. {
  43. final User user = ess.getUser(event.getPlayer());
  44. user.setDisplayName(user.getNick());
  45. updateCompass(user);
  46. if (ess.getSettings().changeDisplayName())
  47. {
  48. user.setDisplayName(user.getNick());
  49. }
  50. }
  51. @Override
  52. public void onPlayerChat(final PlayerChatEvent event)
  53. {
  54. final User user = ess.getUser(event.getPlayer());
  55. if (user.isMuted())
  56. {
  57. event.setCancelled(true);
  58. user.sendMessage(Util.i18n("playerMuted"));
  59. LOGGER.info(Util.format("mutedUserSpeaks", user.getName()));
  60. }
  61. final Iterator<Player> it = event.getRecipients().iterator();
  62. while (it.hasNext())
  63. {
  64. final User u = ess.getUser(it.next());
  65. if (u.isIgnoredPlayer(user.getName()))
  66. {
  67. it.remove();
  68. }
  69. }
  70. user.updateActivity(true);
  71. if (ess.getSettings().changeDisplayName())
  72. {
  73. user.setDisplayName(user.getNick());
  74. }
  75. }
  76. @Override
  77. public void onPlayerMove(final PlayerMoveEvent event)
  78. {
  79. if (event.isCancelled())
  80. {
  81. return;
  82. }
  83. final User user = ess.getUser(event.getPlayer());
  84. if (user.isAfk() && ess.getSettings().getFreezeAfkPlayers())
  85. {
  86. final Location from = event.getFrom();
  87. final Location to = event.getTo().clone();
  88. to.setX(from.getX());
  89. to.setY(from.getY());
  90. to.setZ(from.getZ());
  91. try
  92. {
  93. event.setTo(Util.getSafeDestination(to));
  94. }
  95. catch (Exception ex)
  96. {
  97. event.setTo(to);
  98. }
  99. return;
  100. }
  101. Location afk = user.getAfkPosition();
  102. if (afk == null || !event.getTo().getWorld().equals(afk.getWorld()) || afk.distanceSquared(event.getTo()) > 9) {
  103. user.updateActivity(true);
  104. }
  105. if (!ess.getSettings().getNetherPortalsEnabled())
  106. {
  107. return;
  108. }
  109. final Block block = event.getPlayer().getWorld().getBlockAt(event.getTo().getBlockX(), event.getTo().getBlockY(), event.getTo().getBlockZ());
  110. final List<World> worlds = server.getWorlds();
  111. if (block.getType() == Material.PORTAL && worlds.size() > 1 && user.isAuthorized("essentials.portal"))
  112. {
  113. if (user.getJustPortaled())
  114. {
  115. return;
  116. }
  117. World nether = server.getWorld(ess.getSettings().getNetherName());
  118. if (nether == null)
  119. {
  120. for (World world : worlds)
  121. {
  122. if (world.getEnvironment() == World.Environment.NETHER)
  123. {
  124. nether = world;
  125. break;
  126. }
  127. }
  128. if (nether == null)
  129. {
  130. return;
  131. }
  132. }
  133. final World world = user.getWorld() == nether ? worlds.get(0) : nether;
  134. double factor;
  135. if (user.getWorld().getEnvironment() == World.Environment.NETHER && world.getEnvironment() == World.Environment.NORMAL)
  136. {
  137. factor = ess.getSettings().getNetherRatio();
  138. }
  139. else if (user.getWorld().getEnvironment() == World.Environment.NORMAL && world.getEnvironment() == World.Environment.NETHER)
  140. {
  141. factor = 1.0 / ess.getSettings().getNetherRatio();
  142. }
  143. else
  144. {
  145. factor = 1.0;
  146. }
  147. Location loc = event.getTo();
  148. int x = loc.getBlockX();
  149. int y = loc.getBlockY();
  150. int z = loc.getBlockZ();
  151. if (user.getWorld().getBlockAt(x, y, z - 1).getType() == Material.PORTAL)
  152. {
  153. z--;
  154. }
  155. if (user.getWorld().getBlockAt(x - 1, y, z).getType() == Material.PORTAL)
  156. {
  157. x--;
  158. }
  159. x = (int)(x * factor);
  160. z = (int)(z * factor);
  161. loc = new Location(world, x + .5, y, z + .5);
  162. Block dest = world.getBlockAt(x, y, z);
  163. NetherPortal portal = NetherPortal.findPortal(dest);
  164. if (portal == null)
  165. {
  166. if (world.getEnvironment() == World.Environment.NETHER || ess.getSettings().getGenerateExitPortals())
  167. {
  168. portal = NetherPortal.createPortal(dest);
  169. LOGGER.info(Util.format("userCreatedPortal", event.getPlayer().getName()));
  170. user.sendMessage(Util.i18n("generatingPortal"));
  171. loc = portal.getSpawn();
  172. }
  173. }
  174. else
  175. {
  176. LOGGER.info(Util.format("userUsedPortal", event.getPlayer().getName()));
  177. user.sendMessage(Util.i18n("usingPortal"));
  178. loc = portal.getSpawn();
  179. }
  180. event.setFrom(loc);
  181. event.setTo(loc);
  182. try
  183. {
  184. user.getTeleport().now(loc, new Trade("portal", ess));
  185. }
  186. catch (Exception ex)
  187. {
  188. user.sendMessage(ex.getMessage());
  189. }
  190. user.setJustPortaled(true);
  191. user.sendMessage(Util.i18n("teleportingPortal"));
  192. event.setCancelled(true);
  193. return;
  194. }
  195. user.setJustPortaled(false);
  196. }
  197. @Override
  198. public void onPlayerQuit(final PlayerQuitEvent event)
  199. {
  200. final User user = ess.getUser(event.getPlayer());
  201. if (ess.getSettings().removeGodOnDisconnect() && user.isGodModeEnabled())
  202. {
  203. user.toggleGodModeEnabled();
  204. }
  205. if (user.getSavedInventory() != null)
  206. {
  207. user.getInventory().setContents(user.getSavedInventory());
  208. user.setSavedInventory(null);
  209. }
  210. user.updateActivity(false);
  211. user.dispose();
  212. if (!ess.getSettings().getReclaimSetting())
  213. {
  214. return;
  215. }
  216. final Thread thread = new Thread(new Runnable()
  217. {
  218. public void run()
  219. {
  220. try
  221. {
  222. Thread.sleep(1000);
  223. Runtime rt = Runtime.getRuntime();
  224. double mem = rt.freeMemory();
  225. rt.runFinalization();
  226. rt.gc();
  227. mem = rt.freeMemory() - mem;
  228. mem /= 1024 * 1024;
  229. LOGGER.log(Level.INFO, Util.format("freedMemory", mem));
  230. }
  231. catch (InterruptedException ex)
  232. {
  233. return;
  234. }
  235. }
  236. });
  237. thread.setPriority(Thread.MIN_PRIORITY);
  238. thread.start();
  239. }
  240. @Override
  241. public void onPlayerJoin(final PlayerJoinEvent event)
  242. {
  243. ess.getBackup().onPlayerJoin();
  244. final User user = ess.getUser(event.getPlayer());
  245. if (ess.getSettings().changeDisplayName())
  246. {
  247. user.setDisplayName(user.getNick());
  248. }
  249. user.updateActivity(false);
  250. if (user.isAuthorized("essentials.sleepingignored"))
  251. {
  252. user.setSleepingIgnored(true);
  253. }
  254. if (!ess.getSettings().isCommandDisabled("motd") && user.isAuthorized("essentials.motd"))
  255. {
  256. for (String m : ess.getMotd(user, null))
  257. {
  258. if (m == null)
  259. {
  260. continue;
  261. }
  262. user.sendMessage(m);
  263. }
  264. }
  265. if (!ess.getSettings().isCommandDisabled("mail") && user.isAuthorized("essentials.mail"))
  266. {
  267. final List<String> mail = user.getMails();
  268. if (mail.isEmpty())
  269. {
  270. user.sendMessage(Util.i18n("noNewMail"));
  271. }
  272. else
  273. {
  274. user.sendMessage(Util.format("youHaveNewMail", mail.size()));
  275. }
  276. }
  277. }
  278. @Override
  279. public void onPlayerLogin(final PlayerLoginEvent event)
  280. {
  281. if (event.getResult() != Result.ALLOWED && event.getResult() != Result.KICK_FULL && event.getResult() != Result.KICK_BANNED)
  282. {
  283. return;
  284. }
  285. User user = ess.getUser(event.getPlayer());
  286. user.setNPC(false);
  287. final long currentTime = System.currentTimeMillis();
  288. user.checkBanTimeout(currentTime);
  289. user.checkMuteTimeout(currentTime);
  290. user.checkJailTimeout(currentTime);
  291. if (user.isBanned())
  292. {
  293. final String banReason = user.getBanReason();
  294. event.disallow(Result.KICK_BANNED, banReason != null && !banReason.isEmpty() ? banReason : Util.i18n("defaultBanReason"));
  295. return;
  296. }
  297. if (server.getOnlinePlayers().length >= server.getMaxPlayers() && !user.isAuthorized("essentials.joinfullserver"))
  298. {
  299. event.disallow(Result.KICK_FULL, Util.i18n("serverFull"));
  300. return;
  301. }
  302. event.allow();
  303. user.setLastLogin(System.currentTimeMillis());
  304. updateCompass(user);
  305. }
  306. private void updateCompass(final User user)
  307. {
  308. try
  309. {
  310. user.setCompassTarget(user.getHome(user.getLocation()));
  311. }
  312. catch (Exception ex)
  313. {
  314. }
  315. }
  316. @Override
  317. public void onPlayerTeleport(PlayerTeleportEvent event)
  318. {
  319. if (event.isCancelled())
  320. {
  321. return;
  322. }
  323. final User user = ess.getUser(event.getPlayer());
  324. if (ess.getSettings().changeDisplayName())
  325. {
  326. user.setDisplayName(user.getNick());
  327. }
  328. updateCompass(user);
  329. }
  330. @Override
  331. public void onPlayerInteract(final PlayerInteractEvent event)
  332. {
  333. if (event.isCancelled())
  334. {
  335. return;
  336. }
  337. if (event.getAction() != Action.RIGHT_CLICK_BLOCK)
  338. {
  339. return;
  340. }
  341. if (ess.getSettings().getBedSetsHome() && event.getClickedBlock().getType() == Material.BED_BLOCK)
  342. {
  343. try
  344. {
  345. final User user = ess.getUser(event.getPlayer());
  346. user.setHome();
  347. user.sendMessage(Util.i18n("homeSetToBed"));
  348. }
  349. catch (Throwable ex)
  350. {
  351. }
  352. }
  353. }
  354. @Override
  355. public void onPlayerEggThrow(final PlayerEggThrowEvent event)
  356. {
  357. final User user = ess.getUser(event.getPlayer());
  358. final ItemStack is = new ItemStack(Material.EGG, 1);
  359. if (user.hasUnlimited(is))
  360. {
  361. user.getInventory().addItem(is);
  362. user.updateInventory();
  363. }
  364. }
  365. @Override
  366. public void onPlayerBucketEmpty(final PlayerBucketEmptyEvent event)
  367. {
  368. final User user = ess.getUser(event.getPlayer());
  369. if (user.hasUnlimited(new ItemStack(event.getBucket())))
  370. {
  371. event.getItemStack().setType(event.getBucket());
  372. ess.scheduleSyncDelayedTask(new Runnable()
  373. {
  374. public void run()
  375. {
  376. user.updateInventory();
  377. }
  378. });
  379. }
  380. }
  381. @Override
  382. public void onPlayerAnimation(final PlayerAnimationEvent event)
  383. {
  384. usePowertools(event);
  385. }
  386. private void usePowertools(final PlayerAnimationEvent event)
  387. {
  388. if (event.getAnimationType() != PlayerAnimationType.ARM_SWING)
  389. {
  390. return;
  391. }
  392. final User user = ess.getUser(event.getPlayer());
  393. final ItemStack is = user.getItemInHand();
  394. if (is == null || is.getType() == Material.AIR)
  395. {
  396. return;
  397. }
  398. final List<String> commandList = user.getPowertool(is);
  399. if (commandList == null || commandList.isEmpty())
  400. {
  401. return;
  402. }
  403. // We need to loop through each command and execute
  404. for (String command : commandList)
  405. {
  406. if (command.matches(".*\\{player\\}.*"))
  407. {
  408. //user.sendMessage("Click a player to use this command");
  409. continue;
  410. }
  411. else if (command.startsWith("c:"))
  412. {
  413. for (Player p : server.getOnlinePlayers())
  414. {
  415. p.sendMessage(user.getDisplayName() + ":" + command.substring(2));
  416. }
  417. }
  418. else
  419. {
  420. user.getServer().dispatchCommand(event.getPlayer(), command);
  421. }
  422. }
  423. }
  424. @Override
  425. public void onPlayerCommandPreprocess(final PlayerCommandPreprocessEvent event)
  426. {
  427. if (event.isCancelled())
  428. {
  429. return;
  430. }
  431. final User user = ess.getUser(event.getPlayer());
  432. final String cmd = event.getMessage().toLowerCase().split(" ")[0].replace("/", "").toLowerCase();
  433. final List<String> commands = Arrays.asList("msg", "r", "mail", "m", "t", "emsg", "tell", "er", "reply", "ereply", "email");
  434. if (commands.contains(cmd))
  435. {
  436. for (Player player : ess.getServer().getOnlinePlayers())
  437. {
  438. if (ess.getUser(player).isSocialSpyEnabled())
  439. {
  440. player.sendMessage(user.getDisplayName() + " : " + event.getMessage());
  441. }
  442. }
  443. }
  444. if (!cmd.equalsIgnoreCase("afk"))
  445. {
  446. user.updateActivity(true);
  447. }
  448. }
  449. }