#2723: Improved Send Command

Previously sending player(s) would always tell the command sender that
they were successfully summoned even if they were not. This change
checks the result of the callback to determine how many players were
sent based on which result (SUCCESS,EVENT_CANCEL,etc) and lists the
count for each type. Additionally a hoverable delimited list of player
names is shown for each result type message component in the chat.
This commit is contained in:
Mystiflow 2019-12-07 20:57:09 +00:00 committed by md_5
parent 065893b523
commit 7ed4c41d39

View File

@ -1,11 +1,23 @@
package net.md_5.bungee.module.cmd.send; package net.md_5.bungee.module.cmd.send;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
import java.util.Set; import java.util.Set;
import net.md_5.bungee.api.Callback;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.ServerConnectRequest;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.event.ServerConnectEvent; import net.md_5.bungee.api.event.ServerConnectEvent;
@ -15,6 +27,69 @@ import net.md_5.bungee.api.plugin.TabExecutor;
public class CommandSend extends Command implements TabExecutor public class CommandSend extends Command implements TabExecutor
{ {
protected static class SendCallback
{
private final Map<ServerConnectRequest.Result, List<String>> results = new HashMap<>();
private final CommandSender sender;
private int count = 0;
public SendCallback(CommandSender sender)
{
this.sender = sender;
for ( ServerConnectRequest.Result result : ServerConnectRequest.Result.values() )
{
results.put( result, new ArrayList<String>() );
}
}
public void lastEntryDone()
{
sender.sendMessage( ChatColor.GREEN.toString() + ChatColor.BOLD + "Send Results:" );
for ( Map.Entry<ServerConnectRequest.Result, List<String>> entry : results.entrySet() )
{
ComponentBuilder builder = new ComponentBuilder( "" );
if ( !entry.getValue().isEmpty() )
{
builder.event( new HoverEvent( HoverEvent.Action.SHOW_TEXT,
new ComponentBuilder( Joiner.on( ", " ).join( entry.getValue() ) ).color( ChatColor.YELLOW ).create() ) );
}
builder.append( entry.getKey().name() + ": " ).color( ChatColor.GREEN );
builder.append( "" + entry.getValue().size() ).bold( true );
sender.sendMessage( builder.create() );
}
}
public static class Entry implements Callback<ServerConnectRequest.Result>
{
private final SendCallback callback;
private final ProxiedPlayer target;
public Entry(SendCallback callback, ProxiedPlayer target)
{
this.callback = callback;
this.target = target;
this.callback.count++;
}
@Override
public void done(ServerConnectRequest.Result result, Throwable error)
{
callback.results.get( result ).add( target.getName() );
if ( result == ServerConnectRequest.Result.SUCCESS )
{
target.sendMessage( ProxyServer.getInstance().getTranslation( "you_got_summoned", target.getName(), callback.sender.getName() ) );
}
if ( --callback.count == 0 )
{
callback.lastEntryDone();
}
}
}
}
public CommandSend() public CommandSend()
{ {
super( "send", "bungeecord.command.send" ); super( "send", "bungeecord.command.send" );
@ -28,19 +103,17 @@ public class CommandSend extends Command implements TabExecutor
sender.sendMessage( ProxyServer.getInstance().getTranslation( "send_cmd_usage" ) ); sender.sendMessage( ProxyServer.getInstance().getTranslation( "send_cmd_usage" ) );
return; return;
} }
ServerInfo target = ProxyServer.getInstance().getServerInfo( args[1] ); ServerInfo server = ProxyServer.getInstance().getServerInfo( args[1] );
if ( target == null ) if ( server == null )
{ {
sender.sendMessage( ProxyServer.getInstance().getTranslation( "no_server" ) ); sender.sendMessage( ProxyServer.getInstance().getTranslation( "no_server" ) );
return; return;
} }
List<ProxiedPlayer> targets;
if ( args[0].equalsIgnoreCase( "all" ) ) if ( args[0].equalsIgnoreCase( "all" ) )
{ {
for ( ProxiedPlayer p : ProxyServer.getInstance().getPlayers() ) targets = new ArrayList<>( ProxyServer.getInstance().getPlayers() );
{
summon( p, target, sender );
}
} else if ( args[0].equalsIgnoreCase( "current" ) ) } else if ( args[0].equalsIgnoreCase( "current" ) )
{ {
if ( !( sender instanceof ProxiedPlayer ) ) if ( !( sender instanceof ProxiedPlayer ) )
@ -49,20 +122,14 @@ public class CommandSend extends Command implements TabExecutor
return; return;
} }
ProxiedPlayer player = (ProxiedPlayer) sender; ProxiedPlayer player = (ProxiedPlayer) sender;
for ( ProxiedPlayer p : player.getServer().getInfo().getPlayers() ) targets = new ArrayList<>( player.getServer().getInfo().getPlayers() );
{
summon( p, target, sender );
}
} else } else
{ {
// If we use a server name, send the entire server. This takes priority over players. // If we use a server name, send the entire server. This takes priority over players.
ServerInfo serverTarget = ProxyServer.getInstance().getServerInfo( args[0] ); ServerInfo serverTarget = ProxyServer.getInstance().getServerInfo( args[0] );
if ( serverTarget != null ) if ( serverTarget != null )
{ {
for ( ProxiedPlayer p : serverTarget.getPlayers() ) targets = new ArrayList<>( serverTarget.getPlayers() );
{
summon( p, target, sender );
}
} else } else
{ {
ProxiedPlayer player = ProxyServer.getInstance().getPlayer( args[0] ); ProxiedPlayer player = ProxyServer.getInstance().getPlayer( args[0] );
@ -71,19 +138,24 @@ public class CommandSend extends Command implements TabExecutor
sender.sendMessage( ProxyServer.getInstance().getTranslation( "user_not_online" ) ); sender.sendMessage( ProxyServer.getInstance().getTranslation( "user_not_online" ) );
return; return;
} }
summon( player, target, sender ); targets = Collections.singletonList( player );
} }
} }
sender.sendMessage( ProxyServer.getInstance().getTranslation( "successfully_summoned" ) );
}
private void summon(ProxiedPlayer player, ServerInfo target, CommandSender sender) final SendCallback callback = new SendCallback( sender );
{ Iterator<ProxiedPlayer> iterator = targets.iterator();
if ( player.getServer() != null && !player.getServer().getInfo().equals( target ) ) while ( iterator.hasNext() )
{ {
player.connect( target, ServerConnectEvent.Reason.COMMAND ); ProxiedPlayer target = iterator.next();
player.sendMessage( ProxyServer.getInstance().getTranslation( "you_got_summoned", target.getName(), sender.getName() ) ); ServerConnectRequest request = ServerConnectRequest.builder()
.target( server )
.reason( ServerConnectEvent.Reason.COMMAND )
.callback( new SendCallback.Entry( callback, target ) )
.build();
target.connect( request );
} }
sender.sendMessage( ChatColor.DARK_GREEN + "Attempting to send " + targets.size() + " players to " + server.getName() );
} }
@Override @Override