Check if args[1] contains in config

Discussion in 'Plugin Development' started by Plugers11, May 11, 2014.

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

    Plugers11

    Hi, I have question. How to check
    If args[1] - guild is already in config ?
     
  2. Plugers11
    Code:
    if(getConfig().get("YOUR_STRING").equals(args[1])) {
        //continue
    }
     
  3. Offline

    xMrPoi

    Code:java
    1. if(getConfig().contains(args[1])){}
     
  4. Offline

    MineStein

    xMrPoi A little off topic, but, that profile picture though...
     
  5. Offline

    Plugers11

    if(Config.getConfig("g").getString("Guilds." + args[1] + ".Leader").equalsIgnoreCase(p.getName())){

    Why this doesn't work ?
     
  6. Offline

    IvanTheForth

  7. Offline

    Plugers11

    It's saying You don't be leader of this guild But i was leader :(
     
  8. Offline

    IvanTheForth

    Plugers11
    When replying, please tagh people, and what is your Config class?
     
  9. Offline

    Plugers11

    IvanTheForth

    Code:java
    1. import java.io.File;
    2. import java.io.FileNotFoundException;
    3. import java.io.FileOutputStream;
    4. import java.io.IOException;
    5. import java.io.InputStream;
    6. import java.io.OutputStream;
    7. import java.util.ArrayList;
    8. import java.util.List;
    9.  
    10. import org.bukkit.configuration.InvalidConfigurationException;
    11. import org.bukkit.configuration.file.YamlConfiguration;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class Config {
    15.  
    16. private static List<RConfig> configs = new ArrayList<RConfig>();
    17.  
    18. public static boolean registerConfig(String id, String fileName, JavaPlugin plugin){
    19. File file = new File(plugin.getDataFolder(), fileName);
    20.  
    21. if(!file.exists()){
    22. file.getParentFile().mkdirs();
    23. try{ copy(plugin.getResource(fileName), file); } catch (Exception e) {}
    24. }
    25.  
    26. RConfig c = new RConfig(id, file);
    27. for(RConfig x : configs){
    28. if(x.equals(c))return false;
    29. }
    30.  
    31. configs.add(c);
    32. return true;
    33. }
    34.  
    35. public static boolean unregisterConfig(String id){
    36. return configs.remove(getConfig(id));
    37. }
    38.  
    39. public static RConfig getConfig(String id){
    40. for(RConfig c : configs){
    41. if(c.getConfigId().equalsIgnoreCase(id))return c;
    42. }
    43. return null;
    44. }
    45.  
    46. public static boolean save(String id){
    47. RConfig c = getConfig(id);
    48. if(c == null)return false;
    49. try {
    50. c.save();
    51. } catch (Exception e) {
    52. print("An error occurred while saving a config with id "+id);
    53. e.printStackTrace();
    54. return false;
    55. }
    56. return true;
    57. }
    58.  
    59. public static boolean saveAll(){
    60. try{
    61. for(RConfig c : configs){
    62. c.save();
    63. }
    64. }catch(Exception e){
    65. print("An error occurred while saving all configs");
    66. e.printStackTrace();
    67. return false;
    68. }
    69. return true;
    70. }
    71. public static boolean load(String id){
    72. RConfig c = getConfig(id);
    73. if(c == null)return false;
    74. try {
    75. c.load();
    76. } catch (Exception e) {
    77. print("An error occurred while loading a config with id "+id);
    78. e.printStackTrace();
    79. return false;
    80. }
    81. return true;
    82. }
    83. public static boolean loadAll(){
    84. try{
    85. for(RConfig c : configs){
    86. c.load();
    87. }
    88. }catch(Exception e){
    89. print("An error occurred while loading all configs");
    90. e.printStackTrace();
    91. return false;
    92. }
    93. return true;
    94. }
    95.  
    96. public static void clear(String id){
    97. RConfig c = getConfig(id);
    98. if(c == null)return;
    99.  
    100. configs.remove(c);
    101. configs.add(new RConfig(c.getConfigId(), c.getFile()));
    102. }
    103.  
    104. private static void print(String msg){
    105. System.out.println("Config: "+msg);
    106. }
    107.  
    108. public static class RConfig extends YamlConfiguration {
    109. private String id;
    110. private File file;
    111.  
    112. public String getConfigId(){return id;}
    113. public File getFile(){return file;}
    114.  
    115.  
    116.  
    117. private RConfig(String id, File file){
    118. super();
    119. this.id = id;
    120. this.file = file;
    121. }
    122.  
    123. public void save() throws IOException{
    124. save(file);
    125. }
    126.  
    127. public void load() throws InvalidConfigurationException, FileNotFoundException, IOException{
    128. load(file);
    129. }
    130.  
    131. public boolean equals(RConfig c){
    132. return c.getConfigId().equalsIgnoreCase(this.id);
    133. }
    134. }
    135.  
    136. private static void copy(InputStream in, File file) throws IOException {
    137. OutputStream out = new FileOutputStream(file);
    138. byte[] buf = new byte[1024];
    139. int len;
    140.  
    141. while((len = in.read(buf)) > 0){
    142. out.write(buf, 0, len);
    143. }
    144.  
    145. out.close();
    146. in.close();
    147. }
    148. }
     
  10. Offline

    IvanTheForth

    Plugers11
    Where do you run the registerConfig method?
     
  11. Offline

    Plugers11

    I have question

    How to get guild from player ?
     
Thread Status:
Not open for further replies.

Share This Page