PageRenderTime 53ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/sk89q/commandbook/CommandBookPlayerListener.java

https://github.com/Noobidoo/commandbook
Java | 219 lines | 143 code | 26 blank | 50 comment | 23 complexity | 812e4b1e3574caf8f5ec4bf6840bdffa MD5 | raw file
  1. // $Id$
  2. /*
  3. * CommandBook
  4. * Copyright (C) 2010, 2011 sk89q <http://www.sk89q.com>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.sk89q.commandbook;
  20. import static com.sk89q.commandbook.CommandBookUtil.replaceColorMacros;
  21. import static com.sk89q.commandbook.CommandBookUtil.sendMessage;
  22. import java.io.File;
  23. import java.io.FileNotFoundException;
  24. import java.io.IOException;
  25. import java.util.logging.Level;
  26. import java.util.logging.Logger;
  27. import java.util.regex.Pattern;
  28. import javax.sound.midi.InvalidMidiDataException;
  29. import javax.sound.midi.MidiUnavailableException;
  30. import org.bukkit.ChatColor;
  31. import org.bukkit.Location;
  32. import org.bukkit.World;
  33. import org.bukkit.block.Block;
  34. import org.bukkit.entity.Player;
  35. import org.bukkit.event.block.Action;
  36. import org.bukkit.event.player.PlayerChatEvent;
  37. import org.bukkit.event.player.PlayerInteractEvent;
  38. import org.bukkit.event.player.PlayerJoinEvent;
  39. import org.bukkit.event.player.PlayerListener;
  40. import org.bukkit.event.player.PlayerLoginEvent;
  41. import org.bukkit.event.player.PlayerRespawnEvent;
  42. import org.bukkit.event.player.PlayerLoginEvent.Result;
  43. import org.bukkit.event.player.PlayerQuitEvent;
  44. import com.sk89q.commandbook.events.MOTDSendEvent;
  45. import com.sk89q.commandbook.events.OnlineListSendEvent;
  46. import com.sk89q.jinglenote.MidiJingleSequencer;
  47. import org.bukkit.event.player.PlayerTeleportEvent;
  48. /**
  49. * Handler for player events.
  50. *
  51. * @author sk89q
  52. */
  53. public class CommandBookPlayerListener extends PlayerListener {
  54. protected static final Logger logger = Logger.getLogger("Minecraft.CommandBook");
  55. protected final static Pattern namePattern = Pattern.compile(
  56. "^[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_]{1,16}$");
  57. protected CommandBookPlugin plugin;
  58. /**
  59. * Construct the listener.
  60. *
  61. * @param plugin
  62. */
  63. public CommandBookPlayerListener(CommandBookPlugin plugin) {
  64. this.plugin = plugin;
  65. }
  66. /**
  67. * Called on player login.
  68. */
  69. @Override
  70. public void onPlayerLogin(PlayerLoginEvent event) {
  71. Player player = event.getPlayer();
  72. if (plugin.verifyNameFormat && !namePattern.matcher(player.getName()).matches()) {
  73. logger.info("Name verification: " + player.getName() + " was kicked " +
  74. "for having an invalid name (to disable, turn off verify-name-format in CommandBook)");
  75. event.disallow(Result.KICK_OTHER, "Invalid player name detected!");
  76. return;
  77. }
  78. try {
  79. if (plugin.getBanDatabase().isBannedName(event.getPlayer().getName())
  80. || plugin.getBanDatabase().isBannedAddress(
  81. event.getPlayer().getAddress().getAddress())) {
  82. event.disallow(Result.KICK_BANNED, plugin.getBanMessage());
  83. return;
  84. }
  85. } catch (NullPointerException e) {
  86. // Bug in CraftBukkit
  87. }
  88. }
  89. /**
  90. * Called on player chat.
  91. */
  92. @Override
  93. public void onPlayerChat(PlayerChatEvent event) {
  94. if (plugin.getAdminSession(event.getPlayer()).isMute()) {
  95. event.getPlayer().sendMessage(ChatColor.RED + "You are muted.");
  96. event.setCancelled(true);
  97. return;
  98. }
  99. }
  100. /**
  101. * Called on player join.
  102. */
  103. @Override
  104. public void onPlayerJoin(PlayerJoinEvent event) {
  105. Player player = event.getPlayer();
  106. // Trigger the session
  107. plugin.getSession(player).handleReconnect();
  108. // Show the MOTD.
  109. String motd = plugin.getMessage("motd");
  110. if (motd != null) {
  111. plugin.getServer().getPluginManager().callEvent(
  112. new MOTDSendEvent(player));
  113. sendMessage(player,
  114. replaceColorMacros(
  115. plugin.replaceMacros(
  116. player, motd)));
  117. }
  118. // Show the online list
  119. if (plugin.getConfiguration().getBoolean("online-on-join", true)) {
  120. plugin.getServer().getPluginManager().callEvent(
  121. new OnlineListSendEvent(player));
  122. CommandBookUtil.sendOnlineList(
  123. plugin.getServer().getOnlinePlayers(), player, plugin);
  124. }
  125. if (!plugin.disableMidi) {
  126. try {
  127. MidiJingleSequencer sequencer = new MidiJingleSequencer(
  128. new File(plugin.getDataFolder(), "intro.mid"));
  129. plugin.getJingleNoteManager().play(player, sequencer, 2000);
  130. } catch (MidiUnavailableException e) {
  131. logger.log(Level.WARNING, "CommandBook: Failed to access MIDI: "
  132. + e.getMessage());
  133. } catch (InvalidMidiDataException e) {
  134. logger.log(Level.WARNING, "CommandBook: Failed to read intro MIDI file: "
  135. + e.getMessage());
  136. } catch (FileNotFoundException e) {
  137. } catch (IOException e) {
  138. logger.log(Level.WARNING, "CommandBook: Failed to read intro MIDI file: "
  139. + e.getMessage());
  140. }
  141. }
  142. World defaultWorld = plugin.getServer().getWorlds().get(0);
  143. if (!new File(defaultWorld.getName() + File.separatorChar + "players" +
  144. File.separatorChar + player.getName() + ".dat").exists() && plugin.exactSpawn)
  145. player.teleport(plugin.getSpawnManager().getWorldSpawn(player.getWorld()));
  146. }
  147. /**
  148. * Called on player respawn.
  149. */
  150. @Override
  151. public void onPlayerRespawn(PlayerRespawnEvent event) {
  152. Player player = event.getPlayer();
  153. if (plugin.exactSpawn && !event.isBedSpawn()) {
  154. event.setRespawnLocation(plugin.getSpawnManager().getWorldSpawn(player.getWorld()));
  155. }
  156. }
  157. /**
  158. * Called when a player interacts.
  159. */
  160. @Override
  161. public void onPlayerInteract(PlayerInteractEvent event) {
  162. Player player = event.getPlayer();
  163. if (plugin.getSession(player).hasThor()) {
  164. if (!plugin.thorItems.contains(player.getItemInHand().getTypeId())) {
  165. return;
  166. }
  167. if (event.getAction() == Action.LEFT_CLICK_AIR) {
  168. Block block = player.getTargetBlock(null, 300);
  169. if (block != null) {
  170. player.getWorld().strikeLightning(block.getLocation());
  171. }
  172. } else if (event.getAction() == Action.LEFT_CLICK_BLOCK) {
  173. Block block = event.getClickedBlock();
  174. player.getWorld().strikeLightning(block.getLocation());
  175. }
  176. }
  177. }
  178. /**
  179. * Called on player disconnect.
  180. */
  181. @Override
  182. public void onPlayerQuit(PlayerQuitEvent event) {
  183. plugin.getSession(event.getPlayer()).handleDisconnect();
  184. plugin.getAdminSession(event.getPlayer()).handleDisconnect();
  185. plugin.getJingleNoteManager().stop(event.getPlayer());
  186. }
  187. @Override
  188. public void onPlayerTeleport(PlayerTeleportEvent event) {
  189. Location loc = event.getTo();
  190. if (loc.equals(loc.getWorld().getSpawnLocation()))
  191. event.setTo(plugin.getSpawnManager().getWorldSpawn(loc.getWorld()));
  192. }
  193. }