[SOLVED] Can't get this to work...

Discussion in 'Plugin Development' started by Dark Shock, Aug 15, 2012.

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

    Chiller

    Show me your whole onCommand method
     
  2. Offline

    Dark Shock

    Code:
    public boolean onPlayerCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if(cmd.getName().equalsIgnoreCase("stopcakeshame"));
    {
        cake = false;
       
        sender.sendMessage("CakeShame is now disabled for you.");
       
       
               
       
       
       
    }
     
           
     
        if(cmd.getName().equalsIgnoreCase("allowcakeshame"));
    {
        cake = true;
        sender.sendMessage("CakeShame is now enabled for you.");
     
    }
     
       
       
               
    return true;       
        }
        
     
  3. Offline

    dchaosknight

    Just a little thing that I'm noticing, no biggie, but why not just use onCommand and check if the sender is a player?
     
  4. Offline

    Chiller

    Don't put semicolons after your if statements...

    **Agree**

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  5. Offline

    Dark Shock

    Still not working, and okay.
    (Sorry if i'm wasting your time btw, :()
     
  6. Offline

    Chiller

    Well im puzzled as to why it won't work...?
     
  7. Offline

    Dark Shock

    Should I post the entire code?

    Code:
    package com.alifakih;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Shockworld extends JavaPlugin implements Listener{
        private static boolean cake = true;
       
        public void onEnable() {
            getLogger().info("CakeShame has been enabled.");
            getServer().getPluginManager().registerEvents(this, this);
           
        }
       
        public void onDisable() {
            getLogger().info("CakeShame is now disabled.");
           
        }
       
        @EventHandler
        public void onPlayerInteractBlock(BlockBreakEvent evt)
        {
            Block b = evt.getBlock();
            if(cake == true)
            {
            if(b.getType() == Material.CAKE_BLOCK){
                evt.getPlayer().sendMessage("Shame on you! That's good cake!");
                evt.getPlayer().setFoodLevel(2);
            }
            else evt.getPlayer().sendMessage("");
               
            }
        }
       
    public boolean onPlayerCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if(cmd.getName().equalsIgnoreCase("stopcakeshame"))
    {
        cake = false;
       
        sender.sendMessage("CakeShame is now disabled for you.");
       
       
               
       
       
       
    }
     
           
     
        if(cmd.getName().equalsIgnoreCase("allowcakeshame"))
    {
        cake = true;
        sender.sendMessage("CakeShame is now enabled for you.");
     
    }
     
       
       
               
    return true;       
        }
       
       
        public void onPlayerJoin(PlayerJoinEvent event) {
            event.getPlayer().sendMessage("Breaking cake causes you to lose hunger! Cake is precious!");
           
        }
       
     
    }
    
    There's the entire class file.
    Same issue.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  8. Offline

    dchaosknight

    Dark Shock
    I don't know if this'll make a difference, but try to use label instead of cmd.getName().
     
  9. Offline

    Chiller

    You have to register the command!
    Put getCommand("command here").setExecutor(this);
     
  10. Offline

    Dark Shock

    Nope.

    Like this?
    Code:
    public boolean onPlayerCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if(cmd.get().equalsIgnoreCase("stopcakeshame"))
    {
            getCommand("stopcakeshame").setExecutor(this);
        cake = false;
       
        sender.sendMessage("CakeShame is now disabled for you.");
       
       
               
       
       
       
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  11. Offline

    Chiller

    Meant to say: Put getCommand("command here").setExecutor(this); in the onEnable method
     
  12. Offline

    Dark Shock

    Still not working..
     
  13. Offline

    Chiller

    Sorry but idk...
     
  14. Offline

    Dark Shock

    Thanks for your help. :)
    I'll try to fix it.
     
  15. Offline

    kyle1320

    Try using onCommand instead of onPlayerCommand then put
    Code:
    if (sender instanceof Player) {
        Player player = (Player)sender;
    in front of everything, replacing sender in your code with player. Like this:
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (sender instanceof Player)
        {
            Player player = (Player)sender;
            
            if(cmd.getName().equalsIgnoreCase("stopcakeshame"))
            {
                cake = false;
                player.sendMessage("CakeShame is now disabled for you.");
                
            }
     
            if(cmd.getName().equalsIgnoreCase("allowcakeshame"))
            {
                cake = true;
                player.sendMessage("CakeShame is now enabled for you.");
                
            }
            
            return true;
            
        }
        return false;   
    }
     
  16. Offline

    Dark Shock

    It's working now, thank you everyone!
     
Thread Status:
Not open for further replies.

Share This Page