/src/main/java/com/laytonsmith/aliasengine/User.java

https://github.com/albatrossen/commandhelper · Java · 100 lines · 80 code · 11 blank · 9 comment · 4 complexity · ba958748f031205652541b80db7ed1f2 MD5 · raw file

  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package com.laytonsmith.aliasengine;
  6. import com.laytonsmith.PureUtilities.SerializedPersistance;
  7. import java.util.ArrayList;
  8. import java.util.Map;
  9. import java.util.logging.Level;
  10. import java.util.logging.Logger;
  11. import org.bukkit.ChatColor;
  12. import org.bukkit.entity.Player;
  13. /**
  14. *
  15. * @author layton
  16. */
  17. public class User {
  18. Player player;
  19. SerializedPersistance persist;
  20. public User(Player player, SerializedPersistance persist){
  21. //(new PermissionsResolverServerListener(perms)).register(this);
  22. this.player = player;
  23. this.persist = persist;
  24. }
  25. public int addAlias(String alias){
  26. try {
  27. ArrayList<Map.Entry> list = persist.getNamespaceValues(new String[]{player.getName(), "aliases"});
  28. System.out.println(list);
  29. Integer nextValue = 0;
  30. for (Map.Entry e : list) {
  31. String[] x = e.getKey().toString().split("\\.");
  32. Integer thisX = Integer.parseInt(x[x.length - 1]);
  33. nextValue = Math.max(thisX + 1, nextValue + 1);
  34. System.out.println("Next Value is: " + nextValue);
  35. }
  36. persist.setValue(new String[]{player.getName(), "aliases", nextValue.toString()}, alias);
  37. persist.save();
  38. return nextValue;
  39. } catch (Exception ex) {
  40. player.sendMessage("Could not add the alias. Please check the error logs for more information.");
  41. Logger.getLogger("Minecraft").log(Level.SEVERE, null, ex);
  42. return -1;
  43. }
  44. }
  45. public String getAlias(int id){
  46. return (String) persist.getValue(new String[]{player.getName(), "aliases", Integer.toString(id)});
  47. }
  48. public String getAllAliases(){
  49. ArrayList<Map.Entry> al = persist.getNamespaceValues(new String[]{player.getName(), "aliases"});
  50. StringBuilder b = new StringBuilder();
  51. System.out.println(al);
  52. for(Map.Entry e : al){
  53. String [] key = e.getKey().toString().split("\\.");
  54. b.append(ChatColor.AQUA)
  55. .append(key[key.length - 1])
  56. .append(":")
  57. .append(e.getValue().toString().substring(0, Math.min(e.getValue().toString().length(), 45)))
  58. .append(e.getValue().toString().length() > 45?"...":"")
  59. .append("\n");
  60. }
  61. if(al.isEmpty()){
  62. b.append(ChatColor.AQUA).append("You have no aliases defined");
  63. }
  64. return b.toString();
  65. }
  66. public ArrayList<String> getAliasesAsArray(){
  67. ArrayList<Map.Entry> al = persist.getNamespaceValues(new String[]{player.getName(), "aliases"});
  68. StringBuilder b = new StringBuilder();
  69. ArrayList<String> commands = new ArrayList<String>();
  70. for(Map.Entry e : al){
  71. String [] key = e.getKey().toString().split("\\.");
  72. b.append(e.getValue().toString()).append("\n");
  73. commands.add(b.toString());
  74. b = new StringBuilder();
  75. }
  76. return commands;
  77. }
  78. public int getTotalAliases(){
  79. return persist.getNamespaceValues(new String[]{player.getName(), "aliases"}).size();
  80. }
  81. public void removeAlias(int id){
  82. try {
  83. persist.setValue(new String[]{player.getName(), "aliases", Integer.toString(id)}, null);
  84. persist.save();
  85. } catch (Exception ex) {
  86. Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
  87. }
  88. }
  89. }