Skip to content

Commit

Permalink
Added ProxyDefineCommandsEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
brickmonster committed Apr 3, 2024
1 parent 84d0ea7 commit 14a8c3f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
@@ -0,0 +1,35 @@
package net.md_5.bungee.api.event;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import net.md_5.bungee.api.connection.Connection;
import net.md_5.bungee.api.plugin.Command;

/**
* Called when the proxy intercepts the Commands packet, allowing plugins to
* hide commands that clients have permission for without disabling them.
*/
@Data
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class ProxyDefineCommandsEvent extends TargetedEvent
{
/**
* The commands to send to the player
*/
private final Map<String, Command> commands;

public ProxyDefineCommandsEvent(Connection sender, Connection receiver, Collection<Map.Entry<String, Command>> commands)
{
super( sender, receiver );
this.commands = new HashMap<>( commands.size() );
for ( Map.Entry<String, Command> command : commands )
{
this.commands.put( command.getKey(), command.getValue() );
}
}
}
Expand Up @@ -19,6 +19,7 @@
import java.io.DataInput;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand All @@ -36,6 +37,7 @@
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.connection.Server;
import net.md_5.bungee.api.event.PluginMessageEvent;
import net.md_5.bungee.api.event.ProxyDefineCommandsEvent;
import net.md_5.bungee.api.event.ServerConnectEvent;
import net.md_5.bungee.api.event.ServerDisconnectEvent;
import net.md_5.bungee.api.event.ServerKickEvent;
Expand Down Expand Up @@ -743,13 +745,32 @@ public void handle(Respawn respawn)
@Override
public void handle(Commands commands) throws Exception
{
final Collection<Map.Entry<String, Command>> unmodifiedCommandMap = bungee.getPluginManager().getCommands();
final ProxyDefineCommandsEvent event = new ProxyDefineCommandsEvent( server, con, unmodifiedCommandMap );
bungee.getPluginManager().callEvent( event );

boolean modified = false;

for ( Map.Entry<String, Command> command : bungee.getPluginManager().getCommands() )
// If an event handler REMOVED OR CHANGED a command then set modified.
for ( Map.Entry<String, Command> unmodifiedCommand : unmodifiedCommandMap )
{
if ( event.getCommands().get( unmodifiedCommand.getKey() ) != unmodifiedCommand.getValue() )
{
modified = true;
}
}

for ( Map.Entry<String, Command> command : event.getCommands().entrySet() )
{
// If an event handler ADDED a command then set modified.
if ( unmodifiedCommandMap.stream().noneMatch( entry -> entry.getKey().equals( command.getKey() ) ) )
{
modified = true;
}

if ( !bungee.getDisabledCommands().contains( command.getKey() ) && commands.getRoot().getChild( command.getKey() ) == null && command.getValue().hasPermission( con ) )
{
CommandNode dummy = LiteralArgumentBuilder.literal( command.getKey() ).executes( DUMMY_COMMAND )
CommandNode<?> dummy = LiteralArgumentBuilder.literal( command.getKey() ).executes( DUMMY_COMMAND )
.then( RequiredArgumentBuilder.argument( "args", StringArgumentType.greedyString() )
.suggests( Commands.SuggestionRegistry.ASK_SERVER ).executes( DUMMY_COMMAND ) )
.build();
Expand Down

0 comments on commit 14a8c3f

Please sign in to comment.