123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- package me.km.chatmanager;
- import me.km.api.Module;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.UUID;
- import java.util.stream.Collectors;
- import me.km.permissions.Permission;
- import me.km.permissions.Permissions;
- import net.minecraft.command.ICommandSender;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.util.text.TextFormatting;
- public class ChatManager extends Module
- {
- private final HashMap<Integer, ArrayList<Rank>> ranks;
- private final HashMap<UUID, String> fakeRank;
- private final String fallback;
- private final HashMap<UUID, HashSet<String>> playerRanks;
- public ChatManager(String mname, String prefix, TextFormatting color)
- {
- super(mname, prefix, color);
- ranks = new HashMap<>();
- fakeRank = new HashMap<>();
- fallback = "§8Geist";
- playerRanks = new HashMap<>();
- }
- // -----------------------------------------------------------------------------------
- // Rank-Handling
- // -----------------------------------------------------------------------------------
- /** Registers a rank by its permission, group and name. The idea is that ranks in the
- * same group overwrite themselves by picking the first match in the list.
- *
- * @param name the name of the rank to identify it
- * @param group the id of the rank group
- * @param text the text, which will be shown for the rank
- */
- public void registerRank(String name, int group, String text)
- {
- ArrayList<Rank> list = ranks.get(group);
- if(list == null)
- {
- list = new ArrayList<>();
- ranks.put(group, list);
- }
- list.add(new Rank(name, text));
- }
-
- /** Registers a rank for a players uuid
- *
- * @param uuid a uuid of a player
- * @param rank the rank, which should be registered
- */
- public void registerPlayerRank(UUID uuid, String rank)
- {
- HashSet<String> set = playerRanks.get(uuid);
- if(set == null)
- {
- set = new HashSet<>();
- }
- set.add(rank);
- }
-
- /** Gets all texts of the players ranks
- *
- * @param p a player
- * @return rank texts as list, the list will never be empty due to fallback
- */
- public ArrayList<String> getRanks(EntityPlayer p)
- {
- HashSet<String> pRanks = playerRanks.get(p.getUniqueID());
- if(pRanks != null)
- {
- ArrayList<String> list = ranks.values().stream()
- .map(l ->
- {
- for(Rank r : l)
- {
- if(pRanks.contains(r.getName()))
- {
- return r.getName();
- }
- }
- return null;
- })
- .filter(s -> s != null)
- .collect(Collectors.toCollection(ArrayList::new));
- if(list.isEmpty())
- {
- list.add(fallback);
- }
- return list;
- }
- else
- {
- ArrayList<String> list = new ArrayList<>();
- list.add(fallback);
- return list;
- }
- }
-
- /** Removes all ranks and their player registry from the cache
- *
- */
- public void clearRanks()
- {
- ranks.clear();
- playerRanks.clear();
- }
-
- /** Gets the players display name and its ranks
- *
- * @param p a player
- * @return players display name and ranks collected with specific format
- */
- public String getFullName(EntityPlayer p)
- {
- String fake = fakeRank.get(p.getUniqueID());
- if(fake != null)
- {
- return fake + " " + p.getDisplayNameString();
- }
- return getRanks(p).stream().collect(Collectors.joining("§r][", "[", "§r] " + p.getDisplayNameString()));
- }
-
- // -------------------------------------------------------------------------
- // Fakeranks
- // -------------------------------------------------------------------------
-
- public void addFakeRank(EntityPlayer p, String rank)
- {
- fakeRank.put(p.getUniqueID(), rank);
- }
-
- public String getFakeRank(EntityPlayer p)
- {
- return fakeRank.get(p.getUniqueID());
- }
- public void removeFakeRank(EntityPlayer p)
- {
- fakeRank.remove(p.getUniqueID());
- }
-
- // -------------------------------------------------------------------------
- // Text-Coloring-Helper
- // -------------------------------------------------------------------------
-
- public static String colorMessage(String text, ICommandSender cs)
- {
- text = text.replace("&k", "");
- if(Permission.has(cs, Permissions.USE_COLOR))
- {
- text = text.replace("&", "§");
- return text;
- }
- text = text.replaceAll("&.", "");
- return text;
- }
- }
|