Solved Config file

Discussion in 'Plugin Development' started by martinke123, May 30, 2014.

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

    martinke123

    I need some help whit making a config file that is easy to change.
    How can i make a config file for this code?
    I want to make a config for the world coords, the world name, and the welcome message,

    Code:java
    1. package me.martinke123.main;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5. import java.util.ArrayList;
    6. import java.util.logging.Logger;
    7.  
    8. import org.bukkit.Bukkit;
    9. import org.bukkit.ChatColor;
    10. import org.bukkit.Location;
    11. import org.bukkit.Material;
    12. import org.bukkit.command.Command;
    13. import org.bukkit.command.CommandSender;
    14. import org.bukkit.configuration.file.FileConfiguration;
    15. import org.bukkit.entity.Player;
    16. import org.bukkit.event.EventHandler;
    17. import org.bukkit.event.EventPriority;
    18. import org.bukkit.event.Listener;
    19. import org.bukkit.event.entity.EntityDamageEvent;
    20. import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    21. import org.bukkit.event.player.AsyncPlayerChatEvent;
    22. import org.bukkit.event.player.PlayerJoinEvent;
    23. import org.bukkit.event.player.PlayerQuitEvent;
    24. import org.bukkit.inventory.ItemStack;
    25. import org.bukkit.plugin.PluginDescriptionFile;
    26. import org.bukkit.plugin.java.JavaPlugin;
    27. import org.bukkit.potion.PotionEffect;
    28. import org.bukkit.potion.PotionEffectType;
    29.  
    30. @SuppressWarnings("unused")
    31. public class main extends JavaPlugin implements Listener {
    32.  
    33. public final Logger logger = Logger.getLogger("Bukkit");
    34.  
    35. public void onEnable() {
    36. Bukkit.getServer().getPluginManager().registerEvents(this , this);
    37. PluginDescriptionFile file = this.getDescription();
    38. this.logger.info(file.getName() + " has been enabled!");
    39.  
    40.  
    41. }
    42.  
    43. @EventHandler
    44. public void Join (PlayerJoinEvent e) {
    45. e.setJoinMessage(null);
    46. }
    47.  
    48. @SuppressWarnings("deprecation")
    49. @EventHandler
    50. public void Quit (PlayerQuitEvent e) {
    51. e.setQuitMessage(null);
    52. e.getPlayer().setMaxHealth(20);
    53. e.getPlayer().setHealth(20);
    54. e.getPlayer().setFoodLevel(20);
    55. e.getPlayer().removePotionEffect(PotionEffectType.REGENERATION);
    56. e.getPlayer().removePotionEffect(PotionEffectType.JUMP);
    57. e.getPlayer().removePotionEffect(PotionEffectType.FIRE_RESISTANCE);
    58. }
    59.  
    60. @SuppressWarnings("deprecation")
    61. @EventHandler
    62. public void tJoin (PlayerJoinEvent e) {
    63. e.getPlayer().teleport(new Location(Bukkit.getWorld("world"), 236.54499, 21.000, -184.54373));
    64. e.getPlayer().sendMessage(ChatColor.AQUA + "[" + ChatColor.RED + "Welcome on the server!" + ChatColor.AQUA + "]");
    65. e.getPlayer().closeInventory();
    66. e.getPlayer().setMaxHealth(80);
    67. e.getPlayer().setHealth(80);
    68. e.getPlayer().setFoodLevel(20);
    69. e.getPlayer().getInventory().clear();
    70. }
    71.  
    72. @EventHandler(priority = EventPriority.HIGH)
    73. public void onFallDamage(EntityDamageEvent event){
    74. if(event.getEntity() instanceof Player && event.getCause() == DamageCause.FALL)
    75. event.setCancelled(true);
    76. }
    77.  
    78.  
    79.  
    80. }
    81. }
    82.  
    83.  
    84.  
    85.  
    86.  
    87.  
    88.  
     
  2. Offline

    DxDy

  3. Offline

    Adriani6

    So, lets do it quickly.

    Create a new method for your defaults

    Code:java
    1. getConfig().addDefault("Config.Message", "Default message");
    2. getConfig().options().copyDefaults(true);
    3. saveConfig();


    Then run this methond onEnable, if config is not present it will auto generate one for you with the defaults defined in it.

    Then to add to the config you may to the following

    Code:java
    1. getConfig().set("coords", "Put your coordinated here");
     
  4. Offline

    martinke123

    Adriani6

    But what do i have to change in this line now for coords?

    Code:java
    1. e.getPlayer().teleport(new Location(Bukkit.getWorld("world"), 236.54499, 21.000, -184.54373));
     
  5. Offline

    Adriani6

    What do you mean ?
    How to save this or load this in place of the coords ?
     
  6. Offline

    martinke123

    Adriani6

    I want to put the coords that are in the config in there
     
  7. Offline

    Adriani6

    martinke123

    you can do something like this

    Code:java
    1. getConfig().getInt("coords.x")


    This would get you the x coordinate from your config, make sure the path is correct though.
     
  8. Offline

    martinke123

    Adriani6

    If i do that i am only getting teleported to 0, 0, 0 even when i edit the config file

    Adriani6

    So did i do something wrong in here?

    Code:java
    1. getConfig().addDefault("Config.Message", "Default message");
    2. getConfig().set("coords.x", "0");
    3. getConfig().set("coords.y", "100");
    4. getConfig().set("coords.z", "0");
    5. saveConfig();


    or in here?

    Code:java
    1. e.getPlayer().teleport(new Location(Bukkit.getWorld("world"), getConfig().getInt("coords.x"), getConfig().getInt("coords.y"), getConfig().getInt("coords.z")));


    I fixed the problem! thanks Adriani6 for helping!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 1, 2016
  9. Offline

    Adriani6

    martinke123

    Those are not ints, they are strings.

    martinke123

    oh, I was getting food, sorry for not replying faster lol :)

    You're welcome

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 1, 2016
Thread Status:
Not open for further replies.

Share This Page