Probably a really nooby question but....

Discussion in 'Plugin Development' started by bkleinman1, Aug 19, 2013.

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

    bkleinman1

    I am just learning how to code plugins and i am trying to make a plugin where people can join different groups and then it will change their display name to something that corresponds to the group they joined, but what i am having trouble on, is if a player joins a group and then leaves the server, when they log back on, their display name is back to normal and they aren't in the group anymore. Could someone please help me fix this. I'm guessing there is some kind of way to store all of the players in a txt file and then check what group there in, but i'm not sure :/

    If possible could you give me the code as i have only been learning bukkit today :p

    thanks.
     
  2. Offline

    Mattredsox

    Hello! Welcome to Bukkit!

    What I would do is first follow this tutorial and make a config/data YML file to store your players group info in.

    Once you have that set up, I would do this whenever a player joins a group:

    Code:java
    1. CONFIG.set("players." + PLAYER.getName(), GROUP);


    (remember to save the config)

    Next, follow this tutorial to make an event handler with this tutorial and use the PlayerJoinEvent and do this code to change their name if they are in a group:

    Code:java
    1. if(config.contains("players." + PLAYER.getName()))
    2. {
    3. String groupName = config.getString("players." + PLAYER.getName())
    4.  
    5. //SET THE PLAYERS NAME HERE
    6. }
    7.  


    Good luck, feel free to ask if you have any more questions about Bukkit!
     
  3. Offline

    bkleinman1

    Sorry, i've only had enough time to look over it now. When you said:
    1. CONFIG.set("players." + PLAYER.getName(), GROUP);
    What does the GROUP bit mean?
     
  4. Offline

    Mattredsox

    It should be a string that is the name of the group you want the player to be.
     
  5. Offline

    bkleinman1

    Mattredsox Im still a little confused, this is my code before i added the line of code you said to:

    Code:java
    1. import java.util.logging.Logger;
    2. import org.bukkit.ChatColor;
    3. import org.bukkit.command.CommandSender;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.plugin.java.JavaPlugin;
    7. import java.io.File;
    8. import java.io.FileOutputStream;
    9. import java.io.IOException;
    10. import java.io.InputStream;
    11. import java.io.OutputStream;
    12. import org.bukkit.configuration.file.FileConfiguration;
    13. import org.bukkit.configuration.file.YamlConfiguration;
    14.  
    15. public class Test extends JavaPlugin {
    16. Logger log = Logger.getLogger("Minecraft");
    17.  
    18. File configFile;
    19. File groupsFile;
    20. File historyFile;
    21. FileConfiguration config;
    22. FileConfiguration groups;
    23. FileConfiguration history;
    24.  
    25.  
    26. @Override
    27. public void onEnable() {
    28. log.info("Your plugin has been enabled!");
    29. configFile = new File(getDataFolder(), "config.yml");
    30. try {
    31. firstRun();
    32. } catch (Exception e) {
    33. e.printStackTrace();
    34. config = new YamlConfiguration();
    35. loadYamls();
    36. }
    37. }
    38.  
    39. @Override
    40. public void onDisable() {
    41. log.info("Your plugin has been disabled.");
    42. saveYamls();
    43. }
    44.  
    45.  
    46. private void firstRun() throws Exception {
    47. if(!configFile.exists()){
    48. configFile.getParentFile().mkdirs();
    49. copy(getResource("config.yml"), configFile);
    50. }
    51. }
    52.  
    53. private void copy(InputStream in, File file) {
    54. try {
    55. OutputStream out = new FileOutputStream(file);
    56. byte[] buf = new byte[1024];
    57. int len;
    58. while((len=in.read(buf))>0){
    59. out.write(buf,0,len);
    60. }
    61. out.close();
    62. in.close();
    63. } catch (Exception e) {
    64. e.printStackTrace();
    65. }
    66. }
    67.  
    68.  
    69. public void saveYamls() {
    70. try {
    71. config.save(configFile);
    72. groups.save(groupsFile);
    73. history.save(historyFile);
    74. } catch (IOException e) {
    75. e.printStackTrace();
    76. }
    77. }
    78.  
    79. public void loadYamls() {
    80. try {
    81. config.load(configFile);
    82. groups.load(groupsFile);
    83. history.load(historyFile);
    84. } catch (Exception e) {
    85. e.printStackTrace();
    86. }
    87. }
    88.  
    89.  
    90. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    91. Player player = (Player) sender;
    92. //factions
    93. if (commandLabel.equalsIgnoreCase("f")) {
    94. if (args.length == 0) {
    95. player.sendMessage(ChatColor.DARK_RED + "[Factions]" + ChatColor.GRAY + "Needs more aguments, /f [join/leave]");
    96. }
    97. else if (args.length == 1) {
    98. if (args[0].equalsIgnoreCase("join")) {
    99. player.sendMessage(ChatColor.DARK_RED + "[Factions]" + ChatColor.GRAY + "Needs more aguments, /f join miner/gamer/pvp/staff");
    100. }
    101. else if (args[0].equalsIgnoreCase("leave")) {
    102. player.sendMessage(ChatColor.DARK_RED + "[Factions]" + ChatColor.GRAY + "You have left your faction");
    103. player.setDisplayName(player.getName());
    104. }
    105. }
    106. else if (args.length == 2) {
    107. if (args[1].equalsIgnoreCase("miner")) {
    108. if (player.hasPermission("core.factions.miner")) {
    109. sender.sendMessage(ChatColor.DARK_RED + "[Factions]" + ChatColor.GRAY + "You have joined the Miner faction!");
    110. player.setDisplayName(ChatColor.BLUE + "[Miner] " + ChatColor.RESET + player.getName());
    111. }
    112. }
    113. if (args[1].equalsIgnoreCase("gamer")) {
    114. if (player.hasPermission("core.factions.gamer")) {
    115. sender.sendMessage(ChatColor.DARK_RED + "[Factions]" + ChatColor.GRAY + "You have joined the Gamer faction!");
    116. player.setDisplayName(ChatColor.BLUE + "[Gamer] " + ChatColor.RESET + player.getName());
    117. }
    118. }
    119. if (args[1].equalsIgnoreCase("pvp")) {
    120. if (player.hasPermission("core.factions.pvp")) {
    121. sender.sendMessage(ChatColor.DARK_RED + "[Factions]" + ChatColor.GRAY + "You have joined the PVP faction!");
    122. player.setDisplayName(ChatColor.BLUE + "[PVP] " + ChatColor.RESET + player.getName());
    123. }
    124. }
    125. if (args[1].equalsIgnoreCase("staff")) {
    126. if (player.hasPermission("core.factions.staff")) {
    127. sender.sendMessage(ChatColor.DARK_RED + "[Factions]" + ChatColor.GRAY + "You have joined the Staff faction!");
    128. player.setDisplayName(ChatColor.BLUE + "[Staff] " + ChatColor.RESET + player.getName());
    129. }
    130. }
    131. }
    132. }
    133. return true;
    134. }
    135. }


    Its probably something really simple that i'm missing lol.
     
Thread Status:
Not open for further replies.

Share This Page