Block place and config file

Discussion in 'Plugin Development' started by DexoD, Sep 24, 2011.

Thread Status:
Not open for further replies.
  1. So, i want to create config, i have this code in my BlockListener,

    Code:
            if(block.getType() == Material.MOB_SPAWNER && player.hasPermission("badblocks.allowmobspawner") == false)
            {
                event.setCancelled(true);
                player.sendMessage(ChatColor.RED + "You are not allowed to use mobspawner on this server !");
                String msg = player.getName() + " tryed to place mobspawner at " + block.getX() + "," + block.getY() + "," + block.getZ() + ".";
                plugin.logMessage(msg);
                for (Player onlinePlayer : plugin.getServer().getOnlinePlayers())
                {
                    if (onlinePlayer.hasPermission("badblocks.notify"))
                    {
                        onlinePlayer.sendMessage(ChatColor.RED + msg);
                    }
                }
            }
    and i want something like this in config file:

    AllowMobSpawner: False

    if is false to do like in my code, but if is

    AllowMobSpawner: True

    i want disable my code, so then players can place MobSpawner. How i can do it?

    Please help

    Please help ... i rly need it

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

    Feed_Dante

  3. Offline

    Bilkokuya

    The best format for config files these days (in my opinion) is YAML - as it's an included library in the bukkit build. Here's the documentation for the SnakeYAML library that bukkit utilises - it's rather in-depth and will give you a good idea of how to get the information out of your config.yml file.
    http://code.google.com/p/snakeyaml/wiki/Documentation

    For actually using the information, you could create a static boolean for whether the AllowMobSpawner's is true or false. Then simply reference whether it's true or false in whatever methods you plan to make for this.#

    There is also a tutorial on using the bukkit standard configuration methods:
    http://forums.bukkit.org/threads/bukkits-standard-configuration-tutorial.25861/
     
  4. Offline

    Zaros

  5. ok, i created config file, and works perfect but i want in this code something like:

    if (config.getBoolean(AllowMobSpawner) == false) and do this code down...

    all this is in BlockListener so i don't know how to do it ...

    Code:
            if(block.getType() == Material.MOB_SPAWNER && player.hasPermission("badblocks.allowmobspawner") == false)
            {
                event.setCancelled(true);
                player.sendMessage(ChatColor.RED + "You are not allowed to use mobspawner on this server !");
                String msg = player.getName() + " tryed to place mobspawner at " + block.getX() + "," + block.getY() + "," + block.getZ() + ".";
                plugin.logMessage(msg);
                for (Player onlinePlayer : plugin.getServer().getOnlinePlayers())
                {
                    if (onlinePlayer.hasPermission("badblocks.notify"))
                    {
                        onlinePlayer.sendMessage(ChatColor.RED + msg);
                    }
                }
            }
     
  6. Offline

    ItsHarry

    Ehm.. I think you understood wrong, what you did is use permissions, not a config file.
     
  7. Offline

    Nitnelave

    You have to create the setting with config.setProperty("allow-spawners", true) and then you can read it with config.getBoolean("allow-spawners")
     
    ben657 likes this.
  8. Offline

    Darkman2412

    Or just use config.getBoolean("allow-spawners", true);
     
  9. Offline

    ben657

    @DexoD so you know how to do the config, you just want to use it in the listener?
     
  10. Yes, i want use == if(config.getBoolean("AllowMobSpawner", true)) == in my BlockListener ...
     
  11. Offline

    ben657

    @DexoD well in your main class, make all the config stuff, then in your listener, set it up so you can use the main class, so like:

    Code:
    MainClass plugin;
    public Listener(MainClass plugin){
        this.plugin = plugin;
    }
    
    then use plugin.config instead of just config :) that SHOULD work, if not, tell me what happens :p

    EDIT: and substitute MainClass for whatever your main classes name is, and Listener for whatever your listener is called.
     
    DexoD likes this.
  12. Offline

    Nitnelave

    Do you have a reference to your main pljgin instance in your listener? If so, you can do mainPlugin.config.getBoolean
    The problem is that config is only defined for your main plugin, so you have To access your main plugin to get to the co fig.
     
  13. ok, now i have:

    Code:
            if(plugin.config.getBoolean("AllowMobSpawner", false))
            {
                if(block.getType() == Material.MOB_SPAWNER && player.hasPermission("badblocks.allowmobspawner") == false)
                {
                    event.setCancelled(true);
                    player.sendMessage(ChatColor.RED + "You are not allowed to use mobspawner on this server !");
                    String msg = player.getName() + " tryed to place mobspawner at " + block.getX() + "," + block.getY() + "," + block.getZ() + ".";
                    plugin.logMessage(msg);
                    for (Player onlinePlayer : plugin.getServer().getOnlinePlayers())
                    {
                        if (onlinePlayer.hasPermission("badblocks.notify"))
                        {
                        onlinePlayer.sendMessage(ChatColor.RED + msg);
                        }
                    }
                }
            }
    no errors but when i put in my config file AllowMobSpawner to True, and then nothing changed I cant put MobSpawner ..
     
  14. Offline

    Nitnelave

    Add a ! In the first if : if(!plugin.config...)

    You're currently testing if the setting is true, and if it's true, prevent block placing. The false in the getBoolean is only the default value.
     
    DexoD likes this.
  15. THXXXX GUYYYSSS, works ... :)
     
  16. Offline

    ben657

    Glad you got it working ^^
     
Thread Status:
Not open for further replies.

Share This Page