Why does set Gamemode not work right!?

Discussion in 'Plugin Development' started by codejunkies, Nov 10, 2011.

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

    codejunkies

    I and other new authors would sure like some help
    OK, so i create a VERY simple plugin to allow users to change their own gamemode. This is the code.
    Code:
    package me.serveradmin.gamechanger;
    import java.util.logging.Logger;
    import org.bukkit.GameMode;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    public class Gamechanger extends JavaPlugin {
    Logger logger = Logger.getLogger("Minecraft");
    public void onEnable(){
        logger.info(" Gamechanger is now enabled");
    }
    public void onDisable(){
    }
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
        if(commandLabel.equalsIgnoreCase("gm1")){
            Player player = (Player) sender;
            player.setGameMode(GameMode.CREATIVE);
            player.sendMessage("Your gamemode is now set to Creative Mode");
            logger.info(player.getName() + " has changed their gamemode to Creative");
        }
        if(commandLabel.equalsIgnoreCase("gm0")){
            Player player = (Player) sender;
            player.setGameMode(GameMode.SURVIVAL);
            player.sendMessage("Your gamemode is now set to Survival Mode");
            logger.info(player.getName() + " has changed their gamemode to Survival");
        }
        return true;
     }
    }
    The trouble is When players use the commands, it partially works. it changes their mode to survival but they are still immortal like in creative mode (they have no problem switching back.

    Is there something i am missing?

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

    fromgate

    it changes their mode to survival but they are still immortal like in creative mode

    Hmm... new kind of /god plugin? :)
     
  3. Offline

    Smex

    Did you register your commands in your plugin.yml?
     
  4. Offline

    Kaikz

    Something is probably conflicting with the /gm bit.

    Also, you didn't return true when finishing the commands.
     
  5. Offline

    codejunkies

    yes yml is good to go

    well i tried both false and true same result

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

    theguynextdoor

    Try this for one of your commands

    Code:
            this.getCommand("gm1").setExecutor(new CommandExecutor() {
    
                public boolean onCommand(CommandSender sender, Command cmd,
                        String label, String[] args) {
                    if (cmd.getName().toLowerCase().equals("gm1")) {
                        Player player = (Player) sender;
                        player.setGameMode(GameMode.CREATIVE);
                        player.sendMessage("Your gamemode is now set to Creative Mode");
                        logger.info(player.getName()
                                + " has changed their gamemode to Creative");
                        return true;
                    }
                    return false;
                }
            });
    And do that for the other one but altered accordingly ofc

    I put them in my onEnable but as long as its in the main class i dont suppose it matters too much (Correct me if im wrong).
     
  7. Offline

    codejunkies

    Thanks a lot that helped me clean up my code a lot and yes it was ok to place the code into
    public void onEnable() {}
    alas the players are still immortal
     
Thread Status:
Not open for further replies.

Share This Page