How to use a hashmap for enable/disable plugin?

Discussion in 'Plugin Development' started by lasmite, Jun 1, 2012.

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

    lasmite

    Ok so i still got this problem with my enable/disable system.
    Basicly what i want is when a op player enters "/explode" , the plugins function will be turned off and when an op player enters it again the opposit happens. please help.
    This is my main class code:
    Code:
    package com.weebly.lasmite.bukkit.NoExplodings;
     
    import java.util.HashMap;
    import java.util.Map;
    import java.util.logging.Logger;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
        public class NoExplodings extends JavaPlugin{
       
        private Logger log = Logger.getLogger("Minecraft");
       
        public Map<Player, Boolean> pluginEnabled = new HashMap<Player, Boolean>();
       
        //server start
        public void onEnable(){
            this.logMessage("Enabled");
           
            PluginManager manager = this.getServer().getPluginManager();
           
            manager.registerEvents(new NoExplodeListener(), this);
           
            }
           
     
        //server stop
        public void onDisable(){
            this.logMessage("Disabled");
        }
        public void logMessage(String msg){
            PluginDescriptionFile pdFile = this.getDescription();
            this.log.info(pdFile.getName() + " " + pdFile.getVersion() + ": " + msg);
        }
        //when a command is entered
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args ){
            if (cmd.getName().equalsIgnoreCase("explode")){
                if (sender.isOp()){
                    //commandeffect 
                   
                   
                    Player player = (Player) sender;
                    togglePluginState(player);
                   
                   
                    return true;
                }else {
                    //if the player is not op
                    sender.sendMessage(ChatColor.RED + "You don't have permission");
                }
           
            }else if (cmd.getName().equalsIgnoreCase("explodedisable")){
                   
                    if (sender.isOp()){
                        //commandeffect
                        sender.sendMessage(ChatColor.YELLOW + "Noexplodings unloaded from plugins!");
                        Bukkit.getServer().getPluginManager().disablePlugin(this);
                        return true;
                    }else {
                        //if the player is not op
                        sender.sendMessage(ChatColor.RED + "You don't have permission");
                    return false;}
                  }
            return false;
        }
     
     
        public void togglePluginState(Player player){
           
            if(pluginEnabled.containsKey(player)){
                if(pluginEnabled.get(player)){
                    pluginEnabled.put(player, false);
                    player.sendMessage(ChatColor.RED + "Plugin disabled");
                   
                    //do stuff here
                   
                } else {
                    pluginEnabled.put(player, true);
                    player.sendMessage(ChatColor.GREEN + "Plugin enabled");
                }
            } else {
                pluginEnabled.put(player, true);
                player.sendMessage(ChatColor.GREEN + "Plugin enabled");
            }
       
        }
     
    }
       
     
       
    
     
  2. Offline

    zyczu

    There is no need to turn off plugin per-op. Try this:

    Code:
    public class NoExplodings extends JavaPlugin {
          public boolean enabled = true;
          // ....
      public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args ){
            if (cmd.getName().equalsIgnoreCase("explode")){
                if (sender.isOp()){
                    if (enabled) {
                           // disable plugin here
                           enabled = false;
                    } else {
                           // enable plugin here
                           enabled = true;
                    }
                }
            // .....
    
    And in the functionality part simply check
    Code:
    if (enabled) { }
     
  3. Offline

    lasmite

    Thanks, I Was a bit unclear in the main post, i meant that the whole plugins function was supposed to turn off.
    your submission sound good, so i'll try it.

    Did not work :/

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  4. If you just want a toggler use my code here.

    Code:
     public static Vector<String> disabled_users;
     
    public static void toggleUser(Player player)
            {
                // make sure the player is real
                if(player == null) { return; }
               
                // initialize vector if not already intialized
                if(disabled_users == null) {  disabled_users = new Vector<String>(); }
               
                // see if the user is already disabled
                int indexOfPlayer = disabled_users.indexOf(player.getName());
                if(indexOfPlayer > -1)
                {
                    // remove the player from list
                     disabled_users .remove(indexOfPlayer);
                    // tell the player
                    player.sendMessage(ChatColor.RED + "------------------ " + ChatColor.GRAY + "" + Main.plugin.Name() + "" + ChatColor.RED + " ---------------------");
                    player.sendMessage("  " + ChatColor.GOLD + "You enabled " + Main.plugin.Name() + ".");
                }
                else
                {
                    // add the player to the list
                    disabled_users .add(player.getName());
                    // inform the player
                    player.sendMessage(ChatColor.RED + "------------------ " + ChatColor.GRAY + "" + Main.plugin.Name() + "" + ChatColor.RED + " ---------------------");
                    player.sendMessage("  " + ChatColor.GOLD + "You disabled  " + Main.plugin.Name() + ".");
                }
            }
    Add that to a class such as PlayerManager.class then call it with PlayerManager.toggleUser((Player) sender);

    Then just do something such as
    Code:
    if(PlayerManager.disabled_users.contains(event.getPlayer().getName())
    {
    //do something
    }
     
Thread Status:
Not open for further replies.

Share This Page