[Class/Library] Profile save easy User Data @author Yekllurt

Discussion in 'Resources' started by Yekllurt, Aug 19, 2014.

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

    Yekllurt

    Hello people,
    this is my Class with wich you can simply create profiles of your users.
    Please notice that this class only give's the values of the user you stored back.
    This Class ist made so you can easily save the data

    Code:java
    1. package me.Yekllurt.Main;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5. import java.util.UUID;
    6.  
    7. import org.bukkit.configuration.file.YamlConfiguration;
    8.  
    9. /**
    10. * @author Yekllurt
    11. */
    12.  
    13. public class Profile {
    14.  
    15. //Here we have all the values that can be saved
    16. private File file;
    17. private YamlConfiguration yml;
    18.  
    19. private UUID id;
    20. private String ip;
    21. private String name;
    22. private int day;
    23. private int month;
    24. private int year;
    25. private String land;
    26. private String city;
    27. private int houseNumber;
    28. private String street;
    29. private String postalCode;
    30. private int second;
    31. private int minute;
    32. private int hour;
    33.  
    34. //We make an Object named Profile wich needs the paramters id (UUID), f (file),
    35. // yml (YamlConfiguratiom). The id is the UUID of the User, the f is the data file in wich we
    36. // save the User's dat, yml is the way how we take the data out of the data file
    37. public Profile(UUID id, File f, YamlConfiguration yml){
    38. this.file = f;
    39. this.yml = yml;
    40. this.id = id;
    41. }
    42.  
    43. // Method to create a Profile
    44. public void setupProfile(){
    45. setup();
    46. }
    47.  
    48. //Method to update/load the Profile
    49. public void loadProfile(){
    50. load();
    51. }
    52.  
    53. //Method to check if the player has already a profile
    54. public boolean existsProfile(){
    55. boolean b;
    56. if(this.yml.contains(this.id.toString() + ".IP")){
    57. b = true;
    58. }else{
    59. b = false;
    60. }
    61. return b;
    62. }
    63.  
    64. //Private Method for saving the data file
    65. private void save(){
    66. try {
    67. this.yml.save(this.file);
    68. } catch (IOException e) {
    69. e.printStackTrace();
    70. }
    71. }
    72.  
    73. //The real load method for loading the profile
    74. private void load(){
    75. this.ip = this.yml.getString(this.id.toString() + ".IP");
    76. this.name = this.yml.getString(this.id.toString() + ".Name");
    77. this.day = this.yml.getInt(this.id.toString() + ".Day");
    78. this.month = this.yml.getInt(this.id.toString() + ".Month");
    79. this.year = this.yml.getInt(this.id.toString() + ".Year");
    80. this.land = this.yml.getString(this.id.toString() + ".Land");
    81. this.city = this.yml.getString(this.id.toString() + ".City");
    82. this.houseNumber = this.yml.getInt(this.id.toString() + ".HouseNumber");
    83. this.street = this.yml.getString(this.id.toString() + ".Street");
    84. this.postalCode = this.yml.getString(this.id.toString() + ".PostalCode");
    85. this.second = this.yml.getInt(this.id.toString() + ".Second");
    86. this.minute = this.yml.getInt(this.id.toString() + ".Minute");
    87. this.hour = this.yml.getInt(this.id.toString() + ".Hour");
    88. }
    89.  
    90. //The real setup method for setting up the profile
    91. private void setup(){
    92. this.yml.set(this.id.toString() + ".IP", "0.0.0.0");
    93. this.yml.set(this.id.toString() + ".Name", "Name");
    94. this.yml.set(this.id.toString() + ".Day", 0);
    95. this.yml.set(this.id.toString() + ".Month", 0);
    96. this.yml.set(this.id.toString() + ".Year", 0);
    97. this.yml.set(this.id.toString() + ".Land", "Land");
    98. this.yml.set(this.id.toString() + ".City", "City");
    99. this.yml.set(this.id.toString() + ".HouseNumber", 0);
    100. this.yml.set(this.id.toString() + ".Street", "Street");
    101. this.yml.set(this.id.toString() + ".PostalCode", "0000");
    102. this.yml.set(this.id.toString() + ".Second", 0);
    103. this.yml.set(this.id.toString() + ".Minute", 0);
    104. this.yml.set(this.id.toString() + ".Hour", 0);
    105. save();
    106. }
    107.  
    108. //Various methods to get the values from the profile
    109. // if you want that you dont always have to load the profile new for example: because of
    110. // changing values then just put before the return state the loadProfile();
    111.  
    112. public UUID getID(){
    113. return this.id;
    114. }
    115.  
    116. public String getIP(){
    117. return this.ip;
    118. }
    119.  
    120. public String getName(){
    121. return this.name;
    122. }
    123.  
    124. public int getBirthdayDay(){
    125. return this.day;
    126. }
    127.  
    128. public int getBirthdayMonth(){
    129. return this.month;
    130. }
    131.  
    132. public int getBirthdayYear(){
    133. return this.year;
    134. }
    135.  
    136. public String getLand(){
    137. return this.land;
    138. }
    139.  
    140. public String getCity(){
    141. return this.city;
    142. }
    143.  
    144. public int getHouseNumber(){
    145. return this.houseNumber;
    146. }
    147.  
    148. public String getStreet(){
    149. return this.street;
    150. }
    151.  
    152. public String getPostalCode(){
    153. return this.postalCode;
    154. }
    155.  
    156. public int getSecond(){
    157. return this.second;
    158. }
    159.  
    160. public int getMinute(){
    161. return this.minute;
    162. }
    163.  
    164. public int getHour(){
    165. return this.hour;
    166. }
    167.  
    168. //Various methods to edit the profile
    169.  
    170. public void setIP(String ip){
    171. this.yml.set(this.id.toString() + ".IP", ip);
    172. save();
    173. }
    174.  
    175. public void setName(String name){
    176. this.yml.set(this.id.toString() + ".Name", name);
    177. save();
    178. }
    179.  
    180. public void setBirthdayDay(int day){
    181. this.yml.set(this.id.toString() + ".Day", day);
    182. save();
    183. }
    184.  
    185. public void setBirthdayMonth(int month){
    186. this.yml.set(this.id.toString() + ".Month", month);
    187. save();
    188. }
    189.  
    190. public void setBirthdayYear(int year){
    191. this.yml.set(this.id.toString() + ".Year", year);
    192. save();
    193. }
    194.  
    195. public void setLand(String land){
    196. this.yml.set(this.id.toString() + ".Land", land);
    197. save();
    198. }
    199.  
    200. public void setCity(String city){
    201. this.yml.set(this.id.toString() + ".City", city);
    202. save();
    203. }
    204.  
    205. public void setHouseNumber(int housenumber){
    206. this.yml.set(this.id.toString() + ".HouseNumber", housenumber);
    207. save();
    208. }
    209.  
    210. public void setStreet(String street){
    211. this.yml.set(this.id.toString() + ".Street", street);
    212. save();
    213. }
    214.  
    215. public void setPostalCode(String postalcode){
    216. this.yml.set(this.id.toString() + ".PostalCode", postalcode);
    217. save();
    218. }
    219.  
    220. public void setSecond(int second){
    221. this.yml.set(this.id.toString() + ".Second", second);
    222. save();
    223. }
    224.  
    225. public void setMinute(int minute){
    226. this.yml.set(this.id.toString() + ".Minute", minute);
    227. save();
    228. }
    229.  
    230. public void setHour(int hour){
    231. this.yml.set(this.id.toString() + ".Hour", hour);
    232. save();
    233. }
    234.  
    235. }
    236.  


    if you want to easy add values to the Profile class just add 'private' String or int and name the value then add it to the setup method and and a getMethod and a setMethod and it's done
    If you have ideas write them in the comments

    Example how to use it:
    Code:java
    1.  
    2. /**
    3. * @author Yekllurt
    4. */
    5. File f = new File("plugins/Test", "Profile.yml");
    6. YamlConfiguration yml = YamlConfiguration.loadConfiguration(f);
    7.  
    8. @EventHandler
    9. public void onMove(AsyncPlayerChatEvent e){
    10. Player p = e.getPlayer();
    11. Profile profile = new Profile(p.getUniqueId(), f, yml);
    12. String message = e.getMessage();
    13. if(message.equals("ip")){
    14. profile.loadProfile();
    15. p.sendMessage("IP: " + profile.getIP());
    16. }
    17. if(message.equals("setip")){
    18. profile.setIP(p.getAddress().getAddress().getHostAddress());
    19. profile.loadProfile();
    20. p.sendMessage("Your new IP:" + profile.getIP());
    21. }
    22. }
     
  2. Offline

    coco5843

    Thank you ! But to make database can you use an other format like authme ?
     
  3. Offline

    Yekllurt

    Do you mean like an login plugin ? or do you mean save the valuse in a MySql Database?
     
  4. Offline

    coco5843

    Yeah a mysql database
     
  5. Offline

    EnderTroll68

    You should find a way to serialize the profile, it would make it a lot more manageable in my opinion
     
    skyrimfan1 likes this.
  6. Offline

    skyrimfan1

    I understand this to be basically a wrapper class for individual YAML files. Like Endertroll suggested, it might be better to save the info in a serializable file rather than resort to the cumbersome and slow nature of YAMLs.
     
  7. Offline

    xTrollxDudex

    YAML isn't neccessarily slow, in fact, I'm willing to bet it is faster than Java serialization
     
  8. Offline

    RawCode

    YAML, XML and JSON all have exactly same speed - write speed of your HDD.
     
    RenegadeEagle and TigerHix like this.
  9. Offline

    coco5843

    Not work for me :

    Code:java
    1. File f = new File("plugins/UtilData", "UtilData.yml");
    2. YamlConfiguration yml = YamlConfiguration.loadConfiguration(f);
    3.  
    4. @EventHandler(priority=EventPriority.LOW)
    5. public void PlayerJoin(PlayerJoinEvent event)
    6. {
    7. Player p = event.getPlayer();
    8.  
    9. if (p.hasPlayedBefore()) {
    10. Location l = UtilLocation.getLocation("spawn");
    11. p.teleport(l);
    12.  
    13. if (p.getHealth() < 20) {
    14. p.setHealth(p.getMaxHealth());
    15. } else if (p.getFoodLevel() < 20) {
    16. p.setFoodLevel(20);
    17. }
    18.  
    19. UtilData profile = new UtilData(p.getUniqueId(), f, yml);
    20. profile.loadProfile();
    21. p.sendMessage("IP: " + profile.getIP());
    22. event.setJoinMessage(null);
    23. } else {
    24. Location l = UtilLocation.getLocation("spawn");
    25. p.teleport(l);
    26. UtilData profile = new UtilData(p.getUniqueId(), f, yml);
    27. profile.setIP(p.getAddress().getAddress().getHostAddress());
    28. profile.loadProfile();
    29. p.sendMessage("IP: " + profile.getIP());
    30. event.setJoinMessage(null);
    31. }
    32.  
    33.  
    34. }
    35.  
    36.  


    The file isn't created
     
  10. Offline

    PandazNWafflez

    coco5843 create it then. The Profile class doesn't have anything that would indicate it creates the file for you.
     
Thread Status:
Not open for further replies.

Share This Page