/src/main/java/com/sk89q/commandbook/commands/MessageCommands.java

https://github.com/Noobidoo/commandbook · Java · 183 lines · 123 code · 34 blank · 26 comment · 17 complexity · 606466bc1621c6a3c0e81f16379baf78 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.commands;
  20. import static com.sk89q.commandbook.CommandBookUtil.replaceColorMacros;
  21. import java.util.logging.Logger;
  22. import org.bukkit.ChatColor;
  23. import org.bukkit.command.CommandSender;
  24. import org.bukkit.entity.Player;
  25. import org.bukkit.event.player.PlayerChatEvent;
  26. import com.sk89q.commandbook.CommandBookPlugin;
  27. import com.sk89q.commandbook.events.CommandSenderMessageEvent;
  28. import com.sk89q.commandbook.events.SharedMessageEvent;
  29. import com.sk89q.minecraft.util.commands.Command;
  30. import com.sk89q.minecraft.util.commands.CommandContext;
  31. import com.sk89q.minecraft.util.commands.CommandException;
  32. import com.sk89q.minecraft.util.commands.CommandPermissions;
  33. public class MessageCommands {
  34. protected static final Logger logger = Logger.getLogger("Minecraft.CommandBook");
  35. @Command(aliases = {"me"},
  36. usage = "<message...>", desc = "Send an action message",
  37. min = 1, max = -1)
  38. @CommandPermissions({"commandbook.say.me"})
  39. public static void me(CommandContext args, CommandBookPlugin plugin,
  40. CommandSender sender) throws CommandException {
  41. if (sender instanceof Player && plugin.getAdminSession((Player) sender).isMute()) {
  42. sender.sendMessage(ChatColor.RED + "You are muted.");
  43. return;
  44. }
  45. String name = plugin.toName(sender);
  46. String msg = args.getJoinedStrings(0);
  47. plugin.getServer().getPluginManager().callEvent(
  48. new SharedMessageEvent(name + " " + msg));
  49. Logger.getLogger("Minecraft").info("<" + name + ">: " + msg);
  50. plugin.getServer().broadcastMessage("* " + name + " " + msg);
  51. }
  52. @Command(aliases = {"say"},
  53. usage = "<message...>", desc = "Send a message",
  54. min = 1, max = -1)
  55. @CommandPermissions({"commandbook.say"})
  56. public static void say(CommandContext args, CommandBookPlugin plugin,
  57. CommandSender sender) throws CommandException {
  58. if (sender instanceof Player && plugin.getAdminSession((Player) sender).isMute()) {
  59. sender.sendMessage(ChatColor.RED + "You are muted.");
  60. return;
  61. }
  62. String name = plugin.toColoredName(sender, ChatColor.WHITE);
  63. String msg = args.getJoinedStrings(0);
  64. if (sender instanceof Player) {
  65. PlayerChatEvent event = new PlayerChatEvent((Player) sender, msg);
  66. plugin.getServer().getPluginManager().callEvent(event);
  67. if (event.isCancelled()) {
  68. return;
  69. }
  70. }
  71. plugin.getServer().getPluginManager().callEvent(
  72. new CommandSenderMessageEvent(sender, msg));
  73. Logger.getLogger("Minecraft").info("<" + name + ">: " + msg);
  74. if (sender instanceof Player) {
  75. plugin.getServer().broadcastMessage(
  76. "<" + plugin.toColoredName(sender, ChatColor.WHITE)
  77. + "> " + args.getJoinedStrings(0));
  78. } else {
  79. plugin.getServer().broadcastMessage(
  80. replaceColorMacros(plugin.consoleSayFormat).replace(
  81. "%s", args.getJoinedStrings(0)));
  82. }
  83. }
  84. @Command(aliases = {"msg"},
  85. usage = "<target> <message...>", desc = "Private message a user",
  86. min = 2, max = -1)
  87. @CommandPermissions({"commandbook.msg"})
  88. public static void msg(CommandContext args, CommandBookPlugin plugin,
  89. CommandSender sender) throws CommandException {
  90. // This will throw errors as needed
  91. CommandSender receiver =
  92. plugin.matchPlayerOrConsole(sender, args.getString(0));
  93. String message = args.getJoinedStrings(1);
  94. if (receiver instanceof Player && plugin.getSession((Player) receiver).getIdleStatus() != null) {
  95. String status = plugin.getSession((Player) receiver).getIdleStatus();
  96. sender.sendMessage(ChatColor.GRAY + plugin.toName(receiver) + " is afk. "
  97. + "They might not see your message."
  98. + (status.isEmpty() ? "" : " (" + status + ")"));
  99. }
  100. receiver.sendMessage(ChatColor.GRAY + "(From "
  101. + plugin.toName(sender) + "): "
  102. + ChatColor.WHITE + message);
  103. sender.sendMessage(ChatColor.GRAY + "(To "
  104. + plugin.toName(receiver) + "): "
  105. + ChatColor.WHITE + message);
  106. logger.info("[CommandBook]" + plugin.toName(sender) + " told "
  107. + plugin.toName(receiver) + ": " + message);
  108. plugin.getSession(sender).setLastRecipient(receiver);
  109. // If the receiver hasn't had any player talk to them yet or hasn't
  110. // send a message, then we add it to the receiver's last message target
  111. // so s/he can /reply easily
  112. plugin.getSession(receiver).setNewLastRecipient(sender);
  113. }
  114. @Command(aliases = {"reply"},
  115. usage = "<message...>", desc = "Reply to last user",
  116. min = 1, max = -1)
  117. @CommandPermissions({"commandbook.msg"})
  118. public static void reply(CommandContext args, CommandBookPlugin plugin,
  119. CommandSender sender) throws CommandException {
  120. String message = args.getJoinedStrings(0);
  121. CommandSender receiver;
  122. String lastRecipient = plugin.getSession(sender).getLastRecipient();
  123. if (lastRecipient != null) {
  124. // This will throw errors as needed
  125. receiver = plugin.matchPlayerOrConsole(sender, lastRecipient);
  126. } else {
  127. sender.sendMessage(ChatColor.RED + "You haven't messaged anyone.");
  128. return;
  129. }
  130. if (receiver instanceof Player && plugin.getSession((Player) receiver).getIdleStatus() != null) {
  131. String status = plugin.getSession((Player) receiver).getIdleStatus();
  132. sender.sendMessage(ChatColor.GRAY + plugin.toName(receiver) + " is afk. "
  133. + "They might not see your message."
  134. + (status.isEmpty() ? "" : " (" + status + ")"));
  135. }
  136. receiver.sendMessage(ChatColor.GRAY + "(From "
  137. + plugin.toName(sender) + "): "
  138. + ChatColor.WHITE + message);
  139. sender.sendMessage(ChatColor.GRAY + "(To "
  140. + plugin.toName(receiver) + "): "
  141. + ChatColor.WHITE + message);
  142. logger.info("[CommandBook]" + plugin.toName(sender) + " told "
  143. + plugin.toName(receiver) + ": " + message);
  144. // If the receiver hasn't had any player talk to them yet or hasn't
  145. // send a message, then we add it to the receiver's last message target
  146. // so s/he can /reply easily
  147. plugin.getSession(receiver).setNewLastRecipient(sender);
  148. }
  149. }