Guest User

KOD ComponentBuilder

a guest
Sep 4th, 2017
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.05 KB | None | 0 0
  1. public class ComponentBuilder
  2. {
  3.   private TextComponent current;
  4.   private final List<BaseComponent> parts = new ArrayList<>();
  5.  
  6.   public ComponentBuilder(ComponentBuilder original)
  7.   {
  8.     this.current = new TextComponent(original.current);
  9.     for (BaseComponent baseComponent : original.parts) {
  10.       this.parts.add(baseComponent.duplicate());
  11.     }
  12.   }
  13.  
  14.   public ComponentBuilder(String text)
  15.   {
  16.     this.current = new TextComponent(text);
  17.   }
  18.  
  19.   public ComponentBuilder append(String text)
  20.   {
  21.     return append(text, FormatRetention.ALL);
  22.   }
  23.  
  24.   public ComponentBuilder append(String text, FormatRetention retention)
  25.   {
  26.     this.parts.add(this.current);
  27.    
  28.     this.current = new TextComponent(this.current);
  29.     this.current.setText(text);
  30.     retain(retention);
  31.    
  32.     return this;
  33.   }
  34.  
  35.   public ComponentBuilder color(ChatColor color)
  36.   {
  37.     this.current.setColor(color);
  38.     return this;
  39.   }
  40.  
  41.   public ComponentBuilder bold(boolean bold)
  42.   {
  43.     this.current.setBold(Boolean.valueOf(bold));
  44.     return this;
  45.   }
  46.  
  47.   public ComponentBuilder italic(boolean italic)
  48.   {
  49.     this.current.setItalic(Boolean.valueOf(italic));
  50.     return this;
  51.   }
  52.  
  53.   public ComponentBuilder underlined(boolean underlined)
  54.   {
  55.     this.current.setUnderlined(Boolean.valueOf(underlined));
  56.     return this;
  57.   }
  58.  
  59.   public ComponentBuilder strikethrough(boolean strikethrough)
  60.   {
  61.     this.current.setStrikethrough(Boolean.valueOf(strikethrough));
  62.     return this;
  63.   }
  64.  
  65.   public ComponentBuilder obfuscated(boolean obfuscated)
  66.   {
  67.     this.current.setObfuscated(Boolean.valueOf(obfuscated));
  68.     return this;
  69.   }
  70.  
  71.   public ComponentBuilder insertion(String insertion)
  72.   {
  73.     this.current.setInsertion(insertion);
  74.     return this;
  75.   }
  76.  
  77.   public ComponentBuilder event(ClickEvent clickEvent)
  78.   {
  79.     this.current.setClickEvent(clickEvent);
  80.     return this;
  81.   }
  82.  
  83.   public ComponentBuilder event(HoverEvent hoverEvent)
  84.   {
  85.     this.current.setHoverEvent(hoverEvent);
  86.     return this;
  87.   }
  88.  
  89.   public ComponentBuilder reset()
  90.   {
  91.     return retain(FormatRetention.NONE);
  92.   }
  93.  
  94.   public ComponentBuilder retain(FormatRetention retention)
  95.   {
  96.     BaseComponent previous = this.current;
  97.     switch (retention)
  98.     {
  99.     case ALL:
  100.       this.current = new TextComponent(this.current.getText());
  101.       break;
  102.     case NONE:
  103.       break;
  104.     case FORMATTING:
  105.       this.current = new TextComponent(this.current.getText());
  106.       this.current.setInsertion(previous.getInsertion());
  107.       this.current.setClickEvent(previous.getClickEvent());
  108.       this.current.setHoverEvent(previous.getHoverEvent());
  109.       break;
  110.     case EVENTS:
  111.       this.current.setClickEvent(null);
  112.       this.current.setHoverEvent(null);
  113.     }
  114.     return this;
  115.   }
  116.  
  117.   public BaseComponent[] create()
  118.   {
  119.     this.parts.add(this.current);
  120.     return (BaseComponent[])this.parts.toArray(new BaseComponent[this.parts.size()]);
  121.   }
  122.  
  123.   public static enum FormatRetention
  124.   {
  125.     NONE,  FORMATTING,  EVENTS,  ALL;
  126.   }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment