'player cannot be resolved' Issue... :)

Discussion in 'Plugin Development' started by Rocky_990, Dec 22, 2014.

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

    Rocky_990

    Hey there :)

    I've recently started to develop my first real plugin, but I've come across one bug, which I can't seem to be able to fix.. Any help would be much appreciated c:

    Code:
    package com.gmail.JakeECode.testingplugin;
    
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public final class TestingPlugin extends JavaPlugin {
        @Override
        public void onEnable() {
    		getLogger().info("onEnable has been invoked!");
        }
     
        @Override
        public void onDisable() {
            getLogger().info("onDisable has been invoked!");
        }
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        	if (cmd.getName().equalsIgnoreCase("smeltify")) { // If the player ran /smeltify, do the following:
        		 PlayerInventory inventory = player.getInventory(); // The player's inventory
        		    ItemStack goldingot = new ItemStack(Material.GOLD_INGOT, 1); // A golden ingot
        		    if (inventory.contains(Material.GOLD_ORE, 1)) {
        		        inventory.addItem(goldingot); // Adds a golden ingot to the players inv
        		        player.sendMessage("Gold ore smeltified. :)");
        		    }
        		return true;
        	}   
        	return false; 
        }
    }
    
    Many thanks,
    Rocky_990 :)
     
  2. @Rocky_990
    Where are you getting player? I don't see where it is being initialized.
    Always check if the sender is the instance of Player before casting to a player otherwise console will have errors.
    You also do not need the loggers in onEnable and onDisable as it does it for you.

    Also stacktrace, error line number?
     
  3. Offline

    Rocky_990

    Ah okay, I'll figure that one out... Thanks :)
    24 and 28 are the lines with errors, and okay, thanks :)
     
  4. Offline

    Skionz

    @Rocky_990 You never initialized player; therefore, it does not exist. I suggest learning basic Java before a Java API.
     
  5. Offline

    Rocky_990

    After changing the code to this:
    Code:
    package com.gmail.JakeECode.testingplugin;
    
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public final class TestingPlugin extends JavaPlugin {
        @Override
        public void onEnable() {
    		getLogger().info("onEnable has been invoked!");
        }
     
        @Override
        public void onDisable() {
            getLogger().info("onDisable has been invoked!");
        }
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        	if (cmd.getName().equalsIgnoreCase("smeltify")) { // If the player ran /smeltify, do the following:
        		if (!(sender instanceof Player)) {
        			sender.sendMessage("This command is only available to players.");
        		} else {
        			Player player = (Player) sender;
           		 PlayerInventory inventory = player.getInventory(); // The player's inventory
     		    ItemStack goldingot = new ItemStack(Material.GOLD_INGOT, 1); // A golden ingot
     		    if (inventory.contains(Material.GOLD_ORE, 1)) {
     		        inventory.addItem(goldingot); // Adds a golden ingot to the players inv
     		        player.sendMessage("Gold ore smeltified. :)");
        		}
    
        		    }
        		return true;
        	}   
        	return false; 
        }
    }
    
    it worked!! :) Thank you very much you guys.
     
  6. @Rocky_990 I will just have to do spoonfeed a little here.

    Code:java
    1.  
    2. if(sender instanceof Player){
    3. Player player = (Player) sender;
    4. //TODO Add code.
    5. }
    6.  


    Now you have player, always do this.

    EDIT: Fantastic, please mark the thread as solved.
     
Thread Status:
Not open for further replies.

Share This Page