Storing strings related to player

Discussion in 'Plugin Development' started by candyfloss20, Jun 26, 2014.

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

    candyfloss20

    So i've got this ban command
    and when you are baned your kicked whit a message
    and i need to know how to store the ban info (who baned them, date, reason)
    in the config file under there UUID so i can kick them for it in the PlayerLoginEvent
    dose anyone know how i can do this ?

    Code:
    Code:java
    1. } else {
    2.  
    3. DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    4. Date date = new Date();
    5.  
    6. Date curr_date = new Date(System.currentTimeMillis());
    7. String kickline1 = "§4§l>>>>>>>>>>>>>>>>>>§c§nBANNED§4§l<<<<<<<<<<<<<<<<<<";
    8. String kickline2 = " §cYour account has been suspened ";
    9. String kickline3 = " §cAppeal @ xcraftmc.net ";
    10. String kickline4 = "§dReason: §e" + kickmessage;
    11. String kickline5 = "§dBanned By: §e" + sender.getName();
    12. String kickline6 = "§dDate: §e" + dateFormat.format(date);
    13. String kickline7 = "§4§l>>>>>>>>>>>>>>>>>>§c§nBANNED§4§l<<<<<<<<<<<<<<<<<<";
    14.  
    15. String name = target.getUniqueId().toString();
    16. String reason = kickline1 + "\n" + kickline2 + "\n" + kickline3 + "\n" + kickline4 + "\n" + kickline5 + "\n" + kickline6 + "\n" + kickline7;
    17.  
    18. List<String> banned = plugin.getConfig().getStringList("banned");
    19. banned.add(name + "@" + reason);
    20.  
    21. plugin.getConfig().createSection("banned");
    22. plugin.getConfig().set("banned", banned);
    23.  
    24.  
    25. target.kickPlayer(kickline1 + "\n" + kickline2 + "\n" + kickline3 + "\n" + kickline4 + "\n" + kickline5 + "\n" + kickline6 + "\n" + kickline7);
    26. }
    27. }
    28. }
    29. }
    30. return true;
    31. }
    32. @EventHandler
    33. public void onLogin (PlayerLoginEvent event) {
    34. String entry = //what ever i put here gives me a error :(
    35. String[] s = entry.split("@");
    36.  
    37. String name = s[0];
    38. String reason = s[1];
    39. if (event.getPlayer().getUniqueId().toString().equalsIgnoreCase(s[0])) {
    40. event.disallow(PlayerLoginEvent.Result.KICK_OTHER, s[1]);
    41. }
    42. }
    43. }[I][/I]



    Regards candyfloss20
    :)
     
  2. Offline

    mythbusterma

    candyfloss20

    Make sure you use UUID.toString(String) for proper storage, also you can create a ConfigurationSerializable that stores all the info you need, and then make a Map of type <String, ? extends ConfigurationSerializable> then store that with ConfigurationSection#set(String,Object) where the String is the key and the Object is your map.

    Don't forget to save the config file so it reflects the changes.
     
  3. Offline

    candyfloss20

    mythbusterma,
    Can you show me how to eg: with bukkit code ?
     
  4. Offline

    mythbusterma

    Use Google to find examples, javadocs, and the wiki entry on it.
     
  5. Offline

    candyfloss20

  6. Offline

    mine-care

    Instead if kickline1 kickline2 ect, you can use a String[]
    Just saying ;)
     
  7. Offline

    candyfloss20

    mine-care and mythbusterma,
    ive got this now:
    but its still not working any help ?

    Class1:
    Code:java
    1. package com.xCraft.xSuite.Commands.Ban;
    2.  
    3. import com.xCraft.xSuite.ChatAPI;
    4. import com.xCraft.xSuite.xSuite;
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandExecutor;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.Listener;
    11.  
    12. import java.text.DateFormat;
    13. import java.text.SimpleDateFormat;
    14. import java.util.Date;
    15. import java.util.UUID;
    16.  
    17. public class BanCommand implements CommandExecutor, Listener {
    18. xSuite plugin;
    19.  
    20. public BanCommand(xSuite instance) {
    21. plugin = instance;
    22. }
    23.  
    24. @Override
    25. public boolean onCommand(CommandSender sender, Command command, String string, String[] args) {
    26. if (command.getName().equalsIgnoreCase("ban")) {
    27. if (sender instanceof Player && sender.hasPermission("xcraft.ban")) {
    28. if (args.length < 2) {
    29. sender.sendMessage(ChatAPI.COMMAND_ERROR + "Invalid arg format, Try: /ban <name> <reason>");
    30. } else {
    31. Player target = Bukkit.getPlayer(args[0]);
    32. String uuid = target.getUniqueId().toString();
    33. Player fromUUID = Bukkit.getServer().getPlayer(UUID.fromString(uuid));
    34. String kickmessage = "";
    35.  
    36. for (int i = 1; i < args.length; i++) {
    37. kickmessage = kickmessage + (args[i] + " ");
    38. }
    39. if (target.hasPermission("xcraft.antiban")) {
    40. sender.sendMessage(ChatAPI.COMMAND_ERROR + "This player has permission: xcraft.antiban, so cant be baned");
    41. } else {
    42. if (!plugin.getConfig().contains(uuid)) {
    43. plugin.getConfig().createSection(uuid);
    44. plugin.getConfig().getConfigurationSection(uuid).set("uuid", 1);
    45. plugin.saveConfig();
    46.  
    47. DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    48. Date date = new Date();
    49.  
    50. Date curr_date = new Date(System.currentTimeMillis());
    51.  
    52. String kickline1 = "§4§l>>>>>>>>>>>>>>>>>>§c§nBANNED§4§l<<<<<<<<<<<<<<<<<<";
    53. String kickline2 = "§cYour account has been suspened ";
    54. String kickline3 = "§eAppeal @ xcraftmc.net\"";
    55. String kickline4 = "§dReason: §e" + kickmessage;
    56. String kickline5 = "§dBanned By: §e" + sender.getName();
    57. String kickline6 = "§dDate: §e" + dateFormat.format(date);
    58. String kickline7 = "§4§l>>>>>>>>>>>>>>>>>>§c§nBANNED§4§l<<<<<<<<<<<<<<<<<<";
    59.  
    60. target.kickPlayer(kickline1 + "\n" + kickline2 + "\n" + kickline3 + "\n" + kickline4 + "\n" + kickline5 + "\n" + kickline6 + "\n" + kickline7);
    61. String banmsg = kickline1 + "\n" + kickline2 + "\n" + kickline3 + "\n" + kickline4 + "\n" + kickline5 + "\n" + kickline6 + "\n" + kickline7;
    62. plugin.getConfig().getConfigurationSection(uuid).set(banmsg, 1);
    63. }
    64. }
    65. }
    66. }
    67. }
    68. return true;
    69. }
    70. }[I][/I][/i]


    Class2:
    Code:java
    1.  
    Code:java
    1. [I]package com.xCraft.xSuite.Commands.Ban;[/I]
    2. [I][/I]
    3. [I]import com.xCraft.xSuite.xSuite;[/I]
    4. [I]import org.bukkit.event.EventHandler;[/I]
    5. [I]import org.bukkit.event.Listener;[/I]
    6. [I]import org.bukkit.event.player.PlayerLoginEvent;[/I]
    7. [I][/I]
    8. [I]public class BanListener implements Listener{[/I]
    9. [I] xSuite plugin;[/I]
    10. [I][/I]
    11. [I] public BanListener(xSuite instance) {[/I]
    12. [I] plugin = instance;[/I]
    13. [I] }[/I]
    14. [I][/I]
    15. [I][/I]
    16. [I] @EventHandler[/I]
    17. [I] public void onLogin(PlayerLoginEvent event) {[/I]
    18. [I] String uuid = event.getPlayer().getUniqueId().toString();[/I]
    19. [I][/I]
    20. [I] if (plugin.getConfig().getConfigurationSection(uuid).get("uuid").equals(1)) {[/I]
    21. [I] event.disallow(PlayerLoginEvent.Result.KICK_OTHER, (String) plugin.getConfig().getConfigurationSection(uuid).get("banmsg"));[/I]
    22. [I] }[/I]
    23. [I] }[/I]
    24. [I]}[/I]
    25. [I][/I]

    config makes this when banned but lets you back in:

    Code:
    ddabe7fd-7e87-375a-91b6-e53baba8f691: 1
     
    
     
  8. Offline

    mine-care

    Ok I'll pay a look on it when I get to a pc (in 15 hours... Lol)
     
  9. Offline

    mythbusterma

    candyfloss20

    Because right now your code is looking for:

    Code:
    ddabe7fd-7e87-375a-91b6-e53baba8f691:
      uuid: 1
    
     
  10. Offline

    candyfloss20

    mythbusterma, mine-care,
    Updated main code to what i have now but i dont know what to put here:
    String entry = //what ever i put here gives me a error :(
    String[] s = entry.split("@");

    String name = s[0];
    String reason = s[1];
    if (event.getPlayer().getUniqueId().toString().equalsIgnoreCase(s[0])) {
    event.disallow(PlayerLoginEvent.Result.KICK_OTHER, s[1]);
     
  11. Offline

    mythbusterma

    I don't quite understand why you would be storing a string that is "<something>@<something>" for your ban reason.

    Like I said before, just use a Map of a UUID and ConfigurationSerializable.
     
Thread Status:
Not open for further replies.

Share This Page