How do I make a command for a special player? Go in please, I explain better inside!

Discussion in 'Plugin Development' started by adde, Mar 26, 2012.

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

    adde

    I want to know how to make a command that only works for a specified player?
    Any idea how I should do this?

    What I mean is, is it able to make a command only works for only ONE player? Like if I want only me to use the command: "/blabla" is there any method that make me able to do that, any questions on what I mean?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
  2. Offline

    Perdog

    Just add a check to make sure the player name matches your name, otherwise, tell them they can't use that command
    Code:java
    1.  
    2. Player player = null;
    3. if (sender instanceof Player) {
    4. player = (Player) sender;
    5. }
    6.  
    7. if (player != null && cmd.getName().equalsIgnoreCase("blabla")) {
    8. if (player.getName().equalsIgnoreCase("adde")) {
    9. // Do what you want the command to do
    10. return true;
    11. } else {
    12. player.sendMessage("You cannot use that command because you aren't adde");
    13. return true;
    14. }
    15. }
     
  3. Offline

    anerach

    Maybe you could make an if statement that checks the player's name
    Code:
    if(player.getName().equalsIgnoreCase("playername")) {
        //code here
    }
    
     
  4. Offline

    adde

    Ok thx guys, but do I have to do first a method in Main class, then a AddeCommand??
     
  5. Offline

    Perdog

    Method in main class? I think you mean the onCommand method but I'm not sure. Can you explain?

    EDIT: Below is an example :p
     
  6. Offline

    anerach

    You'll need an onCommand method
    Code:JAVA
    1. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    2. //code here
    3. }
     
  7. Offline

    adde

    YEs the onCommand method, now it looks like this:
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, Player player, PlayerChatEvent event) {
    if (sender instanceof Player) {
    player = (Player) sender;
    Player player1 = event.getPlayer(); // The player who joined
    player1.getInventory();
    }
     
    if (player != null && cmd.getName().equalsIgnoreCase("diamonds")) {
    if (player.getName().equalsIgnoreCase("addemod")) {
    new ItemStack(Material.DIAMOND, 64);
    return true;
    } else {
    player.sendMessage("You cannot use that command because you aren't addemod");
    return true;
    }
    }
    return false;
    }
    
     
  8. Offline

    Perdog

    Uhmm .... noooooooooooo

    Your onCommand method must look exactly like the one anerach posted
     
  9. Offline

    adde

    so that I had in onCommand, should be in my BlablaCommandExecutor.class??
     
  10. Offline

    anerach

    Place this in your main class file NOT the listener
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    2. Player player;
    3. if (sender instanceof Player)
    4. player = (Player) sender;
    5.  
    6. if(player == null)
    7. return false;
    8.  
    9. if (!cmd.getName().equalsIgnoreCase("diamonds"))
    10. return false;
    11.  
    12. if (player.getName().equalsIgnoreCase("addemod")) {
    13. player.getInventory().addItem(ItemStack(Material.DIAMOND, 64));
    14. return true;
    15. } else {
    16. player.sendMessage("You cannot use that command because you aren't addemod");
    17. return true;
    18. }
    19. return false;
    20. }
     
  11. Offline

    rockyandirou

    You can do


    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
                if (sender.getName().equals("adde"))
            {
                sender.sendMessage("Yay! You're adde!");
                return true;
                //Do your stuff here
            } else
            {
                sender.sendMessage("You're not adde.");
                //Do your stuff here
            }
     
  12. Offline

    adde

    it is, but do I need to create this file:
    Code:
    package com.fivegamings.plugins;
    
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.player.PlayerChatEvent;
    import org.bukkit.inventory.ItemStack;
    
    public class DiamondCommandExecutor implements CommandExecutor {
        
        private FiveGamings plugin;
     
        public DiamondCommandExecutor(FiveGamings plugin) {
            this.plugin = plugin;
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, Player player, PlayerChatEvent event) {
            if (sender instanceof Player) {
            player = (Player) sender;
            Player player1 = event.getPlayer(); // The player who joined
            player1.getInventory();
            }
    
            if (player != null && cmd.getName().equalsIgnoreCase("diamonds")) {
            if (player.getName().equalsIgnoreCase("addemod")) {
            new ItemStack(Material.DIAMOND, 64);
            return true;
            } else {
            player.sendMessage("You cannot use that command because you aren't addemod");
            return true;
            }
            }
            return false;
        }
    }
    
    
    
     
  13. Offline

    anerach

    You don't have to, just place the code I just posted in your main class file
     
  14. Offline

    adde

    Ok :)

    but now I have to add a method with ItemStack

    Nvm, I just put a "new" in front of ItemStack

    Thx it works, will test to login with my second acc and see if I can give myself diamonds with no addemod
    Edit: IT WORKS THX MAN!! :)

    Not even op's can use it xD

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
  15. Offline

    Perdog

    Well ya, OP's don't have your name, and this command is meant to work with only your name.
     
  16. Offline

    adde

  17. Offline

    adde

    Now, in 1.2.4-R1.0, I can't use it, it says: Access denied for addemod (Not an operator), then I make me op, but still the same

    Wait... nvm

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
Thread Status:
Not open for further replies.

Share This Page