mirror of
https://github.com/EssentialsX/Essentials.git
synced 2025-02-04 22:41:33 +01:00
Create broadcastworld command for broadcasting to a world instead of the whole server.
This commit is contained in:
parent
134fbdf1df
commit
2d3691107d
@ -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<Player> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -52,6 +52,10 @@ commands:
|
||||
description: Broadcasts a message to the entire server.
|
||||
usage: /<command> <msg>
|
||||
aliases: [bc,ebc,bcast,ebcast,ebroadcast,shout,eshout]
|
||||
broadcastworld:
|
||||
description: Broadcasts a message to a world.
|
||||
usage: /<command> <world> <msg>
|
||||
aliases: [bcw,ebcw,bcastw,ebcastw,ebroadcastworld,shoutworld,eshoutworld]
|
||||
bigtree:
|
||||
description: Spawn a big tree where you are looking.
|
||||
usage: /<command> <tree|redwood|jungle>
|
||||
|
Loading…
Reference in New Issue
Block a user