SelectEverywhere 1.0.3

Demo plugin to show simple usage of selector API.

  1. md_5
    Native Minecraft Version:
    1.13
    Tested Minecraft Versions:
    • 1.13
    Look At Me: .............. ... ...........I'm the description field.
    Dependencies: ........... .... .......Latest version of Spigot 1.13.2


    This is a demo plugin to show the new selector API. It will replace any @ selectors in commands that match a single entity with the entity's name (if player), or otherwise it's UUID.

    Source Code:
    Code (Java):

    package net.md_5;

    import com.google.common.base.Joiner;
    import java.util.List;
    import java.util.logging.Level;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    import org.bukkit.event.server.ServerCommandEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    public class SelectEverywhere extends JavaPlugin implements Listener
    {

        private boolean debug;

        @Override
        public void onEnable()
        {
            getConfig().addDefault( "debug", false );
            getConfig().options().copyDefaults( true );
            saveConfig();

            debug = getConfig().getBoolean( "debug" );

            getServer().getPluginManager().registerEvents( this, this );
        }

        private String handle(CommandSender sender, String command)
        {
            String[] args = command.split( " " );

            for ( int i = 0; i < args.length; i++ )
            {
                String arg = args[i];

                if ( arg.startsWith( "@" ) )
                {
                    try
                    {
                        List<Entity> selected = getServer().selectEntities( sender, arg );

                        if ( selected.size() == 1 )
                        {
                            Entity entity = selected.iterator().next();
                            args[i] = ( entity instanceof Player ) ? entity.getName() : entity.getUniqueId().toString();
                        } else
                        {
                            if ( debug )
                            {
                                getLogger().log( Level.INFO, "{2}: Could not select entities from {0} (got {1} entities)", new Object[]
                                {
                                    arg, selected.size(), sender
                                } );
                            }
                        }
                    } catch ( IllegalArgumentException ex )
                    {
                        if ( debug )
                        {
                            getLogger().log( Level.INFO, "{1}: Ignored unparseable selector {0}", new Object[]
                            {
                                arg, sender
                            } );
                        }
                    }
                }
            }

            return Joiner.on( ' ' ).join( args );
        }

        @EventHandler(priority = EventPriority.LOWEST)
        public void command(PlayerCommandPreprocessEvent event)
        {
            event.setMessage( handle( event.getPlayer(), event.getMessage() ) );
        }

        @EventHandler(priority = EventPriority.LOWEST)
        public void command(ServerCommandEvent event)
        {
            event.setCommand( handle( event.getSender(), event.getCommand() ) );
        }
    }


     
    Lee, DrOreo002, fren_gor and 8 others like this.

Recent Reviews

  1. MrMorganCz
    MrMorganCz
    5/5,
    Version: 1.0.1
    Nice, thank you added this feature!
    .............................................