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

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