Add Tab Completion loosely based on @TheUnnamedDude's work.

This commit is contained in:
md_5 2013-09-15 06:46:10 +10:00
parent 042f47cbb9
commit 29c897c9cf
6 changed files with 102 additions and 2 deletions

View File

@ -11,6 +11,7 @@ import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Stack; import java.util.Stack;
import java.util.jar.JarEntry; import java.util.jar.JarEntry;
@ -75,6 +76,11 @@ public class PluginManager
commandMap.values().remove( command ); commandMap.values().remove( command );
} }
public boolean dispatchCommand(CommandSender sender, String commandLine)
{
return dispatchCommand( sender, commandLine, null );
}
/** /**
* Execute a command if it is registered, else return false. * Execute a command if it is registered, else return false.
* *
@ -83,7 +89,7 @@ public class PluginManager
* arguments * arguments
* @return whether the command was handled * @return whether the command was handled
*/ */
public boolean dispatchCommand(CommandSender sender, String commandLine) public boolean dispatchCommand(CommandSender sender, String commandLine, List<String> tabResults)
{ {
String[] split = argsSplit.split( commandLine ); String[] split = argsSplit.split( commandLine );
// Check for chat that only contains " " // Check for chat that only contains " "
@ -112,8 +118,14 @@ public class PluginManager
String[] args = Arrays.copyOfRange( split, 1, split.length ); String[] args = Arrays.copyOfRange( split, 1, split.length );
try try
{
if ( tabResults == null )
{ {
command.execute( sender, args ); command.execute( sender, args );
} else if ( command instanceof TabExecutor )
{
tabResults.addAll( ( (TabExecutor) command ).onTabComplete( sender, args ) );
}
} catch ( Exception ex ) } catch ( Exception ex )
{ {
sender.sendMessage( ChatColor.RED + "An internal error occurred whilst executing this command, please check the console log for details." ); sender.sendMessage( ChatColor.RED + "An internal error occurred whilst executing this command, please check the console log for details." );

View File

@ -0,0 +1,11 @@
package net.md_5.bungee.api.plugin;
import net.md_5.bungee.api.CommandSender;
import java.util.List;
public interface TabExecutor
{
public List<String> onTabComplete(CommandSender sender, String[] args);
}

View File

@ -13,6 +13,7 @@ import net.md_5.bungee.protocol.packet.Packet2Handshake;
import net.md_5.bungee.protocol.packet.Packet3Chat; import net.md_5.bungee.protocol.packet.Packet3Chat;
import net.md_5.bungee.protocol.packet.Packet9Respawn; import net.md_5.bungee.protocol.packet.Packet9Respawn;
import net.md_5.bungee.protocol.packet.PacketC9PlayerListItem; import net.md_5.bungee.protocol.packet.PacketC9PlayerListItem;
import net.md_5.bungee.protocol.packet.PacketCBTabComplete;
import net.md_5.bungee.protocol.packet.PacketCCSettings; import net.md_5.bungee.protocol.packet.PacketCCSettings;
import net.md_5.bungee.protocol.packet.PacketCDClientStatus; import net.md_5.bungee.protocol.packet.PacketCDClientStatus;
import net.md_5.bungee.protocol.packet.PacketCEScoreboardObjective; import net.md_5.bungee.protocol.packet.PacketCEScoreboardObjective;
@ -56,6 +57,7 @@ public class Vanilla implements Protocol
classes[0xC9] = PacketC9PlayerListItem.class; classes[0xC9] = PacketC9PlayerListItem.class;
classes[0x2C] = Packet2CEntityProperties.class; classes[0x2C] = Packet2CEntityProperties.class;
classes[0xCC] = PacketCCSettings.class; classes[0xCC] = PacketCCSettings.class;
classes[0xCB] = PacketCBTabComplete.class;
classes[0xCD] = PacketCDClientStatus.class; classes[0xCD] = PacketCDClientStatus.class;
classes[0xCE] = PacketCEScoreboardObjective.class; classes[0xCE] = PacketCEScoreboardObjective.class;
classes[0xCF] = PacketCFScoreboardScore.class; classes[0xCF] = PacketCFScoreboardScore.class;

View File

@ -74,4 +74,8 @@ public abstract class AbstractPacketHandler
public void handle(PacketFFKick kick) throws Exception public void handle(PacketFFKick kick) throws Exception
{ {
} }
public void handle(PacketCBTabComplete tabComplete) throws Exception
{
}
} }

View File

@ -0,0 +1,56 @@
package net.md_5.bungee.protocol.packet;
import io.netty.buffer.ByteBuf;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
@Getter
@ToString
@EqualsAndHashCode(callSuper = false)
public class PacketCBTabComplete extends DefinedPacket
{
private String cursor;
private String[] commands;
private PacketCBTabComplete()
{
super( 0xCB );
}
public PacketCBTabComplete(String[] alternatives)
{
this();
commands = alternatives;
}
@Override
public void read(ByteBuf buf)
{
cursor = readString( buf );
}
@Override
public void write(ByteBuf buf)
{
String tab = "";
for ( String alternative : commands )
{
if ( tab.isEmpty() )
{
tab = alternative + " ";
} else
{
tab += "\0" + alternative + " ";
}
}
writeString( tab, buf );
}
@Override
public void handle(AbstractPacketHandler handler) throws Exception
{
handler.handle( this );
}
}

View File

@ -13,8 +13,11 @@ import net.md_5.bungee.netty.PacketHandler;
import net.md_5.bungee.netty.PacketWrapper; import net.md_5.bungee.netty.PacketWrapper;
import net.md_5.bungee.protocol.packet.Packet0KeepAlive; import net.md_5.bungee.protocol.packet.Packet0KeepAlive;
import net.md_5.bungee.protocol.packet.Packet3Chat; import net.md_5.bungee.protocol.packet.Packet3Chat;
import net.md_5.bungee.protocol.packet.PacketCBTabComplete;
import net.md_5.bungee.protocol.packet.PacketCCSettings; import net.md_5.bungee.protocol.packet.PacketCCSettings;
import net.md_5.bungee.protocol.packet.PacketFAPluginMessage; import net.md_5.bungee.protocol.packet.PacketFAPluginMessage;
import java.util.ArrayList;
import java.util.List;
public class UpstreamBridge extends PacketHandler public class UpstreamBridge extends PacketHandler
{ {
@ -89,6 +92,18 @@ public class UpstreamBridge extends PacketHandler
throw new CancelSendSignal(); throw new CancelSendSignal();
} }
@Override
public void handle(PacketCBTabComplete tabComplete) throws Exception
{
if ( tabComplete.getCursor().startsWith( "/" ) )
{
List<String> results = new ArrayList<>();
bungee.getPluginManager().dispatchCommand( con, tabComplete.getCursor(), results );
con.unsafe().sendPacket( new PacketCBTabComplete( results.toArray( new String[ results.size() ] ) ) );
throw new CancelSendSignal();
}
}
@Override @Override
public void handle(PacketCCSettings settings) throws Exception public void handle(PacketCCSettings settings) throws Exception
{ {