{Not-Fixed} Command gives no errors but doesn't work.

Discussion in 'Plugin Development' started by Jackcheetham, Oct 22, 2013.

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

    Jackcheetham

    I'm not sure why my code gives no errors but it will not work in game, even in console no errors.
    Any help?

    Plugin code:
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
    2. {
    3. Player player = (Player)sender;
    4. PlayerInventory pi = player.getInventory();
    5. if ((commandLabel.equalsIgnoreCase("wolf")) && (player.hasPermission("McArcade.wolf"))) {
    6. if (!wolf.contains(player.getName())) {
    7. player.sendMessage(ChatColor.GRAY + "[" + ChatColor.GREEN + "McArcade" + ChatColor.GRAY + "]" + ChatColor.BLUE + " Enjoy your wolf reward!");
    8. wolf.add(player.getName());
    9. player.getLocation().getWorld().spawnEntity(player.getLocation(), EntityType.WOLF);
    10. pi.addItem(new ItemStack[] { new ItemStack(Material.BONE, 5) });
    11. return true;
    12. }
    13. else if ((commandLabel.equalsIgnoreCase("horse")) && (player.hasPermission("McArcade.horse"))) {
    14. if (!horse.contains(player.getName())) {
    15. player.sendMessage(ChatColor.GRAY + "[" + ChatColor.GREEN + "McArcade" + ChatColor.GRAY + "]" + ChatColor.BLUE + " Enjoy your horse reward!");
    16. horse.add(player.getName());
    17. player.getLocation().getWorld().spawnEntity(player.getLocation(), EntityType.HORSE);
    18. pi.addItem(new ItemStack[] { new ItemStack(Material.APPLE, 10) });
    19. pi.addItem(new ItemStack[] { new ItemStack(Material.SADDLE, 1) });
    20. return true;
    21. }
    22. }
    23. }
    24. return false;
    25. }
    26. }


    Plugin.yml
    Code:java
    1. name: Horses
    2. main: me.jackcheetham.horses.Main
    3. version: 1.0
    4. author: Jackcheetham
    5. commands:
    6. horse:
    7. permission: McArcade.horse
    8. permission-message: You are not a diamond or emerald rank! If you belive this a error, talk to harry.
    9. description: Spawns horse reward!
    10. wolf:
    11. permission: McArcade.wolf
    12. permission-message: You are not a diamond or emerald rank! If you belive this a error, talk to harry.
    13. description: Spawns wolf reward!


    Thanks if you can help :)
     
  2. Offline

    Jalau

    commandLabel try to replace this with: cmd.getName().equalsIgnoreCase....! I use cmd.getName() in other plugins and it works fine :)
     
  3. Offline

    Jackcheetham


    I changed it to that, now the wolf works with commandLabel, but horse doesn't work with cmd or commandLabel :/

    new code is:
    Code:java
    1. package me.jackcheetham.horses;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5.  
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Material;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Entity;
    11. import org.bukkit.entity.EntityType;
    12. import org.bukkit.entity.Horse;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.entity.Wolf;
    15. import org.bukkit.event.EventHandler;
    16. import org.bukkit.event.Listener;
    17. import org.bukkit.event.player.PlayerQuitEvent;
    18. import org.bukkit.inventory.ItemStack;
    19. import org.bukkit.inventory.PlayerInventory;
    20. import org.bukkit.plugin.java.JavaPlugin;
    21.  
    22. public class Main extends JavaPlugin implements Listener {
    23.  
    24. @Override
    25. public void onEnable() {
    26. getServer().getPluginManager().registerEvents(this, this);
    27. }
    28. ArrayList<String> wolf = new ArrayList<String>();
    29. ArrayList<String> horse = new ArrayList<String>();
    30.  
    31. @SuppressWarnings("deprecation")
    32. @EventHandler
    33. public void playerWolf(PlayerQuitEvent e) {
    34. Player p = e.getPlayer();
    35. //
    36. wolf.remove(p.getName());
    37. List<Entity> ents = p.getNearbyEntities(10, p.getWorld().getMaxHeight()*2, 10);
    38.  
    39. for(Entity ent : ents){
    40. if(ent instanceof Wolf){
    41. ((Wolf) ent).setHealth(0);
    42. }
    43. }
    44. }
    45.  
    46. @SuppressWarnings("deprecation")
    47. @EventHandler
    48. public void playerHorse(PlayerQuitEvent e) {
    49. Player p = e.getPlayer();
    50. //
    51. horse.remove(p.getName());
    52. List<Entity> ents = p.getNearbyEntities(10, p.getWorld().getMaxHeight()*2, 10);
    53.  
    54. for(Entity ent : ents){
    55. if(ent instanceof Horse){
    56. ((Horse) ent).damage(1000000);}
    57. }
    58. }
    59.  
    60.  
    61. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
    62. {
    63. Player player = (Player)sender;
    64. PlayerInventory pi = player.getInventory();
    65. if ((cmd.getName().equalsIgnoreCase("wolf")) && (player.hasPermission("McArcade.wolf"))) {
    66. if (!wolf.contains(player.getName())) {
    67. player.sendMessage(ChatColor.GRAY + "[" + ChatColor.GREEN + "McArcade" + ChatColor.GRAY + "]" + ChatColor.BLUE + " Enjoy your wolf reward!");
    68. wolf.add(player.getName());
    69. player.getLocation().getWorld().spawnEntity(player.getLocation(), EntityType.WOLF);
    70. pi.addItem(new ItemStack[] { new ItemStack(Material.BONE, 5) });
    71. return true;
    72. }
    73. else if ((cmd.getName().equalsIgnoreCase("horse")) && (player.hasPermission("McArcade.horse"))) {
    74. if (!horse.contains(player.getName())) {
    75. player.sendMessage(ChatColor.GRAY + "[" + ChatColor.GREEN + "McArcade" + ChatColor.GRAY + "]" + ChatColor.BLUE + " Enjoy your horse reward!");
    76. horse.add(player.getName());
    77. player.getLocation().getWorld().spawnEntity(player.getLocation(), EntityType.HORSE);
    78. pi.addItem(new ItemStack[] { new ItemStack(Material.APPLE, 10) });
    79. pi.addItem(new ItemStack[] { new ItemStack(Material.SADDLE, 1) });
    80. return true;
    81. }
    82. }
    83. }
    84. return false;
    85. }
    86. }
     
  4. Offline

    Jalau

    Insert checks? for example here:
    System.out.println("check1");
    if (!horse.contains(player.getName())) {
     
  5. Offline

    Jackcheetham

    new code:
    Code:java
    1. else if ((cmd.getName().equalsIgnoreCase("horse")) && (player.hasPermission("McArcade.horse"))) {
    2. System.out.println("check1");
    3. if (!horse.contains(player.getName())) {


    Console:
    [​IMG]
     
  6. Offline

    Jalau

    how the hack check0??? Should be check1?! So if check1 isn't printed out you know probably that the command isnt working or the permissions! Oh a known bug that i remember from my time is that: else if doesnt work, try else {
    if { (split it in 2 lines) :)
     
  7. Offline

    BajanAmerican

    Try casting your player variables after the commandlabel. Also, check if that player is a player.
    Code:java
    1. if(commandLabel.equalsIgnoreCase("wolf")){
    2. if(sender instanceof Player){
    3. Player player = (Player) sender;
    4. if(player.hasPermission("..."){
    5. PlayerInventory pi = player.getInventory();
    6.  
    7. [rest of code here]
    8.  
    9. }
    10. } else {
    11. sender.sendMessage("You must be a player to execute this command!");
    12. }
    13. }

    Jackcheetham
     
  8. Offline

    sgavster

    Jackcheetham Because your brackets are messed up.
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
    2. {
    3. Player player = (Player)sender;
    4. PlayerInventory pi = player.getInventory();
    5. if ((cmd.getName().equalsIgnoreCase("wolf"))) {
    6. if(player.hasPermission("McArcate.wolf")) {
    7. if (!wolf.contains(player.getName())) {
    8. player.sendMessage(ChatColor.GRAY + "[" + ChatColor.GREEN + "McArcade" + ChatColor.GRAY + "]" + ChatColor.BLUE + " Enjoy your wolf reward!");
    9. wolf.add(player.getName());
    10. player.getLocation().getWorld().spawnEntity(player.getLocation(), EntityType.WOLF);
    11. pi.addItem(new ItemStack[] { new ItemStack(Material.BONE, 5) });
    12. return true;
    13. }
    14. }
    15. }
    16. else if ((cmd.getName().equalsIgnoreCase("horse"))) {
    17. if(player.hasPermission("McArcade.horse")) {
    18. if (!horse.contains(player.getName())) {
    19. player.sendMessage(ChatColor.GRAY + "[" + ChatColor.GREEN + "McArcade" + ChatColor.GRAY + "]" + ChatColor.BLUE + " Enjoy your horse reward!");
    20. horse.add(player.getName());
    21. player.getLocation().getWorld().spawnEntity(player.getLocation(), EntityType.HORSE);
    22. pi.addItem(new ItemStack[] { new ItemStack(Material.APPLE, 10) });
    23. pi.addItem(new ItemStack[] { new ItemStack(Material.SADDLE, 1) });
    24. return true;
    25. }
    26. }
    27.  
     
  9. Offline

    1Rogue

    What does your plugin.yml look like?
     
Thread Status:
Not open for further replies.

Share This Page