Player p = (Player)sender; not working

Discussion in 'Plugin Development' started by JellyYods, Jun 5, 2014.

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

    JellyYods

    Code:java
    1. public class CommandSwitcher implements CommandExecutor, Listener {
    2.  
    3.  
    4. ArrayList<Player> Life = new ArrayList<Player>(); {
    5.  
    6. }
    7. public boolean onCommand(CommandSender sender, Command cmd, String label,
    8. String[] args) {
    9. if (!(sender instanceof Player)) {
    10. return true;
    11.  
    12. Player p = (Player)sender; //This is where the error is I don't know what do.
    13.  
    14. if(cmd.getName().equalsIgnoreCase("switcher"));
    15. if(Life.contains(p)) {
    16. p.sendMessage("You may only have one kit per life!");
    17.  
    18. } else {
    19.  
    20. p.getInventory().clear();
    21.  
    22. p.getInventory().setHelmet(new ItemStack(Material.DIAMOND_HELMET));
    23. p.getInventory().setChestplate(new ItemStack(Material.CHAINMAIL_LEGGINGS));
    24. p.getInventory().setLeggings(new ItemStack(Material.DIAMOND_LEGGINGS));
    25. p.getInventory().setBoots(new ItemStack(Material.CHAINMAIL_BOOTS));
    26.  
    27. ItemStack Sword = new ItemStack(Material.IRON_SWORD);
    28. Sword.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 2);
    29. Sword.addUnsafeEnchantment(Enchantment.DURABILITY, 3);
    30.  
    31. p.getInventory().addItem(new ItemStack[] { Sword });
    32.  
    33. p.getInventory().addItem(new ItemStack(Material.SNOW_BALL, 10));
    34.  
    35. ItemStack Soup = new ItemStack(Material.MUSHROOM_SOUP);
    36.  
    37. for(int i = 0; 35 > 1; i++) {
    38. p.getInventory().addItem(new ItemStack[] { Soup });
    39. }
    40.  
    41. p.sendMessage(ChatColor.YELLOW + "You have recieved the Switcher Kit!");
    42.  
    43. Life.add(p);
    44. }
    45. return false;
    46. }
    47. return false;
    48. }
    49.  
    50. @EventHandler
    51. public void onPlayerDeath(PlayerDeathEvent event) {
    52. Player player = (Player)event.getEntity();
    53. if(Life.contains(player.getName())){
    54. Life.remove(player.getName());
    55. }
    56. }
    57. }



    I'm making a kit per life and I get an error I don't know how to fix it!
     
  2. Offline

    JBoss925

    WTF DID YOU DO TO YOUR IF STATEMENTS?
    EDIT:
    AND EVERYTHING ELSE?
     
    es359 and AoH_Ruthless like this.
  3. Offline

    JellyYods

  4. Offline

    Go Hard

  5. Offline

    NathanWolf


    I'm presuming stuff like this is what he meant:

    Code:
    if(cmd.getName().equalsIgnoreCase("switcher"));
    That statement does nothing - you pretty much never want a semicolon right at the end of an "if" like that.

    You should go through and fix all of your indentation, and (my personal preference) wrap ALL of your if bodies in {} brackets. Then see if it's still broken.
     
  6. Offline

    Synapz

    In your code you do

    If(!(sender instanceof Player){
    Return true;

    Then u do

    Player player = (Player) sender;

    If the sender is not an instanceof Player, then how will you make the sender to a player?

    Try

    If(!(sender instanceof Player){
    return true;
    }
    if (sender instanceof Player){
    Player player = (Player) sender;
     
  7. Offline

    AoH_Ruthless

    JellyYods
    You clearly have almost no idea what you are doing as there are so many things wrong with your code that are just part of OOP. Sorry to be rash, but you really need to learn some Java before going any further with this: You are going down a road that you do not want to go down (I can tell you from personal experience).

    Your Java errors:
    1. You name a globalized variable starting with a capitalize letter. It's a variable.
    2. Your globalized variable begins brackets. There are so many things wrong with this I can't even begin to describe.
    3. In your instance check where you check to see if the sender is not a player, you never close that if-statement. You just leave it open.
    4. Because the if-block is never closed, the sender can never ever be a player.
    5. You end your cmd name check statement (an if statement) with a semi-colon.
    6. Your for loop will run indefinitely because 35 is always greater than 1... I don't understand what you were trying to do there.
     
    xTigerRebornx likes this.
  8. Offline

    JellyYods

    Code:java
    1. package DoodlieKits.Commands;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Material;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandExecutor;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.enchantments.Enchantment;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.Listener;
    14. import org.bukkit.event.entity.PlayerDeathEvent;
    15. import org.bukkit.inventory.ItemStack;
    16.  
    17. public class CommandSwitcher implements CommandExecutor, Listener {
    18.  
    19.  
    20. ArrayList<Player> Life = new ArrayList<Player>(); {
    21.  
    22. }
    23. public boolean onCommand(CommandSender sender, Command cmd, String label,
    24. String[] args) {
    25. if (!(sender instanceof Player)) {
    26. return true;
    27. }
    28. if(sender instanceof Player);
    29. Player p = (Player)sender;
    30.  
    31. if(cmd.getName().equalsIgnoreCase("switcher")) {}
    32. if(Life.contains(p)) {
    33. p.sendMessage("You may only have one kit per life!");
    34.  
    35. } else {
    36.  
    37. p.getInventory().clear();
    38.  
    39. p.getInventory().setHelmet(new ItemStack(Material.DIAMOND_HELMET));
    40. p.getInventory().setChestplate(new ItemStack(Material.CHAINMAIL_LEGGINGS));
    41. p.getInventory().setLeggings(new ItemStack(Material.DIAMOND_LEGGINGS));
    42. p.getInventory().setBoots(new ItemStack(Material.CHAINMAIL_BOOTS));
    43.  
    44. ItemStack Sword = new ItemStack(Material.IRON_SWORD);
    45. Sword.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 2);
    46. Sword.addUnsafeEnchantment(Enchantment.DURABILITY, 3);
    47.  
    48. p.getInventory().addItem(new ItemStack[] { Sword });
    49.  
    50. p.getInventory().addItem(new ItemStack(Material.SNOW_BALL, 10));
    51.  
    52. ItemStack Soup = new ItemStack(Material.MUSHROOM_SOUP);
    53.  
    54. for(int i = 0; 35 > 1; i++) {
    55. p.getInventory().addItem(new ItemStack[] { Soup });
    56. }
    57.  
    58. p.sendMessage(ChatColor.YELLOW + "You have recieved the Switcher Kit!");
    59.  
    60. Life.add(p);
    61. }
    62. return false; //All right here are errors
    63. }
    64. return false;
    65. } //error
    66.  
    67. @EventHandler
    68. public void onPlayerDeath(PlayerDeathEvent event) { //errors in here
    69. Player player = (Player)event.getEntity();
    70. if(Life.contains(player.getName())){
    71. Life.remove(player.getName());
    72. }
    73. }
    74. }


    O my fricin god every time I ask for help I get told to go learn java I know basic java you don't need to be a master at it to do this.

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

    Synapz

    JellyYods
    What are the errors?
    You showed us where there were, but what do they say?

    EDIT:
    if(sender instanceof Player);
    Should be
    If(sender instanceof Player){

    }
    Don't forget the brackets there very important :D

    Ok, you messed up a lot actually.. I'll show u the correct way on how to do this. It doesn't seem you know where to put the brackets.

    Code:java
    1. package DoodlieKits.Commands;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Material;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandExecutor;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.enchantments.Enchantment;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.Listener;
    14. import org.bukkit.event.entity.PlayerDeathEvent;
    15. import org.bukkit.inventory.ItemStack;
    16.  
    17. public class CommandSwitcher implements CommandExecutor, Listener {
    18.  
    19.  
    20. ArrayList<Player> life = new ArrayList<Player>(); { //this should be "life", first part should be lower case =P
    21.  
    22. }
    23. public boolean onCommand(CommandSender sender, Command cmd, String label,
    24. String[] args) {
    25. if (!(sender instanceof Player)) {
    26. return true;
    27. }
    28. if(sender instanceof Player){
    29. Player p = (Player)sender;
    30.  
    31.  
    32. if(cmd.getName().equalsIgnoreCase("switcher")) { //Beging of "switcher"
    33. if(Life.contains(p)) {
    34. p.sendMessage("You may only have one kit per life!");
    35.  
    36. } else {
    37.  
    38. p.getInventory().clear();
    39.  
    40. p.getInventory().setHelmet(new ItemStack(Material.DIAMOND_HELMET));
    41. p.getInventory().setChestplate(new ItemStack(Material.CHAINMAIL_LEGGINGS));
    42. p.getInventory().setLeggings(new ItemStack(Material.DIAMOND_LEGGINGS));
    43. p.getInventory().setBoots(new ItemStack(Material.CHAINMAIL_BOOTS));
    44.  
    45. ItemStack Sword = new ItemStack(Material.IRON_SWORD);
    46. Sword.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 2);
    47. Sword.addUnsafeEnchantment(Enchantment.DURABILITY, 3);
    48.  
    49. p.getInventory().addItem(new ItemStack[] { Sword });
    50.  
    51. p.getInventory().addItem(new ItemStack(Material.SNOW_BALL, 10));
    52.  
    53. ItemStack Soup = new ItemStack(Material.MUSHROOM_SOUP);
    54.  
    55. for(int i = 0; 35 > i; i++) { //You put 1 here, I replaced the 1 with i
    56. p.getInventory().addItem(new ItemStack[] { Soup });
    57. }
    58.  
    59. p.sendMessage(ChatColor.YELLOW + "You have recieved the Switcher Kit!");
    60.  
    61. Life.add(p);
    62. } //end of else
    63. }//End of "Switcher"
    64. }//End of instance of Player
    65. return false;
    66. }//End of onCamnd
    67.  
    68. @EventHandler //errors in here
    69. public void onDeath(PlayerDeathEvent event){
    70. Player player = (Player)event.getEntity();
    71. if(Life.contains(player.getName())){
    72. Life.remove(player.getName());
    73. }
    74. }//end of PLayerDeathEvent
    75. }//End of class


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

    JellyYods

    Figure this out my self
     
  11. Offline

    es359

    No, you don't need to be a master. But you need to get the basics. Which you clearly don't understand. You're asking for help, and they are giving you the answer...
     
    AoH_Ruthless likes this.
  12. Offline

    idontcare1025

    The error is you have if (blah blah) { return true;
    Don't have an open curly brace without a closed one. The reason you're getting an error is because the line after it will never be used as the method is returning.
     
  13. Offline

    Kassestral

    I think closing a bracket is something even someone with basic java knowledge would know.
     
  14. Offline

    JBoss925

    You most certainly do not know basic java. If you did, your code wouldn't look like an ape who knows sign language coded it. There are just so many things wrong I cannot believe it. Listen, I'm not trying to be an ass, though you most likely think of me as one now, but please try out TheNewBoston he's a great resource.
     
  15. Offline

    Fantah

    JellyYods There is a complete difference between getting "help" and us spoon feeding you code....
     
  16. Offline

    NathanWolf

    Wow, holy cow, just want to point out the OP said they figured the problem out like four posts ago. Maybe stop piling on the vitriol?

    Also, OP, mark this as solved if you don't need anymore help :)
     
Thread Status:
Not open for further replies.

Share This Page