Make a player perform a command?

Discussion in 'Plugin Development' started by tacos1223, May 13, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    tacos1223

    Hey, I was wondering, would it be possible to make a player perform a command when they right click a emerald or something? I have been trying something like this, so when a player right clicks a emerald, they will perform the command /buy from buycraft. But every time I try something like this, it keeps telling me unknown command.
     
  2. Offline

    rfsantos1996

    player.performCommand(String);
     
  3. Offline

    JoeyDevs

    You was to quik i was not sure if there was one because i remember i saw one but oke True. I think Problem Solved!
     
  4. Offline

    tacos1223

    Yea, that is what I was doing, the command works perfectly when I type /buy as a player, but when I right click the emerald it says unknown command. My guess is it is a exception from the buycraft plugin.

    Code:
      public void onPlayerInteract(PlayerInteractEvent e)
      {
        if (((e.getAction() == Action.RIGHT_CLICK_AIR) || (e.getAction().equals(Action.RIGHT_CLICK_BLOCK))) &&
          (e.getPlayer().getItemInHand().getType().equals(Material.EMERALD)) &&
          (e.getPlayer().getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.GREEN + "-=[" + ChatColor.AQUA + "Server Shop" + ChatColor.GREEN + "]=-"))) {
          e.getPlayer().performCommand("buy");
        }
      }
    This is not the whole class. Only a piece of it by the way.

    Never mind, I was able to get it working. All I did was use:
    Code:
                  e.getPlayer().chat("/buy");
    I think the reason for this was because with the chat, the plugin was allowing the player to type the command rather than the server executing it for them.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  5. Offline

    rfsantos1996

    I think perform command doesn't need "/" (or it needs). I had this issue before and I fixed by adding or removing the "/", but yeah, the .chat will work perfecly too (;
     
Thread Status:
Not open for further replies.

Share This Page