still dont understand

Discussion in 'Plugin Development' started by krooked590, Nov 8, 2011.

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

    krooked590

    all i want to do is have a yml file that contains a list of players. i would like to somewhere in my code take a player that has used a command and see if there name is on that list. that's all.
    thank you in advanced
     
  2. Offline

    cjc343

    yml isn't meant to be a plain text file, it's meant to follow a specific format. You can have an array of player names stored in yml under a key such as "users:" but you can't just put a list of playernames with no formatting.

    That said, nothing forces you to use yml. You can read and write standard text files too and can define custom formats, such as a simple one-per-line list yourself.
     
  3. Offline

    krooked590

    No what i am trying to do is ban ppl from a world upon death so the format would be world: then a list of everyone who is banned from that world
     
  4. You have to have a (Array)List for each world containing the player names, than save it with:
    config.set("worldName", yourList);
    and later you can read it with:
    List<Object> = config.getList("worldName");
    of course you need to do a bit of casting to get all to your plugin internal structure.
     
  5. Offline

    krooked590

    Yea that's what I'm having trouble with is the casting part. I keep getting cannot convert list to a string and some stuff like that btw that's an awesome dog.
     
  6. Offline

    bergerkiller

    Continuing V10lators code:
    Code:
    List<Object> list = config.getList("worldName");
    ArrayList<String> bannedPlayers = new ArrayList<String>();
    for (Object o : list) {
        if (o instanceof String) {
            bannedPlayers.add((String) o);
        }
    }
     
  7. Offline

    krooked590

    Thank you guys for the help. Sorry if I'm being a bother. I usually try to solve these problems on my own but sometimes I just have to admit that I don't know everything. I will try what u guys said as soon as I get home and I'll post an update.
     
  8. Offline

    AndrewM16921

    If you eventually get fed up with YML (as I did), I made my own configuration utility that I use for my plugins.

    I'd be happy to share it with you, if you'd ever want to use it.
    You essentially construct (or load it from file) a tree of ConfigNodes, then read it (or save it to file).
    Code:java
    1. package org.nubcraft.nubbukkit.util.config;
    2.  
    3. import java.io.BufferedReader;
    4. import java.io.BufferedWriter;
    5. import java.io.File;
    6. import java.io.FileReader;
    7. import java.io.FileWriter;
    8. import java.io.IOException;
    9. import java.util.HashMap;
    10.  
    11. public class ConfigNode
    12. {
    13. //Variables
    14. public final static int INDENT = 4;
    15. private String name, value;
    16. private HashMap<String, ConfigNode> children;
    17. //Constructors
    18. public ConfigNode(String name, String value)
    19. {
    20. this.name = name;
    21. this.value = value;
    22. children = new HashMap<String, ConfigNode>();
    23. }
    24. public ConfigNode(String name)
    25. {
    26. this.name = name;
    27. value = null;
    28. children = new HashMap<String, ConfigNode>();
    29. }
    30. private ConfigNode()
    31. {
    32. name = "Root";
    33. value = null;
    34. children = new HashMap<String, ConfigNode>();
    35. }
    36. //get methods
    37. public String getName()
    38. {
    39. return name;
    40. }
    41. public boolean hasValue()
    42. {
    43. return value != null;
    44. }
    45. public String get()
    46. {
    47. return value;
    48. }
    49. public String getString()
    50. {
    51. return value;
    52. }
    53. public Boolean getBoolean()
    54. {
    55. String val = value.toString();
    56. if(val.equalsIgnoreCase("true"))
    57. return true;
    58. return false;
    59. }
    60. public Integer getInteger()
    61. {
    62. try
    63. {
    64. int i = Integer.parseInt(value);
    65. return i;
    66. }
    67. {
    68. return null;
    69. }
    70. }
    71. public Short getShort()
    72. {
    73. try
    74. {
    75. short s = Short.parseShort(value);
    76. return s;
    77. }
    78. {
    79. return null;
    80. }
    81. }
    82. public Float getFloat()
    83. {
    84. try
    85. {
    86. float f = Float.parseFloat(value);
    87. return f;
    88. }
    89. {
    90. return null;
    91. }
    92. }
    93. public Double getDouble()
    94. {
    95. try
    96. {
    97. double d = Double.parseDouble(value);
    98. return d;
    99. }
    100. {
    101. return null;
    102. }
    103. }
    104. //is methods
    105. public boolean isBoolean()
    106. {
    107. return getBoolean() != null;
    108. }
    109. public boolean isInteger()
    110. {
    111. return getInteger() != null;
    112. }
    113. public boolean isDouble()
    114. {
    115. return getDouble() != null;
    116. }
    117. //Methods involving children
    118. public boolean isLeaf()
    119. {
    120. return numChildren() == 0;
    121. }
    122. public int numChildren()
    123. {
    124. return children.size();
    125. }
    126. public boolean hasPath(String path)
    127. {
    128. String[] names = path.split("\\.");
    129. ConfigNode current = this;
    130. for(String name : names)
    131. {
    132. if(!current.hasChild(name))
    133. return false;
    134. current = current.getChild(name);
    135. }
    136. return true;
    137. }
    138. public void addPath(String path)
    139. {
    140. String[] names = path.split("\\.");
    141. ConfigNode current = this;
    142. for(String name : names)
    143. {
    144. if(!current.hasChild(name))
    145. current.addChild(name);
    146. current = current.getChild(name);
    147. }
    148. }
    149. public boolean hasChild(String name)
    150. {
    151. return children.containsKey(name);
    152. }
    153. public boolean hasChildByPath(String path, String name)
    154. {
    155. if(hasPath(path))
    156. {
    157. return getChildByPath(path).hasChild(name);
    158. }
    159. return false;
    160. }
    161. public boolean hasChildren()
    162. {
    163. return numChildren() > 0;
    164. }
    165. public boolean hasChildrenByPath(String path)
    166. {
    167. if(hasPath(path))
    168. {
    169. return getChildByPath(path).hasChildren();
    170. }
    171. return false;
    172. }
    173. public ConfigNode getChild(String name)
    174. {
    175. return children.get(name);
    176. }
    177. public ConfigNode[] getChildren()
    178. {
    179. return children.values().toArray(new ConfigNode[children.size()]);
    180. }
    181. public ConfigNode getChildByPath(String path)
    182. {
    183. String[] names = path.split("\\.");
    184. ConfigNode current = this;
    185. for(String name : names)
    186. {
    187. if(!current.hasChild(name))
    188. return null;
    189. current = current.getChild(name);
    190. }
    191. return current;
    192. }
    193. public ConfigNode getChildByPath(String path, String name)
    194. {
    195. String[] names = path.split("\\.");
    196. ConfigNode current = this;
    197. for(String cname : names)
    198. {
    199. if(!current.hasChild(cname))
    200. return null;
    201. current = current.getChild(cname);
    202. }
    203. if(current.hasChild(name))
    204. return current.getChild(name);
    205. return null;
    206. }
    207. public void addChild(ConfigNode node)
    208. {
    209. children.put(node.getName(), node);
    210. }
    211. public void addChild(String name)
    212. {
    213. addChild(new ConfigNode(name, null));
    214. }
    215. public void addChild(String name, String value)
    216. {
    217. addChild(new ConfigNode(name, value));
    218. }
    219. public void addChildren(ConfigNode[] nodes)
    220. {
    221. for(ConfigNode node : nodes)
    222. children.put(node.getName(), node);
    223. }
    224. public void addChildren(String[] names)
    225. {
    226. for(String name : names)
    227. children.put(name, new ConfigNode(name));
    228. }
    229. public void addChildByPath(String path, ConfigNode node)
    230. {
    231. addPath(path);
    232. ConfigNode current = getChildByPath(path);
    233. if(current != null)
    234. current.addChild(node);
    235. }
    236. public void addChildByPath(String path, String name)
    237. {
    238. addPath(path);
    239. ConfigNode current = getChildByPath(path);
    240. if(current != null)
    241. current.addChild(name, null);
    242. }
    243. public void addChildByPath(String path, String name, String value)
    244. {
    245. addPath(path);
    246. ConfigNode current = getChildByPath(path);
    247. if(current != null)
    248. current.addChild(name, value);
    249. }
    250. public void removeChild(String name)
    251. {
    252. children.remove(name);
    253. }
    254. public void removeChildren()
    255. {
    256. children.clear();
    257. }
    258. public void removeChildren(String[] names)
    259. {
    260. for(String name : names)
    261. children.remove(name);
    262. }
    263. public void removeChildByPath(String path, String name)
    264. {
    265. ConfigNode current = getChildByPath(path);
    266. if(current != null && current.hasChild(name))
    267. current.removeChild(name);
    268. }
    269. //Other methods
    270. public static ConfigNode createRoot()
    271. {
    272. return new ConfigNode();
    273. }
    274. //File handling
    275. public static ConfigNode load(String path, String filename) throws IOException
    276. {
    277. new File(path).mkdirs();
    278. BufferedReader IN = new BufferedReader(new FileReader(path+filename));
    279. String line = "";
    280. HashMap<Integer, ConfigNode> parents = new HashMap<Integer, ConfigNode>();
    281. ConfigNode root = createRoot(), current = null;
    282. parents.put(-1, root);
    283. while(true)
    284. {
    285. line = IN.readLine();
    286. if(line == null)
    287. break;
    288. int indentation = 0;
    289. for(int i = 0; i < line.length(); i++)
    290. {
    291. if(line.charAt(i) == ' ')
    292. indentation++;
    293. else
    294. break;
    295. }
    296. indentation /= INDENT;
    297. String[] lineComponents = line.trim().split(": ");
    298. comment: {
    299. if(lineComponents.length > 0)
    300. {
    301. if(lineComponents[0].startsWith("#") || lineComponents[0].equals(""))
    302. break comment;
    303. if(lineComponents.length == 1)
    304. {
    305. current = new ConfigNode(lineComponents[0], null);
    306. }
    307. else
    308. {
    309. current = new ConfigNode(lineComponents[0], lineComponents[1]);
    310. }
    311. parents.put(indentation, current);
    312. parents.get(indentation-1).addChild(current);
    313. current = null;
    314. }
    315. }
    316. }
    317. IN.close();
    318. return root;
    319. }
    320. public static void save(String path, String filename, ConfigNode config) throws IOException
    321. {
    322. new File(path).mkdirs();
    323. BufferedWriter OUT = new BufferedWriter(new FileWriter(path+filename));
    324. OUT.write(toStringRecursive(config));
    325. OUT.close();
    326. }
    327. //toString methods
    328. public String toString()
    329. {
    330. if(value == null)
    331. return name;
    332. else
    333. return name + ": " + value;
    334. }
    335. public String toString(int indentation)
    336. {
    337. indentation *= INDENT;
    338. String str = "";
    339. for(int i = 0; i < indentation; i++)
    340. {
    341. str += ' ';
    342. }
    343. return str+toString();
    344. }
    345. public String toStringRecursive()
    346. {
    347. return toStringRecursive(this, -1);
    348. }
    349. public String toStringRecursive(int indentation)
    350. {
    351. return toStringRecursive(this, indentation);
    352. }
    353. public static String toStringRecursive(ConfigNode config)
    354. {
    355. return toStringRecursive(config, -1);
    356. }
    357. public static String toStringRecursive(ConfigNode config, int indentation)
    358. {
    359. String str = "";
    360. if(indentation >= 0)
    361. str = config.toString(indentation) + '\n';
    362. indentation++;
    363. for(ConfigNode child : config.getChildren())
    364. {
    365. str += toStringRecursive(child, indentation);
    366. }
    367. indentation--;
    368. return str;
    369. }
    370. }
    371.  
     
  9. Offline

    krooked590

    ok so that worked beautifully, and i cant believe that i didn't remember that the list itself ISN'T a string so you have to go threw the list and add each string to the ArrayList. now all i need to do is check whether or not a players name is on that list to allow or deny access to something.

    I finally got everything to work. Once I added all strings to the arrayList I can compare the player name by using:
    If(bannedplayers.contains(sender.getName());

    Thanks a lot to everyone who has helped!!!

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

    halley

    If you are collecting a bunch of strings, and you expect to be checking if the collection contains a given string often, then don't use a list, use a set or a map. This is far more efficient for contain checks, especially if the collection is large or checks are done often.
     
  11. Yea, changing it to a HashSet might be a little faster.
     
  12. Offline

    krooked590

    I'll look into doing that as soon as a I get a fully working build.
     
Thread Status:
Not open for further replies.

Share This Page