1.7.10 Getting the args from a command into an Event

Discussion in 'Spigot Plugin Development' started by Brew, Feb 21, 2020.

  1. Hello Spigot Forums, I'm making a Rank & Punishment core & I need to find the way to get the args from my /grant command into my event that controls my grant inventory to add the first arg in the command to said rank.
     
  2. So what exactly are you doing? There might be a better way of doing this.
     
  3. Let me get this better, are you trying to use the command preprocess event to overwrite a command from another plugin? If yes, just let me say that is not, in my opinion, the best way to do this. But here's how I would have done it using your way.

    Code (Java):
    @EventHandler
    public void onCommand(PlayerCommandPreprocessEvent e) {

          if (e.getMessage() ! = null) {

               String[] args = e.getMessage().split(" ");

         }

    }
    I didn't use any IDE to write this short code, so let's skip the various intent errors. Anyway I tried to recreate the arguments, those in the normal method of creating commands, dividing the command each space in order to obtain an array of strings containing the arguments and starting from 0 as the original one.
     
  4. I don't understand what you are trying do to.
    To make a normal bukkit command do:
    Code (Text):
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    //actions here
        if(sender instanceof Player) { //check if the command sender is a player

        }
         return true;
    }
    There is a precommandevent, if you are trying to cancel commands.
     
  5. My problem isn't creating the command, I need to get the args from a command into an event
     
  6. Can you explain why you need that?
     
  7. Hey! Add my discord (cold#0006) and I can explain further, and show code etc.
     
  8. Create a custom event with a constructor requiring a string array.

    That’s not the point of a forum, others can’t solve their problems with your private DMs.
     
  9. I'm assuming you're using InventoryClickEvent, I suggest you to use InventoryHolder to get the player name.

    Example:
    Code (Java):
    class GrantInventoryHolder implements InventoryHolder {

      private final Player player;

      public GrantInventoryHolder(Player player){
        this.player = player;
      }

      public Player getPlayer(){
        return player;
      }

      public Inventory getInventory() {
            return null;
        }

    Code (Java):
    class InventoryClass implements Listener {

      // Create the Inventory
      Player target = Bukkit.getServer().getPlayer(args[0]);
      Inventory gui = Bukkit.createInventory(new GrantInventoryHolder(target), size, title);

      @EventHandler
      public void inventoryClick(InventoryClickEvent e){
        Player player = (Player) e.getWhoClicked();
        Inventory inv = e.getInventory();
        ItemStack item = e.getCurrentItem();
        if(inv.getHolder() instanceof GrantInventoryHolder){
          GrantInventoryHolder holder = (GrantInventoryHolder) inv.getHolder();
          // Get the player targeted player from /grant <player>
          Player target = holder.getPlayer();
        }
      }

    }
     
    #9 aglerr, Feb 24, 2020
    Last edited: Feb 24, 2020
  10. Just make a hashmap store the players name and args to it then when they click retrieve and delete said data.
     
  11. InventoryHolder shouldn't be used to identify custom GUIs. Instead, OP ought to create a wrapper class that contains his inventory and other information. It can then be stored as a value in a Map with a player/UUID as the key.
     
    • Agree Agree x 1
  12. OP only want to get the argument of the command, which is a player, you can get their name, uuid, and everything, however it works fine for me.
    https://www.spigotmc.org/resources/...-shift-rightclick-player-information-⭐.75394/
     
  13. It works, sure, but it's still not the intended use of the InventoryHolder class, as it is both being used to identify a plugin GUI and does not belong to an in-game entity. I'd also heard there were intentions to deprecate creating inventories with a specified holder - why it hasn't happened yet, I'm not sure.