I need help for 2 things

Discussion in 'Plugin Development' started by azyhd, Jul 12, 2012.

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

    azyhd

    The first one: i have this code that lets u fly, but when it is turned on and u want it to go off the turn on message will apear and the flying will be turned on the 2nd time, how to fix?
    ------------------------------------------------------------------------------------------------------------------------
    if(commandLabel.equalsIgnoreCase("fly")){
    if(player.isOp()){
    if(!player.getAllowFlight()){
    player.setAllowFlight(true);
    player.sendMessage(ChatColor.GREEN + "Flying is now allowed!");
    }
    } else {
    player.setAllowFlight(false);
    player.sendMessage(ChatColor.RED + "Flying is now disallowed");
    }
    if(!player.isOp()){
    player.sendMessage(ChatColor.RED + "U need to be op to use this command");
    }
    }
    ------------------------------------------------------------------------------------------------------------------------
    The 2nd thing i want to ask is: how do u make it so u type /heal notch, and notch gets a message saying u have been healed by Wolftic?

    and my /feed command doesnt work

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

    mcgamer99

    Try this:
    Code:
        public boolean onCommand(CommandSender sender, Command command,String label, String[] args) {
            Player s = (Player) sender;
            Player target = s.getServer().getPlayer(args[0]);
           
            if (command.getName().equalsIgnoreCase("heal")) {
               
                if(args[1].equals(target) && args.lenght == 1) {
                    target.setHealth(20);
                    target.sendMessage("You have been healed by " + "" + s.getName());
                    return true;
                }
               
                return true;
            }
           
            return false;   
        }
    Try this:
    Code:
        public boolean onCommand(CommandSender sender, Command command,String label, String[] args) {
            Player s = (Player) sender;
            Player target = s.getServer().getPlayer(args[0]);
         
    if (command.getName().equalsIgnoreCase("feed")) {
     
    if(args.length == 0) {
    s.setFoodLevel(20);
    return true;
     
    if(args.length == 1 && args[1].equals(target)) {
    target.setFoodLevel(20);
    return true;
                            }
       }
    }
         
            return false; 
        }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  3. Offline

    one4me

    mcgamer99
    Do not cast Player on variables that may not be a Player.
     
  4. Offline

    mcgamer99

    You mean this?

    Code:
    Player s = (Player) sender;
    This is perfectly, if there isn't this, the sender is for Console and have a few Player methods, but with this, the sender is specified a Player and all player's methods can be used.
    This is useful and necessary for set health or food of players!
     
  5. azyhd
    About the 1st thing, does anything even trigger ? Because you're not printing enough information, you're just bolding that isOp() method which I have no ideea why you did that.
    So print some debug messages :)

    If your commands just don't trigger, post more code, preferably the entire onCommand() and make sure it's either in the main class OR it's an CommandExecutor extender class - but there you wouldn't need to check for command name, so I doubt you're using that.
    If you got errors, post them!

    Also, you've been using bad grammar too often and now it leaks into your projects, it looks really bad when the game prints "U need to be ...", gives the plugin a bad image in my opinion.


    mcgamer99
    I don't get your 2nd code... first you just assume it's a player and he send a command with at least 1 argument (using args[0]), then you're checking if the 2nd argument is equal to the Player object got from the 1st argument... and then you're checking if there's only 1 argument supplied... it's confusing, and the code will also agree when you try to use it.
    And your first code is as confusing and wrong as the other one.

    EDIT:
    No it's not. You don't understand how the command works... if you type the command from the server console you'll get an error for trying to cast ConsoleSender to Player which is impossible because the console doesn't have methods that player has, so you have to act accordingly, check if sender instanceof Player and do the code, otherwise send a message to sender notifying him that the command is only usable by players.

    You should read: http://forums.bukkit.org/threads/how-to-make-your-plugin-better.77899/ at the first thing :)
     
  6. Offline

    azyhd

    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            Player player = (Player) sender;
            if(commandLabel.equalsIgnoreCase("fly")){
                if(player.isOp()){
                    if(!player.getAllowFlight()){
                        player.setAllowFlight(true);
                        player.sendMessage(ChatColor.GREEN + "Flying is now allowed!");
                    }
                    } else {
                        player.setAllowFlight(false);
                        player.sendMessage(ChatColor.RED + "Flying is now disallowed");
                    }
                if(!player.isOp()){
                    player.sendMessage(ChatColor.RED + "You need to be op to use this command");
                    }
                }
    This is the hole code and btw my english isnt the best because i aint English.
     
  7. That's all you got from my post ? ... Then I'm sorry, I can't help if you won't cooperate :)

    Also, my native language is not english either, it's not even a germanic based language, it's a latin based one. :}
     
  8. Offline

    CevinWa

    try with an arraylist on the fly thing. it's the easiest way .
    Code:
    if(commandLabel.equalsIgnoreCase("fly")){
    if(player.isOp()){
    if(plugin.list.add(playername)){
    if(!player.getAllowFlight()){
    player.setAllowFlight(true);
    player.sendMessage(ChatColor.GREEN + "Flying is now allowed!");
    }
    }else if{
    if(plugin.list.contains(playername)){
    player.setAllowFlight(false)
    } else {
    player.setAllowFlight(false);
    player.sendMessage(ChatColor.RED + "Flying is now disallowed");
    }
    if(!player.isOp()){
    player.sendMessage(ChatColor.RED + "U need to be op to use this command");
    }
    }
    and then in the main plugins folder do
    Code:
    protected ArrayList<String> list;
    Here i wanted to call it list but you could call it whatever u whant.
    Then under ur onEnable do
    Code:
    this.list = new ArrayList<String>();
    it's much varnings about lines don't being used but it worked for me.

    and there u've done.
    :D
    //CevinWa
     
  9. an set is better than an list, because an set cant contain the same values, so is more memmory safe incase the plugin starters write bad code
     
  10. True, it's also faster with contains() than a List but it's slower when it must be looped...

    However, in this case, CevinWa, neither are good, the getAllowFlight() is internally stored and is the best option here.
     
Thread Status:
Not open for further replies.

Share This Page