ChatManager.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package me.km.chatmanager;
  2. import me.km.api.Module;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.HashSet;
  6. import java.util.UUID;
  7. import java.util.stream.Collectors;
  8. import me.km.permissions.Permission;
  9. import me.km.permissions.Permissions;
  10. import net.minecraft.command.ICommandSender;
  11. import net.minecraft.entity.player.EntityPlayer;
  12. import net.minecraft.util.text.TextFormatting;
  13. public class ChatManager extends Module
  14. {
  15. private final HashMap<Integer, ArrayList<Rank>> ranks;
  16. private final HashMap<UUID, String> fakeRank;
  17. private final String fallback;
  18. private final HashMap<UUID, HashSet<String>> playerRanks;
  19. public ChatManager(String mname, String prefix, TextFormatting color)
  20. {
  21. super(mname, prefix, color);
  22. ranks = new HashMap<>();
  23. fakeRank = new HashMap<>();
  24. fallback = "§8Geist";
  25. playerRanks = new HashMap<>();
  26. }
  27. // -----------------------------------------------------------------------------------
  28. // Rank-Handling
  29. // -----------------------------------------------------------------------------------
  30. /** Registers a rank by its permission, group and name. The idea is that ranks in the
  31. * same group overwrite themselves by picking the first match in the list.
  32. *
  33. * @param name the name of the rank to identify it
  34. * @param group the id of the rank group
  35. * @param text the text, which will be shown for the rank
  36. */
  37. public void registerRank(String name, int group, String text)
  38. {
  39. ArrayList<Rank> list = ranks.get(group);
  40. if(list == null)
  41. {
  42. list = new ArrayList<>();
  43. ranks.put(group, list);
  44. }
  45. list.add(new Rank(name, text));
  46. }
  47. /** Registers a rank for a players uuid
  48. *
  49. * @param uuid a uuid of a player
  50. * @param rank the rank, which should be registered
  51. */
  52. public void registerPlayerRank(UUID uuid, String rank)
  53. {
  54. HashSet<String> set = playerRanks.get(uuid);
  55. if(set == null)
  56. {
  57. set = new HashSet<>();
  58. }
  59. set.add(rank);
  60. }
  61. /** Gets all texts of the players ranks
  62. *
  63. * @param p a player
  64. * @return rank texts as list, the list will never be empty due to fallback
  65. */
  66. public ArrayList<String> getRanks(EntityPlayer p)
  67. {
  68. HashSet<String> pRanks = playerRanks.get(p.getUniqueID());
  69. if(pRanks != null)
  70. {
  71. ArrayList<String> list = ranks.values().stream()
  72. .map(l ->
  73. {
  74. for(Rank r : l)
  75. {
  76. if(pRanks.contains(r.getName()))
  77. {
  78. return r.getName();
  79. }
  80. }
  81. return null;
  82. })
  83. .filter(s -> s != null)
  84. .collect(Collectors.toCollection(ArrayList::new));
  85. if(list.isEmpty())
  86. {
  87. list.add(fallback);
  88. }
  89. return list;
  90. }
  91. else
  92. {
  93. ArrayList<String> list = new ArrayList<>();
  94. list.add(fallback);
  95. return list;
  96. }
  97. }
  98. /** Removes all ranks and their player registry from the cache
  99. *
  100. */
  101. public void clearRanks()
  102. {
  103. ranks.clear();
  104. playerRanks.clear();
  105. }
  106. /** Gets the players display name and its ranks
  107. *
  108. * @param p a player
  109. * @return players display name and ranks collected with specific format
  110. */
  111. public String getFullName(EntityPlayer p)
  112. {
  113. String fake = fakeRank.get(p.getUniqueID());
  114. if(fake != null)
  115. {
  116. return fake + " " + p.getDisplayNameString();
  117. }
  118. return getRanks(p).stream().collect(Collectors.joining("§r][", "[", "§r] " + p.getDisplayNameString()));
  119. }
  120. // -------------------------------------------------------------------------
  121. // Fakeranks
  122. // -------------------------------------------------------------------------
  123. public void addFakeRank(EntityPlayer p, String rank)
  124. {
  125. fakeRank.put(p.getUniqueID(), rank);
  126. }
  127. public String getFakeRank(EntityPlayer p)
  128. {
  129. return fakeRank.get(p.getUniqueID());
  130. }
  131. public void removeFakeRank(EntityPlayer p)
  132. {
  133. fakeRank.remove(p.getUniqueID());
  134. }
  135. // -------------------------------------------------------------------------
  136. // Text-Coloring-Helper
  137. // -------------------------------------------------------------------------
  138. public static String colorMessage(String text, ICommandSender cs)
  139. {
  140. text = text.replace("&k", "");
  141. if(Permission.has(cs, Permissions.USE_COLOR))
  142. {
  143. text = text.replace("&", "§");
  144. return text;
  145. }
  146. text = text.replaceAll("&.", "");
  147. return text;
  148. }
  149. }