How can i do like players can't send command to himself?

Discussion in 'Plugin Development' started by GorguZ, Jul 1, 2014.

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

    GorguZ

    Hello!

    I am creating a plugin and i need little help...

    I dont want like players can "hug" himself... I My command its /kram and players will "kram" others but not "kram" himself. Exempel: My ing name its gurraplurra01 so i will not be allowed to do /kram gurraplurra01 but i will be allowed to do /kram notch or /kram jeb_

    Whats the code?
     
  2. Offline

    fefe2008

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String CommandLabel, String[] args)
    2. {
    3. Player p = (Player)sender;
    4.  
    5. if ((cmd.getName().equalsIgnoreCase("kram")) && (p.hasPermission("kram.use")) &&
    6. (args.length == 1){
    7. Player target = getServer().getPlayer(args[0]);
    8. if (target == p){
    9. //You cannto hug yourself
    10. }
    11. else if (target != p)
    12. //Hug someone
    13. }
    14. }

    GorguZ

    Check if the player target of args[0] (in your case) is the command sender. If target and sender are identical do something.
     
  3. Offline

    CynutsBR

    1º wtf is kram? this word a actually dont exist in the google translate so i cant know what it is.
    something like dat, watch some tutorials in youtube about hugs i dont know nothing about this...
    Code:java
    1. public boolean Comand(CommandSender sender, Command cmd, String str, String[] args){
    2. if(sender instanceof Player){
    3. Player s = (Player) sender;
    4. if(cmd.getName().equalsIgnoreCase("kram")){//(i dont know wtf is dat)
    5. if(args[] !== s.getName()){
    6. //CAN "KRAM" (i dont know wtf is dat) AND MAKE LOVE
    7. }else{s.sendMessage(ChatColor.PURPLE + "U cant kram(i dont know wtf is dat) urself");}
    8. }else{return;}
    9. }
    10. return false;
    11. }
     
  4. Offline

    fefe2008

  5. Offline

    GorguZ

    Hmm... when i try to "kram" me i dont get a message that i have "kram" me but i get a message that i have been "kram" but me... Would i send the code?
     
  6. Offline

    fefe2008

    GorguZ Yes please send the code. That was meant to be an Example
     
  7. Offline

    GorguZ

    Here is the code, so far...
    Code:java
    1. package me.gustavleithe.Kram;
    2.  
    3.  
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9.  
    10.  
    11. public class Kram extends JavaPlugin{
    12.  
    13.  
    14.  
    15.  
    16. public void onEnable(){
    17. System.out.println("Kram onEnable");}
    18.  
    19. public void onDisable(){
    20. System.out.println("Kram onDisable");}
    21.  
    22.  
    23.  
    24. @SuppressWarnings("deprecation")
    25. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    26. Player player = (Player) sender;
    27. if(player.hasPermission("kram.sendkram")){
    28. }else{
    29. player.sendMessage(ChatColor.RED + ("Denna funktion är fortfarnde i BETA"));
    30. if(cmd.getName().equalsIgnoreCase("kram")){
    31.  
    32. }
    33.  
    34. if(args.length == 0){
    35. //kram = 0 args //kram gurraplurra01 = 1 args
    36. sender.sendMessage(ChatColor.RED + ("För få argument! Andvänd: /kram spelarnamn för korrekt andvändning!"));
    37. }else if (args.length == 1){
    38. Player target = getServer().getPlayer(args[0]);
    39. if (target == player){}
    40.  
    41.  
    42. else if (target != player)
    43.  
    44. sender.sendMessage(ChatColor.GREEN + ("Du har kramat" + ChatColor.YELLOW + player.getName() + ChatColor.GREEN + "!"));
    45. player.sendMessage(ChatColor.GREEN + ("Du har blivit kramad av" + ChatColor.YELLOW + sender.getName() + ChatColor.GREEN + "!"));
    46. }else{
    47. player.sendMessage(ChatColor.RED + ("Spelaren är inte online!"));
    48.  
    49.  
    50.  
    51. }
    52.  
    53. if (args.length > 2) {
    54. sender.sendMessage(ChatColor.RED + ("För många argument! Andvänd: /kram spelarnamn för korrekt andvändning!"));
    55.  
    56.  
    57.  
    58.  
    59.  
    60.  
    61. }
    62.  
    63.  
    64. }
    65. return true;
    66. }{
    67.  
    68.  
    69. }{
    70.  
    71.  
    72.  
    73.  
    74.  
    75. }}
    76.  
    77.  
     
  8. Offline

    MoeMix

    GorguZ
    Check the player names instead of player instances. Get the sender's name, then the targets's name (using args[1]) then use an if statement with those two variables.
     
    DoctorDark likes this.
  9. Offline

    GorguZ

    Can you please send a exempel?
     
  10. Offline

    fefe2008

    MoeMix Thats exactly what i said before xD
     
  11. Offline

    DoctorDark

    GorguZ
    if (sender.getName().equals(Bukkit.getServer().getPlayer(args[0]).getName()) {

    Something like this. Ofcourse check if that player isnt null first.
     
  12. Offline

    GorguZ

    Where should i put in this?
     
  13. Offline

    MoeMix

    fefe2008
    LOL. I don't read the previous conversation xD

    GorguZ
    I don't have access to a computer atm. However, this should be fairly simple.
    Get the sender's name..
    Then use an if statement that looks something like if(playername.equals(args[1]){ blah blah blah}

    What do you mean? it's an if statement so put it where you want to check before running a piece of code.

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

    GorguZ

    Dont work... Same like i say before and now when i try to "kram" a offline player i get i message that i have "kram" myself... Did you want to see that the code again with the code you give me?
     
  15. Offline

    MoeMix

  16. Offline

    GorguZ

    Code:java
    1. package me.gustavleithe.Kram;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9.  
    10.  
    11. public class Kram extends JavaPlugin{
    12.  
    13.  
    14.  
    15.  
    16. public void onEnable(){
    17. System.out.println("Kram onEnable");}
    18.  
    19. public void onDisable(){
    20. System.out.println("Kram onDisable");}
    21.  
    22.  
    23.  
    24. @SuppressWarnings("deprecation")
    25. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    26. Player player = (Player) sender;
    27. if(player.hasPermission("kram.sendkram")){
    28. }else{
    29. player.sendMessage(ChatColor.RED + ("Denna funktion är fortfarnde i BETA"));
    30. if(cmd.getName().equalsIgnoreCase("kram")){
    31.  
    32. }
    33.  
    34. if(args.length == 0){
    35. //kram = 0 args //kram gurraplurra01 = 1 args
    36. sender.sendMessage(ChatColor.RED + ("För få argument! Andvänd: /kram spelarnamn för korrekt andvändning!"));
    37. }else if (args.length == 1){
    38. Player target = getServer().getPlayer(args[0]);
    39. if (target == player){}
    40.  
    41.  
    42. else if (target != player)
    43.  
    44. sender.sendMessage(ChatColor.GREEN + ("Du har kramat" + ChatColor.YELLOW + player.getName() + ChatColor.GREEN + "!"));
    45. player.sendMessage(ChatColor.GREEN + ("Du har blivit kramad av" + ChatColor.YELLOW + sender.getName() + ChatColor.GREEN + "!"));
    46. }else{
    47. player.sendMessage(ChatColor.RED + ("Spelaren är inte online!"));
    48.  
    49. if (sender.getName().equals(Bukkit.getServer().getPlayer(args[0]).getName())) {
    50.  
    51. }
    52.  
    53. if (args.length > 2) {
    54. sender.sendMessage(ChatColor.RED + ("För många argument! Andvänd: /kram spelarnamn för korrekt andvändning!"));
    55.  
    56.  
    57.  
    58.  
    59.  
    60.  
    61. }
    62.  
    63.  
    64. }
    65.  
    66. }{
    67.  
    68.  
    69. }{
    70.  
    71.  
    72.  
    73.  
    74.  
    75. }
    76. return true;}}
    77.  
    78.  
     
  17. Offline

    MoeMix

    GorguZ
    No, in line 49 just do: if(sender.getName().equals(args[1]){ }
    This will check if the sender is trying to hug himself
     
  18. Offline

    GorguZ

    Dosent help... Same problem should i post the new code? :p
     
  19. Offline

    MoeMix

    GorguZ
    add me on skype: sudani_moe
    I don't want this thread to grow outrageously large over this topic.
     
  20. Offline

    GorguZ

    Uhm i only have skype on ipad so its would be hard to send files... Maybe can we use the PM function on bukkit?
     
  21. Offline

    Necrodoom

    Or just speak here, because taking it private is pointless.
     
  22. Offline

    MoeMix

    yeah ok
     
  23. Offline

    fefe2008

    GorguZ You should fromat your code a bit. Its hard to read with the brackets set up like that
     
  24. Offline

    MoeMix

    GorguZ
    I'm home now just send me your code and I'll edit it to what it should be.

    GorguZ
    Ok, final code. This should work:
    Code:java
    1. package me.gustavleithe.Kram;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9.  
    10.  
    11. public class Kram extends JavaPlugin{
    12.  
    13.  
    14.  
    15.  
    16. public void onEnable(){
    17. System.out.println("Kram onEnable");}
    18.  
    19. public void onDisable(){
    20. System.out.println("Kram onDisable");}
    21.  
    22.  
    23.  
    24. @SuppressWarnings("deprecation")
    25. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    26. Player player = (Player) sender;
    27. if(player.hasPermission("kram.sendkram")){
    28. }else{
    29. player.sendMessage(ChatColor.RED + ("Denna funktion är fortfarnde i BETA"));
    30. if(cmd.getName().equalsIgnoreCase("kram")){
    31.  
    32. }
    33.  
    34. if(args.length == 0){
    35. //kram = 0 args //kram gurraplurra01 = 1 args
    36. sender.sendMessage(ChatColor.RED + ("För få argument! Andvänd: /kram spelarnamn för korrekt andvändning!"));
    37. }
    38. else if(args.length == 1){
    39. if (args[1].equals(player.getName())){}
    40. if(Bukkit.getPlayer(args[1]).isOnline()){
    41. Bukkit.getPlayer(args[1]).sendMessage(ChatColor.GREEN + ("Du har kramat " + ChatColor.YELLOW + player.getName() + ChatColor.GREEN + "!"));
    42. player.sendMessage(ChatColor.GREEN + ("Du har blivit kramad av" + ChatColor.YELLOW + sender.getName() + ChatColor.GREEN + "!"));
    43. }
    44. else{
    45. player.sendMessage("Spelaren är inte online!");
    46. }
    47. }
    48.  
    49. if (args.length > 2) {
    50. sender.sendMessage(ChatColor.RED + ("För många argument! Andvänd: /kram spelarnamn för korrekt andvändning!"));
    51.  
    52.  
    53.  
    54.  
    55.  
    56.  
    57. }
    58.  
    59.  
    60. }
    61. return true;
    62.  
    63. }{
    64.  
    65.  
    66. }{
    67.  
    68.  
    69.  
    70.  
    71.  
    72. }{}}
    73.  


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

    Necrodoom

    MoeMix you seem to need to learn Java before trying to code, as your brackets are extremely off, you open and close them for no reason, your if checks are also problematic, as you make a blank if check, then do the code in the else when you could reverse the if, and you try to access args[1] when you made sure it wouldn't exist since args length is 1.
     
    L33m4n123 likes this.
  26. Offline

    MoeMix

    Necrodoom
    Trust me, if I rewrote this plugin for him, I would have been much more organized. You, however, are jumping to conclusions. I simply took his code and edited what needed to be fixed. He didn't ask for a complete formatting. I simply fixed his issue. And what do you mean my if checks are problematic? I am checking if the player is online then using an else statement if the player is not online. Also, args[1] does exist. there is an if statement checking if args.length = 1.
     
  27. Offline

    xTigerRebornx

    MoeMix args[1] does not exist, as that would be the 2nd argument. args[0] is the first argument.
    You also shouldn't just go around and "edit" in what needs to be fixed. Generally follows the same idea of Spoon-feeding, the person whos code you fix won't learn anything and expect that someone will always come and "edit" their code on here for them. This helps nobody, only makes OP more confused and irritates others when the OP comes with another post asking for code rather then asking for help.
    Edit: Your ifs are problematic because you simply have this blank if statement, and do everything in the else. Necrodoom is saying that you should reverse the if statement rather then use a blank if and then do everything in the else, makes the code look better and leads to better coding habits.
     
  28. Offline

    MoeMix

    xTigerRebornx
    the second argument is exactly what I'm talking about since the first argument is "kram"
    the command is /kram playername therefore playername is args[1]. Is there something I missed? kram would be args[0] and playername would be args[1]
     
  29. Offline

    xTigerRebornx

    MoeMix Yes, you may be referring to the 2nd argument, but you do a check if the args length is 1, not 2, then try to access args[1] even though you've already determined that there is only 1 argument, not 2.
     
  30. Offline

    MoeMix

    xTigerRebornx
    As for the problematic ifs I just edited the code I wasn't trying to make it perfect. Simply made it do what he wanted it to do from the OP. And I tried showing him how to do it but he wouldn't understand so I wrote it out for him hoping he would understand
     
Thread Status:
Not open for further replies.

Share This Page