Get list of properties

Discussion in 'Plugin Development' started by beatcomet, Jul 18, 2011.

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

    beatcomet

    Hi guys I need some help here :)
    I need to make a list of all the properties in a file (bukkit configuration).
    And I'm not sure how.
     
  2. Offline

    Tster

    I have never tried, but i assume youd make a config file in the project called "config" and load * from config.yml
    I gues thee lay out would be:
    speed: 5
    etc:
     
  3. Offline

    ZephyrSigmar

  4. Offline

    beatcomet

  5. Offline

    ZephyrSigmar

    Lol.If i understood u correctly,then you need a list of all the properties what you want to store in a file.
    And thats what i gave to you..Read the comments in it and u easily understand it.
     
  6. Offline

    beatcomet

    Well i'm not using this king of config file, I'm using bukkit config
    Here is the source (under development)

    :

    Code:java
    1.  
    2. package me.beatcomet.CM;
    3.  
    4. import java.io.File;
    5. import java.util.Random;
    6. import java.util.logging.Logger;
    7.  
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.Event;
    13. import org.bukkit.plugin.Plugin;
    14. import org.bukkit.plugin.PluginManager;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16. import org.bukkit.util.config.Configuration;
    17.  
    18. import com.nijiko.permissions.PermissionHandler;
    19. import com.nijikokun.bukkit.Permissions.Permissions;
    20.  
    21. public class CM extends JavaPlugin{
    22. //Get the minecraft logger
    23. Logger log = Logger.getLogger("Minecraft");
    24. //Create a player listener to regeister playrs joining the server
    25. /*PListener playerListener = new PListener(this);*/
    26. //New config file
    27. File configFile = new File("plugins/" + "CodeMania" + "/codes.nrg");
    28. File cfgfile = new File("/plugins/" + "CodeMania" + "/players.nrg");
    29. Configuration config = new Configuration(configFile);
    30. Configuration cfg = new Configuration (cfgfile);
    31. Random rand = new Random();
    32. public static PermissionHandler permissionHandler;
    33.  
    34. private void setupPermissions() {
    35. if (permissionHandler != null) {
    36. return;
    37. }
    38.  
    39. Plugin permissionsPlugin = this.getServer().getPluginManager().getPlugin("Permissions");
    40.  
    41. if (permissionsPlugin == null) {
    42. log.info("Permission system not detected, defaulting to OP");
    43. return;
    44. }
    45.  
    46. permissionHandler = ((Permissions) permissionsPlugin).getHandler();
    47. log.info("Found and will use plugin "+((Permissions)permissionsPlugin).getDescription().getFullName());
    48. }
    49. public void onEnable(){
    50. log.info(this.getDescription().getName() + " Version " + this.getDescription().getVersion() + " is Enabled!");
    51. setupPermissions();
    52. /*PluginManager pm = this.getServer().getPluginManager();
    53. pm.registerEvent(Event.Type.PLAYER_JOIN, playerListener, Event.Priority.Normal, this);*/
    54. new File("plugins/" + "CodeMania").mkdir();
    55. if (!configFile.exists()) {
    56. try {
    57. configFile.createNewFile();
    58.  
    59. } catch (Exception e) {
    60. // sending console message in case the data file could not be
    61. // created
    62. log.info("[RC] Error when creating data file.");
    63. }
    64. }
    65. log.info("Loading data...");
    66. // Loading data file
    67. config.load();
    68. log.info("Loading finished!");
    69. /* New file used to log players data */
    70. new File("plugins/" + "CodeMania").mkdir();
    71. if (!cfgfile.exists()) {
    72. try {
    73. cfgfile.createNewFile();
    74.  
    75. } catch (Exception e) {
    76. // sending console message in case the data file could not be
    77. // created
    78. log.info("[CodeMania] Error when creating data file.");
    79. }
    80. }
    81. log.info("Loading data...");
    82. // Loading data file
    83. cfg.load();
    84. log.info("Loading finished!");
    85. }
    86. public void onDisable(){
    87. log.info(this.getDescription().getName() + " Version " + this.getDescription().getVersion() + " is Disabled!");
    88. config.save();
    89. cfg.save();
    90. log.info("finished saving data");
    91. }
    92. public int getAmount(String code){
    93. String a[] = ((String) config.getProperty(code)).split(",");
    94. int amount= Integer.parseInt(a[1]);;
    95. return amount;
    96. }
    97. public int getID(String code){
    98. String a[] = (((String) config.getProperty(code)).split(","));
    99. int id = Integer.parseInt(a[0]);
    100. return id;
    101. }
    102. public int getUses(String code){
    103. String a[] = (((String) config.getProperty(code)).split(","));
    104. int uses = Integer.parseInt(a[2]);
    105. return uses;
    106. }
    107. public String generateRandomName(int length) {
    108. final String chars = "abcdefghijklmnopqrstuvwxyz123456789-";
    109. String word = "";
    110. while(word.length() < length){
    111. int x = rand.nextInt(chars.length());
    112. word = word + chars.charAt(x);
    113. // System.out.println(word.toUpperCase());
    114. }
    115. return word.toUpperCase();
    116. }
    117. public int getRndFromTo(int min, int max, boolean inclusive) { //get random from min to max including or excluding max
    118. return rand.nextInt(max-min+(inclusive?1:0))+min;
    119. }
    120.  
    121. public boolean checkPermission(CommandSender who,String type) {
    122. if(who instanceof Player) {
    123. if(permissionHandler!=null)
    124. if(permissionHandler.has((Player)who,type))
    125. return true;
    126. else {
    127. who.sendMessage(ChatColor.DARK_PURPLE + "[CodeMania] " + ChatColor.RED + " permissions You do not have!"); //Yoda style :D
    128. return false;
    129. }
    130. else
    131. if(who.isOp()) return true;
    132. else {
    133. who.sendMessage(ChatColor.DARK_PURPLE + "[CodeMania] " + ChatColor.RED + " operator must You be!"); //Yoda style :D
    134. return false;
    135. }
    136. } else {
    137. who.sendMessage("Prohibited is console use!"); //Yoda style :D
    138. return false;
    139. }
    140. }
    141.  
    142. public int getRandomId() {
    143. final int[][] ranges = {{1,96},{256,359},{2256,2257}};
    144. int total=0;
    145. for(int i=0;i<ranges.length;i++) {
    146. total+=ranges[i][1]-ranges[i][0]+1;
    147. }
    148. int newid=getRndFromTo(0,total,true)+ranges[0][0];
    149. for(int i=0;i<ranges.length-1;i++) {
    150. if(newid>ranges[i][1]) {
    151. newid+=ranges[i+1][0]-ranges[i][1]-1;
    152. }
    153. else break;
    154. }
    155. return newid;
    156. }
    157. public boolean isForbiddenID(int id) {
    158. final int[] forbid={ 7,8,9,10,11,16,18,26,30,31,34,36,46,51,52,54,55,58,61,62,63,64,68,71,73,74,75,78,79,90,93,94,95,327};
    159.  
    160. for(int i=0;i<forbid.length;i++)
    161. if(id==forbid[i])
    162. return true;
    163.  
    164. return false;
    165. }
    166.  
    167. public void setCode(String name, int id, int amount, int uses){
    168. String last = id + "," + amount + "," + uses;
    169. config.setProperty(name, last);
    170. config.save();
    171. }
    172. public void removeCode(String name){
    173. config.removeProperty(name);
    174. config.save();
    175. }
    176. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    177. if(commandLabel.equalsIgnoreCase("code")){
    178. if(args.length == 0 && checkPermission(sender, "code.admin.help")){
    179. sender.sendMessage("--------------------------*|Commands|*--------------------------");
    180. sender.sendMessage("/code set <code> <id> <amount> <uses> : Used to set a new code");
    181. sender.sendMessage("/code remove <code> : Used to remove an existing code");
    182. sender.sendMessage("/code generate <length> : Generate a random code");
    183. sender.sendMessage("/code status <code> : Check the amount of uses left for the code");
    184. sender.sendMessage("/code list : Get the list of codes currently avalible");
    185. sender.sendMessage("/redeem <code> : Reddem a code");
    186. return true;
    187. }
    188. else if(args.length == 0 && sender instanceof Player){
    189. sender.sendMessage("--------------------------*|Commands|*--------------------------");
    190. sender.sendMessage("/redeem <code> : Redeem a code");
    191. return true;
    192. }
    193. if(args[0].equalsIgnoreCase("set") && args.length == 5 && checkPermission(sender, "code.admin.set")){
    194. String name = args[1];
    195. int id =-1;
    196. try{id = Integer.parseInt(args[2]);}catch(Exception ex){log.info("Invalid ID!");}
    197. int amount =-1;
    198. try{amount = Integer.parseInt(args[3]);}catch(Exception e){log.info("Invalid amount!");}
    199. int uses =-1;
    200. try{uses = Integer.parseInt(args[4]);}catch(Exception exc){log.info("Invalid amount of uses!");}
    201. setCode(name, id, amount, uses);
    202. return true;
    203. }
    204. else if(args[0].equalsIgnoreCase("remove") && args.length == 2 && checkPermission(sender, "code.admin.remove")){
    205. String name = args[1];
    206. removeCode(name);
    207. return true;
    208. }
    209. else if (args[0].equalsIgnoreCase("generate") && args.length == 3 && checkPermission(sender, "code.admin.generate")){
    210. int id=-1, amount=-1, redeems=-1;
    211. int length = 8;
    212. try{length = Integer.parseInt(args[1]);}catch(Exception ex){log.info("Invalid length!");}
    213. String name= this.generateRandomName(length);
    214. if(id==-1) {
    215. do {
    216. id = getRandomId();
    217. log.info("Random ID generator returned id "+id+", it is "+(isForbiddenID(id)?"forbidden":"allright"));
    218. } while(isForbiddenID(id));
    219. }
    220. if(amount==-1) {
    221. amount=getRndFromTo(1,15,true);
    222. }
    223. if(redeems==-1) {
    224. redeems=getRndFromTo(5,12,true);
    225. }
    226. this.setCode(name, id, amount, redeems);
    227. return true;
    228. }
    229. else if (args[0].equalsIgnoreCase("status") && args.length == 2 && checkPermission(sender, "code.admin.status")){
    230. String name = args[1];
    231. String status = "";
    232. if(getUses(name) > 0){
    233. status = getUses(name)+ " uses left";
    234. }else{
    235. status = "All used up";
    236. }
    237. sender.sendMessage("[CodeMania] " + name + " :" + status);
    238.  
    239. return true;
    240. }
    241. else if (args[0].equalsIgnoreCase("list") && args.length == 1 && checkPermission(sender, "code.admin.list")){
    242. }
    243. }else if(commandLabel.equalsIgnoreCase("redeem")){
    244. }
    245. return false;
    246. }
    247. }
    248. [I][/I][/i][/i][/i][/i][/i]
     
  7. Offline

    ZephyrSigmar

    @beatcomet Oh,well,then i cant help you.I had some problems with bukkitconfig,thats why i use properties instead of it :)
     
Thread Status:
Not open for further replies.

Share This Page