How do I make another player do this?

Discussion in 'Plugin Development' started by football70500, Aug 5, 2012.

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

    football70500

    So in my plugin, you are allowed to fly, i want to be able to type a command such as /fly <playername> and then set a different player to fly, how would I go about doing this?

    Code:JAVA
    1. package me.football70500.random;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.plugin.PluginDescriptionFile;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class random extends JavaPlugin{
    14. public final Logger logger = Logger.getLogger("Minecraft");
    15.  
    16. @Override
    17. public void onEnable() {
    18. PluginDescriptionFile pdfFile = this.getDescription();
    19. this.logger.info(pdfFile.getName() + " is now enabled.");
    20. }
    21. @Override
    22. public void onDisable() {
    23. PluginDescriptionFile pdfFile = this.getDescription();
    24. this.logger.info(pdfFile.getName() + " is now disabled.");
    25. }
    26.  
    27.  
    28. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    29. readCommand((Player) sender, commandLabel);
    30. return true;
    31. }
    32. public boolean readCommand(Player player, String command) {
    33. if(command.equalsIgnoreCase("fly")){
    34. if(player.hasPermission("simplefly.fly")){
    35. player.setAllowFlight(true);
    36. player.setFlying(isEnabled());
    37. player.sendMessage(ChatColor.AQUA + "You are now flying.");
    38. return true;
    39. }else{
    40. player.sendMessage(ChatColor.DARK_RED + "You do not have permission to fly!");
    41. }
    42.  
    43. }
    44. else if(command.equalsIgnoreCase("nofly")){
    45. if(player.hasPermission("simplefly.nofly")){
    46. player.setAllowFlight(false);
    47. player.setFlying(false);
    48. player.sendMessage(ChatColor.RED + "You are no longer flying.");
    49. return true;
    50. }
    51. }
    52. return false;
    53.  
    54. }
    55. }
     
  2. Offline

    TheSmallBones

    Player target = args[0]
    target.setFlying(true);

    Not sure if that's even close since I'm on my phone but you can try!
     
  3. Offline

    ZeusAllMighty11

    If you have essentials, you can dispatch the sudo command, and make args[0] the player and args[1] the command /fly
     
  4. Offline

    football70500

    I dont want to use essentials, i want to develop a plugin for it
     
  5. Offline

    digga

    The command /fly is already working and you are searching for how to get another player by name method? Dont know if i understood it right. If this is your question then i think its something like this.

    this.getServer().getPlayer("playername");
     
  6. Offline

    football70500

    So my code would look like this?

    Code:JAVA
    1. package me.football70500.random;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.plugin.PluginDescriptionFile;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class random extends JavaPlugin{
    14. public final Logger logger = Logger.getLogger("Minecraft");
    15.  
    16. @Override
    17. public void onEnable() {
    18. PluginDescriptionFile pdfFile = this.getDescription();
    19. this.logger.info(pdfFile.getName() + " is now enabled.");
    20. }
    21. @Override
    22. public void onDisable() {
    23. PluginDescriptionFile pdfFile = this.getDescription();
    24. this.logger.info(pdfFile.getName() + " is now disabled.");
    25. }
    26.  
    27.  
    28. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    29. readCommand((Player) sender, commandLabel);
    30. return true;
    31. }
    32. public boolean readCommand(Player player, String command) {
    33. if(command.equalsIgnoreCase("fly")){
    34. if(player.hasPermission("simplefly.fly")){
    35. this.getServer().getPlayer("playername");
    36. player.setAllowFlight(true);
    37. player.setFlying(isEnabled());
    38. player.sendMessage(ChatColor.AQUA + "You are now flying.");
    39. return true;
    40. }else{
    41. player.sendMessage(ChatColor.DARK_RED + "You do not have permission to fly!");
    42. }
    43.  
    44. }
    45. else if(command.equalsIgnoreCase("nofly")){
    46. if(player.hasPermission("simplefly.nofly")){
    47. this.getServer().getPlayer("playername");
    48. player.setAllowFlight(false);
    49. player.setFlying(false);
    50. player.sendMessage(ChatColor.RED + "You are no longer flying.");
    51. return true;
    52. }
    53. }
    54. return false;
    55.  
    56. }
    57. }
     
  7. Offline

    pzxc

    Line 29 - you are casting sender to a Player, but you never check if sender *is* a Player, if any command of your plugin is executed from the console it will cause an error. Always check type before casting, using something like if (sender instanceof Player)

    Line 32 - readCommand returns a boolean, but where it's called on line 29 it doesn't do anything with the boolean. Why return something you're not going to use? Either use the boolean in your onCommand, or change readCommand to be a void function

    Line 35 - You're using the literal string "playername", unless there happens to be a player on your server with the name "playername" (literally), this won't do what you want it to do. Instead of "playername" you will have to get the playername from the args sent with the command... which aren't passed as a parameter from the onCommand function to the readCommand function. Speaking of which, why is there is a readCommand function at all? Why not just put everything that is in readCommand, into onCommand... especially since onCommand does nothing except call readCommand, seems kind of pointless to have a separate function readCommand at all. If you do move everything to onCommand, you can derive the playername from the String[] args, if you don't move it and keep readCommand, you'll have to pass String[] args as a parameter to readCommand so that it can derive the playername.

    Line 36 - You're setting allowflight of player, but player refers to the person who executed the command. Don't you want to do another player? Then line 35 should be something like Player targetPlayer = getServer().getPlayer(...) and line 36 should be targetPlayer.setAllowFlight(true)

    Line 36 again - You have to check to make sure getPlayer() on line 35 didn't return null, which it will do in the case that it can't find a player by the name given, otherwise when you try to do setAllowFlight or anything else on it, it will cause a nullpointerexception error. So if (targetPlayer != null) { ... }

    Line 37 - what the heck is this: isEnabled()? I don't see a function defined by that name in your code anywhere...
     
  8. Offline

    football70500

    So How do i get the playername?
     
  9. Offline

    pzxc

    If you want the command to be: /fly [playername]
    Then you get the playername like this:
    if (args.length >= 1) String playername = args[0];

    You have to check args.length >= 1 because if they just type /fly, there will be no args[0] and it will cause an indexoutofbounds error
     
    ZeusAllMighty11 likes this.
Thread Status:
Not open for further replies.

Share This Page