diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandbroadcastworld.java b/Essentials/src/com/earth2me/essentials/commands/Commandbroadcastworld.java new file mode 100644 index 000000000..edc32c8b9 --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/commands/Commandbroadcastworld.java @@ -0,0 +1,60 @@ +package com.earth2me.essentials.commands; + +import static com.earth2me.essentials.I18n.tl; + +import com.earth2me.essentials.CommandSource; +import com.earth2me.essentials.User; +import com.earth2me.essentials.textreader.IText; +import com.earth2me.essentials.textreader.KeywordReplacer; +import com.earth2me.essentials.textreader.SimpleTextInput; +import com.earth2me.essentials.utils.FormatUtil; + +import org.bukkit.Server; +import org.bukkit.World; +import org.bukkit.entity.Player; + +import java.util.Collection; + + +public class Commandbroadcastworld extends EssentialsCommand { + + public Commandbroadcastworld() { + super("broadcastworld"); + } + + @Override + public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception { + sendBroadcast(user.getWorld().getName(), user.getDisplayName(), getFinalArg(args, 0)); + } + + @Override + public void run(final Server server, final CommandSource sender, final String commandLabel, final String[] args) throws Exception { + if (args.length < 2) { + throw new NotEnoughArgumentsException("world"); + } + sendBroadcast(args[0], sender.getSender().getName(), getFinalArg(args, 1)); + } + + private void sendBroadcast(final String worldName, final String name, final String message) throws Exception { + World world = ess.getWorld(worldName); + if (world == null) { + throw new Exception(tl("invalidWorld")); + } + sendToWorld(world, tl("broadcast", FormatUtil.replaceFormat(message).replace("\\n", "\n"), name)); + } + + private void sendToWorld(World world, String message) { + IText broadcast = new SimpleTextInput(message); + final Collection players = ess.getOnlinePlayers(); + + for (Player player : players) { + if (player.getWorld().equals(world)) { + final User user = ess.getUser(player); + broadcast = new KeywordReplacer(broadcast, new CommandSource(player), ess, false); + for (String messageText : broadcast.getLines()) { + user.sendMessage(messageText); + } + } + } + } +} diff --git a/Essentials/src/plugin.yml b/Essentials/src/plugin.yml index 619a77b38..d464a0db1 100644 --- a/Essentials/src/plugin.yml +++ b/Essentials/src/plugin.yml @@ -52,6 +52,10 @@ commands: description: Broadcasts a message to the entire server. usage: / aliases: [bc,ebc,bcast,ebcast,ebroadcast,shout,eshout] + broadcastworld: + description: Broadcasts a message to a world. + usage: / + aliases: [bcw,ebcw,bcastw,ebcastw,ebroadcastworld,shoutworld,eshoutworld] bigtree: description: Spawn a big tree where you are looking. usage: /