Need help with data management

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

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

    beatcomet

    I need help, I need to store and get 3 variable,
    The variables are :

    String name;
    Player player;
    Integer amount;

    I can't store them on a config file because they cannot be initialized until the admin executes a command.
     
  2. Offline

    Mixcoatl

    How long are these variables needed? Are they just these three or do you need a copy of these three variables for each player? Online players online or online and offline players?
    Well, you may not be able to store them in the plug-in's configuration file, but you may still be able to store them in a YML file or another form of flat file.
     
  3. Offline

    beatcomet

    What I need it to do is :

    1. player types a command (done)
    2. check if command is right (done)
    3. check if the name exists (done)
    4. if it does assign two values : Player and amount (my probelm)


    Can you pleas teach me how to do that?
     
  4. Offline

    DrBowe

    @Mixcoatl
    I don't even think a Player can be stored, regardless (though obviously you just work around it with the player's name)

    As for the OP, like Mix said, it would help greatly if we knew what your actual intent was with the variables

    EDIT:
    Ah, already did before I posted ;)
     
  5. Offline

    beatcomet

    Do you want me to send the code I wrote up to this point?
     
  6. Offline

    DrBowe

    @beatcomet
    Source is always useful, yes.
    Helps us see the 'greater picture', so we know exactly what your intent is (and often, this helps solve issues you haven't even come across, before they happen)
     
  7. Offline

    Mixcoatl

    I'm assuming you're storing this mapping in memory presently and just need to be able to save them and load them back up when necessary?
    There are two sides to this: saving and loading. Since I don't know what you're data structure looks like it's hard for me to give you code that will just work. I would provide pseudocode but I'm not sure that will be helpful. How are you keeping this data in memory right now?
    Correct; you wouldn't attempt to write the player object, itself. You would have to use its name or some other uniquely identifying characteristic. But we don't even know how the information is being represented in memory, yet.
     
  8. Offline

    beatcomet

    Here :

    Code:java
    1.  
    2. package me.beatcomet.RD;
    3.  
    4. import java.io.File;
    5. import java.util.logging.Logger;
    6.  
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.inventory.ItemStack;
    12. import org.bukkit.plugin.Plugin;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14. import org.bukkit.util.config.Configuration;
    15.  
    16. import com.nijiko.permissions.PermissionHandler;
    17. import com.nijikokun.bukkit.Permissions.Permissions;
    18.  
    19. public class RD extends JavaPlugin{
    20. Logger log = Logger.getLogger("Minecraft");
    21. File configFile = new File("plugins/" + "Redeemed" + "/codes.comet");
    22. Configuration config = new Configuration(configFile);
    23.  
    24. //setup permissions
    25. public static PermissionHandler permissionHandler;
    26.  
    27. private void setupPermissions() {
    28. if (permissionHandler != null) {
    29. return;
    30. }
    31.  
    32. Plugin permissionsPlugin = this.getServer().getPluginManager().getPlugin("Permissions");
    33.  
    34. if (permissionsPlugin == null) {
    35. log.info("Permission system not detected, defaulting to OP");
    36. return;
    37. }
    38.  
    39. permissionHandler = ((Permissions) permissionsPlugin).getHandler();
    40. log.info("Found and will use plugin "+((Permissions)permissionsPlugin).getDescription().getFullName());
    41. }
    42. public void onDisable(){
    43. log.info(this.getDescription().getName() + " Version " + this.getDescription().getVersion() + " is DISABLED!");
    44. config.save();
    45. }
    46. public void onEnable(){
    47. log.info(this.getDescription().getName() + " Version " + this.getDescription().getVersion() + " is ENABLED!");
    48. setupPermissions();
    49. new File("plugins/" + "Redeemed").mkdir();
    50. if (!configFile.exists()) {
    51. try {
    52. configFile.createNewFile();
    53.  
    54. } catch (Exception e) {
    55. // sending console message in case the data file could not be
    56. // created
    57. log.info("[RC] Error when creating data file.");
    58. }
    59. }
    60. // Loading data file
    61. config.load();
    62. if(config.getKeys("props")== null){
    63. config.setProperty("props.Length", 8);
    64. }
    65. config.save();
    66. }
    67. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    68. Player player = (Player) sender;
    69. String codename;
    70. int itemid;
    71. int amount;
    72. int maxuses;
    73. int length = (Integer) config.getProperty("props.Length");
    74. //check if the command has been send by a player
    75. if(sender instanceof Player){
    76. //code related commands, need to have admin permissions (code.*)
    77. if(commandLabel.equalsIgnoreCase("code")){
    78. //check the command length
    79. if(args.length == 1){
    80. }
    81. //code removal process
    82. if(args.length == 2){
    83. }//code creation progress
    84. if(args.length == 4){
    85. //check if its the adding command
    86. if(args[0].equalsIgnoreCase("add")){
    87. codename = args[1];
    88. itemid = Integer.parseInt(args[2]);
    89. amount = Integer.parseInt(args[3]);
    90. maxuses = Integer.parseInt(args[4]);
    91. if((config.getKeys(codename) == null)){
    92. config.setProperty(codename+"."+"ItemID", itemid);
    93. config.setProperty(codename+"."+"Amount", amount);
    94. config.setProperty(codename+"."+"MaxUses", maxuses);
    95. config.save();
    96. }
    97. else{
    98. if(config.getProperty(codename+"."+"ItemID") != null || config.getProperty(codename+"."+"Amount") != null || config.getProperty(codename+"."+"MaxUses") != null){
    99. }else{
    100. player.sendMessage(ChatColor.DARK_PURPLE + "[Redeened] " + ChatColor.DARK_RED + "This code already exists!");
    101. }
    102. }
    103. }
    104. else{
    105. player.sendMessage(ChatColor.DARK_PURPLE + "[Redeened] " + ChatColor.DARK_RED + "Invalid command!");
    106. }
    107. }
    108. else{
    109. player.sendMessage(ChatColor.DARK_PURPLE + "[Redeened] " + ChatColor.DARK_RED + "Invalid command!");
    110. }
    111. }
    112. //redeem command used to redeem the item, no permissions needed
    113. if(commandLabel.equalsIgnoreCase("redeem")){
    114. if(args.length == 1){
    115. //check if the code exist by comparing it to null
    116. if(config.getKeys(args[0] ) != null){
    117. codename = args[0];
    118. itemid = (Integer) config.getProperty(args[0]+"."+"ItemID");
    119. amount = (Integer) config.getProperty(args[0]+"."+"Amount");
    120. maxuses = (Integer) config.getProperty(args[0]+"."+"MaxUses");
    121. ItemStack items = new ItemStack(itemid, amount);
    122. //check if the code has been redeemed
    123. if(maxuses <= 0){
    124. config.setProperty(args[0]+"."+"MaxUses", 0);
    125. config.save();
    126. player.sendMessage(ChatColor.DARK_PURPLE + "[Redeened] " + ChatColor.AQUA + args[0] + ChatColor.DARK_RED + " Has been used up!");
    127. }if(maxuses > 0){
    128. player.getInventory().addItem(items);
    129. player.sendMessage(ChatColor.DARK_PURPLE + "[Redeened] " + ChatColor.AQUA + args[0] + ChatColor.DARK_RED + " Has been redeemed!");
    130. player.sendMessage(ChatColor.DARK_PURPLE + "[Redeened] " + ChatColor.AQUA + (maxuses - 1) + ChatColor.DARK_RED + " Uses left!");
    131. config.setProperty(args[0]+"."+"MaxUses", maxuses -1);
    132. config.save();
    133. }
    134. }
    135. else{
    136. player.sendMessage(ChatColor.DARK_PURPLE + "[Redeened] " + ChatColor.AQUA + args[0] + ChatColor.DARK_RED + " dose not exist!");
    137. }
    138. }
    139. if(args.length == 0){
    140. player.sendMessage(ChatColor.DARK_PURPLE + "[Redeemed] " + ChatColor.DARK_RED + "No Code Inserted!");
    141. }
    142. else{
    143. player.sendMessage(ChatColor.DARK_PURPLE + "[Redeened] " + ChatColor.DARK_RED + "Invalid code inserted!");
    144. }
    145. }
    146. // length command
    147. if(commandLabel.equalsIgnoreCase("length")){
    148. //check args length to see if the command has been used correctly
    149. if(args.length == 0){
    150. //send message to the sender because he didn't set any new values
    151. player.sendMessage(ChatColor.DARK_PURPLE + "[Redeened] " + ChatColor.DARK_RED + "No number inserted!");
    152. }
    153. //this is the correct way, args[0] sets the new length value
    154. if(args.length == 1){
    155. length = Integer.parseInt(args[0]);
    156. config.setProperty("props.Length", length);
    157. config.save();
    158. player.sendMessage(ChatColor.DARK_PURPLE + "[Redeened] " + ChatColor.DARK_RED + "Length set to " + ChatColor.AQUA + length);
    159. }
    160. //completly wrong command inserted
    161. else{
    162. player.sendMessage(ChatColor.DARK_PURPLE + "[Redeened] " + ChatColor.DARK_RED + "Invalid command inserted!");
    163. }
    164. }
    165. }else{
    166. log.info("You must be a player to do that!");
    167. return true;
    168. }
    169. }
    170.  
    171. }
    172.  
    173.  


    BTW, now I noticed that I wrote the messages worngly ([redeened] instead of [redeemed]) xD
    note that it's not done yet

    yes
    Check the code above
    That is the plan :)


    anyone?

    Edit : Iv'e been thinking, and I think I found a solution :

    I will add a PlayerJoinEvent, and because all the code generated buy my plugin are set to be keys, I can get a key list and add each player's name to the key with a certain value.
    It may be a little messy but I think it will work

    Edit 2 : I got an event better idea :

    When a player executes a command it will check if the key (code) contains the players name, if the player is not there it will add him with the default value!

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

Share This Page