Pangamma

CText.java

Dec 8th, 2018
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.98 KB | None | 0 0
  1. package com.lumengaming.skillsaw;
  2.  
  3. import net.md_5.bungee.api.chat.BaseComponent;
  4. import net.md_5.bungee.api.chat.ClickEvent;
  5. import net.md_5.bungee.api.chat.ComponentBuilder;
  6. import net.md_5.bungee.api.chat.HoverEvent;
  7. import net.md_5.bungee.api.chat.TextComponent;
  8. import org.bukkit.ChatColor;
  9.  
  10. /**
  11.  * @author Taylor Love (Pangamma)
  12.  */
  13. public class CText{
  14.  
  15. //    public CText(String text){
  16. //        super(fixLegacy(text));
  17. //    }
  18. //    
  19. //    public CText(ComponentBuilder cb){
  20. //        super(cb);
  21. //    }
  22.  
  23.     /** This method is horrible, but it does the job. Maybe one day I can fix
  24.      * it. For now it just works. It fixes a glitch in the core game where
  25.      * colored chat stops being colored after it spans more than one line.
  26.      * @param orig
  27.      * @return
  28.      */
  29.     private static String fixLegacy(String orig){
  30.         String spacesBefore = ""; String spacesAfter = "";
  31.         for(int i = 0; i < orig.length(); i++){
  32.             char c = orig.charAt(i);
  33.             if (c == ' ')
  34.                 spacesBefore += " ";
  35.             else
  36.                 break;
  37.         }
  38.        
  39.         for(int i = orig.length()-1; i >= 0; i--){
  40.             char c = orig.charAt(i);
  41.             if (c == ' ')
  42.                 spacesAfter += " ";
  43.             else
  44.                 break;
  45.             if (i == 0){
  46.                 spacesAfter = ""; // avoid duplicates. If the whole thing is spaces.
  47.             }
  48.         }
  49.        
  50.         String s = "";
  51.         String[] args = orig.split(" ");
  52.         String lastColors = "";
  53.         for(int i = 0; i < args.length; i++){
  54.             if (i != 0 && !ChatColor.stripColor(args[i]).isEmpty()){
  55.                 s += " ";
  56.             }
  57.            
  58.             String nColor = ChatColor.getLastColors(args[i]);
  59.             if (nColor != null && !nColor.isEmpty()){
  60.                 lastColors = nColor;
  61.             }
  62.             s += lastColors + args[i];
  63.         }
  64.         return spacesBefore + s + spacesAfter;
  65.     }
  66.    
  67.     public static BaseComponent[] legacy(String orig){
  68.         return TextComponent.fromLegacyText(fixLegacy(orig));
  69.     }    
  70.    
  71.     public static String toLegacyString(BaseComponent[] orig){
  72.         return TextComponent.toLegacyText(orig);
  73.     }
  74.    
  75.     /** Merges two arrays of base components. **/
  76.     public static BaseComponent[] merge(BaseComponent[]  o1,BaseComponent[] o2){
  77.         BaseComponent[] n = new BaseComponent[o1.length+o2.length];
  78.         int i = 0;
  79.        
  80.         for(BaseComponent bc : o1)
  81.             n[i++] = bc;
  82.         for(BaseComponent bc : o2)
  83.             n[i++] = bc;
  84.        
  85.         return n;
  86.     }
  87.    
  88.     /** Iterates through all the base components and sets an event on them. **/
  89.     public static void applyEvent(BaseComponent[] bcs,HoverEvent e){
  90.         for (BaseComponent bc : bcs){
  91.             bc.setHoverEvent(e);
  92.         }
  93.     }
  94.     /** Iterates through all the base components and sets an event on them. **/
  95.     public static void applyEvent(BaseComponent[] bcs,ClickEvent e){
  96.         for (BaseComponent bc : bcs){
  97.             bc.setClickEvent(e);
  98.         }
  99.     }
  100.    
  101.     /** Iterates through all the base components and sets an event on them. **/
  102.     public static BaseComponent[] clone(BaseComponent[] bcs){
  103.         BaseComponent[] clone = new BaseComponent[bcs.length];
  104.         for (int i = 0; i < bcs.length;i++){
  105.             clone[i] = bcs[i].duplicate();
  106.         }
  107.         return clone;
  108.     }
  109.    
  110.     /** Iterates through all the base components and sets an event on them. **/
  111.     public static void applyColor(BaseComponent[] bcs, net.md_5.bungee.api.ChatColor c){
  112.         for (BaseComponent bc : bcs){
  113.             bc.setColor(c);
  114.         }
  115.     }
  116.     /**
  117.      * Creates hover text easily.
  118.      * @return
  119.      */
  120.     public static BaseComponent[] hoverText(String displayText,String hoverText){
  121.         BaseComponent[] txt = CText.legacy(displayText);
  122.         CText.applyEvent(txt, new HoverEvent(HoverEvent.Action.SHOW_TEXT, CText.legacy(hoverText)));
  123.         return txt;
  124.     }
  125.    
  126.     /**
  127.      * Creates hover text easily. Click event suggests the command.
  128.      * @return
  129.      */
  130.     public static BaseComponent[] hoverTextSuggest(String displayText,String hoverText,String commandText){
  131.         BaseComponent[] txt = CText.legacy(displayText);
  132.         CText.applyEvent(txt, new HoverEvent(HoverEvent.Action.SHOW_TEXT, CText.legacy(hoverText)));
  133.         CText.applyEvent(txt, new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, commandText));
  134.         return txt;
  135.     }    
  136.    
  137.     /**
  138.      * Creates hover text easily. Click event runs the command.
  139.      * @return
  140.      */
  141.     public static BaseComponent[] hoverTextForce(String displayText,String hoverText,String commandText){
  142.         BaseComponent[] txt = CText.legacy(displayText);
  143.         CText.applyEvent(txt, new HoverEvent(HoverEvent.Action.SHOW_TEXT, CText.legacy(hoverText)));
  144.         CText.applyEvent(txt, new ClickEvent(ClickEvent.Action.RUN_COMMAND, commandText));
  145.         return txt;
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment