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> ranks; private final HashMap fakeRank; private final String fallback; private final HashMap> 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 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 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 getRanks(EntityPlayer p) { HashSet pRanks = playerRanks.get(p.getUniqueID()); if(pRanks != null) { ArrayList 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 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; } }