(Deprecated) Bukkit's Standard Configuration Tutorial

Discussion in 'Resources' started by captainawesome7, Jul 11, 2011.

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

    Sagacious_Zed Bukkit Docs

    @captainawesome7
    reading through the tutorial I noticed two things that need fixing

    1. headers need to have the hash character in front with a space.
    2. inside onEnable you have to call config.load() before any get methods or you will always get the default values

    EDIT: looks like number two is not the case....
     
    Butkicker12 likes this.
  2. Offline

    bowski23

    Pretty good tutorial but i dunno how to solve a problem.
    Code:
    public int Helmettype;
        public LethalAir()
        {
            playerListener = new LethalAirPlayerListener(this);
        }
    
        public void onDisable()
        {
            Logger.getLogger("Minecraft").info("LethalAir Disabled.");
        }
    
        public void onEnable()
        {
            PluginManager pm = getServer().getPluginManager();
            Configuration config = getConfiguration();
            config.load();
            config.setHeader("#You can set which Helmet protects you");
            Helmettype = config.getInt("Helmet type", 86);
            config.save();
            Logger.getLogger("Minecraft").info("LethalAir Enabled! Type /lethalair disable to disable.");
            pm.registerEvent(org.bukkit.event.Event.Type.PLAYER_JOIN, playerListener, org.bukkit.event.Event.Priority.Normal, this);
            pm.registerEvent(org.bukkit.event.Event.Type.PLAYER_RESPAWN, playerListener, org.bukkit.event.Event.Priority.Normal, this);
        }
    
        public LethalAirPlayerListener playerListener;
    }
    and now i want to link my Helmettype Int to
    Code:
    public class LethalAirPlayerListener extends PlayerListener
    {
        public LethalAirPlayerListener(LethalAir lethalAir)
        {
            i = new ItemStack(Helmettype, 1);
            plugin = lethalAir;
        }
    
        public void onPlayerRespawn(PlayerRespawnEvent event)
        {
            event.getPlayer().getInventory().addItem(new ItemStack[] {
                i
            });
            event.getPlayer().getInventory().setHelmet(i);
        }
    
        public void onPlayerJoin(PlayerJoinEvent event)
        {
           if(event.getPlayer().getInventory().getHelmet().getTypeId() !=Helmettype)
            {
                event.getPlayer().sendMessage((new StringBuilder()).append(ChatColor.GOLD).append("LethalAir has detected that you are not wearing your gas mask. One has been given to you if you do not already have one.").toString());
                event.getPlayer().sendMessage((new StringBuilder()).append(ChatColor.GOLD).append("If you do not equip your gas mask, you will begin taking damage.").toString());
            }
            if(!event.getPlayer().getInventory().contains(Helmettype))
                event.getPlayer().getInventory().addItem(new ItemStack[] {
                    i
                });
            plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    
                public void run()
                {
                    Player players[] = plugin.getServer().getOnlinePlayers();
                    Player aplayer[];
                    int k = (aplayer = players).length;
                    for(int j = 0; j < k; j++)
                    {
                        Player p = aplayer[j];
                        if(p.getInventory().getHelmet().getTypeId() != Helmettype)
                            p.damage(1);
                    }
    
                }
    
                final LethalAirPlayerListener this$0;
    
                {
                    this$0 = LethalAirPlayerListener.this;
                }
            }
    Can someone help?I get some Errors on eclipse.
     
  3. Offline

    captainawesome7

    @bowski23 Your first plugin?
    When you load a configured value you should save it in the main class as:
    Code:java
    1.  
    2. public Integer helmetType;
    3.  
    4. public void onEnable() {
    5. //whatevs
    6. Helmettype = config.getInt("Helmet type", 86);
    7. }
    8.  

    And in the player listener just use:
    Code:java
    1.  
    2. public ItemStack i;
    3. public LethalAir plugin;
    4. public LethalAirPlayerListener(LethalAir lethalAir) {
    5. plugin = lethalAir;
    6. i = new ItemStack(plugin.Helmettype, 1);
    7. }
    8.  

    Sorry if you do know how to do this, but from your code you didn't include the basic stuff.
     
  4. Offline

    bowski23

    Yeah "my"(just wanted to add a config) first Plugin this was the only tutorial i used and i dont have Java experience but i have still 2 problems.
    Code:
        public ItemStack i;
        public LethalAir plugin;
        public LethalAirPlayerListener(LethalAir lethalAir)
        {
        }
    
        public void onPlayerRespawn(PlayerRespawnEvent event)
        {
            plugin = LethalAir;
            i = new ItemStack(plugin.Helmettype, 1);
            event.getPlayer().getInventory().addItem(new ItemStack[] {
                i
            });
            event.getPlayer().getInventory().setHelmet(i);
        }
    
        public void onPlayerJoin(PlayerJoinEvent event)
        {
           plugin = LethalAir;
           i = new ItemStack(plugin.Helmettype, 1);
           if(event.getPlayer().getInventory().getHelmet().getTypeId() !=i)
            {
                event.getPlayer().sendMessage((new StringBuilder()).append(ChatColor.GOLD).append("LethalAir has detected that you are not wearing your gas mask. One has been given to you if you do not already have one.").toString());
                event.getPlayer().sendMessage((new StringBuilder()).append(ChatColor.GOLD).append("If you do not equip your gas mask, you will begin taking damage.").toString());
            }
            if(!event.getPlayer().getInventory().contains(i))
                event.getPlayer().getInventory().addItem(new ItemStack[] {
                    i
                });
            plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    
                public void run()
                {
                    Player players[] = plugin.getServer().getOnlinePlayers();
                    Player aplayer[];
                    int k = (aplayer = players).length;
                    for(int j = 0; j < k; j++)
                    {
                        Player p = aplayer[j];
                        if(p.getInventory().getHelmet().getTypeId() != i)
                            p.damage(1);
                    }
    
                }
    
                final LethalAirPlayerListener this$0;
    at "plugin = LethalAir" it says that LethalAir cant be resolved to a variable and at "if(event.getPlayer().getInventory().getHelmet().getTypeId() !=i)" it says at the i that int and ItemStack arent compatible sry for noob questions
     
  5. Offline

    captainawesome7

    Code it yourself, the n00bness is too much to bear :|
     
  6. Offline

    Lolmewn

    new stringbuilder? Append Strings? Send a string toString()?
    you have to be kidding me!
     
  7. Offline

    captainawesome7

    Its just so much n00b concentrated in one place I couldn't handle it.
     
  8. Offline

    Lolmewn

    I only looked briefly and almost got a heart attack.
     
  9. Offline

    Chromana

    @captainawesome7 Any tips for using a hashmap with the configuration? (I'm fine with all the more "basic" stuff that's possible with standard config)
    Thanks
     
  10. Offline

    captainawesome7

    You can't use a hashmap with the config, use a different method.
     
  11. Offline

    Chromana

    I was already using a list of lists to do similar functionality like
    ([a1,a2],[b1,b2],[c1,c2])
    so I guess I'll continue with that. Cheers.
     
  12. Offline

    feildmaster

    No. getConfiguration() does the first loading for you.
     
  13. Offline

    Sagacious_Zed Bukkit Docs

  14. Offline

    feildmaster

    It loads, the only time you need to manually call a config.load() is when you make your own config, or when you make a reload function.
     
  15. Offline

    iPhysX

    Yeah I'm getting mapping value errors.
    Code:
    public void onEnable() {
            conf = new Configuration(new File(getDataFolder().getPath() + "/config.yml"));
            conf.setHeader("CreatureCraftCore");
            conf.setProperty("MOTD",configString);
            conf.setProperty("TntBlockDamage",configBoolean);
            conf.setProperty("CreeperBlockDamage",configBoolean);
            conf.setProperty("GhastBlockDamage",configBoolean);
            conf.setProperty("WeatherOn",configBoolean);
            conf.setProperty("TimeConstant",configBoolean);
            conf.setProperty("PvpOn",configBoolean);
            conf.setProperty("MonsterSpawns",configBoolean);
            conf.setProperty("NerfEnderman",configBoolean);
            conf.setProperty("AllowLogins",configBoolean);
            conf.save();
            conf = new Configuration(new File(getDataFolder().getPath() + "/config.yml"));
            conf.load();
            configString = conf.getString("MOTD", "Message");
            configBoolean = conf.getBoolean("TntBlockDamage",false);
            configBoolean = conf.getBoolean("CreeperBlockDamage",false);
            configBoolean = conf.getBoolean("GhastBlockDamage",false);
            configBoolean = conf.getBoolean("WeatherOn",false);
            configBoolean = conf.getBoolean("TimeConstant",false);
            configBoolean = conf.getBoolean("PvPOn",false);
            configBoolean = conf.getBoolean("MonsterSpawns",true);
            configBoolean = conf.getBoolean("NerfEnderman",true);
            conf.save();
    }
    It's all in the onenable because i have been trying different things :/
    Any help would be appreciated

    Code:
    CreatureCraftCore
    CreeperBlockDamage: false
    WeatherOn: false
    MonsterSpawns: false
    TimeConstant: false
    AllowLogins: false
    TntBlockDamage: false
    GhastBlockDamage: false
    MOTD:
    NerfEnderman: false
    PvpOn: false
    
    That is created btw!
     
  16. Offline

    captainawesome7

    You can change ur code to this:
    Code:java
    1.  
    2. public void onEnable() {
    3. conf = getConfiguration();
    4. conf.load();
    5. motd = conf.getString("MOTD", "Message");
    6. tntblock = conf.getBoolean("TntBlockDamage",false);
    7. creeperblock = conf.getBoolean("CreeperBlockDamage",false);
    8. ghastblock = conf.getBoolean("GhastBlockDamage",false);
    9. weather = conf.getBoolean("WeatherOn",false);
    10. time = conf.getBoolean("TimeConstant",false);
    11. pvp = conf.getBoolean("PvPOn",false);
    12. monsters = conf.getBoolean("MonsterSpawns",true);
    13. nerfendermen = conf.getBoolean("NerfEnderman",true);
    14. conf.save();
    15. }
    16.  

    That's assuming that you declared tntblock, creeperblock, etc. as public variables earlier on in the class. I don't know why you used configBoolean and kept overwriting the same boolean value, but you should do how I did. Anyways, that should work and if it doesn't just post the error.
     
  17. Offline

    iPhysX

    Thanks alot!
    I just misunderstood something i guess :)
    The tutorial is great by the way!
     
  18. Offline

    TopGear93

    @captainawesome7 how could i create a configurable true / false node? im trying to make messages in one of plugins disable by true/false.
     
  19. Offline

    captainawesome7

    config = getConfiguration();
    config.load();
    Boolean messageEnabled = config.getBoolean("EnableMessage", true);
    config.save();
     
  20. Offline

    TopGear93

    awesome thanks, but my next question might be potentially stupid. how would i use this with the message?

    this is what ive tried.

    Code:
    if(this.EnableMessage == true) {
    player.sendMessage(ChatColor.GOLD+LeakyBukkit.LEAKY_MESSAGE);
     
  21. how i can type 2 lines with config.setHeader ?
     
  22. Offline

    captainawesome7

    You don't
     
  23. how then i can make multiple lines?
     
  24. Offline

    DirtyStarfish

    This tutorial is great! Helped a lot!

    How would I make a list of players in the config?

    For example, how would I add a player on a PlayerJoinEvent, and then check if that players name exists in the config file?
     
  25. Offline

    captainawesome7

    use a getStringList
     
  26. Offline

    Butkicker12

    How would I check if a boolean is true/false? If(boolean.pvpon == true){
    etc.
     
  27. Offline

    Sagacious_Zed Bukkit Docs

    Well assuming you want a node in your YAML reading
    Code:yaml
    1. boolean:
    2. pvpon: true


    you can check with this code
    Code:java
    1. Configuration config = this.getConfiguration();
    2. boolean pvpon = cofig.getBoolean("boolean.pvpon", true);
    3. if (pvpon) {
    4. // Do Something
    5. }


    although alternately if you were to inline everything
    Code:java
    1. if (this.getConfiguraiton().getBoolean("boolean.pvpon", true) {
    2. // Do something
    3. }


    config.setHeader() takes a string, and it outputs the string at the top of the YAML file raw. Which is why you have to manually comment it.
    But because it is raw you can do things like this to create a multiline comment section at the top
    Code:java
    1. config.setHeader("# This is a multi-line comment\n# This is the second line\n# This is the third Line\n# do you see the pattern?"/


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 17, 2016
  28. thx, :)
     
  29. Offline

    Alexis

    Hello, how i can recover "Toast" or "Euhcou" just when i know "List", see the example.
    PHP:
    SaveVersion1
    List:
        
    Toast:
            
    EnterMessage:
            
    BlackList:
                
    TypeBLACKLIST
                ItemList
    : []
            
    LeaveMessage:
            
    IgnoreList:
        
    Euhcou:
            
    EnterMessage:
            
    BlackList:
                
    TypeBLACKLIST
                ItemList
    : []
            
    LeaveMessage:
            
    IgnoreList:
    Thanks you in advance :)
    Sorry for my english :)
     
  30. Offline

    Butkicker12

    So
     
Thread Status:
Not open for further replies.

Share This Page